php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #53615
Patch fopen-http-request revision 2010-12-27 12:30 UTC by hungry dot rahly at gmail dot com

Patch fopen-http-request for Streams related Bug #53615

Patch version 2010-12-27 12:30 UTC

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

Developer: hungry.rahly@gmail.com

Index: ext/standard/http_fopen_wrapper.c
===================================================================
--- ext/standard/http_fopen_wrapper.c	(revision 306731)
+++ ext/standard/http_fopen_wrapper.c	(working copy)
@@ -264,7 +264,7 @@
 				|| (Z_STRLEN_PP(tmpzval) == 3 && memcmp("GET", Z_STRVAL_PP(tmpzval), 3) == 0)
 				|| (Z_STRLEN_PP(tmpzval) == 4 && memcmp("HEAD",Z_STRVAL_PP(tmpzval), 4) == 0)
 			) {
-				scratch_len = strlen(path) + 29 + Z_STRLEN_PP(tmpzval);
+				scratch_len = strlen(path) + 512 + Z_STRLEN_PP(tmpzval);
 				scratch = emalloc(scratch_len);
 				strlcpy(scratch, Z_STRVAL_PP(tmpzval), Z_STRLEN_PP(tmpzval) + 1);
 				strcat(scratch, " ");
@@ -279,7 +279,7 @@
 	}
 
 	if (!scratch) {
-		scratch_len = strlen(path) + 29 + protocol_version_len;
+		scratch_len = strlen(path) + 512 + protocol_version_len;
 		scratch = emalloc(scratch_len);
 		strcpy(scratch, "GET ");
 	}
@@ -316,20 +316,7 @@
 		}
 	}
 
-	/* protocol version we are speaking */
-	if (protocol_version) {
-		strlcat(scratch, " HTTP/", scratch_len);
-		strlcat(scratch, protocol_version, scratch_len);
-		strlcat(scratch, "\r\n", scratch_len);
-		efree(protocol_version);
-		protocol_version = NULL;
-	} else {
-		strlcat(scratch, " HTTP/1.0\r\n", scratch_len);
-	}
 
-	/* send it */
-	php_stream_write(stream, scratch, strlen(scratch));
-
 	if (context && php_stream_context_get_option(context, "http", "header", &tmpzval) == SUCCESS) {
 		tmp = NULL;
 		
@@ -389,6 +376,8 @@
 			}
 
 			user_headers = estrdup(tmp);
+            scratch = erealloc(scratch, scratch_len + strlen(tmp));
+            scratch_len += strlen(tmp);
 
 			/* Make lowercase for easy comparison against 'standard' headers */
 			php_strtolower(tmp, strlen(tmp));
@@ -416,6 +405,51 @@
 		}
 	}
 
+	/* protocol version we are speaking */
+	if (protocol_version) {
+		strlcat(scratch, " HTTP/", scratch_len);
+		strlcat(scratch, protocol_version, scratch_len);
+		strlcat(scratch, "\r\n", scratch_len);
+		efree(protocol_version);
+		protocol_version = NULL;
+	} else {
+		strlcat(scratch, " HTTP/1.0\r\n", scratch_len);
+	}
+	if (((have_header & HTTP_HEADER_HOST) == 0) && resource->host) {
+        if ((use_ssl && resource->port != 443 && resource->port != 0) || 
+            (!use_ssl && resource->port != 80 && resource->port != 0)) {
+            strlcat(scratch, "Host: ", scratch_len);
+            strlcat(scratch, resource->host, scratch_len);
+            char p[7];
+            snprintf(p, 7, ":%i", resource->port);
+            strlcat(scratch, p, scratch_len);
+            strlcat(scratch, "\r\n", scratch_len);
+        } else {
+            strlcat(scratch, "Host: ", scratch_len);
+            strlcat(scratch, resource->host, scratch_len);
+            strlcat(scratch, "\r\n", scratch_len);
+        }
+    }
+    if(user_headers) {
+		if (
+				header_init &&
+				context &&
+				!(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
+				php_stream_context_get_option(context, "http", "content", &tmpzval) == SUCCESS &&
+				Z_TYPE_PP(tmpzval) == IS_STRING && Z_STRLEN_PP(tmpzval) > 0
+		) {
+            char sz[512];
+			snprintf(sz, 512, "Content-Length: %d\r\n", Z_STRLEN_PP(tmpzval));
+            strlcat(scratch, sz, scratch_len);
+			have_header |= HTTP_HEADER_CONTENT_LENGTH;
+		}
+        strlcat(scratch, user_headers, scratch_len);
+    }
+
+	/* send it */
+    strlcat(scratch, "\r\n\r\n", scratch_len);
+	php_stream_write(stream, scratch, strlen(scratch));
+
 	/* auth header if it was specified */
 	if (((have_header & HTTP_HEADER_AUTH) == 0) && resource->user) {
 		/* decode the strings first */
@@ -452,17 +486,17 @@
 	}
 
 	/* Send Host: header so name-based virtual hosts work */
-	if ((have_header & HTTP_HEADER_HOST) == 0) {
-		if ((use_ssl && resource->port != 443 && resource->port != 0) || 
-			(!use_ssl && resource->port != 80 && resource->port != 0)) {
-			if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
-				php_stream_write(stream, scratch, strlen(scratch));
-		} else {
-			if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0) {
-				php_stream_write(stream, scratch, strlen(scratch));
-			}
-		}
-	}
+//	if ((have_header & HTTP_HEADER_HOST) == 0) {
+//		if ((use_ssl && resource->port != 443 && resource->port != 0) || 
+//			(!use_ssl && resource->port != 80 && resource->port != 0)) {
+//			if (snprintf(scratch, scratch_len, "Host: %s:%i\r\n", resource->host, resource->port) > 0)
+//				php_stream_write(stream, scratch, strlen(scratch));
+//		} else {
+//			if (snprintf(scratch, scratch_len, "Host: %s\r\n", resource->host) > 0) {
+//				php_stream_write(stream, scratch, strlen(scratch));
+//			}
+//		}
+//	}
 
 	if (context && 
 	    php_stream_context_get_option(context, "http", "user_agent", &ua_zval) == SUCCESS &&
@@ -499,7 +533,7 @@
 		/* A bit weird, but some servers require that Content-Length be sent prior to Content-Type for POST
 		 * see bug #44603 for details. Since Content-Type maybe part of user's headers we need to do this check first.
 		 */
-		if (
+		/*if (
 				header_init &&
 				context &&
 				!(have_header & HTTP_HEADER_CONTENT_LENGTH) &&
@@ -509,10 +543,10 @@
 			scratch_len = slprintf(scratch, scratch_len, "Content-Length: %d\r\n", Z_STRLEN_PP(tmpzval));
 			php_stream_write(stream, scratch, scratch_len);
 			have_header |= HTTP_HEADER_CONTENT_LENGTH;
-		}
+		}*/
 
-		php_stream_write(stream, user_headers, strlen(user_headers));
-		php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
+		//php_stream_write(stream, user_headers, strlen(user_headers));
+		//php_stream_write(stream, "\r\n", sizeof("\r\n")-1);
 		efree(user_headers);
 	}
 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 21:01:28 2024 UTC