SheikhBahman / HTTP-Client

Network programming

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTTP-Client

Network programming

This is my course homework for CS-498 Cloud Networkinh at UIUC.

In this task, I implemented a simple HTTP client. The client should be able to GET correctly from standard web servers.

Author

Bahman Sheikh

Programming Language

  • C (client.c)
  • Socket programming

HTTP communication Basics:

  • The client sends an HTTP get request to the server.
  • The server replies with a response based on the request of the client (e.g. acknowledge the validity of the request with an OK response OR reply with a file not found error response etc.).
  • Based on the response from the server, the client takes further action (e.g. store the message content in a file OR redirect to a different address with a new GET request etc.)

Details on HTTP GET Request

Here's the very simple HTTP GET request generated by wget: GET /test.txt HTTP/1.0 User-Agent: Wget/1.15 (linux-gnu) Accept: / Host: localhost:3490 Connection: Keep-Alive

  • GET /test.txt instructs the server to return the file called test.txt in the server's top-level web directory.
  • User-Agent identifies the type of client.
  • Accept specifies what types of files are desired – the client could say “I only want audio”, or “I want text, and I prefer html text”, etc. In this case it is saying “anything is fine”.
  • Host is the URL that the client was originally told to get from – exactly what the user typed.

Details of an HTTP response:

Example: HTTP/1.1 200 OK Date: Sun, 10 Oct 2010 23:26:07 GMT Server: Apache/2.2.8 (Ubuntu) mod_ssl/2.2.8 OpenSSL/0.9.8g Last-Modified: Sun, 26 Sep 2010 22:04:35 GMT ETag: "45b6-834-49130cc1182c0" Accept-Ranges: bytes Content-Length: 13 Connection: close Content-Type: text/html Message is Hello world! Per the HTTP standard, again an empty line (two CRLF, “\r\n\r\n”) marks the end of the header, and the start of message body. Some of the common response headers are:

  • “HTTP/1.0 200 OK” for correctly returning the requested document.
  • “HTTP/1.0 404 Not Found” when the requested file does not exist.
  • “HTTP/1.0 301 Moved Permanently” when the server was moved to a different address.

Client requirements:

I wrote my code using C. Note that C++11 is not supported by our autograder. The code should compile to generate an executable named http_client, with the usage specified as follows:

./http_client http://hostname[:port]/path_to_file

For example: ./http_client http://illinois.edu/index.html ./http_client http://localhost:5678/somedir/somefile.html

If :port is not specified, default to port 80 – the standard port for HTTP.

The client should send an HTTP GET request based on the first argument it receives. The client should then write the message body of the response received from the server to a file named output.

Non-existent Files:

If the server returns a 404 error for a non-existent file, the program should only write “FILENOTFOUND” to the output file.

Invalid arguments:

The program should be able to handle any invalid arguments as follows:

  • If the protocol specified is not http, the program should write “INVALIDPROTOCOL” to the output file. For example, the following command should be handled as an invalid protocol argument

./http_client htpt://localhost/file

  • If the client is unable to connect to the specified host, the program should write “NOCONNECTION” to the output file.

Testing the client:

You can test the correctness of the client by running a local http server using the following python command line script on a separate terminal window in the relevant directory:

python3 -m http.server 8000 where 8000 is the port number that the server will run on.

For the client program, the hostname for the server will be localhost:8000 and the executable will be run as: ./http_client http://localhost:8000/example_file

About

Network programming


Languages

Language:C 93.0%Language:Makefile 7.0%