HTTP version 1.1 is defined in RFC 2616 https://tools.ietf.org/html/rfc2616
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:
The "Host:" line: = "Host: "
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.
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
Response =
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.
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:
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: