Decoding HTTP chunks

Does anybody have simple C++ code to decode http chunks?
I have my own http class, so I download web pages from the Internet and when I set: Accept-Encoding: chunked, the server returns data in chunks, but I have some troubles to decode it. Very offen the size of the chunk is wrong (it too big or too small) and I can't find next chunk size. May be I does something wrong. I would like to see some examples.

Thank you.
Who is Participating?
ShahankitConnect With a Mentor Commented:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
   char *endptr;
   int i;
   double contentlength;
   char buff[10000];
   char a,b;
   const char *len1 = getenv("CONTENT_LENGTH");
   contentlength=strtol(len1, &endptr, 10);
   fread(buff, contentlength, 1, stdin);
   printf("Content-type: text/html\n\n%s",buff);

you need a brief rundown of how URL encoding works. All of the variables provided in the form (named Var1, name, and address) are set equal to the data they will return, each variable separated by an & (ampersand). All spaces, as I have pointed out before, have been removed and replaced with +'s (plus symbols). Any extraneous ASCII data is converted to hex, as is the ' (apostrophe) in "Calypso's island". Here is a small chunk of code that will decode all of the hex and put the URL string, with the hex and spaces decoded into a variable called buff2[]. The string with the original URL encoded is called buff[]. Please take note of the comment "/* Prevent user from altering URL delimiter sequence */". This means that if a user types an & (ampersand) or an = (equals sign) into your program, it won't mess up the natural delimiting structure of the URL encoding scheme. This also means, on the downside, that you'll have to pass each separate variable string through another loop similar to this one to change the &'s and ='s back into their respective characters.

for (x = 0, y = 0; x < strlen(buff); x++, y++)
      switch (buff[x])
       /* Convert all + chars to space chars */
       case '+':
       buff2[y] = ' ';

       /* Convert all %xy hex codes into ASCII chars */
       case '%':

       /* Copy the two bytes following the % */
       strncpy(hexstr, &buff[x + 1], 2);

       /* Skip over the hex */
       x = x + 2;

       /* Convert the hex to ASCII */
         /* Prevent user from altering URL delimiter sequence */
         if( ((strcmp(hexstr,"26")==0)) || ((strcmp(hexstr,"3D")==0)) )

       buff2[y] = (char)strtol(hexstr, NULL, 16);

       /* Make an exact copy of anything else */
       buff2[y] = buff[x];
Just in case you don't know, the chunk size is provided in hexadecimal.

However, if you can control the HTTP request, the easiest is to use HTTP/1.0 instead of HTTP/1.1 so that you won't get a chunked response.

Sample request:

GET / HTTP/1.0

Good luck.
Does anybody have the same equivalent code for VB6? I m trying to download file thru HTTP from a web server whose response is chunked.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.