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 1220 0.0 %
Date: 2014-12-13 Functions: 0 89 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 13 Dec 2014 06:16:28 +0000 (7 days ago)

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