Created Saturday 09 November 2013
Basics
- HTTP = Hypertext Transfer Protocol.
- It's the network protocol for the Internet.
- A browser is an HTTP client
- A web server is an HTTP server
- Usually HTTP servers listen to port 80 but any port can be used.
- Usually TCP/IP sockets is used
- HTTP is a stateless protocol
- HTTP is used to transmit resources, not just files. Ex: HTML files, image files, query results, or anything else. (A resource is anything that can have a URL)
HTTP 1.0
- one transaction per connection
- one domain (host) per IP address
HTTP 1.1
- allows multiple transactions through a single +HttpPersistentConnection
- cache support
- +HttpChunkedEncoding encoding allows a response to be sent before its total length is known
- multiple domains (hosts) can be served from a single IP address.
HTTP Transactions
HTTP messages look like this:
<initial line, different for request vs. response> Header1: value1 Header2: value2 Header3: value3 etc... [blank line here!!!] <optional message body goes here, like file contents or query data; it can be many lines long, or even binary data $&*%@!^$@>
Notes: Blank lines should be CRLF!!! (but LF should also be handled properly)
Initial Line
Initial Request Line
The initial request line has three parts, each separated by a space:
- a method name (See +HttpMethods),
- the local path of the requested resource, and
- the HTTP version.
Example:
GET /path/to/file/index.html HTTP/1.0
Initial Response Line
The initial response line (aka the status line) has three parts, each separated by a space:
- the HTTP version,
- a response status code, and
- a reason (in "plain English") describing the status code.
Example:
HTTP/1.0 404 Not Found
Headers
Request Headers
- HTTP 1.0 defines 16 headers, though none are required.
- HTTP 1.1 defines 46 headers, and one (
Host:
) is required.
For Net-politeness, using these optional headers is a good idea:
From:
header gives the email address of whoever's making the request, or running the program doing so.User-Agent:
header identifies the program that's making the request in the formProgram-name/x.xx
wherex.xx
is the program version. For example,User-agent: Mozilla/3.0Gold
.
If you don't support persistent connections or if you know a request will be the last on its connection, then include
Connection:
close header to close the persistent connection
Example:
GET /path/file.html HTTP/1.1 Host: www.host1.com:80 [blank line here]
Response Headers
- all headers except for
Date:
are optional (including Host:)
It's a good idea for servers to return these optional headers:
Server:
header identifies the server in the formProgram-name/x.xx
. For example,Server: Apache/1.2b3-dev
.Last-Modified:
header is the last date the resource was modified. It's used in caching and other bandwidth-saving activities. Use Greenwich Mean Time in the formatLast-Modified: Fri, 31 Dec 1999 23:59:59 GMT
To tell the client the connection was closed use
Connection:
close if a response contains this header, then the server will close the connection following that response, and the client shouldn't send any more requests through that connection.
A server might close the connection before all responses are sent, so a client must keep track of requests and resend them as needed. When resending, don't pipeline the requests until you know the connection is persistent.
Caching is an important improvement in HTTP 1.1, and can't work without timestamped responses. So, servers must timestamp every response with a Date: header containing the current time, in the form
Date: Fri, 31 Dec 1999 23:59:59 GMT
No backlinks to this page. comments powered by Disqus