Thread: Help with Parsing HTTP response from server

  1. #1
    Registered User
    Join Date
    Nov 2007

    Help with Parsing HTTP response from server

    I have a server set up to respond to and http request with the following:

    HTTP/1.0 200 OK 
    From: NuNn-DaDdY
    User-Agent: ./server
    Server: Local-Host
    Date: 13/1/2009 
    Last-Modified: Fri Feb 13 10:37:07 2009
    Content-Type: text/html
    Content-Length 8238
    How would I set up my program effectively to parse this response so I know the server returned 200 which means ok,
    User-Agent= ./server
    Content-Length = 8238

    Then, once I find the opening <html> tag, I would like to assign the rest of the response to a variable named body...Thanks!

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Well I know this about the http protocol. After the http header, you can find \r\n\r\n, versus just \r\n. This tells you the start of the data. The data is also then terminated with \r\n\r\n.

    I am pretty sure it is standard for the response number to be in the first line of the http header, so you can just parse the first line and throw down a strstr.

    void checkresponse (char *data)
        char *pch = strstr(data, "200 OK");
        if (pch != NULL)
            printf("We found it!\n");
    Last edited by carrotcake1029; 02-19-2009 at 02:51 PM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    I had originall thought about the strstr function, I believe the strstr function returns a pointer to where the current position is with the target value. From here would you recommend that I use a strtok on that to get the value for say Content-Length after searching for 'Content-Length:' with strstr

  4. #4
    Registered User
    Join Date
    Oct 2008
    To do it completely compliant to the RFC, it's a lot of work. You'll have to completely read it first. There are some strange rules, like when a line starts with a whitespace it is part of the previous line, and stuff like that.

    If you don't need it a 100% perfect, you can simply locate the spaces, newlines and colons. While it wouldn't be a good parser, most of the times it would work...

  5. #5
    Registered User
    Join Date
    Nov 2007
    No need to be compliant on this one with the RFC, Just needs to work was on the right track then with the strtok idea are strstr giving me the pointer?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPv6 multicast client fails to receive server response
    By perfect in forum Networking/Device Communication
    Replies: 10
    Last Post: 06-21-2009, 11:30 PM
  2. Tokenize HTTP server response
    By maganese187 in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-09-2009, 07:17 AM
  3. HTTP Response from web browser
    By zort15 in forum C# Programming
    Replies: 0
    Last Post: 06-25-2007, 08:35 AM
  4. HTTP Server?
    By Nebbuchadnezzar in forum Tech Board
    Replies: 9
    Last Post: 07-30-2005, 03:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM