Solved

Decoding HTTP chunks

Posted on 2004-08-17
3
688 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:Neoliten
3 Comments
 
LVL 2

Accepted Solution

by:
Shahankit earned 125 total points
ID: 11822130
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
   {
   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] = ' ';
       break;

       /* 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]='%';
            y++;
            strcpy(buff2,hexstr);
            y=y+2;
            break;
            }

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

       /* Make an exact copy of anything else */
       default:
       buff2[y] = buff[x];
       break;
       }
      }
0
 
LVL 4

Expert Comment

by:chipple
ID: 11827714
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
Host: www.experts-exchange.com

Good luck.
0
 

Expert Comment

by:Sairamsuriya
ID: 22595664
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question