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 1245 0.0 %
Date: 2014-08-04 Functions: 0 89 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:20 +0000 (29 days ago)

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