2

This is the server I am learning from: http://blog.abhijeetr.com/2010/04/very-simple-http-server-writen-in-c.html.

I completely don't understand get requests servers and the like. How can I send a get request? The reason I don't think I know how to do this is that all tutorials point to get requesting from a website while this is from my current directory or at least from what I understand.

This is a school assignment, we are allowed to copy code for this assignment however we must source the author. The point is to learn how web servers work and I'm not understanding what a Get request does and how I can implement one in this specific server so I can study its effects.

Thank you and down below is the specific code I want to hit. I want to test different get requests to see what responses or errors I could get.

if ( (fd=open(path, O_RDONLY))!=-1 )
{
    send(clients[n], "HTTP/1.0 200 OK\n\n", 17, 0);
    while ( (bytes_read=read(fd, data_to_send, BYTES))>0 )
        write (clients[n], data_to_send, bytes_read);
}
else    write(clients[n], "HTTP/1.0 404 Not Found\n", 23); //FILE NOT FOUND
Rekovni
  • 6,319
  • 3
  • 39
  • 62
TheMangaStand
  • 193
  • 3
  • 13
  • 1
    Why should you send *requests* from the server? The client sends the requests, and you reply with responses. – Some programmer dude Sep 20 '16 at 15:36
  • As for creating requests, just have your local browser do it? Or use command-line tools like `curl`? – Some programmer dude Sep 20 '16 at 15:37
  • Like I said I don't know what I'm doing, I don't know the first thing about servers. Client sends request and I reply. Im sure this server has replies already in. What would a curl command look like? http://stackoverflow.com/questions/27422918/send-http-get-request-using-curl-in-c shows one. But again I don't even know if this has a url and if it does what it would be – TheMangaStand Sep 20 '16 at 16:56

1 Answers1

0

Have a look at the HTTP 1.1 RFC #GET to get an idea for what a GET request is. When you use a browser to visit a website (all websites are running on some sort of HTTP server), your browser acts as the client and the website acts as the server. The client initiates the request and the server responds to that request.

The primary type of request (originated by the browser in response to a URL the user is trying to view) is the GET request.

For example, if the user wants to visit the URL http://www.example.com/index.htm, the browser connects to TCP port 80 (by default) of the server www.example.com. Once the connection is made, the browser starts the "conversation" with a standard GET request that might look like this:

GET /index.htm HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; sv-SE; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

Any headers after the first line (the request line) are optional although any browser will always want to include the "Host" header as most websites will not work without it (this allows multiple websites to share a single IP address). In that case the server can't know which website you are trying to visit without specifying the host name here.

The bare minimum GET request to fetch index.htm on that server would simply be:

GET /index.htm HTTP/1.1

NOTE that the end of the request is terminated by a double-CRLF sequence (ASCII codes 13 and 10), one for the most-recent header line, the other to mark the end of the request headers. Once the double CRLF sequence is received by the server, the server responds accordingly with a status line and code (was the document found?). If the document was found, it is returned in the response usually with a HTTP status code of 200, response headers (these are in the same format as the request headers), double CRLF sequence, followed by the body of the requested file. The HTTP standard calls this the "entity". In this case the returned entity would be the contents of index.htm. Using the example above, the server might respond like this:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 66
Last-Modified: Wed, 14 Sep 2016 13:05:21 GMT

<html>
<body>
    Welcome to my example page
</body>
</html>

So that's a simple example of an HTTP GET/response conversation in a nutshell although you can guess that things can get more complicated depending on the request and response headers.

To answer your question about how to manually send a GET request to a running server, I like to use "curl -i " on a linux machine. Curl is acting as the client/broswer allowing you to specify the URL you want to GET (download). For example:

curl -i http://localhost/test.htm

The -i option causes curl to display the entire webserver response (including the HTTP response header) rather than just the entity which in this case are the contents of test.htm. You'll just need to adjust the URL above to match that of your running webserver whether it be on the same machine or somewhere else on the network.

byteptr
  • 1,275
  • 11
  • 15