Solved

Decoding HTTP chunks

Posted on 2004-08-17
3
682 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now