Get requested address in socket programming with C
c, http, httpserver, sockets
Solution
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 your `connfd` 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()` or `poll()`.
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 the `filename.html` file. Just like the request, a response is separated from data using a blank line.
Problem
I am using something like this to create a server using C. When I go to `127.0.0.1:5000` from my browser I can see `"Hello Worlds"` as I am sending it as buffer. But I want `127.0.0.1:5000/filename.html` to work. But I don't know how to get `filename` that comes after `127.0.0.1:5000` in C. I am using this to make connection: ``` serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr)); connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL); ```