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

Generated by: LCOV version 1.10

Generated at Mon, 18 Jun 2018 17:33:48 +0000 (31 hours ago)

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