dschu012,
If you're using a very light-weight sample as your starting point, then you're going to have to do some work to parse the headers. Since you're only interested in one of the headers, you can cheat by reading the headers one line at a time and processing them. If you find the redirect, you're done, if you don't you have the content.
Pseudo code:
-
looking = inHeaders = true;
-
while (looking && inHeaders)
-
read line
-
if (eof)
-
inHeaders = false
-
else if (line = "")
-
inHeaders = false
-
else if (strncmp(line, "Redirect:", 9))
-
; // noOp - not found
-
else
-
looking = false // found the header we want
-
end while
-
-
if(!looking)
-
process redirect
-
else
-
process result
-
If you read the specification, then you have a good idea of how messy parsing the headers can be.
Rather than re-inventing the wheel to parse headers, it might be worth some time to go find a little better example to start with.
If you're not stuck with C++, there is a really good Perl module for accessing web servers.
On the other hand, if you're stuck with C++, I'd say use a combination of Lex and Yacc to really simplify the work.
Another good option would be to read the entire mess into a single buffer and parse it using regex. I'm pretty sure that it'll be fairly easy to write regular expressions to parse the headers. Start by dividing the headers from the content at the first occurance of "\r\n\r\n". Then tear the headers off of the header block one at a time using one regex, repeatedly.
Failing that, I'd write a simple state machine/recognizer for general headers. The problem is that by the time you're done implementing all the quoting forms and comments, you're going to have a rather complex bit of software.
See
section 2 of the document you sent...