crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

httpcomplete.c (1945B)


      1 #include "crossroads.h"
      2 
      3 static unsigned char const *skipcr (unsigned char const *buf) {
      4     if (*buf == '\r' && *(buf + 1) == '\n')
      5 	buf += 2;
      6     else if (*buf == '\n')
      7 	buf++;
      8     return (buf);
      9 }
     10 
     11 int http_complete (unsigned char const *buf, int buflen) {
     12     char *val;
     13     unsigned char const *body;
     14     int len, chunked, chunk, bodylen;
     15 
     16     msg ("Verifying HTTP buffer (%d bytes) '%s'", buflen, buf);
     17     if ( (val = http_headerval (buf, "content-length")) ) {
     18 	len = atoi (val);
     19 	free (val);
     20 	msg ("Expecting content-length %d", len);
     21 	if (! (body = http_beyondheaders (buf, buflen)) ) {
     22 	    msg ("Body not yet received..");
     23 	    return (0);
     24 	}
     25 	bodylen = buflen - (body - buf);
     26 	if (bodylen >= len) {
     27 	    msg ("Content-length %d satisfied (got %d bytes), done",
     28 		 len, bodylen);
     29 	    return (1);
     30 	}
     31 	msg ("Content-length %d not yet received (only %d)", len, bodylen);
     32 	return (0);
     33     }
     34 
     35     if ( (val = http_headerval (buf, "transfer-encoding")) ) {
     36 	msg ("Analyzing transfer-encoding '%s'", val);
     37 	chunked = !strcasecmp (val, "chunked");
     38 	if (!chunked)
     39 	    error ("Cannot handle tranfer-encoding '%s'", val);
     40 	free (val);
     41 	if (! (body = http_beyondheaders (buf, buflen)) )
     42 	    return (0);
     43 	while (1) {
     44 	    msg ("Verifying chunk, now at '%s'", body);
     45 	    if (! sscanf ( (char const *) body, "%x", &chunk)) {
     46 		msg ("No chunk size in '%s'", body);
     47 		msgdumpbuf (buf, buflen);
     48 		return (0);
     49 	    }
     50 	    msg ("Chunk size: %d (0x%x)", chunk, chunk);
     51 	    if (! chunk)
     52 		return (1);
     53 	    while (ishexdigit (*body))
     54 		body++;
     55 	    body = skipcr (body);
     56 	    bodylen = buflen - (body - buf);
     57 	    if (bodylen < chunk) {
     58 		msg ("Incomplete chunk, should have %d bytes "
     59 		     "(only %d) at '%s'",
     60 		     chunk, bodylen, body);
     61 		// For debugging purposes: extra dump of the full buffer
     62 		// msgdumpbuf (buf, buflen);
     63 		return (0);
     64 	    }
     65 	    body += chunk;
     66 	    body = skipcr (body);
     67 	}
     68     }
     69 
     70     return (http_headers_done (buf));
     71 }