aboutsummaryrefslogtreecommitdiffstats
path: root/main/mini_httpd/fix-cgi.patch
blob: 5b47bcdc8c954f6879d68f875410ce3f1db1f462 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
The headers buffer contains also potentially binary data the CGI program is
sending, and it needs to be sent out later. Use add_data() to cache data so
the cgi output does not get corrupted on first zero byte. add_data() still
always terminates the buffer with zero, so strstr() can used safely.

diff -ru mini_httpd-1.23.orig/mini_httpd.c mini_httpd-1.23/mini_httpd.c
--- mini_httpd-1.23.orig/mini_httpd.c
+++ mini_httpd-1.25/mini_httpd.c
@@ -282,6 +282,7 @@
 static int my_sendfile( int fd, int s, off_t offset, size_t nbytes );
 #endif /* HAVE_SENDFILE */
 static void add_str( char** bufP, size_t* bufsizeP, size_t* buflenP, char* str );
+static void add_data( char** bufP, size_t* bufsizeP, size_t* buflenP, char* str, size_t len );
 static void make_log_entry( void );
 static void check_referrer( void );
 static int really_check_referrer( void );
@@ -1997,7 +1998,7 @@
 	add_str( &headers, &headers_size, &headers_len, (char*) 0 );
 	for (;;)
 	    {
-	    r = read( rfd, buf, sizeof(buf) - 1 );
+	    r = read( rfd, buf, sizeof(buf) );
 	    if ( r < 0 && ( errno == EINTR || errno == EAGAIN ) )
 		{
 		sleep( 1 );
@@ -2008,8 +2009,7 @@
 		br = &(headers[headers_len]);
 		break;
 		}
-	    buf[r] = '\0';
-	    add_str( &headers, &headers_size, &headers_len, buf );
+	    add_data( &headers, &headers_size, &headers_len, buf, r );
 	    if ( ( br = strstr( headers, "\015\012\015\012" ) ) != (char*) 0 ||
 		 ( br = strstr( headers, "\012\012" ) ) != (char*) 0 )
 		break;
@@ -2791,7 +2791,13 @@
 	len = 0;
     else
 	len = strlen( str );
+    add_data( bufP, bufsizeP, buflenP, str, len );
+    }
 
+
+static void
+add_data( char** bufP, size_t* bufsizeP, size_t* buflenP, char* str, size_t len )
+    {
     if ( *bufsizeP == 0 )
 	{
 	*bufsizeP = len + 500;