Skip to content

Commit 77c46ba

Browse files
committed
Merge branch 'PHP-5.4' into PHP-5.5
* PHP-5.4: Fix the spelling of the php_cli_server_http_response_status_code_pair typedef. Change the search in get_status_string() to correctly handle unknown codes.
2 parents 94e7549 + f5c7fe9 commit 77c46ba

5 files changed

Lines changed: 157 additions & 18 deletions

File tree

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3+
?? ??? 2013, PHP 5.5.1
4+
5+
- CLI server:
6+
. Fixed bug #65066 (Cli server not responsive when responding with 422 http
7+
status code). (Adam)
8+
39
20 Jun 2013, PHP 5.5.0
410

511
- Core:

sapi/cli/php_cli_server.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/* $Id: php_cli.c 306938 2011-01-01 02:17:06Z felipe $ */
2121

2222
#include <stdio.h>
23+
#include <stdlib.h>
2324
#include <fcntl.h>
2425
#include <assert.h>
2526

@@ -194,17 +195,17 @@ typedef struct php_cli_server {
194195
HashTable clients;
195196
} php_cli_server;
196197

197-
typedef struct php_cli_server_http_reponse_status_code_pair {
198+
typedef struct php_cli_server_http_response_status_code_pair {
198199
int code;
199200
const char *str;
200-
} php_cli_server_http_reponse_status_code_pair;
201+
} php_cli_server_http_response_status_code_pair;
201202

202203
typedef struct php_cli_server_ext_mime_type_pair {
203204
const char *ext;
204205
const char *mime_type;
205206
} php_cli_server_ext_mime_type_pair;
206207

207-
static php_cli_server_http_reponse_status_code_pair status_map[] = {
208+
static php_cli_server_http_response_status_code_pair status_map[] = {
208209
{ 100, "Continue" },
209210
{ 101, "Switching Protocols" },
210211
{ 200, "OK" },
@@ -251,7 +252,7 @@ static php_cli_server_http_reponse_status_code_pair status_map[] = {
251252
{ 511, "Network Authentication Required" },
252253
};
253254

254-
static php_cli_server_http_reponse_status_code_pair template_map[] = {
255+
static php_cli_server_http_response_status_code_pair template_map[] = {
255256
{ 400, "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
256257
{ 404, "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" },
257258
{ 500, "<h1>%s</h1><p>The server is temporarily unavailable.</p>" },
@@ -337,28 +338,43 @@ static char *get_last_error() /* {{{ */
337338
return pestrdup(strerror(errno), 1);
338339
} /* }}} */
339340

341+
static int status_comp(const void *a, const void *b) /* {{{ */
342+
{
343+
const php_cli_server_http_response_status_code_pair *pa = (const php_cli_server_http_response_status_code_pair *) a;
344+
const php_cli_server_http_response_status_code_pair *pb = (const php_cli_server_http_response_status_code_pair *) b;
345+
346+
if (pa->code < pb->code) {
347+
return -1;
348+
} else if (pa->code > pb->code) {
349+
return 1;
350+
}
351+
352+
return 0;
353+
} /* }}} */
354+
340355
static const char *get_status_string(int code) /* {{{ */
341356
{
342-
size_t e = (sizeof(status_map) / sizeof(php_cli_server_http_reponse_status_code_pair));
343-
size_t s = 0;
357+
php_cli_server_http_response_status_code_pair needle, *result = NULL;
344358

345-
while (e != s) {
346-
size_t c = MIN((e + s + 1) / 2, e - 1);
347-
int d = status_map[c].code;
348-
if (d > code) {
349-
e = c;
350-
} else if (d < code) {
351-
s = c;
352-
} else {
353-
return status_map[c].str;
354-
}
359+
needle.code = code;
360+
needle.str = NULL;
361+
362+
result = bsearch(&needle, status_map, sizeof(status_map) / sizeof(needle), sizeof(needle), status_comp);
363+
364+
if (result) {
365+
return result->str;
355366
}
356-
return NULL;
367+
368+
/* Returning NULL would require complicating append_http_status_line() to
369+
* not segfault in that case, so let's just return a placeholder, since RFC
370+
* 2616 requires a reason phrase. This is basically what a lot of other Web
371+
* servers do in this case anyway. */
372+
return "Unknown Status Code";
357373
} /* }}} */
358374

359375
static const char *get_template_string(int code) /* {{{ */
360376
{
361-
size_t e = (sizeof(template_map) / sizeof(php_cli_server_http_reponse_status_code_pair));
377+
size_t e = (sizeof(template_map) / sizeof(php_cli_server_http_response_status_code_pair));
362378
size_t s = 0;
363379

364380
while (e != s) {

sapi/cli/tests/bug65066_100.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #65066 (Cli server not responsive when responding with 422 http status code): 100 status code
3+
--INI--
4+
allow_url_fopen=1
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
?>
9+
--FILE--
10+
<?php
11+
include "php_cli_server.inc";
12+
php_cli_server_start('http_response_code(100);');
13+
14+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15+
$port = intval($port)?:80;
16+
17+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18+
if (!$fp) {
19+
die("connect failed");
20+
}
21+
22+
if(fwrite($fp, <<<HEADER
23+
GET / HTTP/1.1
24+
Host: {$host}
25+
26+
27+
HEADER
28+
)) {
29+
while (!feof($fp)) {
30+
echo fgets($fp);
31+
}
32+
}
33+
?>
34+
--EXPECTF--
35+
HTTP/1.1 100 Continue
36+
Host: %s
37+
Connection: close
38+
X-Powered-By: PHP/%s
39+
Content-type: text/html

sapi/cli/tests/bug65066_422.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #65066 (Cli server not responsive when responding with 422 http status code): 422 status code
3+
--INI--
4+
allow_url_fopen=1
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
?>
9+
--FILE--
10+
<?php
11+
include "php_cli_server.inc";
12+
php_cli_server_start('http_response_code(422);');
13+
14+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15+
$port = intval($port)?:80;
16+
17+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18+
if (!$fp) {
19+
die("connect failed");
20+
}
21+
22+
if(fwrite($fp, <<<HEADER
23+
GET / HTTP/1.1
24+
Host: {$host}
25+
26+
27+
HEADER
28+
)) {
29+
while (!feof($fp)) {
30+
echo fgets($fp);
31+
}
32+
}
33+
?>
34+
--EXPECTF--
35+
HTTP/1.1 422 Unknown Status Code
36+
Host: %s
37+
Connection: close
38+
X-Powered-By: PHP/%s
39+
Content-type: text/html

sapi/cli/tests/bug65066_511.phpt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #65066 (Cli server not responsive when responding with 422 http status code): 511 status code
3+
--INI--
4+
allow_url_fopen=1
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
?>
9+
--FILE--
10+
<?php
11+
include "php_cli_server.inc";
12+
php_cli_server_start('http_response_code(511);');
13+
14+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15+
$port = intval($port)?:80;
16+
17+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18+
if (!$fp) {
19+
die("connect failed");
20+
}
21+
22+
if(fwrite($fp, <<<HEADER
23+
GET / HTTP/1.1
24+
Host: {$host}
25+
26+
27+
HEADER
28+
)) {
29+
while (!feof($fp)) {
30+
echo fgets($fp);
31+
}
32+
}
33+
?>
34+
--EXPECTF--
35+
HTTP/1.1 511 Network Authentication Required
36+
Host: %s
37+
Connection: close
38+
X-Powered-By: PHP/%s
39+
Content-type: text/html

0 commit comments

Comments
 (0)