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:
Declare a buffer of a sufficient size, perhaps 4096 bytes or more.
Read data into this buffer using
read
and yourconnfd
until:You have received 4095 bytes (in which case your server should respond with error 413)
You have encountered the characters
"\r\n\r\n"
(this indicates a blank line)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()
orpoll()
.
Once you have received the HTTP request into your buffer, parse it:
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.
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.
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 thefilename.html
file. Just like the request, a response is separated from data using a blank line.