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-12-01 Functions: 0 90 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 01 Dec 2018 10:41:09 +0000 (17 days ago)

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