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.c71
1 files changed, 55 insertions, 16 deletions
diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
index 4c1d443d61..a9e08f11bc 100644
--- a/sapi/cli/php_cli_server.c
+++ b/sapi/cli/php_cli_server.c
@@ -96,6 +96,7 @@
#include "ext/standard/html.h"
#include "ext/standard/url.h" /* for php_raw_url_decode() */
#include "ext/standard/php_string.h" /* for php_dirname() */
+#include "ext/date/php_date.h" /* for php_format_date() */
#include "php_network.h"
#include "php_http_parser.h"
@@ -344,15 +345,24 @@ 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 (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_appendl_ex(buffer, "\r\n", 2, persistent);
- }
+ char *val;
+ struct timeval tv = {0};
+
+ 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_appendl_ex(buffer, "\r\n", 2, persistent);
+ }
+
+ if (!gettimeofday(&tv, NULL)) {
+ zend_string *dt = php_format_date("r", 1, tv.tv_sec, 1);
+ smart_str_appendl_ex(buffer, "Date: ", 6, persistent);
+ smart_str_appends_ex(buffer, dt->val, persistent);
+ smart_str_appendl_ex(buffer, "\r\n", 2, persistent);
+ zend_string_release(dt);
}
+
smart_str_appendl_ex(buffer, "Connection: close\r\n", sizeof("Connection: close\r\n") - 1, persistent);
} /* }}} */
@@ -477,7 +487,7 @@ const zend_function_entry server_additional_functions[] = {
PHP_FE(apache_request_headers, arginfo_no_args)
PHP_FE(apache_response_headers, arginfo_no_args)
PHP_FALIAS(getallheaders, apache_request_headers, arginfo_no_args)
- {NULL, NULL, NULL}
+ PHP_FE_END
};
static int sapi_cli_server_startup(sapi_module_struct *sapi_module) /* {{{ */
@@ -684,7 +694,7 @@ static void sapi_cli_server_register_variables(zval *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) /* {{{ */
+static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ */
{
char buf[52];
@@ -997,7 +1007,11 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
if (nbytes_sent < 0) {
*nbytes_sent_total = _nbytes_sent_total;
return php_socket_errno();
+#ifdef PHP_WIN32
} else if (nbytes_sent == chunk->data.heap.len) {
+#else
+ } else if (nbytes_sent == (ssize_t)chunk->data.heap.len) {
+#endif
php_cli_server_chunk_dtor(chunk);
pefree(chunk, 1);
sender->buffer.first = next;
@@ -1020,7 +1034,11 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
if (nbytes_sent < 0) {
*nbytes_sent_total = _nbytes_sent_total;
return php_socket_errno();
+#ifdef PHP_WIN32
} else if (nbytes_sent == chunk->data.immortal.len) {
+#else
+ } else if (nbytes_sent == (ssize_t)chunk->data.immortal.len) {
+#endif
php_cli_server_chunk_dtor(chunk);
pefree(chunk, 1);
sender->buffer.first = next;
@@ -1171,7 +1189,7 @@ static void php_cli_server_logf(const char *format, ...) /* {{{ */
}
if (sapi_module.log_message) {
- sapi_module.log_message(buf);
+ sapi_module.log_message(buf, -1);
}
efree(buf);
@@ -1387,7 +1405,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
*p = '\0';
q = p;
while (q > buf) {
- if (!zend_stat(buf, &sb)) {
+ if (!php_sys_stat(buf, &sb)) {
if (sb.st_mode & S_IFDIR) {
const char **file = index_files;
if (q[-1] != DEFAULT_SLASH) {
@@ -1396,7 +1414,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 (!zend_stat(buf, &sb) && (sb.st_mode & S_IFREG)) {
+ if (!php_sys_stat(buf, &sb) && (sb.st_mode & S_IFREG)) {
q += l;
break;
}
@@ -1750,7 +1768,7 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
}
client->parser.data = client;
nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, nbytes_read);
- if (nbytes_consumed != nbytes_read) {
+ if (nbytes_consumed != (size_t)nbytes_read) {
if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
*errstr = estrdup("Unsupported SSL request");
} else {
@@ -1907,7 +1925,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, ZSTR_VAL(escaped_request_uri));
+ snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string);
chunk->data.heap.len = strlen(chunk->data.heap.p);
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
@@ -2534,6 +2552,10 @@ int do_cli_server(int argc, char **argv) /* {{{ */
const char *server_bind_address = NULL;
extern const opt_struct OPTIONS[];
const char *document_root = NULL;
+#ifdef PHP_WIN32
+ char document_root_tmp[MAXPATHLEN];
+ size_t k;
+#endif
const char *router = NULL;
char document_root_buf[MAXPATHLEN];
@@ -2543,7 +2565,23 @@ int do_cli_server(int argc, char **argv) /* {{{ */
server_bind_address = php_optarg;
break;
case 't':
+#ifndef PHP_WIN32
document_root = php_optarg;
+#else
+ k = strlen(php_optarg);
+ if (k + 1 > MAXPATHLEN) {
+ fprintf(stderr, "Document root path is too long.\n");
+ return 1;
+ }
+ memmove(document_root_tmp, php_optarg, k + 1);
+ /* Clean out any trailing garbage that might have been passed
+ from a batch script. */
+ do {
+ document_root_tmp[k] = '\0';
+ k--;
+ } while ('"' == document_root_tmp[k] || ' ' == document_root_tmp[k]);
+ document_root = document_root_tmp;
+#endif
break;
}
}
@@ -2551,7 +2589,7 @@ int do_cli_server(int argc, char **argv) /* {{{ */
if (document_root) {
zend_stat_t sb;
- if (zend_stat(document_root, &sb)) {
+ if (php_sys_stat(document_root, &sb)) {
fprintf(stderr, "Directory %s does not exist.\n", document_root);
return 1;
}
@@ -2598,6 +2636,7 @@ int do_cli_server(int argc, char **argv) /* {{{ */
#if defined(HAVE_SIGNAL_H) && defined(SIGINT)
signal(SIGINT, php_cli_server_sigint_handler);
+ zend_signal_init();
#endif
php_cli_server_do_event_loop(&server);
php_cli_server_dtor(&server);