Get requested address in socket programming with C

The browser will be sending your server an HTTP request that contains the URL it is after. The request could look like this:

GET /filename.html HTTP/1.1
Host: 127.0.0.1:5000

Your C program must read this request from the socket and parse it to find the URL. Note that the request will likely contain more information than the above, but it should always end with a blank line (so you know where to stop parsing). Lines in HTTP requests should end with both a carriage return and line feed ("\r\n").

You receive data through the same socket that you use to send data. The steps to read an HTTP request could be something like this:

  1. Declare a buffer of a sufficient size, perhaps 4096 bytes or more.

  2. Read data into this buffer using read and your connfd until:

    1. You have received 4095 bytes (in which case your server should respond with error 413)

    2. You have encountered the characters "\r\n\r\n" (this indicates a blank line)

    3. Some amount of time has passed and neither of the above has occurred. In order to implement a timeout you will need to use select() or poll().

  3. Once you have received the HTTP request into your buffer, parse it:

    1. The first line is the request line which dictates the method of the request, the URI, and the protocol version number. A possible way to parse this line is to split it by space.

    2. Subsequent lines represent HTTP header fields, and can generally be parsed as Key: Value\r\n. These header fields contain cookies, information about the client making the request, etc.

  4. You need to form your HTTP response as well. A response for when the URI specifies a valid resource (such as filename.html) might be:

    HTTP/1.1 200 OK
    Date: Thu, 25 Jul 2013 03:55:00 GMT
    Server: sadaf2605-server/1.0
    Content-Type: text/html
    Content-Length: 40595
    
    < contents of filename.html follows here >
    

    In the above, Content-Length refers to the number of bytes in the filename.html file. Just like the request, a response is separated from data using a blank line.