HTTP Fields And Meaning

pages Pages: 4word Words: 890

Question :

USQ CSC8415 2018 S2 Assignment 1

Introduction

The assignment submission will have two parts, program code and a word processed report in PDF format. The code goes into the Git repository, and the report will be submitted using the USQ Studydesk assignment submission. Your work will not be assessed until you submit the report via the USQ submission. The submission date of your assignment is deemed to be the date of the USQ assignment submission.

Assignment Topics

Recall that in assignment 0, you learned :-

  • The use of the Git repository.
  • The use of a makefile.
  • C command line arguments.
  • Using pointers in C
  • Calling functions in C
  • Writing functions in C
  • Encapsulation in C.
  • Configuration
  • Logging
  • Error handling

In Assignment 1, you will build on everything you learned in assignment 0. In addition to the above, you will learn :-

  • Accepting TCP connections.
  • Reading from TCP connections.
  • Writing to TCP connections.
  • Parsing HTTP text.
  • Sending HTTP text.
  • Opening and reading from file.
  • Write a TCP server.

About this assignment

CscNetLib

You will use CscNetLib networking library, beginning with the demonstrations in the Samples directory. You need to obtain the latest version of CscNetLib (i.e. version 1.9.0). The version number of the library exists in the file "std.h". Build it and install it, before attempting the assignment.

Network Framework Documentation

The documentation for CscNetLib routines exists in the network framework header files, and you will need to read these carefully in order to complete the work in this assignment.

For example, if you want to know just what a given netCli_???() routine does, and what the return value is, you will find the documentation for the routine immediately before the prototype for that routine in the file "netCli.h" in the place where you installed it.

If you get stuck

If you are stuck at any stage which is likely then seek help on the forums. Hopefully someone has already had the same problems as yourself, and you can read the solution in the forums. If not, then you need to ask.

Develop a bit at a time.

It is good practice, and in this course your marks depend on it. You need to plan your delta (small improvement) so that your increment can be tested. Get the delta working and test that before moving on.

You MUST check your work in with Git

You need to check your code into the Git repository provided for this course (NOT GitHub), and push it to the server regularly. This is good practice normally, and in this course your marks depend on it. There are marks associated with your report, but failure to check in your code regularly will incur the loss of the marks associated with the code. Code with no development history is worth zero marks.
 
Figure 1: The process of developing your code.

Checked into the correct place

You may need to make a new directory in your Git repository called "Ass1", and it will have subdirectories within that directory as needed.

Quality code

The student is expected to produce quality code. The code should be:-

  • Consistently formatted throughout using Horstmann, Allman or K&R indent style , with Allman being preferred.
  • Block commented.
  • Not use many global variables.
  • Use modules (functions).

Testable on Linux

Your marker will inspect, compile and run your code on the Linux operating system as provided for this course. If it does not work on the linux test machine then the marker will say that it does not work.

It builds using make

The marker will go to the directory associated with the question and inspect the code. He or she will type the command "make" in order to compile the program.

No errors or warnings

Errors mean that it does not work, but compiler warnings will cost marks. Compiler warnings are usually an indication of something not being done correctly. If you cannot ascertain the cause of a compiler error or warning, then seek help in the tutorials or the course forums.

Submit the report

Your work will not be assessed until you submit the report via the USQ submission. The submission date of your assignment is deemed to be the date of the USQ assignment submission.

Your own work please

Be warned, the issue of plagiarism is taken seriously, and the penalties are harsh. USQ policies on plagiarism may be found here.

Part 1

(Goes in the report)

HTTP

·  (2 marks) Explain in your own words the purpose of each of the "words" or "fields" of a request line.

·  (1 marks) Explain how the parameters of a CGI GET request are bundled into the request-URI.

·  (1 marks) Explain percent encoding.

·  (2 marks) Explain in your own words the purpose of each of the "words" or "fields" of a response line.

·  (1 marks) What is the purpose of the "host" header of a request, and why it is mandatory.

·  (1 marks) Explain the Content-Type header. What is the difference between "text/html" and "application/html".

Observing HTTP in action

·  (1 mark) Use telnet to download the file "index.html" from http://example.com. Show the command and the output in report.

·  (1 mark) Use telnet to attempt to download the file "wontBeThere.html" from http://bology.com.au. This one times out pretty fast, so you will have to be ready to paste your HTTP request into your telnet conversation. Show the command and the output in report.

netSrvDemo

·  (2 marks) Build and test netSrvDemo. You will need to use two command line windows to do this. In one command line window, invoke netSrvDemo. In the other command line window, invoke the telnet command with the host "localhost" and the port number 9991 as arguments. Type some text, and press enter. Take screenshots of your two command line windows and paste them into your report.

·  (4 marks) Copy the function main() from netSrvDemo.c into your report, and comment every line of code.

Part 2

Your task

You will implement a very basic HTTP 1.0 web server, with error handling, that can deliver files. You can test your server using telnet while you are developing it, but when completed, you should be able to point a standard web browser at it.

You may well be able to re-use nearly all the code from assignment 0.

Pseudocode 

*  Initialise logging.

*  Read configuration.

*  Initialise server object.

*  Loop:-

    * Get a connection

    * Read only the first line (which should be a request line).

    * Split the line into words

    * if there are the wrong number of words.

        * Send appropriate HTTP rejection. (status code 400)

    * else if the request is not "GET"

        * Send appropriate HTTP rejection. (status code 501)

    * else if the file is not a decent absolute path

        * Send appropriate HTTP rejection. (status code 404)

    * else if the file does not exist in the configuration directory

        * Send appropriate HTTP rejection. (status code 404)

    * else

        * Send appropriate HTTP acceptance. (status code 200)

    * close the connection and free resources.

    

Suggestions

Use HTTP/1.0

Keep it simple. This assignment requires only a very basic server. We are using the protocol HTTP/1.0 in your response so that you dont have to provide any headers that give the length of your file. Just close the connection when you have delivered the file. You will still have to end the HTTP message using a blank line.

Reading a line and splitting into words.

Instructions are given on the course home page.

Concatenating the server filesPath and the requested URL

The use of csc_alloc_str3() is suggested, but remember that you have to free() the result after you have used it.

Transferring file data

The use of csc_xferBytes() is suggested.

Implement a bit at a time.

  • Initially just implement HEAD (i.e. HTTP message and no file delivered), and test it with telnet.
  • Later write a function to deliver the file. If you have such files as "notFound.html" and "notImplemented.html" then this can also deliver the explanatory text in response to anomolous requests.

Testing

(Goes in the report)
Use telnet to demonstrate what happens when the request involves:-

  1. The wrong number of words in the request line.
  2. The request is not "GET"
  3. The file requested is not a decent absolute path
  4. The file does not exist in the configuration directory
  5. Successful deliver of the requested file
Show More

Answer :

Part 1

1. HTTP Fields and meaning

The request line is the first line of an HTTP Request, and is something like:

GET /software/htp/cics/index.html HTTP/1.1

The following works constitute a request line

  1.  METHOD: This is the HTTP Method to which a request is being made, example, GET, PUT, POST, DELETE, etc. 
  2. URI: The URI the request is made to. This can be \*(to indicate the general server), an absolute (URI, eg. http://google.com), a relative URI(eg. /software/htp/cics/index.html), in which case the absolute is specifed by the HOST header.
  3. HTTPVERSION: The version of the HTTP protocol, eg. HTTP/1.1 or HTTP/1.0 (specifies the format of the request)

2. GET parameters

The parameters of a get request are appended to the full URI to form the request URL, in the form of key=value for each parameter, separated by an '&'. For example, if we have two parameters, a = 1 and b = 2 for a request to http://google.com, then the full request URL will be:

http://google.com?a=1&b=2

3. Percent encoding

Percent encoding or URI encoding is a method to encode special characters, such as the &, !, /, which have special meaning, in an HTTP request URI. This is also used to encode form data for post request. For each of the reserved charcters of % encoding, we have a fixed encoding. For example, & == %26 and / == %2F. So to encode two parameters a=1&2 and b=3//4 into a GET URI, we would have the encoded URI:

base.com?a=1%26&b=3%2F%2F4

4. The response line, similar to a request line, is the first line of an HTTP response, and looks something like

HTTP/1.1 200 OK

Where the three fields are HTTPVERSION (similar to in the request), the status code, and the reason for the status code, followed by CLRF(to indicate the EOL). The reason is just one string. The http status code can be of the format 1xx, 2xx, 3xx, 4xx, 5xx, where ‘x’ is a digit from 0 - 9. 

Each series of status codes have a special meaning, for example, 1xx is information, and 2xx is success, 3xx is redirection, 4xx is server error, 5xx is client error. 

5. The HOST header of an HTTP request specifies the domain name of the server and the TCP port on which the server is listening. The HOST field is required because it tells the webserver which virtual host the request is addressing, in case of multiple possible hosts (even for the same physical host). For example, if the request is made to the physical host with IP 8.8.8.8 and port 80, it is possible, there are two virtual hosts, google.com and mail.google.com running on it. The webserver needs to know which host to forward the request to. 

6. The Content-type header of HTTP identifies the MIME type of the resource that is being sent. For example, if the response is coming in as an HTML page, a Content-type of text/html is used. If the response is in the JSON format, then application/json is used. This helps in the proper parsing of the request body, for example in client side Javascript, where JSON objects are automatically parsed. 

7. The telnet command is:

telnet example.com 80

GET /index.html HTTP/1.1

Host: example.com

Output:

8. The telnet command is:

telnet bology.com.au

GET /wontBeThere.html HTTP/1.1

Host: bology.com.au

Output:

The telnet command

9. To run the netSrv.c file given, we do the following:

> git clone https://github.com/drbraithw8/CscNetlib.git # Clone the repo

> cd CscNetLib # Change into directory

> git checkout df0d2c0d5c1 # Checkout to the correct commit

> make # Build the library

> cd .. # Go back to the program directory

> # Build the program

> gcc -I.  -o netSrv netSrvDemo.c CscNetlib/libCscNet.a

> # Run it

Command to run netSrv.c fileCommand to run netSrv.c file

10. Here is the entire commented main() function. Each line contains // comments after it. 

int main(int argc, char **argv)

{   int fd0;  // Define a file descriptor

    char line[MaxLineLen+1]; // Create line buffer

// Create netSrv object.

    csc_srv_t *ntp = csc_srv_new();    assert(ntp!=NULL); // Create server object

    int ret = csc_srv_setAddr(ntp, "TCP", "127.0.0.1", 9991, -1); assert(ret); // Set the server address, port and protocol

// For each successful connection.

    while ((fd0 = csc_srv_accept(ntp)) >= 0) // Accept a connection

    {   fprintf(stdout, "Connection from %s\n", csc_srv_acceptAddr(ntp)); // Print out the connection details to stdout

// Convert file descriptor to input and output streams.

int fd1 = dup(fd0);               assert(fd1!=-1); // Duplicate file descriptor

        FILE *tcpIn = fdopen(fd0, "r");   assert(tcpIn!=NULL); // Open for reading on the stream

        FILE *tcpOut = fdopen(fd1, "w");  assert(tcpOut!=NULL); // Open for writing on the TCP stream

// Do input/output.

        csc_fgetline(tcpIn,line,MaxLineLen); // Get a line from the input stream, into line of len maxLineLen

        fprintf(stdout, "Got line: \"%s\"\n", line); // Print the line to stdout

        fprintf(tcpOut, "You said \"%s\"\n", line); // Send the line back to the output TCP stream

fflush(tcpOut); // Flush the output stream (if buffered)

// Close the streams.

        fclose(tcpOut);

        fclose(tcpIn);

    }

     csc_srv_free(ntp); // Close the server and free the port 

    exit(0); // Exit with status 0

}