summaryrefslogtreecommitdiff
path: root/sapi/cli/php_cli_server.c
diff options
context:
space:
mode:
Diffstat (limited to 'sapi/cli/php_cli_server.c')
-rw-r--r--sapi/cli/php_cli_server.c632
1 files changed, 277 insertions, 355 deletions
diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
index 3b5fe05945..6315cd8ae5 100644
--- a/sapi/cli/php_cli_server.c
+++ b/sapi/cli/php_cli_server.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
@@ -68,6 +68,7 @@
#include "zend_hash.h"
#include "zend_modules.h"
#include "fopen_wrappers.h"
+#include "http_status_codes.h"
#include "zend_compile.h"
#include "zend_execute.h"
@@ -96,7 +97,7 @@
#endif
#include "ext/standard/file.h" /* for php_set_sock_blocking() :-( */
-#include "ext/standard/php_smart_str.h"
+#include "zend_smart_str.h"
#include "ext/standard/html.h"
#include "ext/standard/url.h" /* for php_url_decode() */
#include "ext/standard/php_string.h" /* for php_dirname() */
@@ -104,6 +105,7 @@
#include "php_http_parser.h"
#include "php_cli_server.h"
+#include "mime_type_map.h"
#include "php_cli_process_title.h"
@@ -138,7 +140,7 @@ typedef struct php_cli_server_request {
size_t content_len;
const char *ext;
size_t ext_len;
- struct stat sb;
+ zend_stat_t sb;
} php_cli_server_request;
typedef struct php_cli_server_chunk {
@@ -194,6 +196,7 @@ typedef struct php_cli_server {
size_t router_len;
socklen_t socklen;
HashTable clients;
+ HashTable extension_mime_types;
} php_cli_server;
typedef struct php_cli_server_http_response_status_code_pair {
@@ -201,60 +204,6 @@ typedef struct php_cli_server_http_response_status_code_pair {
const char *str;
} php_cli_server_http_response_status_code_pair;
-typedef struct php_cli_server_ext_mime_type_pair {
- const char *ext;
- const char *mime_type;
-} php_cli_server_ext_mime_type_pair;
-
-static php_cli_server_http_response_status_code_pair status_map[] = {
- { 100, "Continue" },
- { 101, "Switching Protocols" },
- { 200, "OK" },
- { 201, "Created" },
- { 202, "Accepted" },
- { 203, "Non-Authoritative Information" },
- { 204, "No Content" },
- { 205, "Reset Content" },
- { 206, "Partial Content" },
- { 300, "Multiple Choices" },
- { 301, "Moved Permanently" },
- { 302, "Found" },
- { 303, "See Other" },
- { 304, "Not Modified" },
- { 305, "Use Proxy" },
- { 307, "Temporary Redirect" },
- { 308, "Permanent Redirect" },
- { 400, "Bad Request" },
- { 401, "Unauthorized" },
- { 402, "Payment Required" },
- { 403, "Forbidden" },
- { 404, "Not Found" },
- { 405, "Method Not Allowed" },
- { 406, "Not Acceptable" },
- { 407, "Proxy Authentication Required" },
- { 408, "Request Timeout" },
- { 409, "Conflict" },
- { 410, "Gone" },
- { 411, "Length Required" },
- { 412, "Precondition Failed" },
- { 413, "Request Entity Too Large" },
- { 414, "Request-URI Too Long" },
- { 415, "Unsupported Media Type" },
- { 416, "Requested Range Not Satisfiable" },
- { 417, "Expectation Failed" },
- { 426, "Upgrade Required" },
- { 428, "Precondition Required" },
- { 429, "Too Many Requests" },
- { 431, "Request Header Fields Too Large" },
- { 500, "Internal Server Error" },
- { 501, "Not Implemented" },
- { 502, "Bad Gateway" },
- { 503, "Service Unavailable" },
- { 504, "Gateway Timeout" },
- { 505, "HTTP Version Not Supported" },
- { 511, "Network Authentication Required" },
-};
-
static php_cli_server_http_response_status_code_pair template_map[] = {
{ 400, "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
{ 404, "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" },
@@ -262,71 +211,15 @@ static php_cli_server_http_response_status_code_pair template_map[] = {
{ 501, "<h1>%s</h1><p>Request method not supported.</p>" }
};
-static php_cli_server_ext_mime_type_pair mime_type_map[] = {
- { "html", "text/html" },
- { "htm", "text/html" },
- { "js", "text/javascript" },
- { "css", "text/css" },
- { "gif", "image/gif" },
- { "jpg", "image/jpeg" },
- { "jpeg", "image/jpeg" },
- { "jpe", "image/jpeg" },
- { "pdf", "application/pdf" },
- { "png", "image/png" },
- { "svg", "image/svg+xml" },
- { "txt", "text/plain" },
- { "webm", "video/webm" },
- { "ogv", "video/ogg" },
- { "ogg", "audio/ogg" },
- { "3gp", "video/3gpp" }, /* This is standard video format used for MMS in phones */
- { "apk", "application/vnd.android.package-archive" },
- { "avi", "video/x-msvideo" },
- { "bmp", "image/x-ms-bmp" },
- { "csv", "text/comma-separated-values" },
- { "doc", "application/msword" },
- { "docx", "application/msword" },
- { "flac", "audio/flac" },
- { "gz", "application/x-gzip" },
- { "gzip", "application/x-gzip" },
- { "ics", "text/calendar" },
- { "kml", "application/vnd.google-earth.kml+xml" },
- { "kmz", "application/vnd.google-earth.kmz" },
- { "m4a", "audio/mp4" },
- { "mp3", "audio/mpeg" },
- { "mp4", "video/mp4" },
- { "mpg", "video/mpeg" },
- { "mpeg", "video/mpeg" },
- { "mov", "video/quicktime" },
- { "odp", "application/vnd.oasis.opendocument.presentation" },
- { "ods", "application/vnd.oasis.opendocument.spreadsheet" },
- { "odt", "application/vnd.oasis.opendocument.text" },
- { "oga", "audio/ogg" },
- { "pdf", "application/pdf" },
- { "pptx", "application/vnd.ms-powerpoint" },
- { "pps", "application/vnd.ms-powerpoint" },
- { "qt", "video/quicktime" },
- { "swf", "application/x-shockwave-flash" },
- { "tar", "application/x-tar" },
- { "text", "text/plain" },
- { "tif", "image/tiff" },
- { "wav", "audio/wav" },
- { "wmv", "video/x-ms-wmv" },
- { "xls", "application/vnd.ms-excel" },
- { "xlsx", "application/vnd.ms-excel" },
- { "zip", "application/x-zip-compressed" },
- { "xml", "application/xml" },
- { "xsl", "application/xml" },
- { "xsd", "application/xml" },
- { NULL, NULL }
-};
-
+#if HAVE_UNISTD_H
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
+#endif
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len);
static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk);
-static void php_cli_server_logf(const char *format TSRMLS_DC, ...);
-static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message TSRMLS_DC);
+static void php_cli_server_logf(const char *format, ...);
+static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message);
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
@@ -371,9 +264,9 @@ int php_cli_server_get_system_time(char *buf) {
}
#endif
-static void char_ptr_dtor_p(char **p) /* {{{ */
+static void char_ptr_dtor_p(zval *zv) /* {{{ */
{
- pefree(*p, 1);
+ pefree(Z_PTR_P(zv), 1);
} /* }}} */
static char *get_last_error() /* {{{ */
@@ -383,8 +276,8 @@ static char *get_last_error() /* {{{ */
static int status_comp(const void *a, const void *b) /* {{{ */
{
- const php_cli_server_http_response_status_code_pair *pa = (const php_cli_server_http_response_status_code_pair *) a;
- const php_cli_server_http_response_status_code_pair *pb = (const php_cli_server_http_response_status_code_pair *) b;
+ const http_response_status_code_pair *pa = (const http_response_status_code_pair *) a;
+ const http_response_status_code_pair *pb = (const http_response_status_code_pair *) b;
if (pa->code < pb->code) {
return -1;
@@ -397,12 +290,10 @@ static int status_comp(const void *a, const void *b) /* {{{ */
static const char *get_status_string(int code) /* {{{ */
{
- php_cli_server_http_response_status_code_pair needle, *result = NULL;
+ http_response_status_code_pair needle = {code, NULL},
+ *result = NULL;
- needle.code = code;
- needle.str = NULL;
-
- result = bsearch(&needle, status_map, sizeof(status_map) / sizeof(needle), sizeof(needle), status_comp);
+ result = bsearch(&needle, http_status_map, http_status_map_len, sizeof(needle), status_comp);
if (result) {
return result->str;
@@ -441,11 +332,11 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int
}
smart_str_appendl_ex(buffer, "HTTP", 4, persistent);
smart_str_appendc_ex(buffer, '/', persistent);
- smart_str_append_generic_ex(buffer, protocol_version / 100, persistent, int, _unsigned);
+ smart_str_append_long_ex(buffer, protocol_version / 100, persistent);
smart_str_appendc_ex(buffer, '.', persistent);
- smart_str_append_generic_ex(buffer, protocol_version % 100, persistent, int, _unsigned);
+ smart_str_append_long_ex(buffer, protocol_version % 100, persistent);
smart_str_appendc_ex(buffer, ' ', persistent);
- smart_str_append_generic_ex(buffer, response_code, persistent, int, _unsigned);
+ smart_str_append_long_ex(buffer, response_code, persistent);
smart_str_appendc_ex(buffer, ' ', persistent);
smart_str_appends_ex(buffer, get_status_string(response_code), persistent);
smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
@@ -454,37 +345,29 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int
static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, int persistent) /* {{{ */
{
{
- char **val;
- if (SUCCESS == zend_hash_find(&client->request.headers, "host", sizeof("host"), (void**)&val)) {
+ char *val;
+ if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "host", sizeof("host")-1))) {
smart_str_appendl_ex(buffer, "Host", sizeof("Host") - 1, persistent);
smart_str_appendl_ex(buffer, ": ", sizeof(": ") - 1, persistent);
- smart_str_appends_ex(buffer, *val, persistent);
+ smart_str_appends_ex(buffer, val, persistent);
smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
}
}
smart_str_appendl_ex(buffer, "Connection: close\r\n", sizeof("Connection: close\r\n") - 1, persistent);
} /* }}} */
-static const char *get_mime_type(const char *ext, size_t ext_len) /* {{{ */
+static const char *get_mime_type(const php_cli_server *server, const char *ext, size_t ext_len) /* {{{ */
{
- php_cli_server_ext_mime_type_pair *pair;
- for (pair = mime_type_map; pair->ext; pair++) {
- size_t len = strlen(pair->ext);
- if (len == ext_len && memcmp(pair->ext, ext, len) == 0) {
- return pair->mime_type;
- }
- }
- return NULL;
+ return (const char*)zend_hash_str_find_ptr(&server->extension_mime_types, ext, ext_len);
} /* }}} */
PHP_FUNCTION(apache_request_headers) /* {{{ */
{
php_cli_server_client *client;
HashTable *headers;
- char *key;
- uint key_len;
- char **value_pointer;
- HashPosition pos;
+ zend_string *key;
+ char *value;
+ zval tmp;
if (zend_parse_parameters_none() == FAILURE) {
return;
@@ -495,19 +378,17 @@ PHP_FUNCTION(apache_request_headers) /* {{{ */
array_init_size(return_value, zend_hash_num_elements(headers));
- zend_hash_internal_pointer_reset_ex(headers, &pos);
- while (zend_hash_get_current_data_ex(headers, (void **)&value_pointer, &pos) == SUCCESS) {
- zend_hash_get_current_key_ex(headers, &key, &key_len, NULL, 0, &pos);
- add_assoc_string_ex(return_value, key, key_len, *value_pointer, 1);
- zend_hash_move_forward_ex(headers, &pos);
- }
+ ZEND_HASH_FOREACH_STR_KEY_PTR(headers, key, value) {
+ ZVAL_STRING(&tmp, value);
+ zend_symtable_update(Z_ARRVAL_P(return_value), key, &tmp);
+ } ZEND_HASH_FOREACH_END();
}
/* }}} */
-static void add_response_header(sapi_header_struct *h, zval *return_value TSRMLS_DC) /* {{{ */
+static void add_response_header(sapi_header_struct *h, zval *return_value) /* {{{ */
{
char *s, *p;
- int len;
+ ptrdiff_t len;
ALLOCA_FLAG(use_heap)
if (h->header_len > 0) {
@@ -524,7 +405,7 @@ static void add_response_header(sapi_header_struct *h, zval *return_value TSRMLS
do {
p++;
} while (*p == ' ' || *p == '\t');
- add_assoc_stringl_ex(return_value, s, len+1, p, h->header_len - (p - h->header), 1);
+ add_assoc_stringl_ex(return_value, s, (uint)len, p, h->header_len - (p - h->header));
free_alloca(s, use_heap);
}
}
@@ -538,18 +419,15 @@ PHP_FUNCTION(apache_response_headers) /* {{{ */
return;
}
- if (!&SG(sapi_headers).headers) {
- RETURN_FALSE;
- }
array_init(return_value);
- zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value TSRMLS_CC);
+ zend_llist_apply_with_argument(&SG(sapi_headers).headers, (llist_apply_with_arg_func_t)add_response_header, return_value);
}
/* }}} */
/* {{{ cli_server module
*/
-static void cli_server_init_globals(zend_cli_server_globals *cg TSRMLS_DC)
+static void cli_server_init_globals(zend_cli_server_globals *cg)
{
cg->color = 0;
}
@@ -610,7 +488,7 @@ static int sapi_cli_server_startup(sapi_module_struct *sapi_module) /* {{{ */
return SUCCESS;
} /* }}} */
-static int sapi_cli_server_ub_write(const char *str, uint str_length TSRMLS_DC) /* {{{ */
+static size_t sapi_cli_server_ub_write(const char *str, size_t str_length) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (!client) {
@@ -622,29 +500,28 @@ static int sapi_cli_server_ub_write(const char *str, uint str_length TSRMLS_DC)
static void sapi_cli_server_flush(void *server_context) /* {{{ */
{
php_cli_server_client *client = server_context;
- TSRMLS_FETCH();
if (!client) {
return;
}
- if (client->sock < 0) {
+ if (!ZEND_VALID_SOCKET(client->sock)) {
php_handle_aborted_connection();
return;
}
if (!SG(headers_sent)) {
- sapi_send_headers(TSRMLS_C);
+ sapi_send_headers();
SG(headers_sent) = 1;
}
} /* }}} */
-static int sapi_cli_server_discard_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) /* {{{ */{
+static int sapi_cli_server_discard_headers(sapi_headers_struct *sapi_headers) /* {{{ */{
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* }}} */
-static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC) /* {{{ */
+static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
smart_str buffer = { 0 };
@@ -674,24 +551,24 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS
}
smart_str_appendl(&buffer, "\r\n", 2);
- php_cli_server_client_send_through(client, buffer.c, buffer.len);
+ php_cli_server_client_send_through(client, buffer.s->val, buffer.s->len);
smart_str_free(&buffer);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
/* }}} */
-static char *sapi_cli_server_read_cookies(TSRMLS_D) /* {{{ */
+static char *sapi_cli_server_read_cookies(void) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
- char **val;
- if (FAILURE == zend_hash_find(&client->request.headers, "cookie", sizeof("cookie"), (void**)&val)) {
+ char *val;
+ if (NULL == (val = zend_hash_str_find_ptr(&client->request.headers, "cookie", sizeof("cookie")-1))) {
return NULL;
}
- return *val;
+ return val;
} /* }}} */
-static int sapi_cli_server_read_post(char *buf, uint count_bytes TSRMLS_DC) /* {{{ */
+static size_t sapi_cli_server_read_post(char *buf, size_t count_bytes) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (client->request.content) {
@@ -704,27 +581,27 @@ static int sapi_cli_server_read_post(char *buf, uint count_bytes TSRMLS_DC) /* {
return 0;
} /* }}} */
-static void sapi_cli_server_register_variable(zval *track_vars_array, const char *key, const char *val TSRMLS_DC) /* {{{ */
+static void sapi_cli_server_register_variable(zval *track_vars_array, const char *key, const char *val) /* {{{ */
{
char *new_val = (char *)val;
- uint new_val_len;
+ size_t new_val_len;
if (NULL == val) {
return;
}
- if (sapi_module.input_filter(PARSE_SERVER, (char*)key, &new_val, strlen(val), &new_val_len TSRMLS_CC)) {
- php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array TSRMLS_CC);
+ if (sapi_module.input_filter(PARSE_SERVER, (char*)key, &new_val, strlen(val), &new_val_len)) {
+ php_register_variable_safe((char *)key, new_val, new_val_len, track_vars_array);
}
} /* }}} */
-static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
+static int sapi_cli_server_register_entry_cb(char **entry, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
zval *track_vars_array = va_arg(args, zval *);
- if (hash_key->nKeyLength) {
+ if (hash_key->key) {
char *real_key, *key;
uint i;
- key = estrndup(hash_key->arKey, hash_key->nKeyLength);
- for(i=0; i<hash_key->nKeyLength; i++) {
+ key = estrndup(hash_key->key->val, hash_key->key->len);
+ for(i=0; i<hash_key->key->len; i++) {
if (key[i] == '-') {
key[i] = '_';
} else {
@@ -732,7 +609,7 @@ static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_arg
}
}
spprintf(&real_key, 0, "%s_%s", "HTTP", key);
- sapi_cli_server_register_variable(track_vars_array, real_key, *entry TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, real_key, *entry);
efree(key);
efree(real_key);
}
@@ -741,10 +618,10 @@ static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_arg
}
/* }}} */
-static void sapi_cli_server_register_variables(zval *track_vars_array TSRMLS_DC) /* {{{ */
+static void sapi_cli_server_register_variables(zval *track_vars_array) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
- sapi_cli_server_register_variable(track_vars_array, "DOCUMENT_ROOT", client->server->document_root TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "DOCUMENT_ROOT", client->server->document_root);
{
char *tmp;
if ((tmp = strrchr(client->addr_str, ':'))) {
@@ -753,61 +630,61 @@ static void sapi_cli_server_register_variables(zval *track_vars_array TSRMLS_DC)
port[7] = '\0';
strncpy(addr, client->addr_str, tmp - client->addr_str);
addr[tmp - client->addr_str] = '\0';
- sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", addr TSRMLS_CC);
- sapi_cli_server_register_variable(track_vars_array, "REMOTE_PORT", port TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", addr);
+ sapi_cli_server_register_variable(track_vars_array, "REMOTE_PORT", port);
} else {
- sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", client->addr_str TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", client->addr_str);
}
}
{
char *tmp;
spprintf(&tmp, 0, "PHP %s Development Server", PHP_VERSION);
- sapi_cli_server_register_variable(track_vars_array, "SERVER_SOFTWARE", tmp TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SERVER_SOFTWARE", tmp);
efree(tmp);
}
{
char *tmp;
spprintf(&tmp, 0, "HTTP/%d.%d", client->request.protocol_version / 100, client->request.protocol_version % 100);
- sapi_cli_server_register_variable(track_vars_array, "SERVER_PROTOCOL", tmp TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SERVER_PROTOCOL", tmp);
efree(tmp);
}
- sapi_cli_server_register_variable(track_vars_array, "SERVER_NAME", client->server->host TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SERVER_NAME", client->server->host);
{
char *tmp;
spprintf(&tmp, 0, "%i", client->server->port);
- sapi_cli_server_register_variable(track_vars_array, "SERVER_PORT", tmp TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SERVER_PORT", tmp);
efree(tmp);
}
- sapi_cli_server_register_variable(track_vars_array, "REQUEST_URI", client->request.request_uri TSRMLS_CC);
- sapi_cli_server_register_variable(track_vars_array, "REQUEST_METHOD", SG(request_info).request_method TSRMLS_CC);
- sapi_cli_server_register_variable(track_vars_array, "SCRIPT_NAME", client->request.vpath TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "REQUEST_URI", client->request.request_uri);
+ sapi_cli_server_register_variable(track_vars_array, "REQUEST_METHOD", SG(request_info).request_method);
+ sapi_cli_server_register_variable(track_vars_array, "SCRIPT_NAME", client->request.vpath);
if (SG(request_info).path_translated) {
- sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", SG(request_info).path_translated TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", SG(request_info).path_translated);
} else if (client->server->router) {
char *temp;
spprintf(&temp, 0, "%s/%s", client->server->document_root, client->server->router);
- sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", temp TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "SCRIPT_FILENAME", temp);
efree(temp);
}
if (client->request.path_info) {
- sapi_cli_server_register_variable(track_vars_array, "PATH_INFO", client->request.path_info TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "PATH_INFO", client->request.path_info);
}
if (client->request.path_info_len) {
char *tmp;
spprintf(&tmp, 0, "%s%s", client->request.vpath, client->request.path_info);
- sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", tmp TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", tmp);
efree(tmp);
} else {
- sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", client->request.vpath TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "PHP_SELF", client->request.vpath);
}
if (client->request.query_string) {
- sapi_cli_server_register_variable(track_vars_array, "QUERY_STRING", client->request.query_string TSRMLS_CC);
+ sapi_cli_server_register_variable(track_vars_array, "QUERY_STRING", client->request.query_string);
}
- zend_hash_apply_with_arguments(&client->request.headers TSRMLS_CC, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
+ zend_hash_apply_with_arguments(&client->request.headers, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
} /* }}} */
-static void sapi_cli_server_log_message(char *msg TSRMLS_DC) /* {{{ */
+static void sapi_cli_server_log_message(char *msg) /* {{{ */
{
char buf[52];
@@ -866,7 +743,7 @@ static int php_cli_server_poller_ctor(php_cli_server_poller *poller) /* {{{ */
return SUCCESS;
} /* }}} */
-static void php_cli_server_poller_add(php_cli_server_poller *poller, int mode, int fd) /* {{{ */
+static void php_cli_server_poller_add(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
{
if (mode & POLLIN) {
PHP_SAFE_FD_SET(fd, &poller->rfds);
@@ -879,7 +756,7 @@ static void php_cli_server_poller_add(php_cli_server_poller *poller, int mode, i
}
} /* }}} */
-static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode, int fd) /* {{{ */
+static void php_cli_server_poller_remove(php_cli_server_poller *poller, int mode, php_socket_t fd) /* {{{ */
{
if (mode & POLLIN) {
PHP_SAFE_FD_CLR(fd, &poller->rfds);
@@ -907,7 +784,7 @@ static int php_cli_server_poller_poll(php_cli_server_poller *poller, struct time
return php_select(poller->max_fd + 1, &poller->active.rfds, &poller->active.wfds, NULL, tv);
} /* }}} */
-static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, void *opaque, int(*callback)(void *, int fd, int events)) /* {{{ */
+static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, void *opaque, int(*callback)(void *, php_socket_t fd, int events)) /* {{{ */
{
int retval = SUCCESS;
#ifdef PHP_WIN32
@@ -915,7 +792,6 @@ static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, v
SOCKET fd;
int events;
} entries[FD_SETSIZE * 2];
- php_socket_t fd = 0;
size_t i;
struct socket_entry *n = entries, *m;
@@ -1058,7 +934,7 @@ static php_cli_server_chunk *php_cli_server_chunk_immortal_new(const char *buf,
return chunk;
} /* }}} */
-static php_cli_server_chunk *php_cli_server_chunk_heap_new(char *block, char *buf, size_t len) /* {{{ */
+static php_cli_server_chunk *php_cli_server_chunk_heap_new(void *block, char *buf, size_t len) /* {{{ */
{
php_cli_server_chunk *chunk = pemalloc(sizeof(php_cli_server_chunk), 1);
if (!chunk) {
@@ -1104,12 +980,20 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
size_t _nbytes_sent_total = 0;
for (chunk = sender->buffer.first; chunk; chunk = next) {
+#ifdef PHP_WIN32
+ int nbytes_sent;
+#else
ssize_t nbytes_sent;
+#endif
next = chunk->next;
switch (chunk->type) {
case PHP_CLI_SERVER_CHUNK_HEAP:
+#ifdef PHP_WIN32
+ nbytes_sent = send(fd, chunk->data.heap.p, (int)chunk->data.heap.len, 0);
+#else
nbytes_sent = send(fd, chunk->data.heap.p, chunk->data.heap.len, 0);
+#endif
if (nbytes_sent < 0) {
*nbytes_sent_total = _nbytes_sent_total;
return php_socket_errno();
@@ -1128,7 +1012,11 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
break;
case PHP_CLI_SERVER_CHUNK_IMMORTAL:
+#ifdef PHP_WIN32
+ nbytes_sent = send(fd, chunk->data.immortal.p, (int)chunk->data.immortal.len, 0);
+#else
nbytes_sent = send(fd, chunk->data.immortal.p, chunk->data.immortal.len, 0);
+#endif
if (nbytes_sent < 0) {
*nbytes_sent_total = _nbytes_sent_total;
return php_socket_errno();
@@ -1153,14 +1041,21 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sender, int fd, size_t *nbytes_read) /* {{{ */
{
+#ifdef PHP_WIN32
+ int _nbytes_read;
+#else
ssize_t _nbytes_read;
+#endif
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(131072);
+#ifdef PHP_WIN32
+ _nbytes_read = read(fd, chunk->data.heap.p, (unsigned int)chunk->data.heap.len);
+#else
_nbytes_read = read(fd, chunk->data.heap.p, chunk->data.heap.len);
+#endif
if (_nbytes_read < 0) {
char *errstr = get_last_error();
- TSRMLS_FETCH();
- php_cli_server_logf("%s" TSRMLS_CC, errstr);
+ php_cli_server_logf("%s", errstr);
pefree(errstr, 1);
php_cli_server_chunk_dtor(chunk);
pefree(chunk, 1);
@@ -1182,7 +1077,7 @@ static int php_cli_is_output_tty() /* {{{ */
} /* }}} */
#endif
-static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message TSRMLS_DC) /* {{{ */
+static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message) /* {{{ */
{
int color = 0, effective_status = status;
char *basic_buf, *message_buf = "", *error_buf = "";
@@ -1248,9 +1143,9 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
}
if (color) {
- php_cli_server_logf("\x1b[3%dm%s%s%s\x1b[0m" TSRMLS_CC, color, basic_buf, message_buf, error_buf);
+ php_cli_server_logf("\x1b[3%dm%s%s%s\x1b[0m", color, basic_buf, message_buf, error_buf);
} else {
- php_cli_server_logf("%s%s%s" TSRMLS_CC, basic_buf, message_buf, error_buf);
+ php_cli_server_logf("%s%s%s", basic_buf, message_buf, error_buf);
}
efree(basic_buf);
@@ -1262,15 +1157,12 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
}
} /* }}} */
-static void php_cli_server_logf(const char *format TSRMLS_DC, ...) /* {{{ */
+static void php_cli_server_logf(const char *format, ...) /* {{{ */
{
char *buf = NULL;
va_list ap;
-#ifdef ZTS
- va_start(ap, tsrm_ls);
-#else
+
va_start(ap, format);
-#endif
vspprintf(&buf, 0, format, ap);
va_end(ap);
@@ -1279,19 +1171,19 @@ static void php_cli_server_logf(const char *format TSRMLS_DC, ...) /* {{{ */
}
if (sapi_module.log_message) {
- sapi_module.log_message(buf TSRMLS_CC);
+ sapi_module.log_message(buf);
}
efree(buf);
} /* }}} */
-static int php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, char **errstr TSRMLS_DC) /* {{{ */
+static php_socket_t php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, zend_string **errstr) /* {{{ */
{
- int retval = SOCK_ERR;
+ php_socket_t retval = SOCK_ERR;
int err = 0;
struct sockaddr *sa = NULL, **p, **sal;
- int num_addrs = php_network_getaddresses(host, socktype, &sal, errstr TSRMLS_CC);
+ int num_addrs = php_network_getaddresses(host, socktype, &sal, errstr);
if (num_addrs == 0) {
return -1;
}
@@ -1396,11 +1288,11 @@ out:
php_network_freeaddresses(sal);
}
if (err) {
- if (retval >= 0) {
+ if (ZEND_VALID_SOCKET(retval)) {
closesocket(retval);
}
if (errstr) {
- *errstr = php_socket_strerror(err, NULL, 0);
+ *errstr = php_socket_error_str(err);
}
return SOCK_ERR;
}
@@ -1409,6 +1301,9 @@ out:
static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */
{
+#ifdef ZTS
+ZEND_TSRMLS_CACHE_UPDATE;
+#endif
req->protocol_version = 0;
req->request_uri = NULL;
req->request_uri_len = 0;
@@ -1420,7 +1315,7 @@ static int php_cli_server_request_ctor(php_cli_server_request *req) /* {{{ */
req->path_info_len = 0;
req->query_string = NULL;
req->query_string_len = 0;
- zend_hash_init(&req->headers, 0, NULL, (void(*)(void*))char_ptr_dtor_p, 1);
+ zend_hash_init(&req->headers, 0, NULL, char_ptr_dtor_p, 1);
zend_hash_init(&req->headers_original_case, 0, NULL, NULL, 1);
req->content = NULL;
req->content_len = 0;
@@ -1455,7 +1350,7 @@ static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */
static void php_cli_server_request_translate_vpath(php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
{
- struct stat sb;
+ zend_stat_t sb;
static const char *index_files[] = { "index.php", "index.html", NULL };
char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
char *p = buf, *prev_path = NULL, *q, *vpath;
@@ -1492,7 +1387,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
*p = '\0';
q = p;
while (q > buf) {
- if (!stat(buf, &sb)) {
+ if (!zend_stat(buf, &sb)) {
if (sb.st_mode & S_IFDIR) {
const char **file = index_files;
if (q[-1] != DEFAULT_SLASH) {
@@ -1501,7 +1396,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
while (*file) {
size_t l = strlen(*file);
memmove(q, *file, l + 1);
- if (!stat(buf, &sb) && (sb.st_mode & S_IFREG)) {
+ if (!zend_stat(buf, &sb) && (sb.st_mode & S_IFREG)) {
q += l;
break;
}
@@ -1574,7 +1469,7 @@ static void normalize_vpath(char **retval, size_t *retval_len, const char *vpath
return;
}
- decoded_vpath_end = decoded_vpath + php_url_decode(decoded_vpath, vpath_len);
+ decoded_vpath_end = decoded_vpath + php_url_decode(decoded_vpath, (int)vpath_len);
p = decoded_vpath;
@@ -1691,13 +1586,12 @@ static int php_cli_server_client_read_request_on_header_value(php_http_parser *p
}
{
/* strip off the colon */
- char *orig_header_name = estrndup(client->current_header_name, client->current_header_name_len);
+ zend_string *orig_header_name = zend_string_init(client->current_header_name, client->current_header_name_len, 1);
char *lc_header_name = zend_str_tolower_dup(client->current_header_name, client->current_header_name_len);
-
- zend_hash_add(&client->request.headers, lc_header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL);
- zend_hash_add(&client->request.headers_original_case, orig_header_name, client->current_header_name_len + 1, &value, sizeof(char *), NULL);
+ zend_hash_str_add_ptr(&client->request.headers, lc_header_name, client->current_header_name_len, value);
+ zend_hash_add_ptr(&client->request.headers_original_case, orig_header_name, value);
efree(lc_header_name);
- efree(orig_header_name);
+ zend_string_release(orig_header_name);
}
if (client->current_header_name_allocated) {
@@ -1757,7 +1651,7 @@ static int php_cli_server_client_read_request_on_message_complete(php_http_parse
return 0;
}
-static int php_cli_server_client_read_request(php_cli_server_client *client, char **errstr TSRMLS_DC)
+static int php_cli_server_client_read_request(php_cli_server_client *client, char **errstr)
{
char buf[16384];
static const php_http_parser_settings settings = {
@@ -1815,9 +1709,19 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len) /* {{{ */
{
struct timeval tv = { 10, 0 };
- ssize_t nbytes_left = str_len;
+#ifdef PHP_WIN32
+ int nbytes_left = (int)str_len;
+#else
+ ssize_t nbytes_left = (ssize_t)str_len;
+#endif
do {
- ssize_t nbytes_sent = send(client->sock, str + str_len - nbytes_left, nbytes_left, 0);
+#ifdef PHP_WIN32
+ int nbytes_sent;
+#else
+ ssize_t nbytes_sent;
+#endif
+
+ nbytes_sent = send(client->sock, str + str_len - nbytes_left, nbytes_left, 0);
if (nbytes_sent < 0) {
int err = php_socket_errno();
if (err == SOCK_EAGAIN) {
@@ -1846,7 +1750,7 @@ static size_t php_cli_server_client_send_through(php_cli_server_client *client,
static void php_cli_server_client_populate_request_info(const php_cli_server_client *client, sapi_request_info *request_info) /* {{{ */
{
- char **val;
+ char *val;
request_info->request_method = php_http_method_str(client->request.request_method);
request_info->proto_num = client->request.protocol_version;
@@ -1855,8 +1759,8 @@ static void php_cli_server_client_populate_request_info(const php_cli_server_cli
request_info->query_string = client->request.query_string;
request_info->content_length = client->request.content_len;
request_info->auth_user = request_info->auth_password = request_info->auth_digest = NULL;
- if (SUCCESS == zend_hash_find(&client->request.headers, "content-type", sizeof("content-type"), (void**)&val)) {
- request_info->content_type = *val;
+ if (NULL != (val = zend_hash_str_find_ptr(&client->request.headers, "content-type", sizeof("content-type")-1))) {
+ request_info->content_type = val;
}
} /* }}} */
@@ -1864,19 +1768,19 @@ static void destroy_request_info(sapi_request_info *request_info) /* {{{ */
{
} /* }}} */
-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) /* {{{ */
+static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_server *server, php_socket_t client_sock, struct sockaddr *addr, socklen_t addr_len) /* {{{ */
{
client->server = server;
client->sock = client_sock;
client->addr = addr;
client->addr_len = addr_len;
{
- char *addr_str = 0;
- long addr_str_len = 0;
- php_network_populate_name_from_sockaddr(addr, addr_len, &addr_str, &addr_str_len, NULL, 0 TSRMLS_CC);
- client->addr_str = pestrndup(addr_str, addr_str_len, 1);
- client->addr_str_len = addr_str_len;
- efree(addr_str);
+ zend_string *addr_str = 0;
+
+ php_network_populate_name_from_sockaddr(addr, addr_len, &addr_str, NULL, 0);
+ client->addr_str = pestrndup(addr_str->val, addr_str->len, 1);
+ client->addr_str_len = addr_str->len;
+ zend_string_release(addr_str);
}
php_http_parser_init(&client->parser, PHP_HTTP_REQUEST);
client->request_read = 0;
@@ -1906,18 +1810,17 @@ static void php_cli_server_client_dtor(php_cli_server_client *client) /* {{{ */
}
} /* }}} */
-static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
#ifdef DEBUG
- php_cli_server_logf("%s Closing" TSRMLS_CC, client->addr_str);
+ php_cli_server_logf("%s Closing", client->addr_str);
#endif
zend_hash_index_del(&server->clients, client->sock);
} /* }}} */
-static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server_client *client, int status TSRMLS_DC) /* {{{ */
+static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server_client *client, int status) /* {{{ */
{
- char *escaped_request_uri = NULL;
- size_t escaped_request_uri_len;
+ zend_string *escaped_request_uri = NULL;
const char *status_string = get_status_string(status);
const char *content_template = get_template_string(status);
char *errstr = get_last_error();
@@ -1926,7 +1829,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
php_cli_server_content_sender_ctor(&client->content_sender);
client->content_sender_initialized = 1;
- 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);
+ escaped_request_uri = php_escape_html_entities_ex((unsigned char *)client->request.request_uri, client->request.request_uri_len, 0, ENT_QUOTES, NULL, 0);
{
static const char prologue_template[] = "<!doctype html><html><head><title>%d %s</title>";
@@ -1934,7 +1837,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
if (!chunk) {
goto fail;
}
- snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string, escaped_request_uri);
+ snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string, escaped_request_uri->val);
chunk->data.heap.len = strlen(chunk->data.heap.p);
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
@@ -1954,11 +1857,11 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
{
- 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);
+ 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);
if (!chunk) {
goto fail;
}
- snprintf(chunk->data.heap.p, chunk->data.heap.len, content_template, status_string, escaped_request_uri);
+ snprintf(chunk->data.heap.p, chunk->data.heap.len, content_template, status_string, escaped_request_uri->val);
chunk->data.heap.len = strlen(chunk->data.heap.p);
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
@@ -1975,46 +1878,46 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
php_cli_server_chunk *chunk;
smart_str buffer = { 0 };
append_http_status_line(&buffer, client->request.protocol_version, status, 1);
- if (!buffer.c) {
+ if (!buffer.s) {
/* out of memory */
goto fail;
}
append_essential_headers(&buffer, client, 1);
smart_str_appends_ex(&buffer, "Content-Type: text/html; charset=UTF-8\r\n", 1);
smart_str_appends_ex(&buffer, "Content-Length: ", 1);
- smart_str_append_generic_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1, size_t, _unsigned);
+ smart_str_append_unsigned_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1);
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
- chunk = php_cli_server_chunk_heap_new(buffer.c, buffer.c, buffer.len);
+ chunk = php_cli_server_chunk_heap_new(buffer.s, buffer.s->val, buffer.s->len);
if (!chunk) {
- smart_str_free_ex(&buffer, 1);
+ smart_str_free(&buffer);
goto fail;
}
php_cli_server_buffer_prepend(&client->content_sender.buffer, chunk);
}
- php_cli_server_log_response(client, status, errstr ? errstr : "?" TSRMLS_CC);
+ php_cli_server_log_response(client, status, errstr ? errstr : "?");
php_cli_server_poller_add(&server->poller, POLLOUT, client->sock);
if (errstr) {
pefree(errstr, 1);
}
- efree(escaped_request_uri);
+ zend_string_free(escaped_request_uri);
return SUCCESS;
fail:
if (errstr) {
pefree(errstr, 1);
}
- efree(escaped_request_uri);
+ zend_string_free(escaped_request_uri);
return FAILURE;
} /* }}} */
-static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
if (strlen(client->request.path_translated) != client->request.path_translated_len) {
/* can't handle paths that contain nul bytes */
- return php_cli_server_send_error_page(server, client, 400 TSRMLS_CC);
+ return php_cli_server_send_error_page(server, client, 400);
}
{
zend_file_handle zfd;
@@ -2024,27 +1927,27 @@ static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server
zfd.free_filename = 0;
zfd.opened_path = NULL;
zend_try {
- php_execute_script(&zfd TSRMLS_CC);
+ php_execute_script(&zfd);
} zend_end_try();
}
- php_cli_server_log_response(client, SG(sapi_headers).http_response_code, NULL TSRMLS_CC);
+ php_cli_server_log_response(client, SG(sapi_headers).http_response_code, NULL);
return SUCCESS;
} /* }}} */
-static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
int fd;
int status = 200;
if (client->request.path_translated && strlen(client->request.path_translated) != client->request.path_translated_len) {
/* can't handle paths that contain nul bytes */
- return php_cli_server_send_error_page(server, client, 400 TSRMLS_CC);
+ return php_cli_server_send_error_page(server, client, 400);
}
fd = client->request.path_translated ? open(client->request.path_translated, O_RDONLY): -1;
if (fd < 0) {
- return php_cli_server_send_error_page(server, client, 404 TSRMLS_CC);
+ return php_cli_server_send_error_page(server, client, 404);
}
php_cli_server_content_sender_ctor(&client->content_sender);
@@ -2054,15 +1957,15 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv
{
php_cli_server_chunk *chunk;
smart_str buffer = { 0 };
- const char *mime_type = get_mime_type(client->request.ext, client->request.ext_len);
+ const char *mime_type = get_mime_type(server, client->request.ext, client->request.ext_len);
if (!mime_type) {
mime_type = "application/octet-stream";
}
append_http_status_line(&buffer, client->request.protocol_version, status, 1);
- if (!buffer.c) {
+ if (!buffer.s) {
/* out of memory */
- php_cli_server_log_response(client, 500, NULL TSRMLS_CC);
+ php_cli_server_log_response(client, 500, NULL);
return FAILURE;
}
append_essential_headers(&buffer, client, 1);
@@ -2073,31 +1976,31 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv
}
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
smart_str_appends_ex(&buffer, "Content-Length: ", 1);
- smart_str_append_generic_ex(&buffer, client->request.sb.st_size, 1, size_t, _unsigned);
+ smart_str_append_unsigned_ex(&buffer, client->request.sb.st_size, 1);
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
- chunk = php_cli_server_chunk_heap_new(buffer.c, buffer.c, buffer.len);
+ chunk = php_cli_server_chunk_heap_new(buffer.s, buffer.s->val, buffer.s->len);
if (!chunk) {
- smart_str_free_ex(&buffer, 1);
- php_cli_server_log_response(client, 500, NULL TSRMLS_CC);
+ smart_str_free(&buffer);
+ php_cli_server_log_response(client, 500, NULL);
return FAILURE;
}
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
- php_cli_server_log_response(client, 200, NULL TSRMLS_CC);
+ php_cli_server_log_response(client, 200, NULL);
php_cli_server_poller_add(&server->poller, POLLOUT, client->sock);
return SUCCESS;
}
/* }}} */
-static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
- char **auth;
+static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client) { /* {{{ */
+ char *auth;
php_cli_server_client_populate_request_info(client, &SG(request_info));
- if (SUCCESS == zend_hash_find(&client->request.headers, "authorization", sizeof("authorization"), (void**)&auth)) {
- php_handle_auth_data(*auth TSRMLS_CC);
+ if (NULL != (auth = zend_hash_str_find_ptr(&client->request.headers, "authorization", sizeof("authorization")-1))) {
+ php_handle_auth_data(auth);
}
SG(sapi_headers).http_response_code = 200;
- if (FAILURE == php_request_startup(TSRMLS_C)) {
+ if (FAILURE == php_request_startup()) {
/* should never be happen */
destroy_request_info(&SG(request_info));
return FAILURE;
@@ -2108,9 +2011,9 @@ static int php_cli_server_request_startup(php_cli_server *server, php_cli_server
}
/* }}} */
-static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
+static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_server_client *client) { /* {{{ */
php_request_shutdown(0);
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
destroy_request_info(&SG(request_info));
SG(server_context) = NULL;
SG(rfc1867_uploaded_files) = NULL;
@@ -2118,7 +2021,7 @@ static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_serve
}
/* }}} */
-static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
int decline = 0;
zend_file_handle zfd;
@@ -2136,10 +2039,10 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
zfd.opened_path = NULL;
zend_try {
- zval *retval = NULL;
- if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE TSRMLS_CC, &retval, 1, &zfd)) {
- if (retval) {
- decline = Z_TYPE_P(retval) == IS_BOOL && !Z_LVAL_P(retval);
+ zval retval;
+ if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE, &retval, 1, &zfd)) {
+ if (Z_TYPE(retval) != IS_UNDEF) {
+ decline = Z_TYPE(retval) == IS_FALSE;
zval_ptr_dtor(&retval);
}
} else {
@@ -2157,7 +2060,7 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
}
/* }}} */
-static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
int is_static_file = 0;
@@ -2167,30 +2070,30 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
}
if (server->router || !is_static_file) {
- if (FAILURE == php_cli_server_request_startup(server, client TSRMLS_CC)) {
+ if (FAILURE == php_cli_server_request_startup(server, client)) {
SG(server_context) = NULL;
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
destroy_request_info(&SG(request_info));
return SUCCESS;
}
}
if (server->router) {
- if (!php_cli_server_dispatch_router(server, client TSRMLS_CC)) {
- php_cli_server_request_shutdown(server, client TSRMLS_CC);
+ if (!php_cli_server_dispatch_router(server, client)) {
+ php_cli_server_request_shutdown(server, client);
return SUCCESS;
}
}
if (!is_static_file) {
- if (SUCCESS == php_cli_server_dispatch_script(server, client TSRMLS_CC)
- || SUCCESS != php_cli_server_send_error_page(server, client, 500 TSRMLS_CC)) {
- php_cli_server_request_shutdown(server, client TSRMLS_CC);
+ if (SUCCESS == php_cli_server_dispatch_script(server, client)
+ || SUCCESS != php_cli_server_send_error_page(server, client, 500)) {
+ php_cli_server_request_shutdown(server, client);
return SUCCESS;
}
} else {
if (server->router) {
- static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
+ static int (*send_header_func)(sapi_headers_struct *);
send_header_func = sapi_module.send_headers;
/* do not generate default content type header */
SG(sapi_headers).send_default_content_type = 0;
@@ -2201,8 +2104,8 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
SG(sapi_headers).send_default_content_type = 1;
SG(rfc1867_uploaded_files) = NULL;
}
- if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ if (SUCCESS != php_cli_server_begin_send_static(server, client)) {
+ php_cli_server_close_connection(server, client);
}
SG(server_context) = NULL;
return SUCCESS;
@@ -2214,10 +2117,29 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
}
/* }}} */
-static void php_cli_server_dtor(php_cli_server *server TSRMLS_DC) /* {{{ */
+static int php_cli_server_mime_type_ctor(php_cli_server *server, const php_cli_server_ext_mime_type_pair *mime_type_map) /* {{{ */
+{
+ const php_cli_server_ext_mime_type_pair *pair;
+
+ zend_hash_init(&server->extension_mime_types, 0, NULL, NULL, 1);
+
+ for (pair = mime_type_map; pair->ext; pair++) {
+ size_t ext_len = 0, mime_type_len = 0;
+
+ ext_len = strlen(pair->ext);
+ mime_type_len = strlen(pair->mime_type);
+
+ zend_hash_str_add_mem(&server->extension_mime_types, pair->ext, ext_len, (void*)pair->mime_type, mime_type_len + 1);
+ }
+
+ return SUCCESS;
+} /* }}} */
+
+static void php_cli_server_dtor(php_cli_server *server) /* {{{ */
{
zend_hash_destroy(&server->clients);
- if (server->server_sock >= 0) {
+ zend_hash_destroy(&server->extension_mime_types);
+ if (ZEND_VALID_SOCKET(server->server_sock)) {
closesocket(server->server_sock);
}
if (server->host) {
@@ -2231,19 +2153,21 @@ static void php_cli_server_dtor(php_cli_server *server TSRMLS_DC) /* {{{ */
}
} /* }}} */
-static void php_cli_server_client_dtor_wrapper(php_cli_server_client **p) /* {{{ */
+static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
{
- closesocket((*p)->sock);
- php_cli_server_poller_remove(&(*p)->server->poller, POLLIN | POLLOUT, (*p)->sock);
- php_cli_server_client_dtor(*p);
- pefree(*p, 1);
+ php_cli_server_client *p = Z_PTR_P(zv);
+
+ closesocket(p->sock);
+ php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock);
+ php_cli_server_client_dtor(p);
+ pefree(p, 1);
} /* }}} */
-static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router TSRMLS_DC) /* {{{ */
+static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router) /* {{{ */
{
int retval = SUCCESS;
char *host = NULL;
- char *errstr = NULL;
+ zend_string *errstr = NULL;
char *_document_root = NULL;
char *_router = NULL;
int err = 0;
@@ -2288,10 +2212,12 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
goto out;
}
- server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr TSRMLS_CC);
+ server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr);
if (server_sock == SOCK_ERR) {
- php_cli_server_logf("Failed to listen on %s:%d (reason: %s)" TSRMLS_CC, host, port, errstr ? errstr: "?");
- efree(errstr);
+ php_cli_server_logf("Failed to listen on %s:%d (reason: %s)", host, port, errstr ? errstr->val : "?");
+ if (errstr) {
+ zend_string_release(errstr);
+ }
retval = FAILURE;
goto out;
}
@@ -2307,7 +2233,7 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
server->host = host;
server->port = port;
- zend_hash_init(&server->clients, 0, NULL, (void(*)(void*))php_cli_server_client_dtor_wrapper, 1);
+ zend_hash_init(&server->clients, 0, NULL, php_cli_server_client_dtor_wrapper, 1);
{
size_t document_root_len = strlen(document_root);
@@ -2334,6 +2260,11 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
server->router_len = 0;
}
+ if (php_cli_server_mime_type_ctor(server, mime_type_map) == FAILURE) {
+ retval = FAILURE;
+ goto out;
+ }
+
server->is_running = 1;
out:
if (retval != SUCCESS) {
@@ -2353,20 +2284,20 @@ out:
return retval;
} /* }}} */
-static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
char *errstr = NULL;
- int status = php_cli_server_client_read_request(client, &errstr TSRMLS_CC);
+ int status = php_cli_server_client_read_request(client, &errstr);
if (status < 0) {
- php_cli_server_logf("%s Invalid request (%s)" TSRMLS_CC, client->addr_str, errstr);
+ php_cli_server_logf("%s Invalid request (%s)", client->addr_str, errstr);
efree(errstr);
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
return FAILURE;
} else if (status == 1 && client->request.request_method == PHP_HTTP_NOT_IMPLEMENTED) {
- return php_cli_server_send_error_page(server, client, 501 TSRMLS_CC);
+ return php_cli_server_send_error_page(server, client, 501);
} else if (status == 1) {
php_cli_server_poller_remove(&server->poller, POLLIN, client->sock);
- php_cli_server_dispatch(server, client TSRMLS_CC);
+ php_cli_server_dispatch(server, client);
} else {
php_cli_server_poller_add(&server->poller, POLLIN, client->sock);
}
@@ -2374,13 +2305,13 @@ static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cl
return SUCCESS;
} /* }}} */
-static int php_cli_server_send_event(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+static int php_cli_server_send_event(php_cli_server *server, php_cli_server_client *client) /* {{{ */
{
if (client->content_sender_initialized) {
if (client->file_fd >= 0 && !client->content_sender.buffer.first) {
size_t nbytes_read;
if (php_cli_server_content_sender_pull(&client->content_sender, client->file_fd, &nbytes_read)) {
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
return FAILURE;
}
if (nbytes_read == 0) {
@@ -2392,12 +2323,12 @@ static int php_cli_server_send_event(php_cli_server *server, php_cli_server_clie
size_t nbytes_sent;
int err = php_cli_server_content_sender_send(&client->content_sender, client->sock, &nbytes_sent);
if (err && err != SOCK_EAGAIN) {
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
return FAILURE;
}
}
if (!client->content_sender.buffer.first && client->file_fd < 0) {
- php_cli_server_close_connection(server, client TSRMLS_CC);
+ php_cli_server_close_connection(server, client);
}
}
return SUCCESS;
@@ -2405,20 +2336,14 @@ static int php_cli_server_send_event(php_cli_server *server, php_cli_server_clie
/* }}} */
typedef struct php_cli_server_do_event_for_each_fd_callback_params {
-#ifdef ZTS
- void ***tsrm_ls;
-#endif
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);
+ int(*rhandler)(php_cli_server*, php_cli_server_client*);
+ int(*whandler)(php_cli_server*, php_cli_server_client*);
} php_cli_server_do_event_for_each_fd_callback_params;
-static int php_cli_server_do_event_for_each_fd_callback(void *_params, int fd, int event) /* {{{ */
+static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socket_t fd, int event) /* {{{ */
{
php_cli_server_do_event_for_each_fd_callback_params *params = _params;
-#ifdef ZTS
- void ***tsrm_ls = params->tsrm_ls;
-#endif
php_cli_server *server = params->server;
if (server->server_sock == fd) {
php_cli_server_client *client = NULL;
@@ -2429,50 +2354,47 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, int fd, i
return FAILURE;
}
client_sock = accept(server->server_sock, sa, &socklen);
- if (client_sock < 0) {
+ if (!ZEND_VALID_SOCKET(client_sock)) {
char *errstr;
errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
- php_cli_server_logf("Failed to accept a client (reason: %s)" TSRMLS_CC, errstr);
+ php_cli_server_logf("Failed to accept a client (reason: %s)", errstr);
efree(errstr);
pefree(sa, 1);
return SUCCESS;
}
- if (SUCCESS != php_set_sock_blocking(client_sock, 0 TSRMLS_CC)) {
+ if (SUCCESS != php_set_sock_blocking(client_sock, 0)) {
pefree(sa, 1);
closesocket(client_sock);
return SUCCESS;
}
- if (!(client = pemalloc(sizeof(php_cli_server_client), 1)) || FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen TSRMLS_CC)) {
- php_cli_server_logf("Failed to create a new request object" TSRMLS_CC);
+ if (!(client = pemalloc(sizeof(php_cli_server_client), 1)) || FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
+ php_cli_server_logf("Failed to create a new request object");
pefree(sa, 1);
closesocket(client_sock);
return SUCCESS;
}
#ifdef DEBUG
- php_cli_server_logf("%s Accepted" TSRMLS_CC, client->addr_str);
+ php_cli_server_logf("%s Accepted", client->addr_str);
#endif
- zend_hash_index_update(&server->clients, client_sock, &client, sizeof(client), NULL);
- php_cli_server_recv_event_read_request(server, client TSRMLS_CC);
+ zend_hash_index_update_ptr(&server->clients, client_sock, client);
+ php_cli_server_recv_event_read_request(server, client);
} else {
- php_cli_server_client **client;
- if (SUCCESS == zend_hash_index_find(&server->clients, fd, (void **)&client)) {
+ php_cli_server_client *client;
+ if (NULL != (client = zend_hash_index_find_ptr(&server->clients, fd))) {
if (event & POLLIN) {
- params->rhandler(server, *client TSRMLS_CC);
+ params->rhandler(server, client);
}
if (event & POLLOUT) {
- params->whandler(server, *client TSRMLS_CC);
+ params->whandler(server, client);
}
}
}
return SUCCESS;
} /* }}} */
-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) /* {{{ */
+static void php_cli_server_do_event_for_each_fd(php_cli_server *server, int(*rhandler)(php_cli_server*, php_cli_server_client*), int(*whandler)(php_cli_server*, php_cli_server_client*)) /* {{{ */
{
php_cli_server_do_event_for_each_fd_callback_params params = {
-#ifdef ZTS
- tsrm_ls,
-#endif
server,
rhandler,
whandler
@@ -2481,7 +2403,7 @@ static void php_cli_server_do_event_for_each_fd(php_cli_server *server, int(*rha
php_cli_server_poller_iter_on_active(&server->poller, &params, php_cli_server_do_event_for_each_fd_callback);
} /* }}} */
-static int php_cli_server_do_event_loop(php_cli_server *server TSRMLS_DC) /* {{{ */
+static int php_cli_server_do_event_loop(php_cli_server *server) /* {{{ */
{
int retval = SUCCESS;
while (server->is_running) {
@@ -2490,14 +2412,14 @@ static int php_cli_server_do_event_loop(php_cli_server *server TSRMLS_DC) /* {{{
if (n > 0) {
php_cli_server_do_event_for_each_fd(server,
php_cli_server_recv_event_read_request,
- php_cli_server_send_event TSRMLS_CC);
+ php_cli_server_send_event);
} else if (n == 0) {
/* do nothing */
} else {
int err = php_socket_errno();
if (err != SOCK_EINTR) {
char *errstr = php_socket_strerror(err, NULL, 0);
- php_cli_server_logf("%s" TSRMLS_CC, errstr);
+ php_cli_server_logf("%s", errstr);
efree(errstr);
retval = FAILURE;
goto out;
@@ -2516,7 +2438,7 @@ static void php_cli_server_sigint_handler(int sig) /* {{{ */
}
/* }}} */
-int do_cli_server(int argc, char **argv TSRMLS_DC) /* {{{ */
+int do_cli_server(int argc, char **argv) /* {{{ */
{
char *php_optarg = NULL;
int php_optind = 1;
@@ -2539,9 +2461,9 @@ int do_cli_server(int argc, char **argv TSRMLS_DC) /* {{{ */
}
if (document_root) {
- struct stat sb;
+ zend_stat_t sb;
- if (stat(document_root, &sb)) {
+ if (zend_stat(document_root, &sb)) {
fprintf(stderr, "Directory %s does not exist.\n", document_root);
return 1;
}
@@ -2567,7 +2489,7 @@ int do_cli_server(int argc, char **argv TSRMLS_DC) /* {{{ */
router = argv[php_optind];
}
- if (FAILURE == php_cli_server_ctor(&server, server_bind_address, document_root, router TSRMLS_CC)) {
+ if (FAILURE == php_cli_server_ctor(&server, server_bind_address, document_root, router)) {
return 1;
}
sapi_module.phpinfo_as_text = 0;
@@ -2589,8 +2511,8 @@ int do_cli_server(int argc, char **argv TSRMLS_DC) /* {{{ */
#if defined(HAVE_SIGNAL_H) && defined(SIGINT)
signal(SIGINT, php_cli_server_sigint_handler);
#endif
- php_cli_server_do_event_loop(&server TSRMLS_CC);
- php_cli_server_dtor(&server TSRMLS_CC);
+ php_cli_server_do_event_loop(&server);
+ php_cli_server_dtor(&server);
return 0;
} /* }}} */