php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch builtin-web-server-default-favicon.patch.diff for Built-in web server Bug #55094

Patch version 2011-07-20 08:36 UTC

Return to Bug #55094 | Download this patch
Patch Revisions:

Developer: moriyoshi@php.net

Index: sapi/cli/php_cli_server.c
===================================================================
--- sapi/cli/php_cli_server.c	(revision 313464)
+++ sapi/cli/php_cli_server.c	(working copy)
@@ -260,6 +260,92 @@
 	{ NULL, NULL }
 };
 
+static const unsigned char php_cli_server_favicon_ico[] = {
+	 66,  77,  54,   3,   0,   0,   0,   0,   0,   0, 
+	 54,   0,   0,   0,  40,   0,   0,   0,  16,   0, 
+	  0,   0,  16,   0,   0,   0,   1,   0,  24,   0, 
+	  0,   0,   0,   0,   0,   3,   0,   0, 196,  14, 
+	  0,   0, 196,  14,   0,   0,   0,   0,   0,   0, 
+	  0,   0,   0,   0, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	215, 215, 215, 215, 215, 215, 215, 215, 215, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	215, 215, 215, 215, 215, 215, 215, 215, 215, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 215, 215, 
+	215,   0,   0,   0, 215, 215, 215, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 215, 215, 
+	215,   0,   0,   0, 215, 215, 215, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 215, 215, 215,   0, 
+	  0,   0, 215, 215, 215, 215, 215, 215, 192, 128, 
+	128, 192, 128, 128, 215, 215, 215, 192, 128, 128, 
+	192, 128, 128, 215, 215, 215, 215, 215, 215,   0, 
+	  0,   0, 215, 215, 215, 215, 215, 215, 192, 128, 
+	128, 192, 128, 128, 215, 215, 215,   0,   0,   0, 
+	  0,   0,   0,   0,   0,   0, 215, 215, 215, 215, 
+	215, 215,   0,   0,   0, 215, 215, 215, 215, 215, 
+	215,   0,   0,   0, 215, 215, 215,   0,   0,   0, 
+	  0,   0,   0,   0,   0,   0, 215, 215, 215, 215, 
+	215, 215, 215, 215, 215,   0,   0,   0, 215, 215, 
+	215, 215, 215, 215,   0,   0,   0, 215, 215, 215, 
+	  0,   0,   0, 215, 215, 215, 215, 215, 215,   0, 
+	  0,   0, 215, 215, 215,   0,   0,   0, 215, 215, 
+	215, 215, 215, 215,   0,   0,   0, 215, 215, 215, 
+	215, 215, 215,   0,   0,   0, 215, 215, 215, 215, 
+	215, 215,   0,   0,   0, 215, 215, 215,   0,   0, 
+	  0, 215, 215, 215, 215, 215, 215,   0,   0,   0, 
+	215, 215, 215,   0,   0,   0, 215, 215, 215, 215, 
+	215, 215,   0,   0,   0, 215, 215, 215, 215, 215, 
+	215,   0,   0,   0, 215, 215, 215, 215, 215, 215, 
+	  0,   0,   0, 215, 215, 215,   0,   0,   0, 215, 
+	215, 215, 215, 215, 215,   0,   0,   0, 215, 215, 
+	215,   0,   0,   0, 215, 215, 215, 215, 215, 215, 
+	  0,   0,   0, 215, 215, 215, 215, 215, 215,   0, 
+	  0,   0,   0,   0,   0,   0,   0,   0, 215, 215, 
+	215, 215, 215, 215,   0,   0,   0,   0,   0,   0, 
+	  0,   0,   0, 215, 215, 215, 215, 215, 215,   0, 
+	  0,   0,   0,   0,   0,   0,   0,   0, 215, 215, 
+	215, 215, 215, 215, 192, 128, 128, 215, 215, 215, 
+	215, 215, 215, 215, 215, 215, 192, 128, 128, 215, 
+	215, 215,   0,   0,   0, 215, 215, 215, 215, 215, 
+	215, 192, 128, 128, 192, 128, 128, 215, 215, 215, 
+	215, 215, 215, 215, 215, 215, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 215, 215, 215, 
+	  0,   0,   0, 215, 215, 215, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 215, 215, 215, 215, 215, 
+	215, 215, 215, 215, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128, 192, 128, 128, 192, 128, 128, 192, 128, 
+	128, 192, 128, 128, 192, 128, 128, 192, 128, 128, 
+	192, 128, 128, 192, 128, 128, 192, 128, 128, 192, 
+	128, 128
+};
+
 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);
@@ -1626,17 +1712,62 @@
 	return SUCCESS;
 } /* }}} */
 
+static int php_cli_server_begin_send_favicon(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
+{
+	php_cli_server_content_sender_ctor(&client->content_sender);
+	client->content_sender_initialized = 1;
+	client->file_fd = 0;
+
+	{
+		smart_str buffer = { 0 };
+		append_http_status_line(&buffer, client->request.protocol_version, 200, 1);
+		if (!buffer.c) {
+			/* out of memory */
+			return FAILURE;
+		}
+		append_essential_headers(&buffer, client, 1);
+		smart_str_appendl_ex(&buffer, "Content-Type: image/x-icon\r\n", sizeof("Content-Type: image/x-icon\r\n") - 1, 1);
+		smart_str_appends_ex(&buffer, "Content-Length: ", 1);
+		smart_str_append_generic_ex(&buffer, sizeof(php_cli_server_favicon_ico), 1, size_t, _unsigned);
+		smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
+		smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
+		{
+			php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new(buffer.c, buffer.c, buffer.len);
+			if (!chunk) {
+				smart_str_free_ex(&buffer, 1);
+				return FAILURE;
+			}
+			php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
+		}
+		{
+			php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_favicon_ico, sizeof(php_cli_server_favicon_ico));
+			if (!chunk) {
+				return FAILURE;
+			}
+			php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
+		}
+	}
+	php_cli_server_poller_add(&server->poller, POLLOUT, client->sock);
+	return SUCCESS;
+}
+/* }}} */
+
 static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
 {
-	int fd;
+	int fd = -1;
 	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);
+	if (client->request.path_translated) {
+		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);
+		}
+
+		fd = open(client->request.path_translated, O_RDONLY);
+		if (fd < 0 && client->request.path_translated_len >= sizeof("/favicon.ico") - 1 && memcmp(client->request.path_translated + client->request.path_translated_len - sizeof("/favicon.ico") + 1, "/favicon.ico", sizeof("/favicon.ico") - 1) == 0) {
+			return php_cli_server_begin_send_favicon(server, client TSRMLS_CC);
+		}
 	}
-
-	fd = client->request.path_translated ? open(client->request.path_translated, O_RDONLY): -1;
 	if (fd < 0) {
 		char *errstr = get_last_error();
 		if (errstr) {
@@ -1647,7 +1778,6 @@
 		}
 		return php_cli_server_send_error_page(server, client, 404 TSRMLS_CC);
 	}
-
 	php_cli_server_content_sender_ctor(&client->content_sender);
 	client->content_sender_initialized = 1;
 	client->file_fd = fd;
@@ -1805,9 +1935,9 @@
 	int err = 0;
 	int port = 3000;
 	php_socket_t server_sock = SOCK_ERR;
+	char *p = NULL;
 
 	if (addr[0] == '[') {
-		char *p;
 		host = pestrdup(addr + 1, 1);
 		if (!host) {
 			return FAILURE;
@@ -1817,27 +1947,33 @@
 			*p++ = '\0';
 			if (*p == ':') {
 				port = strtol(p + 1, &p, 10);
+				if (port < 0) {
+					p = NULL;
+				}
 			} else if (*p != '\0') {
 				p = NULL;
 			}
 		}
-		if (!p) {
-			fprintf(stderr, "Invalid IPv6 address: %s\n", host);
-			retval = FAILURE;
-			goto out;
-		}
 	} else {
-		char *p;
 		host = pestrdup(addr, 1);
 		if (!host) {
 			return FAILURE;
 		}
-		p = strrchr(host, ':');
+		p = strchr(host, ':');
 		if (p) {
 			*p++ = '\0';
 			port = strtol(p, &p, 10);
+				if (port < 0) {
+					p = NULL;
+				}
 		}
 	}
+	fprintf(stderr, "%d\n", port);
+	if (!p) {
+		fprintf(stderr, "Invalid address: %s\n", addr);
+		retval = FAILURE;
+		goto out;
+	}
 
 	server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr TSRMLS_CC);
 	if (server_sock == SOCK_ERR) {
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 10:01:30 2024 UTC