SE452: HTTP Request Headers [11/20] ![]() ![]() ![]() |
Here's an example HTTP request. This is sent by the client, and is the mechanism that the client uses to tell the server about itself and what it is requesting. Since HTTP is a line based protocol, the headers all appear on a separate line. We'll need to understand what some of these headers are if we are to communicate properly with the client for certain applications.
GET / HTTP/1.1 Accept: image/gif, image/x-xbitmap, image/jpeg, */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461) Host: localhost Connection: Keep-Alive
The class ServletRequest contains methods that provide information about the current request
int getContentLength()
The length, in bytes, of the request body. Returns -1 if length isn't known.
String getContentType()
The MIME type of the request body.
String getProtocol()
The name and version of the protocol the client uses. In the form: protocol / majorVersion . minorVersion (e. g., HTTP/ 1.1 )
String getRemoteAddr()
The IP address of the client.
String getRemoteHost()
The fully qualified host name of the client.
String getScheme()
The name of the scheme (protocol) used to make this request, e. g., http , https , or ftp .
boolean isSecure()
Whether this request was made using a secure channel, such as https .
There are also HTTP specific headers, and these are defined in
HttpServletRequest
String getAuthType()
The name of the authentication scheme used, e. g., BASIC or SSL or null
String getContextPath()
The portion of the request URI that indicates the context of the request.
String getMethod()
The name of the HTTP request method e. g., GET, POST, or PUT.
String getPathInfo()
Any extra path information associated with the URL the client sent.
String getPathTranslated()
Any extra path information after the servlet name but before the query string, and translates it to a real path.
String getQueryString()
The query string that is appended to the request URL after the path.
String getRemoteUser()
The login of the user making this request, if the user has been authenticated, or null .
String getRequestURI()
The part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
String getServletPath()
The part of this request's URL that calls the servlet.
Most of the above Request information is actually stored in a Request Header. Each header is one line of the HTTP request, in a key : value format. To learn what these headers are, it pays to look at the CGI (Common Gateway Interface) specification
The CGI spec is at http://hoohoo.ncsa.uiuc.edu/cgi/overview.html. CGI scripts can be written in almost any language, but most commonly are C, Perl, Python, or a similar scripting language. The CGI spec defines a number of Environment Variables that are used by CGI scripts to pass data from the server to the script (and back out)
Here's a CGI script written in Perl that prints the CGI variables
#!/usr/bin/perl print "Content-type:text/html\n\n"; print <<EOF; <html><head><title>Print CGI Environment</title></head> <body> EOF print "<table><tr><th>Key</th><th>Value</th></tr>" foreach $key (sort(keys %ENV)) { print "<tr><td bgcolor=\"#F76541\">$key</td>"; print "<td>$ENV{$key}</td></tr>\n"; } print "</table>\n"; print "</body></html>";