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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:48:03 +0000 (3 days ago)

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