Solved

Decoding HTTP chunks

Posted on 2004-08-17
3
677 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now