- return 0;
- }
- else
- { /* not chunked ; inside body */
- /* i += 2 seems not to work with GCC -O2 ..
- so i+2 is used instead .. */
- if (len >= (i+2)+ content_len)
- return (i+2)+ content_len;
+ return 0;
+}
+
+static int cs_complete_http(const char *buf, int len, int head_only)
+{
+ /* deal with HTTP request/response */
+ int i, content_len = 0, chunked = 0;
+
+ /* need at least one line followed by \n or \r .. */
+ for (i = 0; ; i++)
+ if (i == len)
+ return 0; /* incomplete */
+ else if (buf[i] == '\n' || buf[i] == '\r')
+ break;
+
+ /* check to see if it's a response with content */
+ if (!head_only && !memcmp(buf, "HTTP/", 5))
+ {
+ int j;
+ for (j = 5; j < i; j++)
+ if (buf[j] == ' ')
+ {
+ ++j;
+ if (buf[j] == '1') /* 1XX */
+ ;
+ else if (!memcmp(buf + j, "204", 3))
+ ;
+ else if (!memcmp(buf + j, "304", 3))
+ ;
+ else
+ content_len = -1;
+ break;
+ }
+ }
+#if 0
+ printf("len = %d\n", len);
+ fwrite (buf, 1, len, stdout);
+ printf("----------\n");
+#endif
+ for (i = 2; i <= len-2; )
+ {
+ if (i > 8192)
+ {
+ return i; /* do not allow more than 8K HTTP header */
+ }
+ if (skip_crlf(buf, len, &i))
+ {
+ if (skip_crlf(buf, len, &i))
+ {
+ /* inside content */
+ if (chunked)
+ return cs_read_chunk(buf, i, len);
+ else
+ { /* not chunked ; inside body */
+ if (content_len == -1)
+ return 0; /* no content length */
+ else if (len >= i + content_len)
+ {
+ return i + content_len;