PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - sapi/cli - php_cli_server.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 1275 0.0 %
Date: 2018-08-17 Functions: 0 90 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2018 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Moriyoshi Koizumi <moriyoshi@php.net>                        |
      16             :    |         Xinchen Hui       <laruence@php.net>                         |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : #include <stdio.h>
      21             : #include <stdlib.h>
      22             : #include <fcntl.h>
      23             : #include <assert.h>
      24             : 
      25             : #ifdef PHP_WIN32
      26             : # include <process.h>
      27             : # include <io.h>
      28             : # include "win32/time.h"
      29             : # include "win32/signal.h"
      30             : # include "win32/php_registry.h"
      31             : # include <sys/timeb.h>
      32             : #else
      33             : # include "php_config.h"
      34             : #endif
      35             : 
      36             : #ifdef __riscos__
      37             : #include <unixlib/local.h>
      38             : #endif
      39             : 
      40             : 
      41             : #if HAVE_TIME_H
      42             : #include <time.h>
      43             : #endif
      44             : #if HAVE_SYS_TIME_H
      45             : #include <sys/time.h>
      46             : #endif
      47             : #if HAVE_UNISTD_H
      48             : #include <unistd.h>
      49             : #endif
      50             : #if HAVE_SIGNAL_H
      51             : #include <signal.h>
      52             : #endif
      53             : #if HAVE_SETLOCALE
      54             : #include <locale.h>
      55             : #endif
      56             : #if HAVE_DLFCN_H
      57             : #include <dlfcn.h>
      58             : #endif
      59             : 
      60             : #include "SAPI.h"
      61             : #include "php.h"
      62             : #include "php_ini.h"
      63             : #include "php_main.h"
      64             : #include "php_globals.h"
      65             : #include "php_variables.h"
      66             : #include "zend_hash.h"
      67             : #include "zend_modules.h"
      68             : #include "fopen_wrappers.h"
      69             : #include "http_status_codes.h"
      70             : 
      71             : #include "zend_compile.h"
      72             : #include "zend_execute.h"
      73             : #include "zend_highlight.h"
      74             : #include "zend_exceptions.h"
      75             : 
      76             : #include "php_getopt.h"
      77             : 
      78             : #ifndef PHP_WIN32
      79             : # define php_select(m, r, w, e, t)      select(m, r, w, e, t)
      80             : # define SOCK_EINVAL EINVAL
      81             : # define SOCK_EAGAIN EAGAIN
      82             : # define SOCK_EINTR EINTR
      83             : # define SOCK_EADDRINUSE EADDRINUSE
      84             : #else
      85             : # include "win32/select.h"
      86             : # define SOCK_EINVAL WSAEINVAL
      87             : # define SOCK_EAGAIN WSAEWOULDBLOCK
      88             : # define SOCK_EINTR WSAEINTR
      89             : # define SOCK_EADDRINUSE WSAEADDRINUSE
      90             : #endif
      91             : 
      92             : #include "ext/standard/file.h" /* for php_set_sock_blocking() :-( */
      93             : #include "zend_smart_str.h"
      94             : #include "ext/standard/html.h"
      95             : #include "ext/standard/url.h" /* for php_raw_url_decode() */
      96             : #include "ext/standard/php_string.h" /* for php_dirname() */
      97             : #include "ext/date/php_date.h" /* for php_format_date() */
      98             : #include "php_network.h"
      99             : 
     100             : #include "php_http_parser.h"
     101             : #include "php_cli_server.h"
     102             : #include "mime_type_map.h"
     103             : 
     104             : #include "php_cli_process_title.h"
     105             : 
     106             : #define OUTPUT_NOT_CHECKED -1
     107             : #define OUTPUT_IS_TTY 1
     108             : #define OUTPUT_NOT_TTY 0
     109             : 
     110             : typedef struct php_cli_server_poller {
     111             :         fd_set rfds, wfds;
     112             :         struct {
     113             :                 fd_set rfds, wfds;
     114             :         } active;
     115             :         php_socket_t max_fd;
     116             : } php_cli_server_poller;
     117             : 
     118             : typedef struct php_cli_server_request {
     119             :         enum php_http_method request_method;
     120             :         int protocol_version;
     121             :         char *request_uri;
     122             :         size_t request_uri_len;
     123             :         char *vpath;
     124             :         size_t vpath_len;
     125             :         char *path_translated;
     126             :         size_t path_translated_len;
     127             :         char *path_info;
     128             :         size_t path_info_len;
     129             :         char *query_string;
     130             :         size_t query_string_len;
     131             :         HashTable headers;
     132             :         HashTable headers_original_case;
     133             :         char *content;
     134             :         size_t content_len;
     135             :         const char *ext;
     136             :         size_t ext_len;
     137             :         zend_stat_t sb;
     138             : } php_cli_server_request;
     139             : 
     140             : typedef struct php_cli_server_chunk {
     141             :         struct php_cli_server_chunk *next;
     142             :         enum php_cli_server_chunk_type {
     143             :                 PHP_CLI_SERVER_CHUNK_HEAP,
     144             :                 PHP_CLI_SERVER_CHUNK_IMMORTAL
     145             :         } type;
     146             :         union {
     147             :                 struct { void *block; char *p; size_t len; } heap;
     148             :                 struct { const char *p; size_t len; } immortal;
     149             :         } data;
     150             : } php_cli_server_chunk;
     151             : 
     152             : typedef struct php_cli_server_buffer {
     153             :         php_cli_server_chunk *first;
     154             :         php_cli_server_chunk *last;
     155             : } php_cli_server_buffer;
     156             : 
     157             : typedef struct php_cli_server_content_sender {
     158             :         php_cli_server_buffer buffer;
     159             : } php_cli_server_content_sender;
     160             : 
     161             : typedef struct php_cli_server_client {
     162             :         struct php_cli_server *server;
     163             :         php_socket_t sock;
     164             :         struct sockaddr *addr;
     165             :         socklen_t addr_len;
     166             :         char *addr_str;
     167             :         size_t addr_str_len;
     168             :         php_http_parser parser;
     169             :         unsigned int request_read:1;
     170             :         char *current_header_name;
     171             :         size_t current_header_name_len;
     172             :         unsigned int current_header_name_allocated:1;
     173             :         char *current_header_value;
     174             :         size_t current_header_value_len;
     175             :         enum { HEADER_NONE=0, HEADER_FIELD, HEADER_VALUE } last_header_element;
     176             :         size_t post_read_offset;
     177             :         php_cli_server_request request;
     178             :         unsigned int content_sender_initialized:1;
     179             :         php_cli_server_content_sender content_sender;
     180             :         int file_fd;
     181             : } php_cli_server_client;
     182             : 
     183             : typedef struct php_cli_server {
     184             :         php_socket_t server_sock;
     185             :         php_cli_server_poller poller;
     186             :         int is_running;
     187             :         char *host;
     188             :         int port;
     189             :         int address_family;
     190             :         char *document_root;
     191             :         size_t document_root_len;
     192             :         char *router;
     193             :         size_t router_len;
     194             :         socklen_t socklen;
     195             :         HashTable clients;
     196             :         HashTable extension_mime_types;
     197             : } php_cli_server;
     198             : 
     199             : typedef struct php_cli_server_http_response_status_code_pair {
     200             :         int code;
     201             :         const char *str;
     202             : } php_cli_server_http_response_status_code_pair;
     203             : 
     204             : static php_cli_server_http_response_status_code_pair template_map[] = {
     205             :         { 400, "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
     206             :         { 404, "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" },
     207             :         { 500, "<h1>%s</h1><p>The server is temporarily unavailable.</p>" },
     208             :         { 501, "<h1>%s</h1><p>Request method not supported.</p>" }
     209             : };
     210             : 
     211             : #if HAVE_UNISTD_H
     212             : static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
     213             : #endif
     214             : 
     215             : static const char php_cli_server_request_error_unexpected_eof[] = "Unexpected EOF";
     216             : 
     217             : static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
     218             : static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len);
     219             : static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk);
     220             : static void php_cli_server_logf(const char *format, ...);
     221             : static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message);
     222             : 
     223             : ZEND_DECLARE_MODULE_GLOBALS(cli_server);
     224             : 
     225             : /* {{{ static char php_cli_server_css[]
     226             :  * copied from ext/standard/info.c
     227             :  */
     228             : static const char php_cli_server_css[] = "<style>\n" \
     229             :                                                                                 "body { background-color: #fcfcfc; color: #333333; margin: 0; padding:0; }\n" \
     230             :                                                                                 "h1 { font-size: 1.5em; font-weight: normal; background-color: #9999cc; min-height:2em; line-height:2em; border-bottom: 1px inset black; margin: 0; }\n" \
     231             :                                                                                 "h1, p { padding-left: 10px; }\n" \
     232             :                                                                                 "code.url { background-color: #eeeeee; font-family:monospace; padding:0 2px;}\n" \
     233             :                                                                                 "</style>\n";
     234             : /* }}} */
     235             : 
     236             : #ifdef PHP_WIN32
     237             : int php_cli_server_get_system_time(char *buf) {
     238             :         struct _timeb system_time;
     239             :         errno_t err;
     240             : 
     241             :         if (buf == NULL) {
     242             :                 return -1;
     243             :         }
     244             : 
     245             :         _ftime(&system_time);
     246             :         err = ctime_s(buf, 52, &(system_time.time) );
     247             :         if (err) {
     248             :                 return -1;
     249             :         }
     250             :         return 0;
     251             : }
     252             : #else
     253           0 : int php_cli_server_get_system_time(char *buf) {
     254             :         struct timeval tv;
     255             :         struct tm tm;
     256             : 
     257           0 :         gettimeofday(&tv, NULL);
     258             : 
     259             :         /* TODO: should be checked for NULL tm/return vaue */
     260           0 :         php_localtime_r(&tv.tv_sec, &tm);
     261           0 :         php_asctime_r(&tm, buf);
     262           0 :         return 0;
     263             : }
     264             : #endif
     265             : 
     266           0 : static void char_ptr_dtor_p(zval *zv) /* {{{ */
     267             : {
     268           0 :         pefree(Z_PTR_P(zv), 1);
     269           0 : } /* }}} */
     270             : 
     271           0 : static char *get_last_error() /* {{{ */
     272             : {
     273           0 :         return pestrdup(strerror(errno), 1);
     274             : } /* }}} */
     275             : 
     276           0 : static int status_comp(const void *a, const void *b) /* {{{ */
     277             : {
     278           0 :         const http_response_status_code_pair *pa = (const http_response_status_code_pair *) a;
     279           0 :         const http_response_status_code_pair *pb = (const http_response_status_code_pair *) b;
     280             : 
     281           0 :         if (pa->code < pb->code) {
     282           0 :                 return -1;
     283           0 :         } else if (pa->code > pb->code) {
     284           0 :                 return 1;
     285             :         }
     286             : 
     287           0 :         return 0;
     288             : } /* }}} */
     289             : 
     290           0 : static const char *get_status_string(int code) /* {{{ */
     291             : {
     292           0 :         http_response_status_code_pair needle = {code, NULL},
     293           0 :                 *result = NULL;
     294             : 
     295           0 :         result = bsearch(&needle, http_status_map, http_status_map_len, sizeof(needle), status_comp);
     296             : 
     297           0 :         if (result) {
     298           0 :                 return result->str;
     299             :         }
     300             : 
     301             :         /* Returning NULL would require complicating append_http_status_line() to
     302             :          * not segfault in that case, so let's just return a placeholder, since RFC
     303             :          * 2616 requires a reason phrase. This is basically what a lot of other Web
     304             :          * servers do in this case anyway. */
     305           0 :         return "Unknown Status Code";
     306             : } /* }}} */
     307             : 
     308           0 : static const char *get_template_string(int code) /* {{{ */
     309             : {
     310           0 :         size_t e = (sizeof(template_map) / sizeof(php_cli_server_http_response_status_code_pair));
     311           0 :         size_t s = 0;
     312             : 
     313           0 :         while (e != s) {
     314           0 :                 size_t c = MIN((e + s + 1) / 2, e - 1);
     315           0 :                 int d = template_map[c].code;
     316           0 :                 if (d > code) {
     317           0 :                         e = c;
     318           0 :                 } else if (d < code) {
     319           0 :                         s = c;
     320             :                 } else {
     321           0 :                         return template_map[c].str;
     322             :                 }
     323             :         }
     324           0 :         return NULL;
     325             : } /* }}} */
     326             : 
     327           0 : static void append_http_status_line(smart_str *buffer, int protocol_version, int response_code, int persistent) /* {{{ */
     328             : {
     329           0 :         if (!response_code) {
     330           0 :                 response_code = 200;
     331             :         }
     332           0 :         smart_str_appendl_ex(buffer, "HTTP", 4, persistent);
     333           0 :         smart_str_appendc_ex(buffer, '/', persistent);
     334           0 :         smart_str_append_long_ex(buffer, protocol_version / 100, persistent);
     335           0 :         smart_str_appendc_ex(buffer, '.', persistent);
     336           0 :         smart_str_append_long_ex(buffer, protocol_version % 100, persistent);
     337           0 :         smart_str_appendc_ex(buffer, ' ', persistent);
     338           0 :         smart_str_append_long_ex(buffer, response_code, persistent);
     339           0 :         smart_str_appendc_ex(buffer, ' ', persistent);
     340           0 :         smart_str_appends_ex(buffer, get_status_string(response_code), persistent);
     341           0 :         smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
     342           0 : } /* }}} */
     343             : 
     344           0 : static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, int persistent) /* {{{ */
     345             : {
     346             :         char *val;
     347           0 :         struct timeval tv = {0};
     348             : 
     349           0 :         if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "host", sizeof("host")-1))) {
     350           0 :                 smart_str_appendl_ex(buffer, "Host", sizeof("Host") - 1, persistent);
     351           0 :                 smart_str_appendl_ex(buffer, ": ", sizeof(": ") - 1, persistent);
     352           0 :                 smart_str_appends_ex(buffer, val, persistent);
     353           0 :                 smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
     354             :         }
     355             : 
     356           0 :         if (!gettimeofday(&tv, NULL)) {
     357           0 :                 zend_string *dt = php_format_date("r", 1, tv.tv_sec, 1);
     358           0 :                 smart_str_appendl_ex(buffer, "Date: ", 6, persistent);
     359           0 :                 smart_str_appends_ex(buffer, dt->val, persistent);
     360           0 :                 smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
     361             :                 zend_string_release_ex(dt, 0);
     362             :         }
     363             : 
     364           0 :         smart_str_appendl_ex(buffer, "Connection: close\r\n", sizeof("Connection: close\r\n") - 1, persistent);
     365           0 : } /* }}} */
     366             : 
     367           0 : static const char *get_mime_type(const php_cli_server *server, const char *ext, size_t ext_len) /* {{{ */
     368             : {
     369           0 :         return (const char*)zend_hash_str_find_ptr(&server->extension_mime_types, ext, ext_len);
     370             : } /* }}} */
     371             : 
     372           0 : PHP_FUNCTION(apache_request_headers) /* {{{ */
     373             : {
     374             :         php_cli_server_client *client;
     375             :         HashTable *headers;
     376             :         zend_string *key;
     377             :         char *value;
     378             :         zval tmp;
     379             : 
     380           0 :         if (zend_parse_parameters_none() == FAILURE) {
     381           0 :                 return;
     382             :         }
     383             : 
     384           0 :         client = SG(server_context);
     385           0 :         headers = &client->request.headers_original_case;
     386             : 
     387           0 :         array_init_size(return_value, zend_hash_num_elements(headers));
     388             : 
     389           0 :         ZEND_HASH_FOREACH_STR_KEY_PTR(headers, key, value) {
     390           0 :                 ZVAL_STRING(&tmp, value);
     391           0 :                 zend_symtable_update(Z_ARRVAL_P(return_value), key, &tmp);
     392             :         } ZEND_HASH_FOREACH_END();
     393             : }
     394             : /* }}} */
     395             : 
     396           0 : static void add_response_header(sapi_header_struct *h, zval *return_value) /* {{{ */
     397             : {
     398             :         char *s, *p;
     399             :         ptrdiff_t  len;
     400             :         ALLOCA_FLAG(use_heap)
     401             : 
     402           0 :         if (h->header_len > 0) {
     403           0 :                 p = strchr(h->header, ':');
     404           0 :                 len = p - h->header;
     405           0 :                 if (p && (len > 0)) {
     406           0 :                         while (len > 0 && (h->header[len-1] == ' ' || h->header[len-1] == '\t')) {
     407           0 :                                 len--;
     408             :                         }
     409           0 :                         if (len) {
     410           0 :                                 s = do_alloca(len + 1, use_heap);
     411           0 :                                 memcpy(s, h->header, len);
     412           0 :                                 s[len] = 0;
     413             :                                 do {
     414           0 :                                         p++;
     415           0 :                                 } while (*p == ' ' || *p == '\t');
     416           0 :                                 add_assoc_stringl_ex(return_value, s, (uint32_t)len, p, h->header_len - (p - h->header));
     417           0 :                                 free_alloca(s, use_heap);
     418             :                         }
     419             :                 }
     420             :         }
     421           0 : }
     422             : /* }}} */
     423             : 
     424           0 : PHP_FUNCTION(apache_response_headers) /* {{{ */
     425             : {
     426           0 :         if (zend_parse_parameters_none() == FAILURE) {
     427           0 :                 return;
     428             :         }
     429             : 
     430           0 :         array_init(return_value);
     431           0 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value);
     432             : }
     433             : /* }}} */
     434             : 
     435             : /* {{{ cli_server module
     436             :  */
     437             : 
     438           0 : static void cli_server_init_globals(zend_cli_server_globals *cg)
     439             : {
     440           0 :         cg->color = 0;
     441           0 : }
     442             : 
     443             : PHP_INI_BEGIN()
     444             :         STD_PHP_INI_BOOLEAN("cli_server.color", "0", PHP_INI_ALL, OnUpdateBool, color, zend_cli_server_globals, cli_server_globals)
     445             : PHP_INI_END()
     446             : 
     447           0 : static PHP_MINIT_FUNCTION(cli_server)
     448             : {
     449           0 :         ZEND_INIT_MODULE_GLOBALS(cli_server, cli_server_init_globals, NULL);
     450           0 :         REGISTER_INI_ENTRIES();
     451           0 :         return SUCCESS;
     452             : }
     453             : 
     454           0 : static PHP_MSHUTDOWN_FUNCTION(cli_server)
     455             : {
     456           0 :         UNREGISTER_INI_ENTRIES();
     457           0 :         return SUCCESS;
     458             : }
     459             : 
     460           0 : static PHP_MINFO_FUNCTION(cli_server)
     461             : {
     462           0 :         DISPLAY_INI_ENTRIES();
     463           0 : }
     464             : 
     465             : zend_module_entry cli_server_module_entry = {
     466             :         STANDARD_MODULE_HEADER,
     467             :         "cli_server",
     468             :         NULL,
     469             :         PHP_MINIT(cli_server),
     470             :         PHP_MSHUTDOWN(cli_server),
     471             :         NULL,
     472             :         NULL,
     473             :         PHP_MINFO(cli_server),
     474             :         PHP_VERSION,
     475             :         STANDARD_MODULE_PROPERTIES
     476             : };
     477             : /* }}} */
     478             : 
     479             : ZEND_BEGIN_ARG_INFO(arginfo_no_args, 0)
     480             : ZEND_END_ARG_INFO()
     481             : 
     482             : const zend_function_entry server_additional_functions[] = {
     483             :         PHP_FE(cli_set_process_title,        arginfo_cli_set_process_title)
     484             :         PHP_FE(cli_get_process_title,        arginfo_cli_get_process_title)
     485             :         PHP_FE(apache_request_headers, arginfo_no_args)
     486             :         PHP_FE(apache_response_headers, arginfo_no_args)
     487             :         PHP_FALIAS(getallheaders, apache_request_headers, arginfo_no_args)
     488             :         PHP_FE_END
     489             : };
     490             : 
     491           0 : static int sapi_cli_server_startup(sapi_module_struct *sapi_module) /* {{{ */
     492             : {
     493           0 :         if (php_module_startup(sapi_module, &cli_server_module_entry, 1) == FAILURE) {
     494           0 :                 return FAILURE;
     495             :         }
     496           0 :         return SUCCESS;
     497             : } /* }}} */
     498             : 
     499           0 : static size_t sapi_cli_server_ub_write(const char *str, size_t str_length) /* {{{ */
     500             : {
     501           0 :         php_cli_server_client *client = SG(server_context);
     502           0 :         if (!client) {
     503           0 :                 return 0;
     504             :         }
     505           0 :         return php_cli_server_client_send_through(client, str, str_length);
     506             : } /* }}} */
     507             : 
     508           0 : static void sapi_cli_server_flush(void *server_context) /* {{{ */
     509             : {
     510           0 :         php_cli_server_client *client = server_context;
     511             : 
     512           0 :         if (!client) {
     513           0 :                 return;
     514             :         }
     515             : 
     516           0 :         if (!ZEND_VALID_SOCKET(client->sock)) {
     517           0 :                 php_handle_aborted_connection();
     518           0 :                 return;
     519             :         }
     520             : 
     521           0 :         if (!SG(headers_sent)) {
     522           0 :                 sapi_send_headers();
     523           0 :                 SG(headers_sent) = 1;
     524             :         }
     525             : } /* }}} */
     526             : 
     527           0 : static int sapi_cli_server_discard_headers(sapi_headers_struct *sapi_headers) /* {{{ */{
     528           0 :         return SAPI_HEADER_SENT_SUCCESSFULLY;
     529             : }
     530             : /* }}} */
     531             : 
     532           0 : static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
     533             : {
     534           0 :         php_cli_server_client *client = SG(server_context);
     535           0 :         smart_str buffer = { 0 };
     536             :         sapi_header_struct *h;
     537             :         zend_llist_position pos;
     538             : 
     539           0 :         if (client == NULL || SG(request_info).no_headers) {
     540           0 :                 return SAPI_HEADER_SENT_SUCCESSFULLY;
     541             :         }
     542             : 
     543           0 :         if (SG(sapi_headers).http_status_line) {
     544           0 :                 smart_str_appends(&buffer, SG(sapi_headers).http_status_line);
     545             :                 smart_str_appendl(&buffer, "\r\n", 2);
     546             :         } else {
     547           0 :                 append_http_status_line(&buffer, client->request.protocol_version, SG(sapi_headers).http_response_code, 0);
     548             :         }
     549             : 
     550           0 :         append_essential_headers(&buffer, client, 0);
     551             : 
     552           0 :         h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
     553           0 :         while (h) {
     554           0 :                 if (h->header_len) {
     555           0 :                         smart_str_appendl(&buffer, h->header, h->header_len);
     556             :                         smart_str_appendl(&buffer, "\r\n", 2);
     557             :                 }
     558           0 :                 h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
     559             :         }
     560             :         smart_str_appendl(&buffer, "\r\n", 2);
     561             : 
     562           0 :         php_cli_server_client_send_through(client, ZSTR_VAL(buffer.s), ZSTR_LEN(buffer.s));
     563             : 
     564             :         smart_str_free(&buffer);
     565           0 :         return SAPI_HEADER_SENT_SUCCESSFULLY;
     566             : }
     567             : /* }}} */
     568             : 
     569           0 : static char *sapi_cli_server_read_cookies(void) /* {{{ */
     570             : {
     571           0 :         php_cli_server_client *client = SG(server_context);
     572             :         char *val;
     573           0 :         if (NULL == (val = zend_hash_str_find_ptr(&client->request.headers, "cookie", sizeof("cookie")-1))) {
     574           0 :                 return NULL;
     575             :         }
     576           0 :         return val;
     577             : } /* }}} */
     578             : 
     579           0 : static size_t sapi_cli_server_read_post(char *buf, size_t count_bytes) /* {{{ */
     580             : {
     581           0 :         php_cli_server_client *client = SG(server_context);
     582           0 :         if (client->request.content) {
     583           0 :                 size_t content_len = client->request.content_len;
     584           0 :                 size_t nbytes_copied = MIN(client->post_read_offset + count_bytes, content_len) - client->post_read_offset;
     585           0 :                 memmove(buf, client->request.content + client->post_read_offset, nbytes_copied);
     586           0 :                 client->post_read_offset += nbytes_copied;
     587           0 :                 return nbytes_copied;
     588             :         }
     589           0 :         return 0;
     590             : } /* }}} */
     591             : 
     592           0 : static void sapi_cli_server_register_variable(zval *track_vars_array, const char *key, const char *val) /* {{{ */
     593             : {
     594           0 :         char *new_val = (char *)val;
     595             :         size_t new_val_len;
     596             : 
     597           0 :         if (NULL == val) {
     598           0 :                 return;
     599             :         }
     600             : 
     601           0 :         if (sapi_module.input_filter(PARSE_SERVER, (char*)key, &new_val, strlen(val), &new_val_len)) {
     602           0 :                 php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array);
     603             :         }
     604             : } /* }}} */
     605             : 
     606           0 : static int sapi_cli_server_register_entry_cb(char **entry, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
     607           0 :         zval *track_vars_array = va_arg(args, zval *);
     608           0 :         if (hash_key->key) {
     609             :                 char *real_key, *key;
     610             :                 uint32_t i;
     611           0 :                 key = estrndup(ZSTR_VAL(hash_key->key), ZSTR_LEN(hash_key->key));
     612           0 :                 for(i=0; i<ZSTR_LEN(hash_key->key); i++) {
     613           0 :                         if (key[i] == '-') {
     614           0 :                                 key[i] = '_';
     615             :                         } else {
     616           0 :                                 key[i] = toupper(key[i]);
     617             :                         }
     618             :                 }
     619           0 :                 spprintf(&real_key, 0, "%s_%s", "HTTP", key);
     620           0 :                 if (strcmp(key, "CONTENT_TYPE") == 0 || strcmp(key, "CONTENT_LENGTH") == 0) {
     621           0 :                         sapi_cli_server_register_variable(track_vars_array, key, *entry);
     622             :                 }
     623           0 :                 sapi_cli_server_register_variable(track_vars_array, real_key, *entry);
     624           0 :                 efree(key);
     625           0 :                 efree(real_key);
     626             :         }
     627             : 
     628           0 :         return ZEND_HASH_APPLY_KEEP;
     629             : }
     630             : /* }}} */
     631             : 
     632           0 : static void sapi_cli_server_register_variables(zval *track_vars_array) /* {{{ */
     633             : {
     634           0 :         php_cli_server_client *client = SG(server_context);
     635           0 :         sapi_cli_server_register_variable(track_vars_array, "DOCUMENT_ROOT", client->server->document_root);
     636             :         {
     637             :                 char *tmp;
     638           0 :                 if ((tmp = strrchr(client->addr_str, ':'))) {
     639             :                         char addr[64], port[8];
     640           0 :                         strncpy(port, tmp + 1, 8);
     641           0 :                         port[7] = '\0';
     642           0 :                         strncpy(addr, client->addr_str, tmp - client->addr_str);
     643           0 :                         addr[tmp - client->addr_str] = '\0';
     644           0 :                         sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", addr);
     645           0 :                         sapi_cli_server_register_variable(track_vars_array, "REMOTE_PORT", port);
     646             :                 } else {
     647           0 :                         sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", client->addr_str);
     648             :                 }
     649             :         }
     650             :         {
     651             :                 char *tmp;
     652           0 :                 spprintf(&tmp, 0, "PHP %s Development Server", PHP_VERSION);
     653           0 :                 sapi_cli_server_register_variable(track_vars_array, "SERVER_SOFTWARE", tmp);
     654           0 :                 efree(tmp);
     655             :         }
     656             :         {
     657             :                 char *tmp;
     658           0 :                 spprintf(&tmp, 0, "HTTP/%d.%d", client->request.protocol_version / 100, client->request.protocol_version % 100);
     659           0 :                 sapi_cli_server_register_variable(track_vars_array, "SERVER_PROTOCOL", tmp);
     660           0 :                 efree(tmp);
     661             :         }
     662           0 :         sapi_cli_server_register_variable(track_vars_array, "SERVER_NAME", client->server->host);
     663             :         {
     664             :                 char *tmp;
     665           0 :                 spprintf(&tmp, 0, "%i",  client->server->port);
     666           0 :                 sapi_cli_server_register_variable(track_vars_array, "SERVER_PORT", tmp);
     667           0 :                 efree(tmp);
     668             :         }
     669             : 
     670           0 :         sapi_cli_server_register_variable(track_vars_array, "REQUEST_URI", client->request.request_uri);
     671           0 :         sapi_cli_server_register_variable(track_vars_array, "REQUEST_METHOD", SG(request_info).request_method);
     672           0 :         sapi_cli_server_register_variable(track_vars_array, "SCRIPT_NAME", client->request.vpath);
     673           0 :         if (SG(request_info).path_translated) {
     674           0 :                 sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", SG(request_info).path_translated);
     675           0 :         } else if (client->server->router) {
     676           0 :                 sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", client->server->router);
     677             :         }
     678           0 :         if (client->request.path_info) {
     679           0 :                 sapi_cli_server_register_variable(track_vars_array, "PATH_INFO", client->request.path_info);
     680             :         }
     681           0 :         if (client->request.path_info_len) {
     682             :                 char *tmp;
     683           0 :                 spprintf(&tmp, 0, "%s%s", client->request.vpath, client->request.path_info);
     684           0 :                 sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", tmp);
     685           0 :                 efree(tmp);
     686             :         } else {
     687           0 :                 sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", client->request.vpath);
     688             :         }
     689           0 :         if (client->request.query_string) {
     690           0 :                 sapi_cli_server_register_variable(track_vars_array, "QUERY_STRING", client->request.query_string);
     691             :         }
     692           0 :         zend_hash_apply_with_arguments(&client->request.headers, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
     693           0 : } /* }}} */
     694             : 
     695           0 : static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ */
     696             : {
     697             :         char buf[52];
     698             : 
     699           0 :         if (php_cli_server_get_system_time(buf) != 0) {
     700           0 :                 memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
     701             :         } else {
     702           0 :                 size_t l = strlen(buf);
     703           0 :                 if (l > 0) {
     704           0 :                         buf[l - 1] = '\0';
     705             :                 } else {
     706           0 :                         memmove(buf, "unknown", sizeof("unknown"));
     707             :                 }
     708             :         }
     709           0 :         fprintf(stderr, "[%s] %s\n", buf, msg);
     710           0 : } /* }}} */
     711             : 
     712             : /* {{{ sapi_module_struct cli_server_sapi_module
     713             :  */
     714             : sapi_module_struct cli_server_sapi_module = {
     715             :         "cli-server",                                                 /* name */
     716             :         "Built-in HTTP server",               /* pretty name */
     717             : 
     718             :         sapi_cli_server_startup,                                /* startup */
     719             :         php_module_shutdown_wrapper,    /* shutdown */
     720             : 
     721             :         NULL,                                                   /* activate */
     722             :         NULL,                                                   /* deactivate */
     723             : 
     724             :         sapi_cli_server_ub_write,               /* unbuffered write */
     725             :         sapi_cli_server_flush,                  /* flush */
     726             :         NULL,                                                   /* get uid */
     727             :         NULL,                                                   /* getenv */
     728             : 
     729             :         php_error,                                              /* error handler */
     730             : 
     731             :         NULL,                                                   /* header handler */
     732             :         sapi_cli_server_send_headers,   /* send headers handler */
     733             :         NULL,                                                   /* send header handler */
     734             : 
     735             :         sapi_cli_server_read_post,              /* read POST data */
     736             :         sapi_cli_server_read_cookies,   /* read Cookies */
     737             : 
     738             :         sapi_cli_server_register_variables,     /* register server variables */
     739             :         sapi_cli_server_log_message,    /* Log message */
     740             :         NULL,                                                   /* Get request time */
     741             :         NULL,                                                   /* Child terminate */
     742             : 
     743             :         STANDARD_SAPI_MODULE_PROPERTIES
     744             : }; /* }}} */
     745             : 
     746           0 : static int php_cli_server_poller_ctor(php_cli_server_poller *poller) /* {{{ */
     747             : {
     748           0 :         FD_ZERO(&poller->rfds);
     749           0 :         FD_ZERO(&poller->wfds);
     750           0 :         poller->max_fd = -1;
     751           0 :         return SUCCESS;
     752             : } /* }}} */
     753             : 
     754           0 : static void php_cli_server_poller_add(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
     755             : {
     756           0 :         if (mode & POLLIN) {
     757           0 :                 PHP_SAFE_FD_SET(fd, &poller->rfds);
     758             :         }
     759           0 :         if (mode & POLLOUT) {
     760           0 :                 PHP_SAFE_FD_SET(fd, &poller->wfds);
     761             :         }
     762           0 :         if (fd > poller->max_fd) {
     763           0 :                 poller->max_fd = fd;
     764             :         }
     765           0 : } /* }}} */
     766             : 
     767           0 : static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
     768             : {
     769           0 :         if (mode & POLLIN) {
     770           0 :                 PHP_SAFE_FD_CLR(fd, &poller->rfds);
     771             :         }
     772           0 :         if (mode & POLLOUT) {
     773           0 :                 PHP_SAFE_FD_CLR(fd, &poller->wfds);
     774             :         }
     775             : #ifndef PHP_WIN32
     776           0 :         if (fd == poller->max_fd) {
     777           0 :                 while (fd > 0) {
     778           0 :                         fd--;
     779           0 :                         if (PHP_SAFE_FD_ISSET(fd, &poller->rfds) || PHP_SAFE_FD_ISSET(fd, &poller->wfds)) {
     780             :                                 break;
     781             :                         }
     782             :                 }
     783           0 :                 poller->max_fd = fd;
     784             :         }
     785             : #endif
     786           0 : } /* }}} */
     787             : 
     788           0 : static int php_cli_server_poller_poll(php_cli_server_poller *poller, struct timeval *tv) /* {{{ */
     789             : {
     790           0 :         memmove(&poller->active.rfds, &poller->rfds, sizeof(poller->rfds));
     791           0 :         memmove(&poller->active.wfds, &poller->wfds, sizeof(poller->wfds));
     792           0 :         return php_select(poller->max_fd + 1, &poller->active.rfds, &poller->active.wfds, NULL, tv);
     793             : } /* }}} */
     794             : 
     795           0 : static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, void *opaque, int(*callback)(void *, php_socket_t fd, int events)) /* {{{ */
     796             : {
     797           0 :         int retval = SUCCESS;
     798             : #ifdef PHP_WIN32
     799             :         struct socket_entry {
     800             :                 SOCKET fd;
     801             :                 int events;
     802             :         } entries[FD_SETSIZE * 2];
     803             :         size_t i;
     804             :         struct socket_entry *n = entries, *m;
     805             : 
     806             :         for (i = 0; i < poller->active.rfds.fd_count; i++) {
     807             :                 n->events = POLLIN;
     808             :                 n->fd = poller->active.rfds.fd_array[i];
     809             :                 n++;
     810             :         }
     811             : 
     812             :         m = n;
     813             :         for (i = 0; i < poller->active.wfds.fd_count; i++) {
     814             :                 struct socket_entry *e;
     815             :                 SOCKET fd = poller->active.wfds.fd_array[i];
     816             :                 for (e = entries; e < m; e++) {
     817             :                         if (e->fd == fd) {
     818             :                                 e->events |= POLLOUT;
     819             :                         }
     820             :                 }
     821             :                 if (e == m) {
     822             :                         assert(n < entries + FD_SETSIZE * 2);
     823             :                         n->events = POLLOUT;
     824             :                         n->fd = fd;
     825             :                         n++;
     826             :                 }
     827             :         }
     828             : 
     829             :         {
     830             :                 struct socket_entry *e = entries;
     831             :                 for (; e < n; e++) {
     832             :                         if (SUCCESS != callback(opaque, e->fd, e->events)) {
     833             :                                 retval = FAILURE;
     834             :                         }
     835             :                 }
     836             :         }
     837             : 
     838             : #else
     839             :         php_socket_t fd;
     840           0 :         const php_socket_t max_fd = poller->max_fd;
     841             : 
     842           0 :         for (fd=0 ; fd<=max_fd ; fd++)  {
     843           0 :                 if (PHP_SAFE_FD_ISSET(fd, &poller->active.rfds)) {
     844           0 :                 if (SUCCESS != callback(opaque, fd, POLLIN)) {
     845           0 :                     retval = FAILURE;
     846             :                 }
     847             :                 }
     848           0 :                 if (PHP_SAFE_FD_ISSET(fd, &poller->active.wfds)) {
     849           0 :                 if (SUCCESS != callback(opaque, fd, POLLOUT)) {
     850           0 :                     retval = FAILURE;
     851             :                 }
     852             :                 }
     853             :         }
     854             : #endif
     855           0 :         return retval;
     856             : } /* }}} */
     857             : 
     858           0 : static size_t php_cli_server_chunk_size(const php_cli_server_chunk *chunk) /* {{{ */
     859             : {
     860           0 :         switch (chunk->type) {
     861           0 :         case PHP_CLI_SERVER_CHUNK_HEAP:
     862           0 :                 return chunk->data.heap.len;
     863           0 :         case PHP_CLI_SERVER_CHUNK_IMMORTAL:
     864           0 :                 return chunk->data.immortal.len;
     865             :         }
     866           0 :         return 0;
     867             : } /* }}} */
     868             : 
     869           0 : static void php_cli_server_chunk_dtor(php_cli_server_chunk *chunk) /* {{{ */
     870             : {
     871           0 :         switch (chunk->type) {
     872           0 :         case PHP_CLI_SERVER_CHUNK_HEAP:
     873           0 :                 if (chunk->data.heap.block != chunk) {
     874           0 :                         pefree(chunk->data.heap.block, 1);
     875             :                 }
     876           0 :                 break;
     877           0 :         case PHP_CLI_SERVER_CHUNK_IMMORTAL:
     878           0 :                 break;
     879             :         }
     880           0 : } /* }}} */
     881             : 
     882           0 : static void php_cli_server_buffer_dtor(php_cli_server_buffer *buffer) /* {{{ */
     883             : {
     884             :         php_cli_server_chunk *chunk, *next;
     885           0 :         for (chunk = buffer->first; chunk; chunk = next) {
     886           0 :                 next = chunk->next;
     887           0 :                 php_cli_server_chunk_dtor(chunk);
     888           0 :                 pefree(chunk, 1);
     889             :         }
     890           0 : } /* }}} */
     891             : 
     892           0 : static void php_cli_server_buffer_ctor(php_cli_server_buffer *buffer) /* {{{ */
     893             : {
     894           0 :         buffer->first = NULL;
     895           0 :         buffer->last = NULL;
     896           0 : } /* }}} */
     897             : 
     898           0 : static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk) /* {{{ */
     899             : {
     900             :         php_cli_server_chunk *last;
     901           0 :         for (last = chunk; last->next; last = last->next);
     902           0 :         if (!buffer->last) {
     903           0 :                 buffer->first = chunk;
     904             :         } else {
     905           0 :                 buffer->last->next = chunk;
     906             :         }
     907           0 :         buffer->last = last;
     908           0 : } /* }}} */
     909             : 
     910           0 : static void php_cli_server_buffer_prepend(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk) /* {{{ */
     911             : {
     912             :         php_cli_server_chunk *last;
     913           0 :         for (last = chunk; last->next; last = last->next);
     914           0 :         last->next = buffer->first;
     915           0 :         if (!buffer->last) {
     916           0 :                 buffer->last = last;
     917             :         }
     918           0 :         buffer->first = chunk;
     919           0 : } /* }}} */
     920             : 
     921           0 : static size_t php_cli_server_buffer_size(const php_cli_server_buffer *buffer) /* {{{ */
     922             : {
     923             :         php_cli_server_chunk *chunk;
     924           0 :         size_t retval = 0;
     925           0 :         for (chunk = buffer->first; chunk; chunk = chunk->next) {
     926           0 :                 retval += php_cli_server_chunk_size(chunk);
     927             :         }
     928           0 :         return retval;
     929             : } /* }}} */
     930             : 
     931           0 : static php_cli_server_chunk *php_cli_server_chunk_immortal_new(const char *buf, size_t len) /* {{{ */
     932             : {
     933           0 :         php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
     934             : 
     935           0 :         chunk->type = PHP_CLI_SERVER_CHUNK_IMMORTAL;
     936           0 :         chunk->next = NULL;
     937           0 :         chunk->data.immortal.p = buf;
     938           0 :         chunk->data.immortal.len = len;
     939           0 :         return chunk;
     940             : } /* }}} */
     941             : 
     942           0 : static php_cli_server_chunk *php_cli_server_chunk_heap_new(void *block, char *buf, size_t len) /* {{{ */
     943             : {
     944           0 :         php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
     945             : 
     946           0 :         chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
     947           0 :         chunk->next = NULL;
     948           0 :         chunk->data.heap.block = block;
     949           0 :         chunk->data.heap.p = buf;
     950           0 :         chunk->data.heap.len = len;
     951           0 :         return chunk;
     952             : } /* }}} */
     953             : 
     954           0 : static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len) /* {{{ */
     955             : {
     956           0 :         php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk) + len, 1);
     957             : 
     958           0 :         chunk->type = PHP_CLI_SERVER_CHUNK_HEAP;
     959           0 :         chunk->next = NULL;
     960           0 :         chunk->data.heap.block = chunk;
     961           0 :         chunk->data.heap.p = (char *)(chunk + 1);
     962           0 :         chunk->data.heap.len = len;
     963           0 :         return chunk;
     964             : } /* }}} */
     965             : 
     966           0 : static void php_cli_server_content_sender_dtor(php_cli_server_content_sender *sender) /* {{{ */
     967             : {
     968           0 :         php_cli_server_buffer_dtor(&sender->buffer);
     969           0 : } /* }}} */
     970             : 
     971           0 : static void php_cli_server_content_sender_ctor(php_cli_server_content_sender *sender) /* {{{ */
     972             : {
     973           0 :         php_cli_server_buffer_ctor(&sender->buffer);
     974           0 : } /* }}} */
     975             : 
     976           0 : static int php_cli_server_content_sender_send(php_cli_server_content_sender *sender, php_socket_t fd, size_t *nbytes_sent_total) /* {{{ */
     977             : {
     978             :         php_cli_server_chunk *chunk, *next;
     979           0 :         size_t _nbytes_sent_total = 0;
     980             : 
     981           0 :         for (chunk = sender->buffer.first; chunk; chunk = next) {
     982             : #ifdef PHP_WIN32
     983             :                 int nbytes_sent;
     984             : #else
     985             :                 ssize_t nbytes_sent;
     986             : #endif
     987           0 :                 next = chunk->next;
     988             : 
     989           0 :                 switch (chunk->type) {
     990           0 :                 case PHP_CLI_SERVER_CHUNK_HEAP:
     991             : #ifdef PHP_WIN32
     992             :                         nbytes_sent = send(fd, chunk->data.heap.p, (int)chunk->data.heap.len, 0);
     993             : #else
     994           0 :                         nbytes_sent = send(fd, chunk->data.heap.p, chunk->data.heap.len, 0);
     995             : #endif
     996           0 :                         if (nbytes_sent < 0) {
     997           0 :                                 *nbytes_sent_total = _nbytes_sent_total;
     998           0 :                                 return php_socket_errno();
     999             : #ifdef PHP_WIN32
    1000             :                         } else if (nbytes_sent == chunk->data.heap.len) {
    1001             : #else
    1002           0 :                         } else if (nbytes_sent == (ssize_t)chunk->data.heap.len) {
    1003             : #endif
    1004           0 :                                 php_cli_server_chunk_dtor(chunk);
    1005           0 :                                 pefree(chunk, 1);
    1006           0 :                                 sender->buffer.first = next;
    1007           0 :                                 if (!next) {
    1008           0 :                                         sender->buffer.last = NULL;
    1009             :                                 }
    1010             :                         } else {
    1011           0 :                                 chunk->data.heap.p += nbytes_sent;
    1012           0 :                                 chunk->data.heap.len -= nbytes_sent;
    1013             :                         }
    1014           0 :                         _nbytes_sent_total += nbytes_sent;
    1015           0 :                         break;
    1016             : 
    1017           0 :                 case PHP_CLI_SERVER_CHUNK_IMMORTAL:
    1018             : #ifdef PHP_WIN32
    1019             :                         nbytes_sent = send(fd, chunk->data.immortal.p, (int)chunk->data.immortal.len, 0);
    1020             : #else
    1021           0 :                         nbytes_sent = send(fd, chunk->data.immortal.p, chunk->data.immortal.len, 0);
    1022             : #endif
    1023           0 :                         if (nbytes_sent < 0) {
    1024           0 :                                 *nbytes_sent_total = _nbytes_sent_total;
    1025           0 :                                 return php_socket_errno();
    1026             : #ifdef PHP_WIN32
    1027             :                         } else if (nbytes_sent == chunk->data.immortal.len) {
    1028             : #else
    1029           0 :                         } else if (nbytes_sent == (ssize_t)chunk->data.immortal.len) {
    1030             : #endif
    1031           0 :                                 php_cli_server_chunk_dtor(chunk);
    1032           0 :                                 pefree(chunk, 1);
    1033           0 :                                 sender->buffer.first = next;
    1034           0 :                                 if (!next) {
    1035           0 :                                         sender->buffer.last = NULL;
    1036             :                                 }
    1037             :                         } else {
    1038           0 :                                 chunk->data.immortal.p += nbytes_sent;
    1039           0 :                                 chunk->data.immortal.len -= nbytes_sent;
    1040             :                         }
    1041           0 :                         _nbytes_sent_total += nbytes_sent;
    1042           0 :                         break;
    1043             :                 }
    1044             :         }
    1045           0 :         *nbytes_sent_total = _nbytes_sent_total;
    1046           0 :         return 0;
    1047             : } /* }}} */
    1048             : 
    1049           0 : static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sender, int fd, size_t *nbytes_read) /* {{{ */
    1050             : {
    1051             : #ifdef PHP_WIN32
    1052             :         int _nbytes_read;
    1053             : #else
    1054             :         ssize_t _nbytes_read;
    1055             : #endif
    1056           0 :         php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(131072);
    1057             : 
    1058             : #ifdef PHP_WIN32
    1059             :         _nbytes_read = read(fd, chunk->data.heap.p, (unsigned int)chunk->data.heap.len);
    1060             : #else
    1061           0 :         _nbytes_read = read(fd, chunk->data.heap.p, chunk->data.heap.len);
    1062             : #endif
    1063           0 :         if (_nbytes_read < 0) {
    1064           0 :                 char *errstr = get_last_error();
    1065           0 :                 php_cli_server_logf("%s", errstr);
    1066           0 :                 pefree(errstr, 1);
    1067           0 :                 php_cli_server_chunk_dtor(chunk);
    1068           0 :                 pefree(chunk, 1);
    1069           0 :                 return 1;
    1070             :         }
    1071           0 :         chunk->data.heap.len = _nbytes_read;
    1072           0 :         php_cli_server_buffer_append(&sender->buffer, chunk);
    1073           0 :         *nbytes_read = _nbytes_read;
    1074           0 :         return 0;
    1075             : } /* }}} */
    1076             : 
    1077             : #if HAVE_UNISTD_H
    1078           0 : static int php_cli_is_output_tty() /* {{{ */
    1079             : {
    1080           0 :         if (php_cli_output_is_tty == OUTPUT_NOT_CHECKED) {
    1081           0 :                 php_cli_output_is_tty = isatty(STDOUT_FILENO);
    1082             :         }
    1083           0 :         return php_cli_output_is_tty;
    1084             : } /* }}} */
    1085             : #endif
    1086             : 
    1087           0 : static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message) /* {{{ */
    1088             : {
    1089           0 :         int color = 0, effective_status = status;
    1090           0 :         char *basic_buf, *message_buf = "", *error_buf = "";
    1091           0 :         zend_bool append_error_message = 0;
    1092             : 
    1093           0 :         if (PG(last_error_message)) {
    1094           0 :                 switch (PG(last_error_type)) {
    1095           0 :                         case E_ERROR:
    1096             :                         case E_CORE_ERROR:
    1097             :                         case E_COMPILE_ERROR:
    1098             :                         case E_USER_ERROR:
    1099             :                         case E_PARSE:
    1100           0 :                                 if (status == 200) {
    1101             :                                         /* the status code isn't changed by a fatal error, so fake it */
    1102           0 :                                         effective_status = 500;
    1103             :                                 }
    1104             : 
    1105           0 :                                 append_error_message = 1;
    1106           0 :                                 break;
    1107             :                 }
    1108             :         }
    1109             : 
    1110             : #if HAVE_UNISTD_H
    1111           0 :         if (CLI_SERVER_G(color) && php_cli_is_output_tty() == OUTPUT_IS_TTY) {
    1112           0 :                 if (effective_status >= 500) {
    1113             :                         /* server error: red */
    1114           0 :                         color = 1;
    1115           0 :                 } else if (effective_status >= 400) {
    1116             :                         /* client error: yellow */
    1117           0 :                         color = 3;
    1118           0 :                 } else if (effective_status >= 200) {
    1119             :                         /* success: green */
    1120           0 :                         color = 2;
    1121             :                 }
    1122             :         }
    1123             : #endif
    1124             : 
    1125             :         /* basic */
    1126           0 :         spprintf(&basic_buf, 0, "%s [%d]: %s", client->addr_str, status, client->request.request_uri);
    1127           0 :         if (!basic_buf) {
    1128           0 :                 return;
    1129             :         }
    1130             : 
    1131             :         /* message */
    1132           0 :         if (message) {
    1133           0 :                 spprintf(&message_buf, 0, " - %s", message);
    1134           0 :                 if (!message_buf) {
    1135           0 :                         efree(basic_buf);
    1136           0 :                         return;
    1137             :                 }
    1138             :         }
    1139             : 
    1140             :         /* error */
    1141           0 :         if (append_error_message) {
    1142           0 :                 spprintf(&error_buf, 0, " - %s in %s on line %d", PG(last_error_message), PG(last_error_file), PG(last_error_lineno));
    1143           0 :                 if (!error_buf) {
    1144           0 :                         efree(basic_buf);
    1145           0 :                         if (message) {
    1146           0 :                                 efree(message_buf);
    1147             :                         }
    1148           0 :                         return;
    1149             :                 }
    1150             :         }
    1151             : 
    1152           0 :         if (color) {
    1153           0 :                 php_cli_server_logf("\x1b[3%dm%s%s%s\x1b[0m", color, basic_buf, message_buf, error_buf);
    1154             :         } else {
    1155           0 :                 php_cli_server_logf("%s%s%s", basic_buf, message_buf, error_buf);
    1156             :         }
    1157             : 
    1158           0 :         efree(basic_buf);
    1159           0 :         if (message) {
    1160           0 :                 efree(message_buf);
    1161             :         }
    1162           0 :         if (append_error_message) {
    1163           0 :                 efree(error_buf);
    1164             :         }
    1165             : } /* }}} */
    1166             : 
    1167           0 : static void php_cli_server_logf(const char *format, ...) /* {{{ */
    1168             : {
    1169           0 :         char *buf = NULL;
    1170             :         va_list ap;
    1171             : 
    1172           0 :         va_start(ap, format);
    1173           0 :         vspprintf(&buf, 0, format, ap);
    1174           0 :         va_end(ap);
    1175             : 
    1176           0 :         if (!buf) {
    1177           0 :                 return;
    1178             :         }
    1179             : 
    1180           0 :         if (sapi_module.log_message) {
    1181           0 :                 sapi_module.log_message(buf, -1);
    1182             :         }
    1183             : 
    1184           0 :         efree(buf);
    1185             : } /* }}} */
    1186             : 
    1187           0 : static php_socket_t php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, zend_string **errstr) /* {{{ */
    1188             : {
    1189           0 :         php_socket_t retval = SOCK_ERR;
    1190           0 :         int err = 0;
    1191           0 :         struct sockaddr *sa = NULL, **p, **sal;
    1192             : 
    1193           0 :         int num_addrs = php_network_getaddresses(host, socktype, &sal, errstr);
    1194           0 :         if (num_addrs == 0) {
    1195           0 :                 return -1;
    1196             :         }
    1197           0 :         for (p = sal; *p; p++) {
    1198           0 :                 if (sa) {
    1199           0 :                         pefree(sa, 1);
    1200           0 :                         sa = NULL;
    1201             :                 }
    1202             : 
    1203           0 :                 retval = socket((*p)->sa_family, socktype, 0);
    1204           0 :                 if (retval == SOCK_ERR) {
    1205           0 :                         continue;
    1206             :                 }
    1207             : 
    1208           0 :                 switch ((*p)->sa_family) {
    1209             : #if HAVE_GETADDRINFO && HAVE_IPV6
    1210           0 :                 case AF_INET6:
    1211           0 :                         sa = pemalloc(sizeof(struct sockaddr_in6), 1);
    1212           0 :                         *(struct sockaddr_in6 *)sa = *(struct sockaddr_in6 *)*p;
    1213           0 :                         ((struct sockaddr_in6 *)sa)->sin6_port = htons(*port);
    1214           0 :                         *socklen = sizeof(struct sockaddr_in6);
    1215           0 :                         break;
    1216             : #endif
    1217           0 :                 case AF_INET:
    1218           0 :                         sa = pemalloc(sizeof(struct sockaddr_in), 1);
    1219           0 :                         *(struct sockaddr_in *)sa = *(struct sockaddr_in *)*p;
    1220           0 :                         ((struct sockaddr_in *)sa)->sin_port = htons(*port);
    1221           0 :                         *socklen = sizeof(struct sockaddr_in);
    1222           0 :                         break;
    1223           0 :                 default:
    1224             :                         /* Unknown family */
    1225           0 :                         *socklen = 0;
    1226           0 :                         closesocket(retval);
    1227           0 :                         continue;
    1228             :                 }
    1229             : 
    1230             : #ifdef SO_REUSEADDR
    1231             :                 {
    1232           0 :                         int val = 1;
    1233           0 :                         setsockopt(retval, SOL_SOCKET, SO_REUSEADDR, (char*)&val, sizeof(val));
    1234             :                 }
    1235             : #endif
    1236             : 
    1237           0 :                 if (bind(retval, sa, *socklen) == SOCK_CONN_ERR) {
    1238           0 :                         err = php_socket_errno();
    1239           0 :                         if (err == SOCK_EINVAL || err == SOCK_EADDRINUSE) {
    1240             :                                 goto out;
    1241             :                         }
    1242           0 :                         closesocket(retval);
    1243           0 :                         retval = SOCK_ERR;
    1244           0 :                         continue;
    1245             :                 }
    1246           0 :                 err = 0;
    1247             : 
    1248           0 :                 *af = sa->sa_family;
    1249           0 :                 if (*port == 0) {
    1250           0 :                         if (getsockname(retval, sa, socklen)) {
    1251           0 :                                 err = php_socket_errno();
    1252           0 :                                 goto out;
    1253             :                         }
    1254           0 :                         switch (sa->sa_family) {
    1255             : #if HAVE_GETADDRINFO && HAVE_IPV6
    1256           0 :                         case AF_INET6:
    1257           0 :                                 *port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
    1258           0 :                                 break;
    1259             : #endif
    1260           0 :                         case AF_INET:
    1261           0 :                                 *port = ntohs(((struct sockaddr_in *)sa)->sin_port);
    1262           0 :                                 break;
    1263             :                         }
    1264             :                 }
    1265             : 
    1266           0 :                 break;
    1267             :         }
    1268             : 
    1269           0 :         if (retval == SOCK_ERR) {
    1270           0 :                 goto out;
    1271             :         }
    1272             : 
    1273           0 :         if (listen(retval, SOMAXCONN)) {
    1274           0 :                 err = php_socket_errno();
    1275           0 :                 goto out;
    1276             :         }
    1277             : 
    1278           0 : out:
    1279           0 :         if (sa) {
    1280           0 :                 pefree(sa, 1);
    1281             :         }
    1282           0 :         if (sal) {
    1283           0 :                 php_network_freeaddresses(sal);
    1284             :         }
    1285           0 :         if (err) {
    1286           0 :                 if (ZEND_VALID_SOCKET(retval)) {
    1287           0 :                         closesocket(retval);
    1288             :                 }
    1289           0 :                 if (errstr) {
    1290           0 :                         *errstr = php_socket_error_str(err);
    1291             :                 }
    1292           0 :                 return SOCK_ERR;
    1293             :         }
    1294           0 :         return retval;
    1295             : } /* }}} */
    1296             : 
    1297           0 : static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */
    1298             : {
    1299             : #ifdef ZTS
    1300             : ZEND_TSRMLS_CACHE_UPDATE();
    1301             : #endif
    1302           0 :         req->protocol_version = 0;
    1303           0 :         req->request_uri = NULL;
    1304           0 :         req->request_uri_len = 0;
    1305           0 :         req->vpath = NULL;
    1306           0 :         req->vpath_len = 0;
    1307           0 :         req->path_translated = NULL;
    1308           0 :         req->path_translated_len = 0;
    1309           0 :         req->path_info = NULL;
    1310           0 :         req->path_info_len = 0;
    1311           0 :         req->query_string = NULL;
    1312           0 :         req->query_string_len = 0;
    1313           0 :         zend_hash_init(&req->headers, 0, NULL, char_ptr_dtor_p, 1);
    1314           0 :         zend_hash_init(&req->headers_original_case, 0, NULL, NULL, 1);
    1315           0 :         req->content = NULL;
    1316           0 :         req->content_len = 0;
    1317           0 :         req->ext = NULL;
    1318           0 :         req->ext_len = 0;
    1319           0 :         return SUCCESS;
    1320             : } /* }}} */
    1321             : 
    1322           0 : static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */
    1323             : {
    1324           0 :         if (req->request_uri) {
    1325           0 :                 pefree(req->request_uri, 1);
    1326             :         }
    1327           0 :         if (req->vpath) {
    1328           0 :                 pefree(req->vpath, 1);
    1329             :         }
    1330           0 :         if (req->path_translated) {
    1331           0 :                 pefree(req->path_translated, 1);
    1332             :         }
    1333           0 :         if (req->path_info) {
    1334           0 :                 pefree(req->path_info, 1);
    1335             :         }
    1336           0 :         if (req->query_string) {
    1337           0 :                 pefree(req->query_string, 1);
    1338             :         }
    1339           0 :         zend_hash_destroy(&req->headers);
    1340           0 :         zend_hash_destroy(&req->headers_original_case);
    1341           0 :         if (req->content) {
    1342           0 :                 pefree(req->content, 1);
    1343             :         }
    1344           0 : } /* }}} */
    1345             : 
    1346           0 : static void php_cli_server_request_translate_vpath(php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
    1347             : {
    1348             :         zend_stat_t sb;
    1349             :         static const char *index_files[] = { "index.php", "index.html", NULL };
    1350           0 :         char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
    1351           0 :         char *p = buf, *prev_path = NULL, *q, *vpath;
    1352           0 :         size_t prev_path_len = 0;
    1353           0 :         int  is_static_file = 0;
    1354             : 
    1355           0 :         memmove(p, document_root, document_root_len);
    1356           0 :         p += document_root_len;
    1357           0 :         vpath = p;
    1358           0 :         if (request->vpath_len > 0 && request->vpath[0] != '/') {
    1359           0 :                 *p++ = DEFAULT_SLASH;
    1360             :         }
    1361           0 :         q = request->vpath + request->vpath_len;
    1362           0 :         while (q > request->vpath) {
    1363           0 :                 if (*q-- == '.') {
    1364           0 :                         is_static_file = 1;
    1365           0 :                         break;
    1366             :                 }
    1367             :         }
    1368           0 :         memmove(p, request->vpath, request->vpath_len);
    1369             : #ifdef PHP_WIN32
    1370             :         q = p + request->vpath_len;
    1371             :         do {
    1372             :                 if (*q == '/') {
    1373             :                         *q = '\\';
    1374             :                 }
    1375             :         } while (q-- > p);
    1376             : #endif
    1377           0 :         p += request->vpath_len;
    1378           0 :         *p = '\0';
    1379           0 :         q = p;
    1380           0 :         while (q > buf) {
    1381           0 :                 if (!php_sys_stat(buf, &sb)) {
    1382           0 :                         if (sb.st_mode & S_IFDIR) {
    1383           0 :                                 const char **file = index_files;
    1384           0 :                                 if (q[-1] != DEFAULT_SLASH) {
    1385           0 :                                         *q++ = DEFAULT_SLASH;
    1386             :                                 }
    1387           0 :                                 while (*file) {
    1388           0 :                                         size_t l = strlen(*file);
    1389           0 :                                         memmove(q, *file, l + 1);
    1390           0 :                                         if (!php_sys_stat(buf, &sb) && (sb.st_mode & S_IFREG)) {
    1391           0 :                                                 q += l;
    1392           0 :                                                 break;
    1393             :                                         }
    1394           0 :                                         file++;
    1395             :                                 }
    1396           0 :                                 if (!*file || is_static_file) {
    1397           0 :                                         if (prev_path) {
    1398           0 :                                                 pefree(prev_path, 1);
    1399             :                                         }
    1400           0 :                                         pefree(buf, 1);
    1401           0 :                                         return;
    1402             :                                 }
    1403             :                         }
    1404           0 :                         break; /* regular file */
    1405             :                 }
    1406           0 :                 if (prev_path) {
    1407           0 :                         pefree(prev_path, 1);
    1408           0 :                         *q = DEFAULT_SLASH;
    1409             :                 }
    1410           0 :                 while (q > buf && *(--q) != DEFAULT_SLASH);
    1411           0 :                 prev_path_len = p - q;
    1412           0 :                 prev_path = pestrndup(q, prev_path_len, 1);
    1413           0 :                 *q = '\0';
    1414             :         }
    1415           0 :         if (prev_path) {
    1416           0 :                 request->path_info_len = prev_path_len;
    1417             : #ifdef PHP_WIN32
    1418             :                 while (prev_path_len--) {
    1419             :                         if (prev_path[prev_path_len] == '\\') {
    1420             :                                 prev_path[prev_path_len] = '/';
    1421             :                         }
    1422             :                 }
    1423             : #endif
    1424           0 :                 request->path_info = prev_path;
    1425           0 :                 pefree(request->vpath, 1);
    1426           0 :                 request->vpath = pestrndup(vpath, q - vpath, 1);
    1427           0 :                 request->vpath_len = q - vpath;
    1428           0 :                 request->path_translated = buf;
    1429           0 :                 request->path_translated_len = q - buf;
    1430             :         } else {
    1431           0 :                 pefree(request->vpath, 1);
    1432           0 :                 request->vpath = pestrndup(vpath, q - vpath, 1);
    1433           0 :                 request->vpath_len = q - vpath;
    1434           0 :                 request->path_translated = buf;
    1435           0 :                 request->path_translated_len = q - buf;
    1436             :         }
    1437             : #ifdef PHP_WIN32
    1438             :         {
    1439             :                 uint32_t i = 0;
    1440             :                 for (;i<request->vpath_len;i++) {
    1441             :                         if (request->vpath[i] == '\\') {
    1442             :                                 request->vpath[i] = '/';
    1443             :                         }
    1444             :                 }
    1445             :         }
    1446             : #endif
    1447           0 :         request->sb = sb;
    1448             : } /* }}} */
    1449             : 
    1450           0 : static void normalize_vpath(char **retval, size_t *retval_len, const char *vpath, size_t vpath_len, int persistent) /* {{{ */
    1451             : {
    1452           0 :         char *decoded_vpath = NULL;
    1453             :         char *decoded_vpath_end;
    1454             :         char *p;
    1455             : 
    1456           0 :         *retval = NULL;
    1457             : 
    1458           0 :         decoded_vpath = pestrndup(vpath, vpath_len, persistent);
    1459           0 :         if (!decoded_vpath) {
    1460           0 :                 return;
    1461             :         }
    1462             : 
    1463           0 :         decoded_vpath_end = decoded_vpath + php_raw_url_decode(decoded_vpath, (int)vpath_len);
    1464             : 
    1465             : #ifdef PHP_WIN32
    1466             :         {
    1467             :                 char *p = decoded_vpath;
    1468             : 
    1469             :                 do {
    1470             :                         if (*p == '\\') {
    1471             :                                 *p = '/';
    1472             :                         }
    1473             :                 } while (*p++);
    1474             :         }
    1475             : #endif
    1476             : 
    1477           0 :         p = decoded_vpath;
    1478             : 
    1479           0 :         if (p < decoded_vpath_end && *p == '/') {
    1480           0 :                 char *n = p;
    1481           0 :                 while (n < decoded_vpath_end && *n == '/') n++;
    1482           0 :                 memmove(++p, n, decoded_vpath_end - n);
    1483           0 :                 decoded_vpath_end -= n - p;
    1484             :         }
    1485             : 
    1486           0 :         while (p < decoded_vpath_end) {
    1487           0 :                 char *n = p;
    1488           0 :                 while (n < decoded_vpath_end && *n != '/') n++;
    1489           0 :                 if (n - p == 2 && p[0] == '.' && p[1] == '.') {
    1490           0 :                         if (p > decoded_vpath) {
    1491           0 :                                 --p;
    1492             :                                 for (;;) {
    1493           0 :                                         if (p == decoded_vpath) {
    1494           0 :                                                 if (*p == '/') {
    1495           0 :                                                         p++;
    1496             :                                                 }
    1497           0 :                                                 break;
    1498             :                                         }
    1499           0 :                                         if (*(--p) == '/') {
    1500           0 :                                                 p++;
    1501           0 :                                                 break;
    1502             :                                         }
    1503             :                                 }
    1504             :                         }
    1505           0 :                         while (n < decoded_vpath_end && *n == '/') n++;
    1506           0 :                         memmove(p, n, decoded_vpath_end - n);
    1507           0 :                         decoded_vpath_end -= n - p;
    1508           0 :                 } else if (n - p == 1 && p[0] == '.') {
    1509           0 :                         while (n < decoded_vpath_end && *n == '/') n++;
    1510           0 :                         memmove(p, n, decoded_vpath_end - n);
    1511           0 :                         decoded_vpath_end -= n - p;
    1512             :                 } else {
    1513           0 :                         if (n < decoded_vpath_end) {
    1514           0 :                                 char *nn = n;
    1515           0 :                                 while (nn < decoded_vpath_end && *nn == '/') nn++;
    1516           0 :                                 p = n + 1;
    1517           0 :                                 memmove(p, nn, decoded_vpath_end - nn);
    1518           0 :                                 decoded_vpath_end -= nn - p;
    1519             :                         } else {
    1520           0 :                                 p = n;
    1521             :                         }
    1522             :                 }
    1523             :         }
    1524             : 
    1525           0 :         *decoded_vpath_end = '\0';
    1526           0 :         *retval = decoded_vpath;
    1527           0 :         *retval_len = decoded_vpath_end - decoded_vpath;
    1528             : } /* }}} */
    1529             : 
    1530             : /* {{{ php_cli_server_client_read_request */
    1531           0 : static int php_cli_server_client_read_request_on_message_begin(php_http_parser *parser)
    1532             : {
    1533           0 :         return 0;
    1534             : }
    1535             : 
    1536           0 : static int php_cli_server_client_read_request_on_path(php_http_parser *parser, const char *at, size_t length)
    1537             : {
    1538           0 :         php_cli_server_client *client = parser->data;
    1539             :         {
    1540             :                 char *vpath;
    1541             :                 size_t vpath_len;
    1542           0 :                 normalize_vpath(&vpath, &vpath_len, at, length, 1);
    1543           0 :                 client->request.vpath = vpath;
    1544           0 :                 client->request.vpath_len = vpath_len;
    1545             :         }
    1546           0 :         return 0;
    1547             : }
    1548             : 
    1549           0 : static int php_cli_server_client_read_request_on_query_string(php_http_parser *parser, const char *at, size_t length)
    1550             : {
    1551           0 :         php_cli_server_client *client = parser->data;
    1552           0 :         client->request.query_string = pestrndup(at, length, 1);
    1553           0 :         client->request.query_string_len = length;
    1554           0 :         return 0;
    1555             : }
    1556             : 
    1557           0 : static int php_cli_server_client_read_request_on_url(php_http_parser *parser, const char *at, size_t length)
    1558             : {
    1559           0 :         php_cli_server_client *client = parser->data;
    1560           0 :         client->request.request_method = parser->method;
    1561           0 :         client->request.request_uri = pestrndup(at, length, 1);
    1562           0 :         client->request.request_uri_len = length;
    1563           0 :         return 0;
    1564             : }
    1565             : 
    1566           0 : static int php_cli_server_client_read_request_on_fragment(php_http_parser *parser, const char *at, size_t length)
    1567             : {
    1568           0 :         return 0;
    1569             : }
    1570             : 
    1571           0 : static void php_cli_server_client_save_header(php_cli_server_client *client)
    1572             : {
    1573             :         /* strip off the colon */
    1574           0 :         zend_string *orig_header_name = zend_string_init(client->current_header_name, client->current_header_name_len, 1);
    1575           0 :         zend_string *lc_header_name = zend_string_alloc(client->current_header_name_len, 1);
    1576           0 :         zend_str_tolower_copy(ZSTR_VAL(lc_header_name), client->current_header_name, client->current_header_name_len);
    1577             :         GC_MAKE_PERSISTENT_LOCAL(orig_header_name);
    1578             :         GC_MAKE_PERSISTENT_LOCAL(lc_header_name);
    1579           0 :         zend_hash_add_ptr(&client->request.headers, lc_header_name, client->current_header_value);
    1580           0 :         zend_hash_add_ptr(&client->request.headers_original_case, orig_header_name, client->current_header_value);
    1581             :         zend_string_release_ex(lc_header_name, 1);
    1582             :         zend_string_release_ex(orig_header_name, 1);
    1583             : 
    1584           0 :         if (client->current_header_name_allocated) {
    1585           0 :                 pefree(client->current_header_name, 1);
    1586           0 :                 client->current_header_name_allocated = 0;
    1587             :         }
    1588           0 :         client->current_header_name = NULL;
    1589           0 :         client->current_header_name_len = 0;
    1590           0 :         client->current_header_value = NULL;
    1591           0 :         client->current_header_value_len = 0;
    1592           0 : }
    1593             : 
    1594           0 : static int php_cli_server_client_read_request_on_header_field(php_http_parser *parser, const char *at, size_t length)
    1595             : {
    1596           0 :         php_cli_server_client *client = parser->data;
    1597           0 :         switch (client->last_header_element) {
    1598           0 :         case HEADER_VALUE:
    1599           0 :                 php_cli_server_client_save_header(client);
    1600             :                 /* break missing intentionally */
    1601           0 :         case HEADER_NONE:
    1602           0 :                 client->current_header_name = (char *)at;
    1603           0 :                 client->current_header_name_len = length;
    1604           0 :                 break;
    1605           0 :         case HEADER_FIELD:
    1606           0 :                 if (client->current_header_name_allocated) {
    1607           0 :                         size_t new_length = client->current_header_name_len + length;
    1608           0 :                         client->current_header_name = perealloc(client->current_header_name, new_length + 1, 1);
    1609           0 :                         memcpy(client->current_header_name + client->current_header_name_len, at, length);
    1610           0 :                         client->current_header_name[new_length] = '\0';
    1611           0 :                         client->current_header_name_len = new_length;
    1612             :                 } else {
    1613           0 :                         size_t new_length = client->current_header_name_len + length;
    1614           0 :                         char* field = pemalloc(new_length + 1, 1);
    1615           0 :                         memcpy(field, client->current_header_name, client->current_header_name_len);
    1616           0 :                         memcpy(field + client->current_header_name_len, at, length);
    1617           0 :                         field[new_length] = '\0';
    1618           0 :                         client->current_header_name = field;
    1619           0 :                         client->current_header_name_len = new_length;
    1620           0 :                         client->current_header_name_allocated = 1;
    1621             :                 }
    1622           0 :                 break;
    1623             :         }
    1624             : 
    1625           0 :         client->last_header_element = HEADER_FIELD;
    1626           0 :         return 0;
    1627             : }
    1628             : 
    1629           0 : static int php_cli_server_client_read_request_on_header_value(php_http_parser *parser, const char *at, size_t length)
    1630             : {
    1631           0 :         php_cli_server_client *client = parser->data;
    1632           0 :         switch (client->last_header_element) {
    1633           0 :         case HEADER_FIELD:
    1634           0 :                 client->current_header_value = pestrndup(at, length, 1);
    1635           0 :                 client->current_header_value_len = length;
    1636           0 :                 break;
    1637           0 :         case HEADER_VALUE:
    1638             :                 {
    1639           0 :                         size_t new_length = client->current_header_value_len + length;
    1640           0 :                         client->current_header_value = perealloc(client->current_header_value, new_length + 1, 1);
    1641           0 :                         memcpy(client->current_header_value + client->current_header_value_len, at, length);
    1642           0 :                         client->current_header_value[new_length] = '\0';
    1643           0 :                         client->current_header_value_len = new_length;
    1644             :                 }
    1645           0 :                 break;
    1646           0 :         case HEADER_NONE:
    1647             :                 // can't happen
    1648             :                 assert(0);
    1649           0 :                 break;
    1650             :         }
    1651           0 :         client->last_header_element = HEADER_VALUE;
    1652           0 :         return 0;
    1653             : }
    1654             : 
    1655           0 : static int php_cli_server_client_read_request_on_headers_complete(php_http_parser *parser)
    1656             : {
    1657           0 :         php_cli_server_client *client = parser->data;
    1658           0 :         switch (client->last_header_element) {
    1659           0 :         case HEADER_NONE:
    1660           0 :                 break;
    1661           0 :         case HEADER_FIELD:
    1662           0 :                 client->current_header_value = pemalloc(1, 1);
    1663           0 :                 *client->current_header_value = '\0';
    1664           0 :                 client->current_header_value_len = 0;
    1665             :                 /* break missing intentionally */
    1666           0 :         case HEADER_VALUE:
    1667           0 :                 php_cli_server_client_save_header(client);
    1668           0 :                 break;
    1669             :         }
    1670           0 :         client->last_header_element = HEADER_NONE;
    1671           0 :         return 0;
    1672             : }
    1673             : 
    1674           0 : static int php_cli_server_client_read_request_on_body(php_http_parser *parser, const char *at, size_t length)
    1675             : {
    1676           0 :         php_cli_server_client *client = parser->data;
    1677           0 :         if (!client->request.content) {
    1678           0 :                 client->request.content = pemalloc(parser->content_length, 1);
    1679           0 :                 client->request.content_len = 0;
    1680             :         }
    1681           0 :         client->request.content = perealloc(client->request.content, client->request.content_len + length, 1);
    1682           0 :         memmove(client->request.content + client->request.content_len, at, length);
    1683           0 :         client->request.content_len += length;
    1684           0 :         return 0;
    1685             : }
    1686             : 
    1687           0 : static int php_cli_server_client_read_request_on_message_complete(php_http_parser *parser)
    1688             : {
    1689           0 :         php_cli_server_client *client = parser->data;
    1690           0 :         client->request.protocol_version = parser->http_major * 100 + parser->http_minor;
    1691           0 :         php_cli_server_request_translate_vpath(&client->request, client->server->document_root, client->server->document_root_len);
    1692             :         {
    1693           0 :                 const char *vpath = client->request.vpath, *end = vpath + client->request.vpath_len, *p = end;
    1694           0 :                 client->request.ext = end;
    1695           0 :                 client->request.ext_len = 0;
    1696           0 :                 while (p > vpath) {
    1697           0 :                         --p;
    1698           0 :                         if (*p == '.') {
    1699           0 :                                 ++p;
    1700           0 :                                 client->request.ext = p;
    1701           0 :                                 client->request.ext_len = end - p;
    1702           0 :                                 break;
    1703             :                         }
    1704             :                 }
    1705             :         }
    1706           0 :         client->request_read = 1;
    1707           0 :         return 0;
    1708             : }
    1709             : 
    1710           0 : static int php_cli_server_client_read_request(php_cli_server_client *client, char **errstr)
    1711             : {
    1712             :         char buf[16384];
    1713             :         static const php_http_parser_settings settings = {
    1714             :                 php_cli_server_client_read_request_on_message_begin,
    1715             :                 php_cli_server_client_read_request_on_path,
    1716             :                 php_cli_server_client_read_request_on_query_string,
    1717             :                 php_cli_server_client_read_request_on_url,
    1718             :                 php_cli_server_client_read_request_on_fragment,
    1719             :                 php_cli_server_client_read_request_on_header_field,
    1720             :                 php_cli_server_client_read_request_on_header_value,
    1721             :                 php_cli_server_client_read_request_on_headers_complete,
    1722             :                 php_cli_server_client_read_request_on_body,
    1723             :                 php_cli_server_client_read_request_on_message_complete
    1724             :         };
    1725             :         size_t nbytes_consumed;
    1726             :         int nbytes_read;
    1727           0 :         if (client->request_read) {
    1728           0 :                 return 1;
    1729             :         }
    1730           0 :         nbytes_read = recv(client->sock, buf, sizeof(buf) - 1, 0);
    1731           0 :         if (nbytes_read < 0) {
    1732           0 :                 int err = php_socket_errno();
    1733           0 :                 if (err == SOCK_EAGAIN) {
    1734           0 :                         return 0;
    1735             :                 }
    1736           0 :                 *errstr = php_socket_strerror(err, NULL, 0);
    1737           0 :                 return -1;
    1738           0 :         } else if (nbytes_read == 0) {
    1739           0 :                 *errstr = estrdup(php_cli_server_request_error_unexpected_eof);
    1740           0 :                 return -1;
    1741             :         }
    1742           0 :         client->parser.data = client;
    1743           0 :         nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, nbytes_read);
    1744           0 :         if (nbytes_consumed != (size_t)nbytes_read) {
    1745           0 :                 if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
    1746           0 :                         *errstr = estrdup("Unsupported SSL request");
    1747             :                 } else {
    1748           0 :                         *errstr = estrdup("Malformed HTTP request");
    1749             :                 }
    1750           0 :                 return -1;
    1751             :         }
    1752           0 :         if (client->current_header_name) {
    1753           0 :                 char *header_name = safe_pemalloc(client->current_header_name_len, 1, 1, 1);
    1754           0 :                 memmove(header_name, client->current_header_name, client->current_header_name_len);
    1755           0 :                 client->current_header_name = header_name;
    1756           0 :                 client->current_header_name_allocated = 1;
    1757             :         }
    1758           0 :         return client->request_read ? 1: 0;
    1759             : }
    1760             : /* }}} */
    1761             : 
    1762           0 : static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len) /* {{{ */
    1763             : {
    1764           0 :         struct timeval tv = { 10, 0 };
    1765             : #ifdef PHP_WIN32
    1766             :         int nbytes_left = (int)str_len;
    1767             : #else
    1768           0 :         ssize_t nbytes_left = (ssize_t)str_len;
    1769             : #endif
    1770             :         do {
    1771             : #ifdef PHP_WIN32
    1772             :                 int nbytes_sent;
    1773             : #else
    1774             :                 ssize_t nbytes_sent;
    1775             : #endif
    1776             : 
    1777           0 :                 nbytes_sent = send(client->sock, str + str_len - nbytes_left, nbytes_left, 0);
    1778           0 :                 if (nbytes_sent < 0) {
    1779           0 :                         int err = php_socket_errno();
    1780           0 :                         if (err == SOCK_EAGAIN) {
    1781           0 :                                 int nfds = php_pollfd_for(client->sock, POLLOUT, &tv);
    1782           0 :                                 if (nfds > 0) {
    1783           0 :                                         continue;
    1784           0 :                                 } else if (nfds < 0) {
    1785             :                                         /* error */
    1786           0 :                                         php_handle_aborted_connection();
    1787           0 :                                         return nbytes_left;
    1788             :                                 } else {
    1789             :                                         /* timeout */
    1790           0 :                                         php_handle_aborted_connection();
    1791           0 :                                         return nbytes_left;
    1792             :                                 }
    1793             :                         } else {
    1794           0 :                                 php_handle_aborted_connection();
    1795           0 :                                 return nbytes_left;
    1796             :                         }
    1797             :                 }
    1798           0 :                 nbytes_left -= nbytes_sent;
    1799           0 :         } while (nbytes_left > 0);
    1800             : 
    1801           0 :         return str_len;
    1802             : } /* }}} */
    1803             : 
    1804           0 : static void php_cli_server_client_populate_request_info(const php_cli_server_client *client, sapi_request_info *request_info) /* {{{ */
    1805             : {
    1806             :         char *val;
    1807             : 
    1808           0 :         request_info->request_method = php_http_method_str(client->request.request_method);
    1809           0 :         request_info->proto_num = client->request.protocol_version;
    1810           0 :         request_info->request_uri = client->request.request_uri;
    1811           0 :         request_info->path_translated = client->request.path_translated;
    1812           0 :         request_info->query_string = client->request.query_string;
    1813           0 :         request_info->content_length = client->request.content_len;
    1814           0 :         request_info->auth_user = request_info->auth_password = request_info->auth_digest = NULL;
    1815           0 :         if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "content-type", sizeof("content-type")-1))) {
    1816           0 :                 request_info->content_type = val;
    1817             :         }
    1818           0 : } /* }}} */
    1819             : 
    1820           0 : static void destroy_request_info(sapi_request_info *request_info) /* {{{ */
    1821             : {
    1822           0 : } /* }}} */
    1823             : 
    1824           0 : static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_server *server, php_socket_t client_sock, struct sockaddr *addr, socklen_t addr_len) /* {{{ */
    1825             : {
    1826           0 :         client->server = server;
    1827           0 :         client->sock = client_sock;
    1828           0 :         client->addr = addr;
    1829           0 :         client->addr_len = addr_len;
    1830             :         {
    1831           0 :                 zend_string *addr_str = 0;
    1832             : 
    1833           0 :                 php_network_populate_name_from_sockaddr(addr, addr_len, &addr_str, NULL, 0);
    1834           0 :                 client->addr_str = pestrndup(ZSTR_VAL(addr_str), ZSTR_LEN(addr_str), 1);
    1835           0 :                 client->addr_str_len = ZSTR_LEN(addr_str);
    1836           0 :                 zend_string_release_ex(addr_str, 0);
    1837             :         }
    1838           0 :         php_http_parser_init(&client->parser, PHP_HTTP_REQUEST);
    1839           0 :         client->request_read = 0;
    1840             : 
    1841           0 :         client->last_header_element = HEADER_NONE;
    1842           0 :         client->current_header_name = NULL;
    1843           0 :         client->current_header_name_len = 0;
    1844           0 :         client->current_header_name_allocated = 0;
    1845           0 :         client->current_header_value = NULL;
    1846           0 :         client->current_header_value_len = 0;
    1847             : 
    1848           0 :         client->post_read_offset = 0;
    1849           0 :         if (FAILURE == php_cli_server_request_ctor(&client->request)) {
    1850           0 :                 return FAILURE;
    1851             :         }
    1852           0 :         client->content_sender_initialized = 0;
    1853           0 :         client->file_fd = -1;
    1854           0 :         return SUCCESS;
    1855             : } /* }}} */
    1856             : 
    1857           0 : static void php_cli_server_client_dtor(php_cli_server_client *client) /* {{{ */
    1858             : {
    1859           0 :         php_cli_server_request_dtor(&client->request);
    1860           0 :         if (client->file_fd >= 0) {
    1861           0 :                 close(client->file_fd);
    1862           0 :                 client->file_fd = -1;
    1863             :         }
    1864           0 :         pefree(client->addr, 1);
    1865           0 :         pefree(client->addr_str, 1);
    1866           0 :         if (client->content_sender_initialized) {
    1867           0 :                 php_cli_server_content_sender_dtor(&client->content_sender);
    1868             :         }
    1869           0 : } /* }}} */
    1870             : 
    1871           0 : static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    1872             : {
    1873             : #if PHP_DEBUG
    1874             :         php_cli_server_logf("%s Closing", client->addr_str);
    1875             : #endif
    1876           0 :         zend_hash_index_del(&server->clients, client->sock);
    1877           0 : } /* }}} */
    1878             : 
    1879           0 : static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server_client *client, int status) /* {{{ */
    1880             : {
    1881           0 :         zend_string *escaped_request_uri = NULL;
    1882           0 :         const char *status_string = get_status_string(status);
    1883           0 :         const char *content_template = get_template_string(status);
    1884           0 :         char *errstr = get_last_error();
    1885             :         assert(status_string && content_template);
    1886             : 
    1887           0 :         php_cli_server_content_sender_ctor(&client->content_sender);
    1888           0 :         client->content_sender_initialized = 1;
    1889             : 
    1890           0 :         escaped_request_uri = php_escape_html_entities_ex((unsigned char *)client->request.request_uri, client->request.request_uri_len, 0, ENT_QUOTES, NULL, 0);
    1891             : 
    1892             :         {
    1893             :                 static const char prologue_template[] = "<!doctype html><html><head><title>%d %s</title>";
    1894           0 :                 php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(prologue_template) + 3 + strlen(status_string) + 1);
    1895           0 :                 if (!chunk) {
    1896           0 :                         goto fail;
    1897             :                 }
    1898           0 :                 snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string);
    1899           0 :                 chunk->data.heap.len = strlen(chunk->data.heap.p);
    1900           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    1901             :         }
    1902             :         {
    1903           0 :                 php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_css, sizeof(php_cli_server_css) - 1);
    1904           0 :                 if (!chunk) {
    1905           0 :                         goto fail;
    1906             :                 }
    1907           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    1908             :         }
    1909             :         {
    1910             :                 static const char template[] = "</head><body>";
    1911           0 :                 php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(template, sizeof(template) - 1);
    1912           0 :                 if (!chunk) {
    1913           0 :                         goto fail;
    1914             :                 }
    1915           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    1916             :         }
    1917             :         {
    1918           0 :                 php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(content_template) + ZSTR_LEN(escaped_request_uri) + 3 + strlen(status_string) + 1);
    1919           0 :                 if (!chunk) {
    1920           0 :                         goto fail;
    1921             :                 }
    1922           0 :                 snprintf(chunk->data.heap.p, chunk->data.heap.len, content_template, status_string, ZSTR_VAL(escaped_request_uri));
    1923           0 :                 chunk->data.heap.len = strlen(chunk->data.heap.p);
    1924           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    1925             :         }
    1926             :         {
    1927             :                 static const char epilogue_template[] = "</body></html>";
    1928           0 :                 php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(epilogue_template, sizeof(epilogue_template) - 1);
    1929           0 :                 if (!chunk) {
    1930           0 :                         goto fail;
    1931             :                 }
    1932           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    1933             :         }
    1934             : 
    1935             :         {
    1936             :                 php_cli_server_chunk *chunk;
    1937           0 :                 smart_str buffer = { 0 };
    1938           0 :                 append_http_status_line(&buffer, client->request.protocol_version, status, 1);
    1939           0 :                 if (!buffer.s) {
    1940             :                         /* out of memory */
    1941           0 :                         goto fail;
    1942             :                 }
    1943           0 :                 append_essential_headers(&buffer, client, 1);
    1944             :                 smart_str_appends_ex(&buffer, "Content-Type: text/html; charset=UTF-8\r\n", 1);
    1945             :                 smart_str_appends_ex(&buffer, "Content-Length: ", 1);
    1946           0 :                 smart_str_append_unsigned_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1);
    1947             :                 smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
    1948             :                 smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
    1949             : 
    1950           0 :                 chunk = php_cli_server_chunk_heap_new(buffer.s, ZSTR_VAL(buffer.s), ZSTR_LEN(buffer.s));
    1951           0 :                 if (!chunk) {
    1952             :                         smart_str_free_ex(&buffer, 1);
    1953           0 :                         goto fail;
    1954             :                 }
    1955           0 :                 php_cli_server_buffer_prepend(&client->content_sender.buffer, chunk);
    1956             :         }
    1957             : 
    1958           0 :         php_cli_server_log_response(client, status, errstr ? errstr : "?");
    1959           0 :         php_cli_server_poller_add(&server->poller, POLLOUT, client->sock);
    1960           0 :         if (errstr) {
    1961           0 :                 pefree(errstr, 1);
    1962             :         }
    1963             :         zend_string_free(escaped_request_uri);
    1964           0 :         return SUCCESS;
    1965             : 
    1966           0 : fail:
    1967           0 :         if (errstr) {
    1968           0 :                 pefree(errstr, 1);
    1969             :         }
    1970             :         zend_string_free(escaped_request_uri);
    1971           0 :         return FAILURE;
    1972             : } /* }}} */
    1973             : 
    1974           0 : static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    1975             : {
    1976           0 :         if (strlen(client->request.path_translated) != client->request.path_translated_len) {
    1977             :                 /* can't handle paths that contain nul bytes */
    1978           0 :                 return php_cli_server_send_error_page(server, client, 400);
    1979             :         }
    1980             :         {
    1981             :                 zend_file_handle zfd;
    1982           0 :                 zfd.type = ZEND_HANDLE_FILENAME;
    1983           0 :                 zfd.filename = SG(request_info).path_translated;
    1984           0 :                 zfd.handle.fp = NULL;
    1985           0 :                 zfd.free_filename = 0;
    1986           0 :                 zfd.opened_path = NULL;
    1987           0 :                 zend_try {
    1988           0 :                         php_execute_script(&zfd);
    1989           0 :                 } zend_end_try();
    1990             :         }
    1991             : 
    1992           0 :         php_cli_server_log_response(client, SG(sapi_headers).http_response_code, NULL);
    1993           0 :         return SUCCESS;
    1994             : } /* }}} */
    1995             : 
    1996           0 : static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    1997             : {
    1998             :         int fd;
    1999           0 :         int status = 200;
    2000             : 
    2001           0 :         if (client->request.path_translated && strlen(client->request.path_translated) != client->request.path_translated_len) {
    2002             :                 /* can't handle paths that contain nul bytes */
    2003           0 :                 return php_cli_server_send_error_page(server, client, 400);
    2004             :         }
    2005             : 
    2006             : #ifdef PHP_WIN32
    2007             :         /* The win32 namespace will cut off trailing dots and spaces. Since the
    2008             :            VCWD functionality isn't used here, a sophisticated functionality
    2009             :            would have to be reimplemented to know ahead there are no files
    2010             :            with invalid names there. The simplest is just to forbid invalid
    2011             :            filenames, which is done here. */
    2012             :         if (client->request.path_translated &&
    2013             :                 ('.' == client->request.path_translated[client->request.path_translated_len-1] ||
    2014             :                  ' ' == client->request.path_translated[client->request.path_translated_len-1])) {
    2015             :                 return php_cli_server_send_error_page(server, client, 500);
    2016             :         }
    2017             : 
    2018             :         fd = client->request.path_translated ? php_win32_ioutil_open(client->request.path_translated, O_RDONLY): -1;
    2019             : #else
    2020           0 :         fd = client->request.path_translated ? open(client->request.path_translated, O_RDONLY): -1;
    2021             : #endif
    2022           0 :         if (fd < 0) {
    2023           0 :                 return php_cli_server_send_error_page(server, client, 404);
    2024             :         }
    2025             : 
    2026           0 :         php_cli_server_content_sender_ctor(&client->content_sender);
    2027           0 :         client->content_sender_initialized = 1;
    2028           0 :         client->file_fd = fd;
    2029             : 
    2030             :         {
    2031             :                 php_cli_server_chunk *chunk;
    2032           0 :                 smart_str buffer = { 0 };
    2033           0 :                 const char *mime_type = get_mime_type(server, client->request.ext, client->request.ext_len);
    2034             : 
    2035           0 :                 append_http_status_line(&buffer, client->request.protocol_version, status, 1);
    2036           0 :                 if (!buffer.s) {
    2037             :                         /* out of memory */
    2038           0 :                         php_cli_server_log_response(client, 500, NULL);
    2039           0 :                         return FAILURE;
    2040             :                 }
    2041           0 :                 append_essential_headers(&buffer, client, 1);
    2042           0 :                 if (mime_type) {
    2043             :                         smart_str_appendl_ex(&buffer, "Content-Type: ", sizeof("Content-Type: ") - 1, 1);
    2044           0 :                         smart_str_appends_ex(&buffer, mime_type, 1);
    2045           0 :                         if (strncmp(mime_type, "text/", 5) == 0) {
    2046             :                                 smart_str_appends_ex(&buffer, "; charset=UTF-8", 1);
    2047             :                         }
    2048             :                         smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
    2049             :                 }
    2050             :                 smart_str_appends_ex(&buffer, "Content-Length: ", 1);
    2051           0 :                 smart_str_append_unsigned_ex(&buffer, client->request.sb.st_size, 1);
    2052             :                 smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
    2053             :                 smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
    2054           0 :                 chunk = php_cli_server_chunk_heap_new(buffer.s, ZSTR_VAL(buffer.s), ZSTR_LEN(buffer.s));
    2055           0 :                 if (!chunk) {
    2056             :                         smart_str_free_ex(&buffer, 1);
    2057           0 :                         php_cli_server_log_response(client, 500, NULL);
    2058           0 :                         return FAILURE;
    2059             :                 }
    2060           0 :                 php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
    2061             :         }
    2062           0 :         php_cli_server_log_response(client, 200, NULL);
    2063           0 :         php_cli_server_poller_add(&server->poller, POLLOUT, client->sock);
    2064           0 :         return SUCCESS;
    2065             : }
    2066             : /* }}} */
    2067             : 
    2068           0 : static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client) { /* {{{ */
    2069             :         char *auth;
    2070           0 :         php_cli_server_client_populate_request_info(client, &SG(request_info));
    2071           0 :         if (NULL != (auth = zend_hash_str_find_ptr(&client->request.headers, "authorization", sizeof("authorization")-1))) {
    2072           0 :                 php_handle_auth_data(auth);
    2073             :         }
    2074           0 :         SG(sapi_headers).http_response_code = 200;
    2075           0 :         if (FAILURE == php_request_startup()) {
    2076             :                 /* should never be happen */
    2077           0 :                 destroy_request_info(&SG(request_info));
    2078           0 :                 return FAILURE;
    2079             :         }
    2080           0 :         PG(during_request_startup) = 0;
    2081             : 
    2082           0 :         return SUCCESS;
    2083             : }
    2084             : /* }}} */
    2085             : 
    2086           0 : static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_server_client *client) { /* {{{ */
    2087           0 :         php_request_shutdown(0);
    2088           0 :         php_cli_server_close_connection(server, client);
    2089           0 :         destroy_request_info(&SG(request_info));
    2090           0 :         SG(server_context) = NULL;
    2091           0 :         SG(rfc1867_uploaded_files) = NULL;
    2092           0 :         return SUCCESS;
    2093             : }
    2094             : /* }}} */
    2095             : 
    2096           0 : static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    2097             : {
    2098           0 :         int decline = 0;
    2099             :         zend_file_handle zfd;
    2100             :         char *old_cwd;
    2101             : 
    2102             :         ALLOCA_FLAG(use_heap)
    2103           0 :         old_cwd = do_alloca(MAXPATHLEN, use_heap);
    2104           0 :         old_cwd[0] = '\0';
    2105           0 :         php_ignore_value(VCWD_GETCWD(old_cwd, MAXPATHLEN - 1));
    2106             : 
    2107           0 :         zfd.type = ZEND_HANDLE_FILENAME;
    2108           0 :         zfd.filename = server->router;
    2109           0 :         zfd.handle.fp = NULL;
    2110           0 :         zfd.free_filename = 0;
    2111           0 :         zfd.opened_path = NULL;
    2112             : 
    2113           0 :         zend_try {
    2114             :                 zval retval;
    2115             : 
    2116           0 :                 ZVAL_UNDEF(&retval);
    2117           0 :                 if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE, &retval, 1, &zfd)) {
    2118           0 :                         if (Z_TYPE(retval) != IS_UNDEF) {
    2119           0 :                                 decline = Z_TYPE(retval) == IS_FALSE;
    2120           0 :                                 zval_ptr_dtor(&retval);
    2121             :                         }
    2122             :                 } else {
    2123           0 :                         decline = 1;
    2124             :                 }
    2125           0 :         } zend_end_try();
    2126             : 
    2127           0 :         if (old_cwd[0] != '\0') {
    2128           0 :                 php_ignore_value(VCWD_CHDIR(old_cwd));
    2129             :         }
    2130             : 
    2131           0 :         free_alloca(old_cwd, use_heap);
    2132             : 
    2133           0 :         return decline;
    2134             : }
    2135             : /* }}} */
    2136             : 
    2137           0 : static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    2138             : {
    2139           0 :         int is_static_file  = 0;
    2140             : 
    2141           0 :         SG(server_context) = client;
    2142           0 :         if (client->request.ext_len != 3 || memcmp(client->request.ext, "php", 3) || !client->request.path_translated) {
    2143           0 :                 is_static_file = 1;
    2144             :         }
    2145             : 
    2146           0 :         if (server->router || !is_static_file) {
    2147           0 :                 if (FAILURE == php_cli_server_request_startup(server, client)) {
    2148           0 :                         SG(server_context) = NULL;
    2149           0 :                         php_cli_server_close_connection(server, client);
    2150           0 :                         destroy_request_info(&SG(request_info));
    2151           0 :                         return SUCCESS;
    2152             :                 }
    2153             :         }
    2154             : 
    2155           0 :         if (server->router) {
    2156           0 :                 if (!php_cli_server_dispatch_router(server, client)) {
    2157           0 :                         php_cli_server_request_shutdown(server, client);
    2158           0 :                         return SUCCESS;
    2159             :                 }
    2160             :         }
    2161             : 
    2162           0 :         if (!is_static_file) {
    2163           0 :                 if (SUCCESS == php_cli_server_dispatch_script(server, client)
    2164           0 :                                 || SUCCESS != php_cli_server_send_error_page(server, client, 500)) {
    2165           0 :                         if (SG(sapi_headers).http_response_code == 304) {
    2166           0 :                                 SG(sapi_headers).send_default_content_type = 0;
    2167             :                         }
    2168           0 :                         php_cli_server_request_shutdown(server, client);
    2169           0 :                         return SUCCESS;
    2170             :                 }
    2171             :         } else {
    2172           0 :                 if (server->router) {
    2173             :                         static int (*send_header_func)(sapi_headers_struct *);
    2174           0 :                         send_header_func = sapi_module.send_headers;
    2175             :                         /* do not generate default content type header */
    2176           0 :                     SG(sapi_headers).send_default_content_type = 0;
    2177             :                         /* we don't want headers to be sent */
    2178           0 :                         sapi_module.send_headers = sapi_cli_server_discard_headers;
    2179           0 :                         php_request_shutdown(0);
    2180           0 :                         sapi_module.send_headers = send_header_func;
    2181           0 :                     SG(sapi_headers).send_default_content_type = 1;
    2182           0 :                         SG(rfc1867_uploaded_files) = NULL;
    2183             :                 }
    2184           0 :                 if (SUCCESS != php_cli_server_begin_send_static(server, client)) {
    2185           0 :                         php_cli_server_close_connection(server, client);
    2186             :                 }
    2187           0 :                 SG(server_context) = NULL;
    2188           0 :                 return SUCCESS;
    2189             :         }
    2190             : 
    2191           0 :         SG(server_context) = NULL;
    2192           0 :         destroy_request_info(&SG(request_info));
    2193           0 :         return SUCCESS;
    2194             : }
    2195             : /* }}} */
    2196             : 
    2197           0 : static int php_cli_server_mime_type_ctor(php_cli_server *server, const php_cli_server_ext_mime_type_pair *mime_type_map) /* {{{ */
    2198             : {
    2199             :         const php_cli_server_ext_mime_type_pair *pair;
    2200             : 
    2201           0 :         zend_hash_init(&server->extension_mime_types, 0, NULL, NULL, 1);
    2202             : 
    2203           0 :         for (pair = mime_type_map; pair->ext; pair++) {
    2204           0 :                 size_t ext_len = strlen(pair->ext);
    2205           0 :                 zend_hash_str_add_ptr(&server->extension_mime_types, pair->ext, ext_len, (void*)pair->mime_type);
    2206             :         }
    2207             : 
    2208           0 :         return SUCCESS;
    2209             : } /* }}} */
    2210             : 
    2211           0 : static void php_cli_server_dtor(php_cli_server *server) /* {{{ */
    2212             : {
    2213           0 :         zend_hash_destroy(&server->clients);
    2214           0 :         zend_hash_destroy(&server->extension_mime_types);
    2215           0 :         if (ZEND_VALID_SOCKET(server->server_sock)) {
    2216           0 :                 closesocket(server->server_sock);
    2217             :         }
    2218           0 :         if (server->host) {
    2219           0 :                 pefree(server->host, 1);
    2220             :         }
    2221           0 :         if (server->document_root) {
    2222           0 :                 pefree(server->document_root, 1);
    2223             :         }
    2224           0 :         if (server->router) {
    2225           0 :                 pefree(server->router, 1);
    2226             :         }
    2227           0 : } /* }}} */
    2228             : 
    2229           0 : static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
    2230             : {
    2231           0 :         php_cli_server_client *p = Z_PTR_P(zv);
    2232             : 
    2233           0 :         closesocket(p->sock);
    2234           0 :         php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock);
    2235           0 :         php_cli_server_client_dtor(p);
    2236           0 :         pefree(p, 1);
    2237           0 : } /* }}} */
    2238             : 
    2239           0 : static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router) /* {{{ */
    2240             : {
    2241           0 :         int retval = SUCCESS;
    2242           0 :         char *host = NULL;
    2243           0 :         zend_string *errstr = NULL;
    2244           0 :         char *_document_root = NULL;
    2245           0 :         char *_router = NULL;
    2246           0 :         int err = 0;
    2247           0 :         int port = 3000;
    2248           0 :         php_socket_t server_sock = SOCK_ERR;
    2249           0 :         char *p = NULL;
    2250             : 
    2251           0 :         if (addr[0] == '[') {
    2252           0 :                 host = pestrdup(addr + 1, 1);
    2253           0 :                 if (!host) {
    2254           0 :                         return FAILURE;
    2255             :                 }
    2256           0 :                 p = strchr(host, ']');
    2257           0 :                 if (p) {
    2258           0 :                         *p++ = '\0';
    2259           0 :                         if (*p == ':') {
    2260           0 :                                 port = strtol(p + 1, &p, 10);
    2261           0 :                                 if (port <= 0 || port > 65535) {
    2262           0 :                                         p = NULL;
    2263             :                                 }
    2264           0 :                         } else if (*p != '\0') {
    2265           0 :                                 p = NULL;
    2266             :                         }
    2267             :                 }
    2268             :         } else {
    2269           0 :                 host = pestrdup(addr, 1);
    2270           0 :                 if (!host) {
    2271           0 :                         return FAILURE;
    2272             :                 }
    2273           0 :                 p = strchr(host, ':');
    2274           0 :                 if (p) {
    2275           0 :                         *p++ = '\0';
    2276           0 :                         port = strtol(p, &p, 10);
    2277           0 :                         if (port <= 0 || port > 65535) {
    2278           0 :                                 p = NULL;
    2279             :                         }
    2280             :                 }
    2281             :         }
    2282           0 :         if (!p) {
    2283           0 :                 fprintf(stderr, "Invalid address: %s\n", addr);
    2284           0 :                 retval = FAILURE;
    2285           0 :                 goto out;
    2286             :         }
    2287             : 
    2288           0 :         server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr);
    2289           0 :         if (server_sock == SOCK_ERR) {
    2290           0 :                 php_cli_server_logf("Failed to listen on %s:%d (reason: %s)", host, port, errstr ? ZSTR_VAL(errstr) : "?");
    2291           0 :                 if (errstr) {
    2292           0 :                         zend_string_release_ex(errstr, 0);
    2293             :                 }
    2294           0 :                 retval = FAILURE;
    2295           0 :                 goto out;
    2296             :         }
    2297           0 :         server->server_sock = server_sock;
    2298             : 
    2299           0 :         err = php_cli_server_poller_ctor(&server->poller);
    2300           0 :         if (SUCCESS != err) {
    2301           0 :                 goto out;
    2302             :         }
    2303             : 
    2304           0 :         php_cli_server_poller_add(&server->poller, POLLIN, server_sock);
    2305             : 
    2306           0 :         server->host = host;
    2307           0 :         server->port = port;
    2308             : 
    2309           0 :         zend_hash_init(&server->clients, 0, NULL, php_cli_server_client_dtor_wrapper, 1);
    2310             : 
    2311             :         {
    2312           0 :                 size_t document_root_len = strlen(document_root);
    2313           0 :                 _document_root = pestrndup(document_root, document_root_len, 1);
    2314           0 :                 if (!_document_root) {
    2315           0 :                         retval = FAILURE;
    2316           0 :                         goto out;
    2317             :                 }
    2318           0 :                 server->document_root = _document_root;
    2319           0 :                 server->document_root_len = document_root_len;
    2320             :         }
    2321             : 
    2322           0 :         if (router) {
    2323           0 :                 size_t router_len = strlen(router);
    2324           0 :                 _router = pestrndup(router, router_len, 1);
    2325           0 :                 if (!_router) {
    2326           0 :                         retval = FAILURE;
    2327           0 :                         goto out;
    2328             :                 }
    2329           0 :                 server->router = _router;
    2330           0 :                 server->router_len = router_len;
    2331             :         } else {
    2332           0 :                 server->router = NULL;
    2333           0 :                 server->router_len = 0;
    2334             :         }
    2335             : 
    2336           0 :         if (php_cli_server_mime_type_ctor(server, mime_type_map) == FAILURE) {
    2337           0 :                 retval = FAILURE;
    2338           0 :                 goto out;
    2339             :         }
    2340             : 
    2341           0 :         server->is_running = 1;
    2342           0 : out:
    2343           0 :         if (retval != SUCCESS) {
    2344           0 :                 if (host) {
    2345           0 :                         pefree(host, 1);
    2346             :                 }
    2347           0 :                 if (_document_root) {
    2348           0 :                         pefree(_document_root, 1);
    2349             :                 }
    2350           0 :                 if (_router) {
    2351           0 :                         pefree(_router, 1);
    2352             :                 }
    2353           0 :                 if (server_sock > -1) {
    2354           0 :                         closesocket(server_sock);
    2355             :                 }
    2356             :         }
    2357           0 :         return retval;
    2358             : } /* }}} */
    2359             : 
    2360           0 : static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    2361             : {
    2362           0 :         char *errstr = NULL;
    2363           0 :         int status = php_cli_server_client_read_request(client, &errstr);
    2364           0 :         if (status < 0) {
    2365           0 :                 if (strcmp(errstr, php_cli_server_request_error_unexpected_eof) == 0 && client->parser.state == s_start_req) {
    2366             : #if PHP_DEBUG
    2367             :                         php_cli_server_logf("%s Closed without sending a request; it was probably just an unused speculative preconnection", client->addr_str);
    2368             : #endif
    2369             :                 } else {
    2370           0 :                         php_cli_server_logf("%s Invalid request (%s)", client->addr_str, errstr);
    2371             :                 }
    2372           0 :                 efree(errstr);
    2373           0 :                 php_cli_server_close_connection(server, client);
    2374           0 :                 return FAILURE;
    2375           0 :         } else if (status == 1 && client->request.request_method == PHP_HTTP_NOT_IMPLEMENTED) {
    2376           0 :                 return php_cli_server_send_error_page(server, client, 501);
    2377           0 :         } else if (status == 1) {
    2378           0 :                 php_cli_server_poller_remove(&server->poller, POLLIN, client->sock);
    2379           0 :                 php_cli_server_dispatch(server, client);
    2380             :         } else {
    2381           0 :                 php_cli_server_poller_add(&server->poller, POLLIN, client->sock);
    2382             :         }
    2383             : 
    2384           0 :         return SUCCESS;
    2385             : } /* }}} */
    2386             : 
    2387           0 : static int php_cli_server_send_event(php_cli_server *server, php_cli_server_client *client) /* {{{ */
    2388             : {
    2389           0 :         if (client->content_sender_initialized) {
    2390           0 :                 if (client->file_fd >= 0 && !client->content_sender.buffer.first) {
    2391             :                         size_t nbytes_read;
    2392           0 :                         if (php_cli_server_content_sender_pull(&client->content_sender, client->file_fd, &nbytes_read)) {
    2393           0 :                                 php_cli_server_close_connection(server, client);
    2394           0 :                                 return FAILURE;
    2395             :                         }
    2396           0 :                         if (nbytes_read == 0) {
    2397           0 :                                 close(client->file_fd);
    2398           0 :                                 client->file_fd = -1;
    2399             :                         }
    2400             :                 }
    2401             :                 {
    2402             :                         size_t nbytes_sent;
    2403           0 :                         int err = php_cli_server_content_sender_send(&client->content_sender, client->sock, &nbytes_sent);
    2404           0 :                         if (err && err != SOCK_EAGAIN) {
    2405           0 :                                 php_cli_server_close_connection(server, client);
    2406           0 :                                 return FAILURE;
    2407             :                         }
    2408             :                 }
    2409           0 :                 if (!client->content_sender.buffer.first && client->file_fd < 0) {
    2410           0 :                         php_cli_server_close_connection(server, client);
    2411             :                 }
    2412             :         }
    2413           0 :         return SUCCESS;
    2414             : }
    2415             : /* }}} */
    2416             : 
    2417             : typedef struct php_cli_server_do_event_for_each_fd_callback_params {
    2418             :         php_cli_server *server;
    2419             :         int(*rhandler)(php_cli_server*, php_cli_server_client*);
    2420             :         int(*whandler)(php_cli_server*, php_cli_server_client*);
    2421             : } php_cli_server_do_event_for_each_fd_callback_params;
    2422             : 
    2423           0 : static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socket_t fd, int event) /* {{{ */
    2424             : {
    2425           0 :         php_cli_server_do_event_for_each_fd_callback_params *params = _params;
    2426           0 :         php_cli_server *server = params->server;
    2427           0 :         if (server->server_sock == fd) {
    2428           0 :                 php_cli_server_client *client = NULL;
    2429             :                 php_socket_t client_sock;
    2430           0 :                 socklen_t socklen = server->socklen;
    2431           0 :                 struct sockaddr *sa = pemalloc(server->socklen, 1);
    2432           0 :                 client_sock = accept(server->server_sock, sa, &socklen);
    2433           0 :                 if (!ZEND_VALID_SOCKET(client_sock)) {
    2434             :                         char *errstr;
    2435           0 :                         errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
    2436           0 :                         php_cli_server_logf("Failed to accept a client (reason: %s)", errstr);
    2437           0 :                         efree(errstr);
    2438           0 :                         pefree(sa, 1);
    2439           0 :                         return SUCCESS;
    2440             :                 }
    2441           0 :                 if (SUCCESS != php_set_sock_blocking(client_sock, 0)) {
    2442           0 :                         pefree(sa, 1);
    2443           0 :                         closesocket(client_sock);
    2444           0 :                         return SUCCESS;
    2445             :                 }
    2446           0 :                 client = pemalloc(sizeof(php_cli_server_client), 1);
    2447           0 :                 if (FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
    2448           0 :                         php_cli_server_logf("Failed to create a new request object");
    2449           0 :                         pefree(sa, 1);
    2450           0 :                         closesocket(client_sock);
    2451           0 :                         return SUCCESS;
    2452             :                 }
    2453             : #if PHP_DEBUG
    2454             :                 php_cli_server_logf("%s Accepted", client->addr_str);
    2455             : #endif
    2456           0 :                 zend_hash_index_update_ptr(&server->clients, client_sock, client);
    2457           0 :                 php_cli_server_recv_event_read_request(server, client);
    2458             :         } else {
    2459             :                 php_cli_server_client *client;
    2460           0 :                 if (NULL != (client = zend_hash_index_find_ptr(&server->clients, fd))) {
    2461           0 :                         if (event & POLLIN) {
    2462           0 :                                 params->rhandler(server, client);
    2463             :                         }
    2464           0 :                         if (event & POLLOUT) {
    2465           0 :                                 params->whandler(server, client);
    2466             :                         }
    2467             :                 }
    2468             :         }
    2469           0 :         return SUCCESS;
    2470             : } /* }}} */
    2471             : 
    2472           0 : static void php_cli_server_do_event_for_each_fd(php_cli_server *server, int(*rhandler)(php_cli_server*, php_cli_server_client*), int(*whandler)(php_cli_server*, php_cli_server_client*)) /* {{{ */
    2473             : {
    2474           0 :         php_cli_server_do_event_for_each_fd_callback_params params = {
    2475             :                 server,
    2476             :                 rhandler,
    2477             :                 whandler
    2478             :         };
    2479             : 
    2480           0 :         php_cli_server_poller_iter_on_active(&server->poller, &params, php_cli_server_do_event_for_each_fd_callback);
    2481           0 : } /* }}} */
    2482             : 
    2483           0 : static int php_cli_server_do_event_loop(php_cli_server *server) /* {{{ */
    2484             : {
    2485           0 :         int retval = SUCCESS;
    2486           0 :         while (server->is_running) {
    2487           0 :                 struct timeval tv = { 1, 0 };
    2488           0 :                 int n = php_cli_server_poller_poll(&server->poller, &tv);
    2489           0 :                 if (n > 0) {
    2490           0 :                         php_cli_server_do_event_for_each_fd(server,
    2491             :                                         php_cli_server_recv_event_read_request,
    2492             :                                         php_cli_server_send_event);
    2493           0 :                 } else if (n == 0) {
    2494             :                         /* do nothing */
    2495             :                 } else {
    2496           0 :                         int err = php_socket_errno();
    2497           0 :                         if (err != SOCK_EINTR) {
    2498           0 :                                 char *errstr = php_socket_strerror(err, NULL, 0);
    2499           0 :                                 php_cli_server_logf("%s", errstr);
    2500           0 :                                 efree(errstr);
    2501           0 :                                 retval = FAILURE;
    2502           0 :                                 goto out;
    2503             :                         }
    2504             :                 }
    2505             :         }
    2506           0 : out:
    2507           0 :         return retval;
    2508             : } /* }}} */
    2509             : 
    2510             : static php_cli_server server;
    2511             : 
    2512           0 : static void php_cli_server_sigint_handler(int sig) /* {{{ */
    2513             : {
    2514           0 :         server.is_running = 0;
    2515           0 : }
    2516             : /* }}} */
    2517             : 
    2518           0 : int do_cli_server(int argc, char **argv) /* {{{ */
    2519             : {
    2520           0 :         char *php_optarg = NULL;
    2521           0 :         int php_optind = 1;
    2522             :         int c;
    2523           0 :         const char *server_bind_address = NULL;
    2524             :         extern const opt_struct OPTIONS[];
    2525           0 :         const char *document_root = NULL;
    2526             : #ifdef PHP_WIN32
    2527             :         char document_root_tmp[MAXPATHLEN];
    2528             :         size_t k;
    2529             : #endif
    2530           0 :         const char *router = NULL;
    2531             :         char document_root_buf[MAXPATHLEN];
    2532             : 
    2533           0 :         while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2))!=-1) {
    2534           0 :                 switch (c) {
    2535           0 :                         case 'S':
    2536           0 :                                 server_bind_address = php_optarg;
    2537           0 :                                 break;
    2538           0 :                         case 't':
    2539             : #ifndef PHP_WIN32
    2540           0 :                                 document_root = php_optarg;
    2541             : #else
    2542             :                                 k = strlen(php_optarg);
    2543             :                                 if (k + 1 > MAXPATHLEN) {
    2544             :                                         fprintf(stderr, "Document root path is too long.\n");
    2545             :                                         return 1;
    2546             :                                 }
    2547             :                                 memmove(document_root_tmp, php_optarg, k + 1);
    2548             :                                 /* Clean out any trailing garbage that might have been passed
    2549             :                                         from a batch script. */
    2550             :                                 do {
    2551             :                                         document_root_tmp[k] = '\0';
    2552             :                                         k--;
    2553             :                                 } while ('"' == document_root_tmp[k] || ' ' == document_root_tmp[k]);
    2554             :                                 document_root = document_root_tmp;
    2555             : #endif
    2556           0 :                                 break;
    2557             :                 }
    2558             :         }
    2559             : 
    2560           0 :         if (document_root) {
    2561             :                 zend_stat_t sb;
    2562             : 
    2563           0 :                 if (php_sys_stat(document_root, &sb)) {
    2564           0 :                         fprintf(stderr, "Directory %s does not exist.\n", document_root);
    2565           0 :                         return 1;
    2566             :                 }
    2567           0 :                 if (!S_ISDIR(sb.st_mode)) {
    2568           0 :                         fprintf(stderr, "%s is not a directory.\n", document_root);
    2569           0 :                         return 1;
    2570             :                 }
    2571           0 :                 if (VCWD_REALPATH(document_root, document_root_buf)) {
    2572           0 :                         document_root = document_root_buf;
    2573             :                 }
    2574             :         } else {
    2575           0 :                 char *ret = NULL;
    2576             : 
    2577             : #if HAVE_GETCWD
    2578           0 :                 ret = VCWD_GETCWD(document_root_buf, MAXPATHLEN);
    2579             : #elif HAVE_GETWD
    2580             :                 ret = VCWD_GETWD(document_root_buf);
    2581             : #endif
    2582           0 :                 document_root = ret ? document_root_buf: ".";
    2583             :         }
    2584             : 
    2585           0 :         if (argc > php_optind) {
    2586           0 :                 router = argv[php_optind];
    2587             :         }
    2588             : 
    2589           0 :         if (FAILURE == php_cli_server_ctor(&server, server_bind_address, document_root, router)) {
    2590           0 :                 return 1;
    2591             :         }
    2592           0 :         sapi_module.phpinfo_as_text = 0;
    2593             : 
    2594             :         {
    2595             :                 char buf[52];
    2596             : 
    2597           0 :                 if (php_cli_server_get_system_time(buf) != 0) {
    2598           0 :                         memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
    2599             :                 }
    2600             : 
    2601           0 :                 printf("PHP %s Development Server started at %s"
    2602             :                                 "Listening on http://%s\n"
    2603             :                                 "Document root is %s\n"
    2604             :                                 "Press Ctrl-C to quit.\n",
    2605             :                                 PHP_VERSION, buf, server_bind_address, document_root);
    2606             :         }
    2607             : 
    2608             : #if defined(HAVE_SIGNAL_H) && defined(SIGINT)
    2609           0 :         signal(SIGINT, php_cli_server_sigint_handler);
    2610           0 :         zend_signal_init();
    2611             : #endif
    2612           0 :         php_cli_server_do_event_loop(&server);
    2613           0 :         php_cli_server_dtor(&server);
    2614           0 :         return 0;
    2615             : } /* }}} */
    2616             : 
    2617             : /*
    2618             :  * Local variables:
    2619             :  * tab-width: 4
    2620             :  * c-basic-offset: 4
    2621             :  * End:
    2622             :  * vim600: noet sw=4 ts=4 fdm=marker
    2623             :  * vim<600: noet sw=4 ts=4
    2624             :  */

Generated by: LCOV version 1.10

Generated at Fri, 17 Aug 2018 22:18:59 +0000 (43 hours ago)

Copyright © 2005-2018 The PHP Group
All rights reserved.