Download source code - 48.1 KB
Introduction :
Static content such as HTML and text files, styles, and client scripts can be compressed to reduce network usage. This article shows how to use already compressed content for transmission over HTTP protocol.
Background
HTTP Request and Response
An HTTP session consists of pairs of requests and responses. Every request and response has a header block that contains metadata about the content. The headers can specify the content type, the encoding, and the cache parameters of the transmitted information. We are interested in the Accept-Encoding header of an HTTP request and the Content-Encoding header of an HTTP response. Most web servers do not set the Content-Encoding header, and the HTTP communication happens as shown on Figure 1. The requested content is transmitted as is.
To save network bandwidth, a web server can be configured to compress (encode) a requested content. Most web browsers and search engine bots support the DEFLATE compression encoding [1]. You may notice Accept-Encoding is set to "gzip,deflate" that passed from your browser to a web server. We are interested in the DEFLATE encoding.
The compression comes with a price: initial response delay and more computation power is required from a web server; that decreases the number of concurrent users the web server may serve at the same time. The problem can be solved by adding a compressed content cache (see Figure 2) or by using pre-compressed data.
The second way looks more attractive – it does not require any processing power from the web server; but, it requires more work upfront such as compression of the content. That gives an additional headache for web designers and content authors when they are trying to publish their content.
ZIP File Format
The ZIP is one of the widely used compression formats. A ZIP file contains multiple files that are compressed by numerous archival methods. The most used one is DEFLATE. The file structure can be presented as two parts, the compressed data and a directory. [2] The compressed data contains pairs of local file headers and compressed data, the directory contains additional file attributes and references to local file headers.
We can use the data that was compressed by DEFLATE or no-compression methods. The DEFLATE'd data can be send over HTTP without additional re-encoding – the data is already compressed. (See Figure 4.) The Content-Encoding header has to be set to "deflate" for the HTTP response to tell a web browser that the content is encoded.
Using the Code
The solution has two parts: a utility library and a web application. The utility library contains a configuration section, web cache, path rewrite module, and ZIP reader classes. The cache classes and path rewrite module can be used only within the web application context.
The web application contains baseline implementations of the HTTP handler (httpzip.ashx) that lists and delivers contents of the registered zip folder. The handler accepts three query string parameters:
name – refers to the registered ZIP archive;
action – list or get;
file – path of the file in the ZIP archive.
To get the rfc1951.txt file from the archive that is registered as deflate-rfcs, use the following URL:
http://servername/path/httpzip.ashx?name=deflate-rfcs&action=get&file=rfc1951.txt
The ZIP files and the rewrite module can be registered in the web.config file:
For convenience, the path rewrite HTTP module is included in the utility library. It can be registered in the web.config file:
To get the rfc1951.txt file from the archive with a prefix rfcs, use the following URL:
http://servername/path/rfcs/rfc1951.txt
READ MORE
Download source code - 48.1 KB
0 comments:
Post a Comment