HTTP Tutorial
HTTP Tutorial
Audience
This tutorial has been prepared for computer science graduates and web
developers to help them understand the basic-to-advanced level concepts
related to Hypertext Transfer Protocol (HTTP).
Prerequisites
Before proceeding with this tutorial, it is good to have a basic understanding of
web concepts, web browsers, web servers, client and server architecture based
softwares.
Table of Contents
About the Tutorial i
Audience i
Prerequisites i
Copyright & Disclaimer i
Table of Contents ii
1. OVERVIEW 1
Basic Features 1
Basic Architecture 1
Client 2
Server 2
2. PARAMETERS 3
HTTP Version 3
Uniform Resource Identifiers 3
Date/Time Formats 4
Character Sets 4
Content Encodings 4
Media Types 5
Language Tags 5
3. MESSAGES 7
Message Start-Line 8
Header Fields 8
Message Body 9
4. REQUESTS 10
Request-Line 10
Request Method 10
ii
Request-URI 11
Request Header Fields 12
Examples of Request Message 13
5. RESPONSES 15
Message Status-Line 15
HTTP Version 15
Status Code 16
Response Header Fields 16
Examples of Response Message 17
6. METHODS 20
GET Method 21
HEAD Method 21
POST Method 22
PUT Method 23
DELETE Method 24
CONNECT Method 25
OPTIONS Method 25
TRACE Method 26
7. STATUS CODES 27
1xx: Information 27
2xx: Successful 28
3xx: Redirection 28
4xx: Client Error 29
5xx: Server Error 30
8. HEADER FIELDS 32
iii
General Headers 32
Cache-Control 32
Connection 34
Date 35
Pragma 35
Trailer 35
Transfer-Encoding 36
Upgrade 36
Via 36
Warning 36
Client Request Headers 37
Accept 37
Accept-Charset 37
Accept-Encoding 37
Accept-Language 38
Authorization 38
Cookie 38
Expect 39
From 39
Host 39
If-Match 39
If-Modified-Since 40
If-None-Match 40
If-Range 41
If-Unmodified-Since 41
Max-Forwards 41
Proxy-Authorization 42
Range 42
Referer 43
TE 43
User-Agent 43
Server Response Headers 44
Accept-Ranges 44
Age 44
ETag 44
Location 45
Proxy-Authenticate 45
Retry-After 45
Server 46
Set-Cookie 46
Vary 47
WWW-Authenticate 47
Entity Headers 48
Allow 48
Content-Encoding 48
Content-Language 48
Content-Length 49
Content-Location 49
iv
Content-MD5 49
Content-Range 50
Content-Type 50
Expires 51
Last-Modified 51
9. CACHING 52
10. URL ENCODING 55
11. SECURITY 61
Personal Information Leakage 61
File and Path Names Based Attack 61
DNS Spoofing 62
Location Headers and Spoofing 62
Authentication Credentials 62
Proxies and Caching 62
1. OVERVIEW
HTTP
Basic Features
There are three basic features that make HTTP a simple but powerful protocol:
Basic Architecture
The following diagram shows a very basic architecture of a web application and
depicts where HTTP sits:
1
HTTP
Client
The HTTP client sends a request to the server in the form of a request method,
URI, and protocol version, followed by a MIME-like message containing request
modifiers, client information, and possible body content over a TCP/IP
connection.
Server
The HTTP server responds with a status line, including the message's protocol
version and a success or error code, followed by a MIME-like message containing
server information, entity meta-information, and possible entity-body content.
2. PARAMETERS
HTTP
This chapter is going to list down few of the important HTTP Protocol Parameters
and their syntax the way they are used in the communication. For example,
format for date, format of URL, etc. This will help you in constructing your
request and response messages while writing HTTP client or server programs.
You will see the complete usage of these parameters in subsequent chapters
while learning the message structure for HTTP requests and responses.
HTTP Version
HTTP uses a <major>.<minor> numbering scheme to indicate versions of the
protocol. The version of an HTTP message is indicated by an HTTP-Version field
in the first line. Here is the general syntax of specifying HTTP version number:
HTTP-Version
Example
HTTP/1.0
or
HTTP/1.1
Example
3
HTTP
Date/Time Formats
All HTTP date/time stamps MUST be represented in Greenwich Mean Time
(GMT), without exception. HTTP applications are allowed to use any of the
following three representations of date/time stamps:
Sun, 06 Nov 1994 08:49:37 GMT
6 08:49:37 1994
Character Sets
We use character sets to specify the character sets that the client prefers.
Multiple character sets can be listed separated by commas. If a value is not
specified, the default is the US-ASCII.
Example
Following are the valid character sets:
US-ASCII
or
ISO-8859-1
or
ISO-8859-7
Content Encodings
A content encoding value indicates that an encoding algorithm has been used to
encode the content before passing it over the network. Content coding are
4
HTTP
Example
Following are the valid encoding schemes:
Accept-encoding: gzip
or
Accept-encoding: compress
or
Accept-encoding: deflate
Media Types
HTTP uses Internet Media Types in the Content-Type and Accept header fields
in order to provide open and extensible data typing and type negotiation. All the
Media-type values are registered with the Internet Assigned Number Authority
(IANA). The general syntax to specify media type is as follows:
media-type
Example
Accept: image/gif
Language Tags
HTTP uses language tags within the Accept-Language and Content-Language
fields. A language tag is composed of one or more parts: a primary language tag
and a possibly empty series of subtags:
language-tag
HTTP
Whitespaces are not allowed within the tags and all tags are case-insensitive.
Example
Example tags include:
en, en-US, en-cockney, i-cherokee, x-pig-latin
where any two-letter primary-tag is an ISO-639 language abbreviation and any
two-letter initial subtag is an ISO-3166 country code.
3. MESSAGES
HTTP
HTTP requests and HTTP responses use a generic message format of RFC 822 for
transferring the required data. This generic message format consists of the
following four items.
A Start-line
An empty line (i.e., a line with nothing preceding the CRLF) indicating
the end of the header fields
Optionally a message-body
In the following sections, we will explain each of the entities used in an HTTP
message.
HTTP
Message Start-Line
A start-line will have the following generic syntax:
start-line = Request-Line | Status-Line
We will discuss Request-Line and Status-Line while discussing HTTP Request and
HTTP Response messages respectively. For now, let's see the examples of start
line in case of request and response:
GET /hello.htm HTTP/1.1
HTTP/1.1 200 OK
Header Fields
HTTP header fields provide required information about the request or response,
or about the object sent in the message body. There are four types of HTTP
message headers:
Response-header: These
response messages.
header
fields
have
applicability
only
for
All the above-mentioned headers follow the same generic format and each of the
header field consists of a name followed by a colon (:) and the field value as
follows:
message-header = field-name ":" [ field-value ]
Following are the examples of various header fields:
User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache
8
HTTP
Message Body
The message body part is optional for an HTTP message but if it is available,
then it is used to carry the entity-body associated with the request or response.
If entity body is associated, then usually Content-Type and Content-Length
headers lines specify the nature of the body associated.
A message body is the one which carries the actual HTTP request data (including
form data and uploaded, etc.) and HTTP response data from the server
(including files, images, etc.). Shown below is the simple content of a message
body:
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
4. REQUESTS
HTTP
A Request-line
An empty line (i.e., a line with nothing preceding the CRLF) indicating
the end of the header fields
Optionally a message-body
The following sections explain each of the entities used in an HTTP request
message.
Request-Line
The Request-Line begins with a method token, followed by the Request-URI and
the protocol version, and ending with CRLF. The elements are separated by
space SP characters.
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
Let's discuss each of the parts mentioned in the Request-Line.
Request Method
The request method indicates the method to be performed on the resource
identified by the given Request-URI. The method is case-sensitive and should
always be mentioned in uppercase. The following table lists all the supported
methods in HTTP/1.1.
10
HTTP
S.N.
GET
The GET method is used to retrieve information from the given server
using a given URI. Requests using GET should only retrieve data and
should have no other effect on the data.
HEAD
Same as GET, but it transfers the status line and the header section
only.
POST
A POST request is used to send data to the server, for example,
customer information, file upload, etc. using HTML forms.
PUT
Replaces all the current representations of the target resource with the
uploaded content.
DELETE
Removes all the current representations of the target resource given by
URI.
CONNECT
Establishes a tunnel to the server identified by a given URI.
OPTIONS
Describes the communication options for the target resource.
TRACE
Performs a message loopback test along with the path to the target
resource.
Request-URI
The Request-URI is a Uniform Resource Identifier and identifies the resource
upon which to apply the request. Following are the most commonly used forms
to specify an URI:
Request-URI = "*" | absoluteURI | abs_path | authority
11
HTTP
S.N.
1
Accept-Charset
Accept-Encoding
Accept-Language
Authorization
12
HTTP
Expect
From
Host
If-Match
If-Modified-Since
If-None-Match
If-Range
If-Unmodified-Since
Max-Forwards
Proxy-Authorization
Range
Referer
TE
User-Agent
You can introduce your custom fields in case you are going to write your own
custom Client and Web Server.
HTTP
licenseID=string&content=string&/paramsXML=string
Here the given URL /cgi-bin/process.cgi will be used to process the passed data
and accordingly, a response will be returned. Here content-type tells the server
that the passed data is a simple web form data and length will be the actual
length of the data put in the message body. The following example shows how
you can pass plain XML to your web server:
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
14
5. RESPONSES
HTTP
A Status-line
An empty line (i.e., a line with nothing preceding the CRLF) indicating
the end of the header fields
Optionally a message-body
The following sections explain each of the entities used in an HTTP response
message.
Message Status-Line
A Status-Line consists of the protocol version followed by a numeric status code
and its associated textual phrase. The elements are separated by space SP
characters.
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
HTTP Version
A server supporting HTTP version 1.1 will return the following version
information:
HTTP-Version = HTTP/1.1
15
HTTP
Status Code
The Status-Code element is a 3-digit integer where first digit of the Status-Code
defines the class of response and the last two digits do not have any
categorization role. There are 5 values for the first digit:
S.N.
1xx: Informational
It means the request was received and the process is continuing.
2xx: Success
It means the action was successfully received, understood, and
accepted.
3xx: Redirection
It means further action must be taken in order to complete the request.
HTTP status codes are extensible and HTTP applications are not required to
understand the meaning of all registered status codes. A list of all the status
codes has been given in a separate chapter for your reference.
Accept-Ranges
Age
ETag
16
HTTP
Location
Proxy-Authenticate
Retry-After
Server
Vary
WWW-Authenticate
You can introduce your custom fields in case you are going to write your own
custom Web Client and Server.
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
The following example shows an HTTP response message displaying error
condition when the web server could not find the requested page:
HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Connection: Closed
17
HTTP
HTTP
</html>
19
6. METHODS
HTTP
The set of common methods for HTTP/1.1 is defined below and this set can be
expanded based on requirements. These method names are case-sensitive and
they must be used in uppercase.
S.N.
GET
The GET method is used to retrieve information from the given server
using a given URI. Requests using GET should only retrieve data and
should have no other effect on the data.
HEAD
Same as GET, but transfers the status line and header section only.
POST
A POST request is used to send data to the server, for example,
customer information, file upload, etc. using HTML forms.
PUT
Replaces all current representations of the target resource with the
uploaded content.
DELETE
Removes all current representations of the target resource given by a
URI.
CONNECT
Establishes a tunnel to the server identified by a given URI.
OPTIONS
Describes the communication options for the target resource.
TRACE
Performs a message loopback test along the path to the target
resource.
20
HTTP
GET Method
A GET request retrieves data from a web server by specifying parameters in the
URL portion of the request. This is the main method used for document retrieval.
The following example makes use of GET method to fetch hello.htm:
GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
The server response against the above GET request will be as follows:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
HEAD Method
The HEAD method is functionally similar to GET, except that the server replies
with a response line and headers, but no entity-body. The following example
makes use of HEAD method to fetch header information about hello.htm:
21
HTTP
POST Method
The POST method is used when you want to send some data to the server, for
example, file update, form data, etc. The following example makes use of POST
method to send a form data to the server, which will be processed by a
process.cgi and finally a response will be returned:
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: 88
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
22
HTTP
<html>
<body>
<h1>Request Processed Successfully</h1>
</body>
</html>
PUT Method
The PUT method is used to request the server to store the included entity-body
at a location specified by the given URL. The following example requests the
server to save the given entity-boy in hello.htm at the root of the server:
PUT /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
23
HTTP
Content-Length: 182
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
The server will store the given entity-body in hello.htm file and will send the
following response back to the client:
HTTP/1.1 201 Created
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>The file was created.</h1>
</body>
</html>
DELETE Method
The DELETE method is used to request the server to delete a file at a location
specified by the given URL. The following example requests the server to delete
the given file hello.htm at the root of the server:
DELETE /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Connection: Keep-Alive
The server will delete the mentioned file hello.htm and will send the following
response back to the client:
24
HTTP
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed
<html>
<body>
<h1>URL deleted.</h1>
</body>
</html>
CONNECT Method
The CONNECT method is used by the client to establish a network connection to
a web server over HTTP. The following example requests a connection with a
web server running on the host tutorialspoint.com:
CONNECT www.tutorialspoint.com HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
The connection is established with the server and the following response is sent
back to the client:
HTTP/1.1 200 Connection established
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
OPTIONS Method
The OPTIONS method is used by the client to find out the HTTP methods and
other options supported by a web server. The client can specify a URL for the
OPTIONS method, or an asterisk (*) to refer to the entire server. The following
example requests a list of methods supported by a web server running on
tutorialspoint.com:
OPTIONS * HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
25
HTTP
The server will send an information based on the current configuration of the
server, for example:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: httpd/unix-directory
TRACE Method
The TRACE method is used to echo the contents of an HTTP Request back to the
requester which can be used for debugging purpose at the time of development.
The following example shows the usage of TRACE method:
TRACE / HTTP/1.1
Host: www.tutorialspoint.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
The server will send the following message in response to the above request:
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-Type: message/http
Content-Length: 39
Connection: Closed
TRACE / HTTP/1.1
Host: www.tutorialspoint.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
26
7. STATUS CODES
HTTP
The Status-Code element in a server response is a 3-digit integer where the first
digit of the Status-Code defines the class of response and the last two digits do
not have any categorization role. There are 5 values for the first digit:
S.N.
1xx: Informational
It means the request has been received and the process is continuing.
2xx: Success
It means the action was successfully received, understood, and
accepted.
3xx: Redirection
It means further action must be taken in order to complete the request.
HTTP status codes are extensible and HTTP applications are not required to
understand the meaning of all the registered status codes. Given below is a list
of all the status codes.
1xx: Information
Message
100 Continue
Description
Only a part of the request has been received
by the server, but as long as it has not been
rejected, the client should continue with the
request.
27
HTTP
2xx: Successful
Message
Description
200 OK
201 Created
202 Accepted
203 Non-authoritative
Information
204 No Content
3xx: Redirection
Message
Description
302 Found
HTTP
a new url.
303 See Other
This is the response code to an If-ModifiedSince or If-None-Match header, where the URL
has not been modified since the specified date.
306 Unused
Description
401 Unauthorized
403 Forbidden
29
HTTP
409 Conflict
410 Gone
416 Requested
Satisfiable
Range
request
Description
HTTP
upstream server.
503 Service Unavailable
31
8. HEADER FIELDS
HTTP
HTTP header fields provide required information about the request or response,
or about the object sent in the message body. There are four types of HTTP
message headers:
General Headers
Cache-Control
The Cache-Control general-header field is used to specify directives that MUST
be obeyed by all the caching system. The syntax is as follows:
Cache-Control : cache-request-directive|cache-response-directive
An HTTP client or server can use the Cache-control general header to specify
parameters for the cache or to request certain kinds of documents from the
cache. The caching directives are specified in a comma-separated list. For
example:
Cache-control: no-cache
The following table lists the important cache request directives that can be used
by the client in its HTTP request:
S.N.
1
HTTP
no-store
The cache should not store anything about the client request or server
response.
max-age = seconds
Indicates that the client is willing to accept a response whose age is not
greater than the specified time in seconds.
max-stale [ = seconds ]
Indicates that the client is willing to accept a response that has
exceeded its expiration time. If seconds are given, it must not be
expired by more than that time.
min-fresh = seconds
Indicates that the client is willing to accept a response whose freshness
lifetime is not less than its current age plus the specified time in
seconds.
no-transform
Does not convert the entity-body.
only-if-cached
Does not retrieve new data. The cache can send a document only if it is
in the cache, and should not contact the origin-server to see if a newer
copy exists.
public
Indicates that the response may be cached by any cache.
private
Indicates that all or part of the response message is intended for a
single user and must not be cached by a shared cache.
no-cache
A cache must not use the response to satisfy a subsequent request
33
HTTP
no-store
The cache should not store anything about the client request or server
response.
no-transform
Does not convert the entity-body.
must-revalidate
The cache must verify the status of the stale documents before using it
and expired ones should not be used.
proxy-revalidate
The proxy-revalidate directive has the same meaning as the mustrevalidate directive, except that it does not apply to non-shared user
agent caches.
max-age = seconds
Indicates that the client is willing to accept a response whose age is not
greater than the specified time in seconds.
s-maxage = seconds
The maximum age specified by this directive overrides the maximum
age specified by either the max-age directive or the Expires header. The
s-maxage directive is always ignored by a private cache.
Connection
The Connection general-header field allows the sender to specify options that
are desired for that particular connection and must not be communicated by
proxies over further connections. Following is the simple syntax for using
connection header:
Connection : "Connection"
HTTP/1.1 defines the "closed" connection option for the sender to signal that the
connection will be closed after completion of the response. For example:
Connection: Closed
By default, HTTP 1.1 uses persistent connections, where the connection does not
automatically close after a transaction. HTTP 1.0, on the other hand, does not
34
HTTP
Date
All HTTP date/time stamps MUST be represented in Greenwich Mean Time
(GMT), without exception. HTTP applications are allowed to use any of the
following three representations of date/time stamps:
Sun, 06 Nov 1994 08:49:37 GMT
6 08:49:37 1994
Pragma
The Pragma general-header field is used to include implementation-specific
directives that might apply to any recipient along the request/response chain.
For example:
Pragma: no-cache
The only directive defined in HTTP/1.0 is the no-cache directive and is
maintained in HTTP 1.1 for backward compatibility. No new Pragma directives
will be defined in the future.
Trailer
The Trailer general field value indicates that the given set of header fields is
present in the trailer of a message encoded with chunked transfer-coding.
Following is the syntax of Trailer header field:
Trailer : field-name
Message header fields listed in the Trailer header field must not include the
following header fields:
Transfer-Encoding
Content-Length
Trailer
35
HTTP
Transfer-Encoding
The Transfer-Encoding general-header field indicates what type of transformation
has been applied to the message body in order to safely transfer it between the
sender and the recipient. This is not the same as content-encoding because
transfer-encodings are a property of the message, not of the entity-body. The
syntax of Transfer-Encoding header field is as follows:
Transfer-Encoding: chunked
All transfer-coding values are case-insensitive.
Upgrade
The Upgrade general-header allows the client to specify what additional
communication protocols it supports and would like to use if the server finds it
appropriate to switch protocols. For example:
Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
The Upgrade header field is intended to provide a simple mechanism for
transition from HTTP/1.1 to some other, incompatible protocol.
Via
The Via general-header must be used by gateways and proxies to indicate the
intermediate protocols and recipients. For example, a request message could be
sent from an HTTP/1.0 user agent to an internal proxy code-named "fred", which
uses HTTP/1.1 to forward the request to a public proxy at nowhere.com, which
completes the request by forwarding it to the origin server at www.ics.uci.edu.
The request received by www.ics.uci.edu would then have the following Via
header field:
Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
The Upgrade header field is intended to provide a simple mechanism for
transition from HTTP/1.1 to some other, incompatible protocol.
Warning
The Warning general-header is used to carry additional information about the
status or transformation of a message which might not be reflected in the
message. A response may carry more than one Warning header.
Warning : warn-code SP warn-agent SP warn-text SP warn-date
36
HTTP
Accept-Charset
The Accept-Charset request-header field can be used to indicate what character
sets are acceptable for the response. Following is the general syntax:
Accept-Charset: character_set [q=qvalue]
Multiple character sets can be listed separated by commas and the optional
qvalue represents an acceptable quality level for non-preferred character sets on
a scale of 0 to 1. Following is an example:
Accept-Charset: iso-8859-5, unicode-1-1; q=0.8
The special value "*", if present in the Accept-Charset field, matches every
character set and if no Accept-Charset header is present, the default is that
any character set is acceptable.
Accept-Encoding
The Accept-Encoding request-header field is similar to Accept, but restricts the
content-codings that are acceptable in the response. The general syntax is:
Accept-Encoding: encoding types
HTTP
Accept-Language
The Accept-Language request-header field is similar to Accept, but restricts the
set of natural languages that are preferred as a response to the request. The
general syntax is:
Accept-Language: language [q=qvalue]
Multiple languages can be listed separated by commas and the optional qvalue
represents an acceptable quality level for non-preferred languages on a scale of
0 to 1. Following is an example:
Accept-Language: da, en-gb;q=0.8, en;q=0.7
Authorization
The Authorization request-header field value consists of credentials containing
the authentication information of the user agent for the realm of the resource
being requested. The general syntax is:
Authorization : credentials
The HTTP/1.0 specification defines the BASIC authorization scheme, where the
authorization parameter is the string of username:password encoded in base
64. Following is an example:
Authorization: BASIC Z3Vlc3Q6Z3Vlc3QxMjM=
The value decodes into is guest:guest123 where guest is user ID and
guest123 is the password.
Cookie
The Cookie request-header field value contains a name/value pair of information
stored for that URL. Following is the general syntax:
Cookie: name=value
38
HTTP
Expect
The Expect request-header field is used to indicate that a particular set of server
behaviors is required by the client. The general syntax is:
Expect : 100-continue | expectation-extension
If a server receives a request containing an Expect field that includes an
expectation-extension that it does not support, it must respond with a 417
(Expectation Failed) status.
From
The From request-header field contains an Internet e-mail address for the
human user who controls the requesting user agent. Following is a simple
example:
From: webmaster@w3.org
This header field may be used for logging purposes and as a means for
identifying the source of invalid or unwanted requests.
Host
The Host request-header field is used to specify the Internet host and the port
number of the resource being requested. The general syntax is:
Host : "Host" ":" host [ ":" port ] ;
A host without any trailing port information implies the default port, which is 80.
For example, a request on the origin server for http://www.w3.org/pub/WWW/
would be:
GET /pub/WWW/ HTTP/1.1
Host: www.w3.org
If-Match
The If-Match request-header field is used with a method to make it conditional.
This header requests the server to perform the requested method only if the
given value in this tag matches the given entity tags represented by ETag. The
general syntax is:
39
HTTP
If-Match : entity-tag
An asterisk (*) matches any entity, and the transaction continues only if the
entity exists. Following are possible examples:
If-Match: "xyzzy"
If-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
If-Match: *
If none of the entity tags match, or if "*" is given and no current entity exists,
the server must not perform the requested method, and must return a 412
(Precondition Failed) response.
If-Modified-Since
The If-Modified-Since request-header field is used with a method to make it
conditional. If the requested URL has not been modified since the time specified
in this field, an entity will not be returned from the server; instead, a 304 (not
modified) response will be returned without any message-body. The general
syntax of if-modified-since is:
If-Modified-Since : HTTP-date
An example of the field is:
If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
If none of the entity tags match, or if "*" is given and no current entity exists,
the server must not perform the requested method, and must return a 412
(Precondition Failed) response.
If-None-Match
The If-None-Match request-header field is used with a method to make it
conditional. This header requests the server to perform the requested method
only if one of the given value in this tag matches the given entity tags
represented by ETag. The general syntax is:
If-None-Match : entity-tag
An asterisk (*) matches any entity, and the transaction continues only if the
entity does not exist. Following are the possible examples:
If-None-Match: "xyzzy"
40
HTTP
If-Range
The If-Range request-header field can be used with a conditional GET to request
only the portion of the entity that is missing, if it has not been changed, and the
entire entity if it has been changed. The general syntax is as follows:
If-Range : entity-tag | HTTP-date
Either an entity tag or a date can be used to identify the partial entity already
received. For example:
If-Range: Sat, 29 Oct 1994 19:43:31 GMT
Here if the document has not been modified since the given date, the server
returns the byte range given by the Range header, otherwise it returns all of the
new document.
If-Unmodified-Since
The If-Unmodified-Since request-header field is used with a method to make it
conditional. The general syntax is:
If-Unmodified-Since : HTTP-date
If the requested resource has not been modified since the time specified in this
field, the server should perform the requested operation as if the If-UnmodifiedSince header were not present. For example:
If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT
If the request results in anything other than a 2xx or 412 status, the IfUnmodified-Since header should be ignored.
Max-Forwards
The Max-Forwards request-header field provides a mechanism with the TRACE
and OPTIONS methods to limit the number of proxies or gateways that can
forward the request to the next inbound server. Here is the general syntax:
Max-Forwards : n
41
HTTP
Proxy-Authorization
The Proxy-Authorization request-header field allows the client to identify itself
(or its user) to a proxy which requires authentication. Here is the general
syntax:
Proxy-Authorization : credentials
The Proxy-Authorization field value consists of credentials containing the
authentication information of the user agent for the proxy and/or realm of the
resource being requested.
Range
The Range request-header field specifies the partial range(s) of the content
requested from the document. The general syntax is:
Range: bytes-unit=first-byte-pos "-" [last-byte-pos]
The first-byte-pos value in a byte-range-spec gives the byte-offset of the first
byte in a range. The last-byte-pos value gives the byte-offset of the last byte in
the range; that is, the byte positions specified are inclusive. You can specify a
byte-unit as bytes. Byte offsets start at zero. Some simple examples are as
follows:
- The first 500 bytes
Range: bytes=0-499
42
HTTP
Referer
The Referer request-header field allows the client to specify the address (URI) of
the resource from which the URL has been requested. The general syntax is as
follows:
Referer : absoluteURI | relativeURI
Following is a simple example:
Referer: http://www.tutorialspoint.org/http/index.htm
If the field value is a relative URI, it should be interpreted relative to
the Request-URI.
TE
The TE request-header field indicates what extension transfer-coding it is willing
to accept in the response and whether or not it is willing to accept trailer fields in
a chunked transfer-coding. Following is the general syntax:
TE
: t-codings
The presence of the keyword "trailers" indicates that the client is willing to
accept trailer fields in a chunked transfer-coding and it is specified either of the
ways:
TE: deflate
TE:
TE: trailers, deflate;q=0.5
If the TE field-value is empty or if no TE field is present, then only transfercoding is chunked. A message with no transfer-coding is always acceptable.
User-Agent
The User-Agent request-header field contains information about the user agent
originating the request. Following is the general syntax:
43
HTTP
Example:
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
: range-unit | none
Age
The Age response-header field conveys the sender's estimate of the amount of
time since the response (or its revalidation) was generated at the origin server.
The general syntax is:
Age : delta-seconds
Age values are non-negative decimal integers, representing time in seconds.
Following is a simple example:
Age: 1030
An HTTP/1.1 server that includes a cache must include an Age header field in
every response generated from its own cache.
ETag
The ETag response-header field provides the current value of the entity tag for
the requested variant. The general syntax is:
44
HTTP
ETag :
entity-tag
Location
The Location response-header field is used to redirect the recipient to a location
other than the Request-URI for completion. The general syntax is:
Location : absoluteURI
Following is a simple example:
Location: http://www.tutorialspoint.org/http/index.htm
The Content-Location header field differs from Location in that the ContentLocation identifies the original location of the entity enclosed in the request.
Proxy-Authenticate
The Proxy-Authenticate response-header field must be included as a part of a
407 (Proxy Authentication Required) response. The general syntax is:
Proxy-Authenticate
: challenge
Retry-After
The Retry-After response-header field can be used with a 503 (Service
Unavailable) response to indicate how long the service is expected to be
unavailable to the requesting client. The general syntax is:
Retry-After : HTTP-date | delta-seconds
Examples:
Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
Retry-After: 120
45
HTTP
Server
The Server response-header field contains information about the software used
by the origin server to handle the request. The general syntax is:
Server : product | comment
Following is a simple example:
Server: Apache/2.2.14 (Win32)
If the response is being forwarded through a proxy, the proxy application must
not modify the Server response-header.
Set-Cookie
The Set-Cookie response-header field contains a name/value pair of information
to retain for this URL. The general syntax is:
Set-Cookie: NAME=VALUE; OPTIONS
Set-Cookie response header comprises the token Set-Cookie, followed by a
comma-separated list of one or more cookies. Here are the possible values you
can specify as options:
S.N.
Comment=comment
This option can be used to specify any comment associated with the
cookie.
Domain=domain
The Domain attribute specifies the domain for which the cookie is valid.
Expires=Date-time
The date the cookie will expire. If it is blank, the cookie will expire when
the visitor quits the browser.
Path=path
The Path attribute specifies the subset of URLs to which this cookie
applies.
46
HTTP
Secure
It instructs the user agent to return the cookie only under a secure
connection.
Vary
The Vary response-header field specifies that the entity has multiple sources and
may therefore vary according to the specified list of request header(s). Following
is the general syntax:
Vary : field-name
You can specify multiple headers separated by commas and a value of asterisk
"*" signals that unspecified parameters are not limited to the request-headers.
Following is a simple example:
Vary: Accept-Language, Accept-Encoding
Here field names are case-insensitive.
WWW-Authenticate
The WWW-Authenticate response-header field must be included in 401
(Unauthorized) response messages. The field value consists of at least one
challenge that indicates the authentication scheme(s) and parameters applicable
to the Request-URI. The general syntax is:
WWW-Authenticate : challenge
WWW-Authenticate field value might contain more than one challenge, or if
more than one WWW-Authenticate header field is provided, the contents of a
challenge itself can contain a comma-separated list of authentication
parameters. Following is a simple example:
WWW-Authenticate: BASIC realm="Admin"
47
HTTP
Entity Headers
Allow
The Allow entity-header field lists the set of methods supported by the resource
identified by the Request-URI. The general syntax is:
Allow : Method
You can specify multiple methods separated by commas. Following is a simple
example:
Allow: GET, HEAD, PUT
This field cannot prevent a client from trying other methods.
Content-Encoding
The Content-Encoding entity-header field is used as a modifier to the mediatype. The general syntax is:
Content-Encoding : content-coding
The content-coding is a characteristic of the entity identified by the Request-URI.
Following is a simple example:
Content-Encoding: gzip
If the content-coding of an entity in a request message is not acceptable to the
origin server, the server should respond with a status code of 415 (Unsupported
Media Type).
Content-Language
The Content-Language entity-header field describes the natural language(s) of
the intended audience for the enclosed entity. Following is the general syntax:
Content-Language : language-tag
Multiple languages may be listed for content that is intended for multiple
audiences. Following is a simple example:
Content-Language: mi, en
The primary purpose of Content-Language is to allow a user to identify and
differentiate entities according to the user's own preferred language.
48
HTTP
Content-Length
The Content-Length entity-header field indicates the size of the entity-body, in
decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
method, the size of the entity-body that would have been sent, had the request
been a GET. The general syntax is:
Content-Length : DIGITS
Following is a simple example:
Content-Length: 3495
Any Content-Length greater than or equal to zero is a valid value.
Content-Location
The Content-Location entity-header field may be used to supply the resource
location for the entity enclosed in the message when that entity is accessible
from a location separate from the requested resource's URI. The general syntax
is:
Content-Location:
absoluteURI | relativeURI
Content-MD5
The Content-MD5 entity-header field may be used to supply an MD5 digest of the
entity for checking the integrity of the message upon receipt. The general syntax
is:
Content-MD5
1864
: 8c2d46911f3f5a326455f0ed7a8ed3b3
The MD5 digest is computed based on the content of the entity-body, including
any content-coding that has been applied, but not including any transferencoding applied to the message-body.
49
HTTP
Content-Range
The Content-Range entity-header field is sent with a partial entity-body to
specify where in the full entity-body the partial body should be applied. The
general syntax is:
Content-Range : bytes-unit SP first-byte-pos "-" last-byte-pos
Examples of byte-content-range-spec values, assuming that the entity contains
a total of 1234 bytes:
- The first 500 bytes:
Content-Range : bytes 0-499/1234
Content-Type
The Content-Type entity-header field indicates the media type of the entity-body
sent to the recipient or, in the case of the HEAD method, the media type that
would have been sent, had the request been a GET. The general syntax is:
50
HTTP
Content-Type : media-type
Following is an example:
Content-Type: text/html; charset=ISO-8859-4
Expires
The Expires entity-header field gives the date/time after which the response is
considered stale. The general syntax is:
Expires : HTTP-date
Following is an example:
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Last-Modified
The Last-Modified entity-header field indicates the date and time at which the
origin server believes the variant was last modified. The general syntax is:
Last-Modified: HTTP-date
Following is an example:
Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT
51
9. CACHING
HTTP
no-cache
A cache must not use the response to satisfy a subsequent request
without successful re-validation with the origin server.
no-store
The cache should not store anything about the client request or server
response.
max-age = seconds
Indicates that the client is willing to accept a response whose age is not
greater than the specified time in seconds.
max-stale [ = seconds ]
Indicates that the client is willing to accept a response that has
exceeded its expiration time. If seconds are given, it must not be
expired by more than that time.
52
HTTP
min-fresh = seconds
Indicates that the client is willing to accept a response whose freshness
lifetime is not less than its current age plus the specified time in
seconds.
no-transform
Does not convert the entity-body.
only-if-cached
Does not retrieve new data. The cache can send a document only if it is
in the cache, and should not contact the origin-server to see if a newer
copy exists.
The following cache response directives can be used by the server in its HTTP
response:
S.N.
public
Indicates that the response may be cached by any cache.
private
Indicates that all or part of the response message is intended for a
single user and must not be cached by a shared cache.
no-cache
A cache must not use the response to satisfy a subsequent request
without successful re-validation with the origin server.
no-store
The cache should not store anything about the client request or server
response.
no-transform
Does not convert the entity-body.
must-revalidate
The cache must verify the status of stale documents before using it and
expired ones should not be used.
53
HTTP
proxy-revalidate
The proxy-revalidate directive has the same meaning as the mustrevalidate directive, except that it does not apply to non-shared user
agent caches.
max-age = seconds
Indicates that the client is willing to accept a response whose age is not
greater than the specified time in seconds.
s-maxage = seconds
The maximum age specified by this directive overrides the maximum
age specified by either the max-age directive or the Expires header. The
s-maxage directive is always ignored by a private cache.
54
HTTP
HTTP URLs can only be sent over the Internet using the ASCII character-set,
which often contain characters outside the ASCII set. So these unsafe characters
must be replaced with a % followed by two hexadecimal digits.
The following table shows the ASCII symbols of the characters and their
replacements which can be used in the URL before passing it to the server:
ASCII
Symbol
< 32
Replacement
Encode with %xx where xx
representation of the character.
32
space
+ or %20
33
%21
34
"
%22
35
%23
36
%24
37
%25
38
&
%26
39
'
%27
40
%28
41
%29
42
43
%2B
is
the
hexadecimal
55
HTTP
44
%2C
45
46
47
%2F
48
49
50
51
52
53
54
55
56
57
58
%3A
59
%3B
60
<
%3C
61
%3D
62
>
%3E
56
HTTP
63
%3F
64
%40
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Q
57
HTTP
82
83
84
85
86
87
88
89
90
91
%5B
92
%5C
93
%5D
94
%5E
95
96
%60
97
98
99
100
d
58
HTTP
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
w
59
HTTP
120
121
122
123
%7B
124
%7C
125
%7D
126
%7E
127
%7F
> 127
is
the
hexadecimal
60
11. SECURITY
HTTP
Revealing the specific software version of the server might allow the
server machine to become more vulnerable to attacks against software
that is known to contain security holes.
The information sent in the From field might conflict with the user's
privacy interests or their site's security policy, and hence, it should not be
transmitted without the user being able to disable, enable, and modify the
contents of the field.
Authors of services that use the HTTP protocol should not use GET based
forms for the submission of sensitive data, because it will cause the data
to be encoded in the Request-URI.
HTTP
DNS Spoofing
Clients using HTTP rely heavily on the Domain Name Service, and are thus
generally prone to security attacks based on the deliberate mis-association of IP
addresses and DNS names. So clients need to be cautious in assuming the
continuing validity of an IP number/DNS name association.
If HTTP clients cache the results of host name lookups in order to achieve a
performance improvement, they must observe the TTL information reported by
the DNS. If HTTP clients do not observe this rule, they could be spoofed when a
previously-accessed server's IP address changes.
Authentication Credentials
Existing HTTP clients and user agents typically retain authentication information
indefinitely. HTTP/1.1 does not provide a method for a server to direct clients to
discard these cached credentials which is a big security risk.
There are a number of workarounds to the parts of this problem, and so it is
recommended to make the use of password protection in screensavers, idle
time-outs, and other methods that mitigate the security problems inherent in
this problem.
HTTP
Example 1
HTTP request to fetch hello.htm page
on tutorialspoint.com.
from
the
web
server
running
Client request
GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Server response
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
63
HTTP
Example 2
HTTP request to fetch t.html page that does not exist on the web server running
on tutorialspoint.com.
Client request
GET /t.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Server response
HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Content-Type: text/html; charset=iso-8859-1
Connection: close
64
HTTP
Example 3
HTTP request to fetch hello.htm page from the web server running
on tutorialspoint.com, but the request goes with an incorrect HTTP version:
Client request
GET /hello.htm HTTP1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Server response
HTTP/1.1 400 Bad Request
Date: Sun, 18 Oct 2012 10:36:20 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 230
Content-Type: text/html; charset=iso-8859-1
Connection: close
65
HTTP
Example 4
HTTP request to post form data to process.cgi CGI page on a web server
running on tutorialspoint.com. The server returns the passed name after setting
them as cookies:
Client request
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: 60
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
first=Zara&last=Ali
Server response
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-Length: 88
Set-Cookie: first=Zara,last=Ali;domain=tutorialspoint.com;Expires=Mon,
19Nov-2010 04:38:14 GMT;Path=/
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello Zara Ali</h1>
</body>
</html>
66