GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing developer.mozilla.org — HTTP conditional requests - HTTP | MDN HTTP conditional requestsHTTP has a concept of conditional requests, where the result, and even the success of a request, can be controlled by comparing the affected resources with a validator. These requests are useful for validating cached content, ensuring that it is only fetched if it differs from the copy that is already available to the browser. Conditional requests are also useful for ensuring the integrity of a document when resuming a download, or preventing lost updates when uploading or modifying a document on the server. PrinciplesHTTP conditional requests are requests that are executed differently, depending on the value of specific headers. These headers define a precondition, and the result of the request will be different if the precondition is matched or not. The different behaviors are defined by the method of the request used, and by the set of headers used for a precondition:
ValidatorsAll conditional headers try to check if the resource stored on the server matches a specific version. To achieve this, the conditional requests need to indicate the version of the resource. As comparing the whole resource byte to byte is impracticable, and not always what is wanted, the request transmits a value describing the version. Such values are called validators, and are of two kinds:
Comparing versions of the same resource is a bit tricky: depending on the context, there are two kinds of equality checks:
The kind of validation is independent of the validator used. Both Strong validationStrong validation consists of guaranteeing that the resource is, byte to byte, identical to the one it is compared to. This is mandatory for some conditional headers, and the default for the others. Strong validation is very strict and may be difficult to guarantee at the server level, but it does guarantee no data loss at any time, sometimes at the expense of performance. It is quite difficult to have a unique identifier for strong validation with Note:
Because a change of content encoding requires a change to an ETag, some servers modify ETags when compressing responses from an origin server (reverse proxies, for example).
Apache Server appends the name of the compression method ( Weak validationWeak validation differs from strong validation, as it considers two versions of the document as identical if the content is equivalent. For example, a page that would differ from another only by a different date in its footer, or different advertising, would be considered identical to the other with weak validation. These same two versions are considered different when using strong validation. Building a system of ETags that uses weak validation is very useful for optimizing cache performance, but may be complex, as it involves knowing the importance of the different elements of a page. Conditional headersSeveral HTTP headers, called conditional headers, lead to conditional requests. These are:
Use casesCache updateThe most common use case for conditional requests is updating a cache. With an empty cache, or without a cache, the requested resource is sent back with a status of Together with the resource, the validators are sent in the headers. In this example, both As long as the cache is not stale, no requests are issued at all. But once it has become stale, this is mostly controlled by the If the resource has not changed, the server sends back a If the resource has changed, the server just sends back a Besides the setting of the validators on the server side, this mechanism is transparent: all browsers manage a cache and send such conditional requests without any special work to be done by Web developers. Integrity of a partial downloadPartial downloading of files is a functionality of HTTP that allows resuming previous operations, saving bandwidth and time, by keeping the already obtained information: A server supporting partial downloads broadcasts this by sending the The principle is simple, but there is one potential problem: if the downloaded resource has been modified between both downloads, the obtained ranges will correspond to two different versions of the resource, and the final document will be corrupted. To prevent this, conditional requests are used. For ranges, there are two ways of doing this. The more flexible one makes use of Even if this method works, it adds an extra response/request exchange when the document has been changed. This impairs performance, and HTTP has a specific header to avoid this scenario: This solution is more efficient, but slightly less flexible, as only one ETag can be used in the condition. Rarely is such additional flexibility needed. Avoiding the lost update problem with optimistic lockingA common operation in Web applications is to update a remote document. This is very common in any file system or source control applications, but any application that allows you to store remote resources needs such a mechanism. Common websites, like wikis and other CMS, have such a need. With the Unfortunately, things get a little inaccurate as soon as we take into account concurrency. While a client is locally modifying its new copy of the resource, a second client can fetch the same resource and do the same on its copy. What happens next is very unfortunate: when they commit back to the server, the modifications from the first client are discarded by the next client push, as this second client is unaware of the first client's changes to the resource. The decision on who wins is not communicated to the other party. Which client's changes are to be kept, will vary with the speed they commit; this depends on the performance of the clients, of the server, and even of the human editing the document at the client. The winner will change from one time to the next. This is a race condition and leads to problematic behaviors, which are difficult to detect and to debug: There is no way to deal with this problem without annoying one of the two clients. However, lost updates and race conditions are to be avoided. We want predictable results, and expect that the clients are notified when their changes are rejected. Conditional requests allow implementing the optimistic locking algorithm (used by most wikis or source control systems). The concept is to allow all clients to get copies of the resource, then let them modify it locally, controlling concurrency by successfully allowing the first client to submit an update. All subsequent updates, based on the now obsolete version of the resource, are rejected: This is implemented using the Dealing with the first upload of a resourceThe first upload of a resource is an edge case of the previous. Like any update of a resource, it is subject to a race condition if two clients try to perform at similar times. To prevent this, conditional requests can be used: by adding
ConclusionConditional requests are a key feature of HTTP, and allow the building of efficient and complex applications. For caching or resuming downloads, the only work required for webmasters is to configure the server correctly; setting correct ETags in some environments can be tricky. Once achieved, the browser will serve the expected conditional requests. For locking mechanisms, it is the opposite: Web developers need to issue a request with the proper headers, while webmasters can mostly rely on the application to carry out the checks for them. In both cases it's clear, conditional requests are a fundamental feature behind the Web. See also
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |