Re: [geocoder.us #21] Attempting to fix bug in http_request_read

"Casey T. Deccio" <[email protected]>
Newsgroups gmane.network.nocat
Organization Sandia National Laboratories
Message-ID <[email protected]>
Sorry about the lack of context (attached is the new diff).  Basically,
the idea is that the old code is not getting the whole request--unless
the request is all contained in one packet.  The condition in the for
loop (n == BUFSIZ) indicates that the loop is terminated when something
less than the requested amount was read.  This will only read a single
packet.  In our trials with non-ie browsers (e.g., firefox, mozilla, and
konqueror) the request (including the post) was broken up into at least
two packets, so the rest of the request would not be read.

In the updated version, the first loop will run until it reaches the
end-of-header "\r\n\r\n" (or a read error occurs).  Then the headers are
parsed (so they don't need to be parsed again).  With the headers
parsed, the content-length header value can be obtained, and the number
of remaining bytes to read can be calculated.  thus, the entire request
will be read, regardless of how many packets it takes to get there.

Casey

P.S.  I've also found some memory leaks that I've somewhat plugged.
However, I had to change existing function calls in a number of places
to make it work, so it's a bit of a hack.  How would I go about
submitting these patches (for what their worth :) ?

On Thu, 2005-02-24 at 13:34 -0700, Schuyler Erle via RT wrote:
> Hi, I'm not sure I see where the problem is. Could you regenerate this
> patch using "diff -Naur" and resend, ideally by replying to this
> email?
> Thanks.
> 
> SDE
> 
> 
>

_______________________________________________
NoCat mailing list
[email protected]
http://lists.nocat.net/mailman/listinfo/nocat
diff.txt (text/plain, 1.6 KB)
--- NoCatSplash-0.92/src/http.c	2004-11-21 17:29:30.000000000 -0800
+++ NoCatSplash/src/http.c	2005-02-23 10:01:21.041211544 -0800
@@ -191,10 +191,34 @@
     gchar *buf = g_new( gchar, BUFSIZ + 1 );
     GIOError r;
     guint n, t;
+    gchar *c_len_hdr;
+    guint c_len;
+    guint tot_req_size;
+    gchar *hdr_end = NULL;
 
     // g_message("entering http_request_read");
-    for (t = 0, n = BUFSIZ; n == BUFSIZ && 
-	    h->buffer->len < MAX_REQUEST_SIZE; t += n ) {
+    for (t = 0, n = BUFSIZ; h->buffer->len < MAX_REQUEST_SIZE &&
+	    (hdr_end = strstr(h->buffer->str, "\r\n\r\n")) == NULL; t += n ) {
+	// g_message("entering read loop");
+	r = g_io_channel_read( h->sock, buf, BUFSIZ, &n );
+	// g_message("read loop: read %d bytes of %d (%d)", n, BUFSIZ, r);
+	if (r != G_IO_ERROR_NONE) {
+	    g_warning( "read_http_request failure: %m" );
+	    g_free(buf);
+	    return 0;
+	}
+	buf[n] = '\0';
+	g_string_append(h->buffer, buf);
+    }
+    http_parse_header( h, h->buffer->str );
+    c_len_hdr = HEADER("Content-length");
+    if (c_len_hdr == NULL) {
+    	c_len = 0;
+    } else {
+    	c_len = atoi( c_len_hdr );
+    }
+    tot_req_size = hdr_end - h->buffer->str + 4 + c_len;
+    for (; t < tot_req_size; t += n ) {
 	// g_message("entering read loop");
 	r = g_io_channel_read( h->sock, buf, BUFSIZ, &n );
 	// g_message("read loop: read %d bytes of %d (%d)", n, BUFSIZ, r);
@@ -217,7 +241,6 @@
 
     if (header_end != NULL) {
 	// g_warning( "inside http_request_ok: header_end found" );
-	http_parse_header( h, h->buffer->str );
 
 	c_len_hdr = HEADER("Content-length");
 	if (c_len_hdr == NULL) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.