|[ Team LiB ]|
A Brief Summary of an HTTP Client/Server Negotiation
It is beyond the scope of this book to explore all the information exchanged between server and client when a request is made, not least because PHP handles most of these details for you. You should gain a basic understanding of this process, however, especially if you intend to write scripts that fetch Web pages or check the status of Web addresses.
HTTP stands for Hypertext Transfer Protocol. It is essentially a set of rules that defines the process by which a client sends a request and a server returns a response. Both client and server provide information about themselves and the data to be transferred. Much of this information becomes available to you in superglobal arrays.
The request line is mandatory. It consists of a request method, typically GET, HEAD, or POST; the address of the required document; and the HTTP version to be used (HTTP/1.0 or HTTP/1.1). A typical request for a document called mydoc.html might look like this:
GET /mydoc.html HTTP/1.0
The client is making a GET request. In other words, it is requesting an entire document but sending no data itself (in fact, you can send small amounts of data as part of a GET request by adding a query string to the URL). The HEAD method would be used if you wanted only information about a document. The POST method is used to transfer data from a client to the server, usually from an HTML form.
Most clients follow the request line with a header section in which name/value pairs can be sent to the server. Some of these become available to you as environment variables. Each client header consists of a key and value on one line separated by a colon. Table 14.2 lists a few of these.
For GET and HEAD methods, the header section ends the request and an empty line is sent to the server. For requests made using the POST method, an empty line is followed by the entity body. An entity body consists of any data to be sent to the server; this is usually a set of URL-encoded name/value pairs similar to those found in a query string.
Listing 14.2 shows a request sent to a server by Mozilla 5.0.
1: GET /index.html HTTP/1.1 2: Host: resources.corrosive.co.uk:9090 3: User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225 4: Accept: text/xml,application/xml,application/xhtml+xml,text/html; 5: Accept-Encoding: gzip, deflate, compress;q=0.9 6: Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 7: Keep-Alive: 300 8: Connection: keep-alive
As you can see, there's a lot of symmetry between a request and a response. In fact, certain headers can be sent by either client or server, especially those that provide information about an entity body.
Many response codes are available that a server can send to a browser. Each code provides some information about the success or otherwise of the request. Table 14.3 lists some of the more common response codes.
HTTP/1.1 200 OK
The header section includes a series of response headers, formatted in the same way as request headers. Table 14.4 lists some headers commonly sent by servers.
Listing 14.3 shows a typical server response. After the headers have been sent (lines 2–6), the server sends an empty line to the client (line 7) followed by the entity body (the document originally requested).
1: HTTP/1.1 200 OK 2: Date: Mon, 08 Sep 2003 19:24:35 GMT 3: Server: Apache/2.0.47 (Unix) PHP/5.0.0b1 4: X-Powered-By: PHP/5.0.0b1 5: Connection: close 6: Content-Type: text/html; charset=ISO-8859-1 7: 8: <!DOCTYPE html PUBLIC 9: "-//W3C//DTD XHTML 1.0 Strict//EN" 10: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 11: <html> 12: <head> 13: <title>Listing 14.3 A server response</title> 14: </head> 15: <body> 16: <div> 17: Hello 18: </div> 19: </body> 20: </html>
|[ Team LiB ]|