Logo  

CS473 - Computer Networks

The HyperText Transfer Protocol (HTTP):

HTTP version 1.1 is defined in RFC 2616 https://tools.ietf.org/html/rfc2616

  • HTTP is divided into requests (as made by a browser) or responses (as made by a server.)

A HTTP/1.1 request:

  • End of line is CR/LF (not just LF as in Unix.)

  • A long line can be continued on the next if the beginning of the next line is prefixed with a space or tab.

  • A Request-Line = \r\n Method: one of: GET, HEAD, PUT, POST, etc. Request-URI:

    • Absolute path to a resource or * if a request does not require a resource. HTTP-Version: the string "HTTP/1.1"
  • The "Host:" line: = "Host: "

    • This allows a server to virtually host many sites with one server, thus: GET / HTTP/1.1 Host: cs.indstate.edu

    is not the same as: GET / HTTP/1.1 Host: mathcs.indstate.edu

    even though both are on the same server.

  • Additional request header fields, such as User-Agent, Accept, etc.

    • We don't really care about them for now.
  • A final blank line indicating that the request is finalized.

An example request header (sent by the browser to the server): GET / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0 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 DNT: 1 Connection: keep-alive

A HTTP/1.1 response:

Response =

  • zero or more ( | | ) \r\n
  • blank line [ message-body ]

Status-Line = \r\n

Status-Code for example could be one of: 200 = "OK" 301 = "Moved Permanently" 302 = "Found" (probably redirects to another page.) 400 = "Bad Request" 401 = "Unauthorized" 404 = "Not Found" lots more

Example response header: (GET / on CS:) HTTP/1.1 302 Found Date: Mon, 18 Mar 2019 18:57:39 GMT Server: Apache/2.4.37 (Unix) OpenSSL/1.1.1a PHP/7.2.13 X-Powered-By: PHP/7.2.13 Location: http://cs.indstate.edu/info/ Content-Length: 0 Content-Type: text/html; charset=UTF-8

  • Make a client program that uses the CR/NL line endings to talk to the http server.

    • Modify code/Mar04/chat.c: if (ev[i].data.fd == STDIN_FILENO) { r = read(STDIN_FILENO, buf, K); if (r <= 0) {printf("error2\n"); done=1; break;} write(sock, buf, r); } to: if (ev[i].data.fd == STDIN_FILENO) { r = read(STDIN_FILENO, buf, K); if (r <= 0) {printf("error2\n"); done=1; break;} char out[K*2]; for(int i=0,o=0; i < r;) { if (buf[i] == '\n') out[j++] = '\r'; out[j++] = buf[i++]; } write(sock, buf, j); }
  • We need to programatically parse the output of the HTTP server. To do this we will split the output into words and look at the words individually:

The strtok(3) C library function:

include <string.h>

char strtok(char str, const char *delim);

  • Returns the next sub-string portion (token) of the original string or NULL when there are no more tokens to return (the end of the string was reached.)

  • Example, splitting a string into words separated by spaces or tabs:

    char *token = strtok(string, " \t\n"); for ( ; token != NULL; token = strtok(NULL, " \t\n")) { // do something with the "token": ... }

  • Example: