Solved

Decoding HTTP chunks

Posted on 2004-08-17
3
698 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 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