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 1236 0.0 %
Date: 2014-09-19 Functions: 0 89 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:23 +0000 (42 hours ago)

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