• No se han encontrado resultados

1. MARCO REFERENCIAL

2.2. CARACTERIZACIÓN GENERAL DEL TRAUMA CRANEOENCEFÁLICO

2.3.2. Accidentes de tránsito: Problema de Salud Pública

The better approach is to use the HTTP Validation model. This model sends each response with a ticket that references the uniqueness of the data. If the client wants to download the content again, the client sends the server a ticket from the last download. The server compares the sent ticket with the ticket that it has; if the server notices the tickets are identical, it sends an HTTP 304 to indicate no changes have occurred. At that point, the client can retrieve the old content from the cache and present it to the user as the latest and greatest. The HTTP Validation

model still requires an HTTP request, but does not include the cost of generating and sending the content again.

In terms of an HTTP conversation, the HTTP Validation model is implemented as follows. This example illustrates a request from a client and the response from the server.

Request 1:

GET /ajax/chap04/cachedpage.html HTTP/1.1 Accept: */*

Accept-Language: en-ca

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; ➥

Windows NT 5.1; SV1; .NET CLR 2.0.50215) Host: 127.0.0.1:8081 Connection: Keep-Alive Response 1: HTTP/1.1 200 OK ETag: W/"45-1123668584000"

Last-Modified: Wed, 10 Aug 2005 10:09:44 GMT Content-Type: text/html

Content-Length: 45

Date: Wed, 10 Aug 2005 10:11:54 GMT Server: Apache-Coyote/1.1 <html> <body> Cached content </body> </html>

The client makes a request for the document /ajax/chap04/cachedpage.html. The server responds with the content, but there is no Cache-Control nor Expires identifier. This seems to indicate that the returned content is not cached, but that is not true. The server has indicated that it is using the HTTP Validation model, and not the HTTP Expiration model. The page that is returned has become part of a cache identified by the unique ETag identifier. The ETag identifier, called an entity tag, could be compared to a unique hash code for an HTML page. The letter W

that is prefixed to the entity tag identifier means that the page is a weak reference and the HTTP server may not immediately reflect updates to the page on the server side.

The next step is to refresh the browser and ask for the same page again. The HTTP conver- sation is illustrated as follows.

Request 2:

GET /ajax/chap04/cachedpage.html HTTP/1.1 Accept: */*

Accept-Language: en-ca

Accept-Encoding: gzip, deflate

If-Modified-Since: Wed, 10 Aug 2005 10:09:44 GMT If-None-Match: W/"45-1123668584000"

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50215) Host: 192.168.1.100:8081 Connection: Keep-Alive Response 2: HTTP/1.1 304 Not Modified

Date: Wed, 10 Aug 2005 10:11:58 GMT Server: Apache-Coyote/1.1

When the client makes the second request, the additional identifiers If-Modified-Since

and If-None-Match are sent in the request. Notice how the identifier If-None-Match references the identifier of the previously sent ETag value. The server queries the URL and generates an entity tag. If the entity tag is identical to the value being sent, the server returns an HTTP 304 code to indicate that the content has not changed.

When using entity tags, the client can send an If-Match or an If-None-Match. If the client sends an If-Match, and the data on the server is out-of-date, the server returns a cache miss error, and not the new data. If the client sends an If-None-Match identifier when the server data is unchanged, the server sends an HTTP 304 return code. If the data is out-of-date, new data is sent.

The advantage of using the HTTP Validation model of caching is that you are always guar- anteed to get the latest version at the time of the request. The clients can make the request every couple of seconds, hours, weeks, or whatever period they choose. It is up to the client to decide when to get a fresh copy of the data. Granted, there is still some HTTP traffic due to the requests, but it has been reduced to a minimum.

Having said all that, there are situations when using the HTTP Expiration model does make sense—for example, when the HTML content is static and changes rarely. For the scope of this book and this pattern, it does not make sense to use the HTTP Expiration model because Ajax applications are inherently using data that does change.

Implementing HTTP validation is simple because the most popular web browsers and HTTP servers already implement it. In this chapter, I will discuss the details of implementing HTTP validation because there are some things the web browser and HTTP server do not do. However, building a more sophisticated infrastructure that supposedly enhances HTTP validation is not recommended because that would be defeating the facilities of HTTP 1.1.

Using the HTTP 1.1 infrastructure means that the server you are communicating with must have implemented the HTTP 1.1 protocol properly. If you are using Microsoft Internet Information Server, Apache Tomcat, or Jetty, you will have no problems. If you are using anything else, check that the server fully understands the HTTP 1.1 protocol. Otherwise, you will have problems with excessive network communications. As an example recommendation, it you plan on using Mono, then use mod_mono with Apache, and not just XSP. Although XSP (1.0.9) is a promising web server, it is not quite ready for prime time, at least at the time of this writing.

Documento similar