-1

I mading a simple webserver in C++ and i need parse the request headers. How i made that?

Here is my headers...

GET /test?username=2 HTTP/1.1
Host: stream.mysite.com:7777
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: auth=asdfasdfaasdfasd

I need get the page (/test?username=2) and the content of the cookie variable auth (asdfasdfaasdfasd).

Thanks!

GabrielBiga
  • 388
  • 5
  • 19
  • I would just get the information into separate variables to be able to treat later in my program. – GabrielBiga Dec 28 '12 at 03:01
  • 1
    I realize that, but have you tried to do it already or do you just want us to write it? If you've already tried and are having problems, you can ask us about specific problems. However, if you just want us to write the code for you, this is not the right place. – icktoofay Dec 28 '12 at 03:02

2 Answers2

4

A simple example to get you started:

#include <iostream>
#include <string>
#include <sstream>
int main() {
  std::string tk1, tk2, line = "GET /test?username=2 HTTP/1.1";
  std::stringstream ss(line);
  ss >> tk1;
  ss >> tk2;
  if (tk1 == "GET") {
    std::cout << "requested path: " << tk2 << std::endl;
  }
  return 0;
}
perreal
  • 94,503
  • 21
  • 155
  • 181
2

An Http request is defined here:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

Basically what you need to know:

GET <URL> <HTTP-Version><CRLF>
<Set of Headers each line terminated with <CRLF>>
<Empty Line with just <CRLF>>

The bit you want will always be the <URL> just after the GET You will need to search the headers for the string "Cookie:"

Martin York
  • 257,169
  • 86
  • 333
  • 562