|
20 | 20 | /* $Id: php_cli.c 306938 2011-01-01 02:17:06Z felipe $ */ |
21 | 21 |
|
22 | 22 | #include <stdio.h> |
| 23 | +#include <stdlib.h> |
23 | 24 | #include <fcntl.h> |
24 | 25 | #include <assert.h> |
25 | 26 |
|
@@ -194,17 +195,17 @@ typedef struct php_cli_server { |
194 | 195 | HashTable clients; |
195 | 196 | } php_cli_server; |
196 | 197 |
|
197 | | -typedef struct php_cli_server_http_reponse_status_code_pair { |
| 198 | +typedef struct php_cli_server_http_response_status_code_pair { |
198 | 199 | int code; |
199 | 200 | const char *str; |
200 | | -} php_cli_server_http_reponse_status_code_pair; |
| 201 | +} php_cli_server_http_response_status_code_pair; |
201 | 202 |
|
202 | 203 | typedef struct php_cli_server_ext_mime_type_pair { |
203 | 204 | const char *ext; |
204 | 205 | const char *mime_type; |
205 | 206 | } php_cli_server_ext_mime_type_pair; |
206 | 207 |
|
207 | | -static php_cli_server_http_reponse_status_code_pair status_map[] = { |
| 208 | +static php_cli_server_http_response_status_code_pair status_map[] = { |
208 | 209 | { 100, "Continue" }, |
209 | 210 | { 101, "Switching Protocols" }, |
210 | 211 | { 200, "OK" }, |
@@ -251,7 +252,7 @@ static php_cli_server_http_reponse_status_code_pair status_map[] = { |
251 | 252 | { 511, "Network Authentication Required" }, |
252 | 253 | }; |
253 | 254 |
|
254 | | -static php_cli_server_http_reponse_status_code_pair template_map[] = { |
| 255 | +static php_cli_server_http_response_status_code_pair template_map[] = { |
255 | 256 | { 400, "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" }, |
256 | 257 | { 404, "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" }, |
257 | 258 | { 500, "<h1>%s</h1><p>The server is temporarily unavailable.</p>" }, |
@@ -337,28 +338,43 @@ static char *get_last_error() /* {{{ */ |
337 | 338 | return pestrdup(strerror(errno), 1); |
338 | 339 | } /* }}} */ |
339 | 340 |
|
| 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 | + |
340 | 355 | static const char *get_status_string(int code) /* {{{ */ |
341 | 356 | { |
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; |
344 | 358 |
|
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; |
355 | 366 | } |
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"; |
357 | 373 | } /* }}} */ |
358 | 374 |
|
359 | 375 | static const char *get_template_string(int code) /* {{{ */ |
360 | 376 | { |
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)); |
362 | 378 | size_t s = 0; |
363 | 379 |
|
364 | 380 | while (e != s) { |
|
0 commit comments