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

Generated by: LCOV version 1.10

Generated at Sun, 21 Sep 2014 15:27:50 +0000 (32 hours ago)

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