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.c307
1 files changed, 130 insertions, 177 deletions
diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
index f333addafd..909e91e3fa 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-2014 The PHP Group |
+----------------------------------------------------------------------+
@@ -96,7 +96,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 +104,7 @@
#include "php_http_parser.h"
#include "php_cli_server.h"
+#include "mime_type_map.h"
#include "php_cli_process_title.h"
@@ -138,7 +139,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 +195,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,11 +203,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" },
@@ -262,64 +259,6 @@ 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 }
-};
-
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
@@ -371,9 +310,9 @@ int php_cli_server_get_system_time(char *buf) {
}
#endif
-static void char_ptr_dtor_p(char **p) /* {{{ */
-{
- pefree(*p, 1);
+static void char_ptr_dtor_p(zval *zv) /* {{{ */
+{
+ pefree(Z_PTR_P(zv), 1);
} /* }}} */
static char *get_last_error() /* {{{ */
@@ -441,11 +380,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 +393,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 +426,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) /* {{{ */
{
char *s, *p;
- int len;
+ ptrdiff_t len;
ALLOCA_FLAG(use_heap)
if (h->header_len > 0) {
@@ -524,7 +453,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, len, p, h->header_len - (p - h->header));
free_alloca(s, use_heap);
}
}
@@ -610,7 +539,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 TSRMLS_DC) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (!client) {
@@ -619,10 +548,9 @@ static int sapi_cli_server_ub_write(const char *str, uint str_length TSRMLS_DC)
return php_cli_server_client_send_through(client, str, str_length);
} /* }}} */
-static void sapi_cli_server_flush(void *server_context) /* {{{ */
+static void sapi_cli_server_flush(void *server_context TSRMLS_DC) /* {{{ */
{
php_cli_server_client *client = server_context;
- TSRMLS_FETCH();
if (!client) {
return;
@@ -674,7 +602,7 @@ 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;
@@ -684,14 +612,14 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS
static char *sapi_cli_server_read_cookies(TSRMLS_D) /* {{{ */
{
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 TSRMLS_DC) /* {{{ */
{
php_cli_server_client *client = SG(server_context);
if (client->request.content) {
@@ -707,7 +635,7 @@ static int sapi_cli_server_read_post(char *buf, uint count_bytes TSRMLS_DC) /* {
static void sapi_cli_server_register_variable(zval *track_vars_array, const char *key, const char *val TSRMLS_DC) /* {{{ */
{
char *new_val = (char *)val;
- uint new_val_len;
+ size_t new_val_len;
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);
}
@@ -715,11 +643,11 @@ static void sapi_cli_server_register_variable(zval *track_vars_array, const char
static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, 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 {
@@ -861,7 +789,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);
@@ -874,7 +802,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);
@@ -902,7 +830,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
@@ -1053,7 +981,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) {
@@ -1146,7 +1074,7 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
return 0;
} /* }}} */
-static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sender, int fd, size_t *nbytes_read) /* {{{ */
+static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sender, int fd, size_t *nbytes_read TSRMLS_DC) /* {{{ */
{
ssize_t _nbytes_read;
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(131072);
@@ -1154,7 +1082,6 @@ static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sen
_nbytes_read = read(fd, chunk->data.heap.p, chunk->data.heap.len);
if (_nbytes_read < 0) {
char *errstr = get_last_error();
- TSRMLS_FETCH();
php_cli_server_logf("%s" TSRMLS_CC, errstr);
pefree(errstr, 1);
php_cli_server_chunk_dtor(chunk);
@@ -1280,9 +1207,9 @@ static void php_cli_server_logf(const char *format TSRMLS_DC, ...) /* {{{ */
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 int php_network_listen_socket(const char *host, int *port, int socktype, int *af, socklen_t *socklen, zend_string **errstr TSRMLS_DC) /* {{{ */
{
- int retval = SOCK_ERR;
+ php_socket_t retval = SOCK_ERR;
int err = 0;
struct sockaddr *sa = NULL, **p, **sal;
@@ -1395,7 +1322,7 @@ out:
closesocket(retval);
}
if (errstr) {
- *errstr = php_socket_strerror(err, NULL, 0);
+ *errstr = php_socket_error_str(err);
}
return SOCK_ERR;
}
@@ -1415,7 +1342,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;
@@ -1450,7 +1377,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;
@@ -1487,7 +1414,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) {
@@ -1496,7 +1423,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;
}
@@ -1686,13 +1613,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) {
@@ -1841,7 +1767,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;
@@ -1850,8 +1776,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;
}
} /* }}} */
@@ -1859,19 +1785,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 TSRMLS_DC) /* {{{ */
{
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 TSRMLS_CC);
+ 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;
@@ -1911,8 +1837,7 @@ static void php_cli_server_close_connection(php_cli_server *server, php_cli_serv
static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server_client *client, int status TSRMLS_DC) /* {{{ */
{
- 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();
@@ -1921,7 +1846,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 TSRMLS_CC);
{
static const char prologue_template[] = "<!doctype html><html><head><title>%d %s</title>";
@@ -1929,7 +1854,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);
}
@@ -1949,11 +1874,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);
}
@@ -1970,20 +1895,20 @@ 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);
@@ -1994,14 +1919,14 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
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;
} /* }}} */
@@ -2049,13 +1974,13 @@ 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);
return FAILURE;
@@ -2068,12 +1993,12 @@ 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);
+ smart_str_free(&buffer);
php_cli_server_log_response(client, 500, NULL TSRMLS_CC);
return FAILURE;
}
@@ -2086,10 +2011,10 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv
/* }}} */
static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
- char **auth;
+ 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 TSRMLS_CC);
}
SG(sapi_headers).http_response_code = 200;
if (FAILURE == php_request_startup(TSRMLS_C)) {
@@ -2131,10 +2056,10 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
zfd.opened_path = NULL;
zend_try {
- zval *retval = NULL;
+ zval retval;
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);
+ if (Z_TYPE(retval) != IS_UNDEF) {
+ decline = Z_TYPE(retval) == IS_FALSE;
zval_ptr_dtor(&retval);
}
} else {
@@ -2209,9 +2134,28 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
}
/* }}} */
+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 TSRMLS_DC) /* {{{ */
{
zend_hash_destroy(&server->clients);
+ zend_hash_destroy(&server->extension_mime_types);
if (server->server_sock >= 0) {
closesocket(server->server_sock);
}
@@ -2226,19 +2170,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) /* {{{ */
{
int retval = SUCCESS;
char *host = NULL;
- char *errstr = NULL;
+ zend_string *errstr = NULL;
char *_document_root = NULL;
char *_router = NULL;
int err = 0;
@@ -2285,8 +2231,10 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr TSRMLS_CC);
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)" TSRMLS_CC, host, port, errstr ? errstr->val : "?");
+ if (errstr) {
+ zend_string_release(errstr);
+ }
retval = FAILURE;
goto out;
}
@@ -2302,7 +2250,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);
@@ -2329,6 +2277,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) {
@@ -2374,7 +2327,7 @@ static int php_cli_server_send_event(php_cli_server *server, php_cli_server_clie
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)) {
+ if (php_cli_server_content_sender_pull(&client->content_sender, client->file_fd, &nbytes_read TSRMLS_CC)) {
php_cli_server_close_connection(server, client TSRMLS_CC);
return FAILURE;
}
@@ -2408,7 +2361,7 @@ typedef struct php_cli_server_do_event_for_each_fd_callback_params {
int(*whandler)(php_cli_server*, php_cli_server_client* TSRMLS_DC);
} 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
@@ -2446,16 +2399,16 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, int fd, i
#ifdef DEBUG
php_cli_server_logf("%s Accepted" TSRMLS_CC, client->addr_str);
#endif
- zend_hash_index_update(&server->clients, client_sock, &client, sizeof(client), NULL);
+ zend_hash_index_update_ptr(&server->clients, client_sock, client);
php_cli_server_recv_event_read_request(server, client TSRMLS_CC);
} 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 TSRMLS_CC);
}
if (event & POLLOUT) {
- params->whandler(server, *client TSRMLS_CC);
+ params->whandler(server, client TSRMLS_CC);
}
}
}
@@ -2534,9 +2487,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;
}