Parsing HTTP Headers

822

Solution 1

You can retrieve the name/value pairs by searching for newline newline or more specifically \r\n\r\n (after this, the body of the message will start).

Then you can simply split the list by the &, and then split each of those returned strings between the = for name/value pairs.

See the HTTP 1.1 RFC.

Solution 2

You need to keep parsing the stream as headers until you see the blank line. The rest is the POST data.

You need to write a little parser for the post data. You can use C library routines to do something quick and dirty, like index, strtok, and sscanf. If you have room for it in your definition of "small", you could do something more elaborate with a regular expression library, or even with flex and bison.

At least, I think this kind of answers your question.

Share:
822
Stefan
Author by

Stefan

Updated on August 06, 2022

Comments

  • Stefan
    Stefan almost 2 years

    I have a vector< vector< pair<int,int> > > and I want to print all its values I tried doing this with 2 iterators, but failed miserably on the 2nd one:

    vector< vector< pair<int, int> > > list;
    vector< vector< pair<int, int> > >::iterator it1;
    vector< pair<int, int> >::iterator it2;
    
    for( it1=list.begin(); it1<list.end(); ++it1 ){
      for( it2=it1.begin(); it2<it1.end(); ++it2 ){
        printf("%d, %d", *it2.first, *it2.second);
      }
    }
    

    any ideas on how to traverse the second vector as well? Thanks

    • GManNickG
      GManNickG over 12 years
      Please use some typedef's. :)
    • sehe
      sehe over 12 years
      Note that no-one else seems to have spotted the uselessness of printf "%d, %d" in a loop. The numbers will glue together. My answer aimed to fix that :)
  • James Kanze
    James Kanze over 12 years
    You'll have to add the declarations for it1 and it2 to get it to compile, of course. (But "somthing like this" suggests you were just posting pseudo-code anyway, to give the idea.)