HTTP Hypertext Transfer Protocol HTTP messages n HTTP

  • Slides: 18
Download presentation
HTTP Hypertext Transfer Protocol

HTTP Hypertext Transfer Protocol

HTTP messages n HTTP is the language that web clients and web servers use

HTTP messages n HTTP is the language that web clients and web servers use to talk to each other n n HTTP is largely “under the hood, ” but a basic understanding can be helpful Each message, whether a request or a response, has three parts: 1. The request or the response line 2. A header section 3. The body of the message 2

What the client does, part I n n The client sends a message to

What the client does, part I n n The client sends a message to the server at a particular port (80 is the default) The first part of the message is the request line, containing: n n A method (HTTP command) such as GET or POST A document address, and An HTTP version number Example: n GET /index. html HTTP/1. 0 3

Other methods n Other methods beside GET and POST are: n n n n

Other methods n Other methods beside GET and POST are: n n n n HEAD: Like GET, but ask that only a header be returned PUT: Request to store the entity-body at the URI DELETE: Request removal of data at the URI LINK: Request header information be associated with a document on the server UNLINK: Request to undo a LINK request OPTIONS: Request information about communications options on the server TRACE: Request that the entity-body be returned as received (used for debugging) 4

What the client does, part II n The second part of a request is

What the client does, part II n The second part of a request is optional header information, such as: n n What the client software is What formats it can accept All information is in the form Name: Value Example: User-Agent: Mozilla/2. 02 Gold (Win. NT; I) Accept: image/gif, image/jpeg, */* n A blank line ends the header 5

Client request headers n Accept: type/subtype, . . . n n Accept-Language: en, fr,

Client request headers n Accept: type/subtype, . . . n n Accept-Language: en, fr, de n n The browser or other client program sending the request From: dave@acm. org n n Preferred language (For example: English, French, German) User-Agent: string n n Specifies media types that the client prefers to accept Email address of user of client program Cookie: name=value n n Information about a cookie for that URL Multiple cookies can be separated by commas 6

What the client does, part III n The third part of a request (after

What the client does, part III n The third part of a request (after the blank line) is the entity-body, which contains optional data n n The entity-body part is used mostly by POST requests The entity-body part is always empty for a GET request 7

What the server does, part I n n The server response is also in

What the server does, part I n n The server response is also in three parts The first part is the status line, which tells: n n n The HTTP version A status code A short description of what the status code means Example: HTTP/1. 1 404 Not Found Status codes are in groups: 100 -199 200 -299 300 -399 400 -499 500 -599 Informational The request was successful The request was redirected The request failed A server error occurred 8

Common status codes n 200 OK n n 301 Moved Permanently n n You

Common status codes n 200 OK n n 301 Moved Permanently n n You can’t do this, and we won’t tell you why 404 Not Found n n There is a xyntax error in your request 403 Forbidden n n URL temporarily out of service, keep the old one but use this one for now 400 Bad Request n n URI was moved, but here’s the new address for your records 302 Moved temporarily n n Everything worked, here’s the data No such document 408 Request Time-out, 504 Gateway Time-out n Request took too long to fulfill for some reason 9

What the server does, part II n n The second part of the response

What the server does, part II n n The second part of the response is header information, ended by a blank line Example: Content-Length: 2532 Connection: Close Server: GWS/2. 0 Date: Sun, 01 Dec 2002 21: 24: 50 GMT Content-Type: text/html Cache-control: private Set-Cookie: PREF=ID=05302 a 93093 ec 661: TM=1038777890: LM=1038777890: S= All on one line y. NWNjraft. Uz 299 RH; expires=Sun, 17 -Jan-2038 19: 14: 07 GMT; path=/; domain=. google. com n 10

Viewing the response n n There is a header viewer at http: //www. delorie.

Viewing the response n n There is a header viewer at http: //www. delorie. com/web/headers. html (with nasty jittery advertisements) Example 2. 3 (Get. Responses) in the Gittleman book does the same thing Here’s an example (from Get. Responses): % java Get. Responses http: //www. cis. upenn. edu/~matuszek/cit 5972003/index. html Status line: HTTP/1. 1 200 OK Response headers: Date: Wed, 10 Sep 2003 00: 26: 53 GMT Server: Apache/1. 3. 26 (Unix) PHP/4. 2. 2 mod_perl/1. 27 mod_ssl/2. 8. 10 Open. SSL/0. 9. 6 e Last-Modified: Tue, 09 Sep 2003 19: 24: 50 GMT ETag: "1 c 1 ad 5 -1654 -3 f 5 e 2902” Accept-Ranges: bytes Content-Length: 5716 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html 11

The Get. Responses program, I n Here’s just the skeleton of the program that

The Get. Responses program, I n Here’s just the skeleton of the program that provided the output on the last slide: n import java. net. *; import java. io. *; public class Get. Responses { public static void main(String [ ] args) { try {. . . interesting code goes here. . . } catch(Exception e) { e. print. Stack. Trace(); } } } 12

The Get. Responses program, II n Here’s the interesting part of the code: n

The Get. Responses program, II n Here’s the interesting part of the code: n URL url = new URL(args[0]); URLConnection c = url. open. Connection(); System. out. println("Status line: "); System. out. println('t' + c. get. Header. Field(0)); System. out. println("Response headers: "); String value = ""; int n = 1; while (true){ value = c. get. Header. Field(n); if (value == null) break; System. out. println('t' + c. get. Header. Field. Key(n++) + ": " + value); } 13

Server response headers n Server: NCSA/1. 3 n n Content-Type: type/subtype n n Name

Server response headers n Server: NCSA/1. 3 n n Content-Type: type/subtype n n Name and version of the server Should be of a type and subtype specified by the client’s Accept header Set-Cookie: name=value; options n Requests the client to store a cookie with the given name and value 14

What the server does, part III n n The third part of a server

What the server does, part III n n The third part of a server response is the entity body This is often an HTML page n But it can also be a jpeg, a gif, plain text, etc. --anything the browser (or other client) is prepared to accept 15

The <meta http-equiv> tag n n n The <meta http-equiv=string content=string> tag may occur

The <meta http-equiv> tag n n n The <meta http-equiv=string content=string> tag may occur in the <head> of an HTML document http-equiv and content typically have the same kinds of values as in the HTTP header This tag asks the client to pretend that the information actually occurred in the header n n n The information is not really in the header As usual, not all browsers handle this information the same way Example: <meta http-equiv="Set-Cookie" content="value=n; expires=date; path=url"> 16

Summary n HTTP is a fairly straightforward protocol with a lot of possible kinds

Summary n HTTP is a fairly straightforward protocol with a lot of possible kinds of predefined header information n n More kinds can be added, so long as client and server agree A request from the client consists of three parts: 1. A header line 2. A block of header information, ending with a blank line 3. The (optional) entity body, containing data n n n A response from the server consists of the same three parts HTTP headers are “under the hood” information, not normally displayed to the user As with most of the things covered in CIT 597, n n We have covered only the fundamentals Much more detail can be found on the Web 17

The End 18

The End 18