Frontend Learning HTTP entities and encodings

Source: Internet
Author: User
Tags ranges response code website server truncated

Previous words

Every day a variety of media objects are transmitted via http, such as text, movies, and software Programs. HTTP ensures that its messages are correctly transmitted, identified, extracted, and properly processed. To achieve these goals, HTTP uses a sophisticated label to describe the entity that hosts the Content. This article will detail the entity and encoding of HTTP

Entity Introduction

If an HTTP message is imagined as a box in an Internet freight system, the HTTP entity is the actual cargo in the Message. Shows a simple entity that is installed in an HTTP response message

The entity header indicates that this is a plain text document (content-type:text/plain), which is only 18 bytes long (content-length:18). As always, a blank line (CRLF) separates the header field from the beginning of the body

The HTTP entity header describes the contents of the HTTP Message. The following 10 basic font header fields are defined in version http/1.1

The         type of the host object in the Content-type entity content-length the        length or size of the transferred entity body Content-language  any transformation (for example, Compression) of the language Content-encoding object data that best matches the transferred object Content-location  an alternate location,   The object can be obtained by the request Content-range  If this is a partial entity, this header indicates which part of the whole is CONTENT-MD5              the checksum of the entity body content last-modified        Date time that the transferred content was created on the server or last modified expires                  Entity data is to be invalidated for the                various request methods allowed by the resource, such as Get and Headetag                The unique verification code for a specific instance of this document Cache-control        indicates how the document should be cached        

Note The ETag and Cache-control headers are not formally defined as entity headers, but it is important for many operations involving entities

"entity body"

The entity body is the original GOODS. Any other descriptive information is included in the Header. Because the goods (I.E. entity bodies) are only raw data, the entity header is required to describe the meaning of the Data. For example, the Content-type entity header tells us how to interpret the data (image or text, etc.), while the content-encoding entity header tells us whether the data has been compressed or re-encoded

The header field ends with a blank CRLF line, followed by the original content of the entity Body. No matter what the content is, text or binary, document or image, compressed or uncompressed, english, French or japanese, followed by this CRLF

Shows an example of two actual HTTP Messages. One is carrying a text entity, and the other is the image Entity. The hexadecimal value shows the actual contents of the Message.

In Figure a, the entity body starts with the 65th byte, followed by the CRLF at the end of the Header. The entity body contains the hi! I ' m a message! " ASCII encoded characters of this sentence

In Figure b, the entity body starts with the 67th Byte. The entity body contains the binary contents of a GIF format image. GIF files start with a 6-byte version flag, followed by a 16-bit width and a 16-bit height, and can be seen directly in the entity body of these 3 items

Entity size

The Content-length header indicates the byte size of the entity body in the Message. This size is inclusive of all content encodings. For example, If the text file is gzip compressed, the content-length header is the size of the compression, not the original size

Unless a block code is used, the Content-length header is a message that must be used with the entity Body. The content-length header is used to detect packet truncation caused by a server crash and to correctly segment multiple messages that share a persistent connection

Earlier versions of HTTP used a closed connection method to delimit the end of a message. however, without content-length, the client cannot differentiate between whether a normal connection is closed at the end of the message, or whether the connection is closed due to a server crash in the message Transmission. Client needs to detect message truncation via Content-length

The problem of message truncation is particularly serious for cache proxy Servers. If the cache server receives a truncated message and does not recognize the truncation, it may store incomplete content and use it multiple times to provide the Service. Cache proxy servers typically do not cache the HTTP principals that do not have an explicit content-length header to reduce the risk of cache truncated messages

The wrong content-length is worse than the lack of content-length. Because some of the earlier clients and servers have some well-known errors in content-length computing, some clients, servers, and proxies contain special algorithms to detect and correct interactions with defective servers. http/1.1 Specifies that the user agent agent should notify the user when it receives and detects an invalid length

The Content-length header is essential for persistent connections. If the response is routed through a persistent connection, there may be another HTTP response immediately following it. The client can know where the message ends and where the next message begins with the Content-length header. Because the connection is persistent, the client cannot rely on the connection shutdown to determine the end of the Message. If there is no content-length header, the HTTP application does not know where an entity principal ends and where the next message begins

There is a case where a persistent connection can be used without the Content-length header, when chunked encoding (chunked Encoding) is Used. In the case of block coding, the data is divided into a series of blocks to send, each block has a size Description. Even if the server does not know the size of the entire entity when it generates the header (usually because the entity is dynamically generated), you can still transfer several known-sized blocks using chunked encoding

HTTP allows you to encode the contents of an entity body, such as making it more secure or compressed to save Space. If the subject is content encoded, The Content-length header describes the length of the bytes of the body after encoding (encoded), not the length of the original body that is not encoded

Some HTTP applications are mistaken in this regard, sending the size of the data before encoding, which can lead to serious errors, especially on persistent connections. unfortunately, there is no header in the http/1.1 specification to illustrate the length of the original, non-encoded body, which makes it difficult for the client to verify the integrity of the decoding process

"determine the rule"

The rules listed below explain how to correctly calculate the length and end position of a body in several different situations. These rules should be applied in order, and whoever matches first

1, if the specific HTTP message type is not allowed with the main body, it ignores the content-length header, it is not actually sent out of the body to Calculate. In this case, the content-length header is suggestive and does not indicate the actual body length

The most important example is the head response. The Head method requests the server to send the header that appears in the equivalent get request, but does not include the Principal. Because the response to get has a content-length header, there is also a head response, but unlike the get response, there is no body in the head response. 1XX, 204, and 304 responses can also have a content-length header, but neither has an entity Body. Those messages that do not have an entity principal, regardless of the header field, must terminate at the first empty line after the header

2. If the message contains a transfer-encoding header that describes the transmission code (not using the default HTTP "identity" encoding), The entity should end with a special mode called a "0-byte block" (zero-byte chunk), unless the message has ended because the connection is closed

3, If the message contains Content-length header (and The message type allows the entity body), and there is no non-identical transfer-encoding header field, Then the value of Content-length is the length of the Body. If the received message has both a Content-length header field and a non-identical transfer-encoding header field, The content-length must be ignored because the transfer encoding alters the representation and transmission of the entity body ( So it may change the number of bytes Transferred)

4, If the message uses the Multipart/byteranges (multipart/byte Range) media type, and does not use the Content-length header to indicate the length of the entity body, then each part of the multipart message should explain its own size. This multipart type is the only type of entity principal that is self-bounding, so the media type cannot be sent unless the sender knows that the receiver can parse it

5. If the above rules do not match, the entity ends when the connection is Closed. In fact, only the server can use the connection shutdown to indicate the end of the Message. The client cannot use the close connection to indicate the end of the client message, because this will cause the server to fail to send back the response

For compatibility with applications that use http/1.0, any http/1.1 request with entity principals must have the correct Content-length header field (unless The server is already known to be compatible with Http/1.1)

The http/1.1 specification recommends that for a request with a subject but no content-length header, the server should send a response to the 411 or the length required response if the message cannot be Determined. The latter scenario indicates that the server requires the correct content-length header to be received

Entity Summary

Although HTTP is usually implemented on the basis of a reliable transport protocol such as tcp/ip, there are still a number of factors that can cause a part of the message to be modified during transmission, such as an incompatible transcoding agent, an intermediary agent error, and so On. In order to detect whether the data of the entity principal is inadvertently modified, the sender can generate a checksum of the data when generating the initial principal, so that the receiver can capture all unexpected entity modifications by examining the Checksum.

The server uses the CONTENT-MD5 header to send the result of running the MD5 algorithm on the entity Body. Only the original server that generated the response can calculate and send the CONTENT-MD5 header. Intermediate proxies and caches should not modify or add this header, otherwise they will conflict with this ultimate purpose of verifying end-to-end Integrity. The CONTENT-MD5 header is calculated after the content has been encoded in all the required content and has not yet been done with any transmission encoding. In order to verify the integrity of the message, the client must first decode the transmission encoding, and then compute the resulting MD5 of the entity body without transmitting the encoding

If a document is compressed using the GZIP algorithm and then sent using chunked encoding, then the entire gzip-compressed body is MD5 calculated

In addition to the integrity of the inspection messages, MD5 can also be used as a hash-list keyword for quickly locating documents and eliminating unnecessary duplication of content Storage. In addition to these possible usages, it is generally not commonly used to CONTENT-MD5 header

As an extension to http, other summary algorithms are presented in the IETF DRAFT. These extensions suggest adding a new Want-digest header, which allows the client to describe the type of digest used in the expected response, and uses quality values to suggest a variety of digest algorithms and describe the order of precedence

Media type

The Content-type header field describes the MIME type of the entity Body. MIME types are standardized names that describe the basic media types (such as HTML files, Microsoft Word documents, or MPEG VIDEOS) that are used as the cargo carrier Entities. Client applications use MIME types to interpret and manipulate their content

The value of Content-type is a normalized MIME type that is registered in the Internet Number distribution Authority (internet Assigned Numbers authority, iana). A MIME type consists of a main media type (such as text, image, or Audio) followed by a slash and a subtype, which is used to further describe the media type

[note] to access the full MIME media type registration list, please go to this

Some of the MIME types commonly used in Content-type headers are listed in the following table

Media type            Description text/html entity body is an             HTML document Text/plain            entity body is a plain text document Image/gif             entity body is an image in GIF format image/jpeg        The entity body is a jpeg-formatted image audio/x-wav    entity Body contains wav-formatted sound data MODEL/VRML    entity body is a three-dimensional VRML model Applicaiion/vnd.ms-powerpoint  The entity body is a Microsoft PowerPoint presentation document Multipart/byteranges    entity body has several sections, each containing a different byte range in the complete document message/http the    entity Body contains Full HTTP message (see Trace)        

It is important to note that the Content-type header describes the media type of the original entity Body. If the entity is content encoded, the Content-type header is still the type of the entity body before the encoding

The Content-type header also supports optional parameters to further describe the type of Content. The charset (character set) parameter is an example of how to convert a bit in an entity to a character in a text file:

content-type:text/html; Charset=iso-8859-4

The multipart (multipart) e-mail message in MIME contains multiple messages, which together are sent as a single, complex message. Each part is independent and has its own set of content that describes it, and the different parts are concatenated with a delimited string

HTTP also supports multi-part principals. however, It is usually used only in one of the following two situations: to submit a completed form, or as a range response that hosts several document fragments

"multi-part form submission"

When you submit a completed HTTP table, the variable length text fields and uploaded objects are sent as separate parts of the multi-part body, so that you can fill in the various types and lengths of values in the Table. For example, you might choose to use a nickname and a small photo to fill out a form that asks for your name and introductory information, and your friend may have filled out her full name and complained about a bunch of public car repair problems in the introductory information sheet.

HTTP uses headers such as Content-type:multipart/form-data or content-type:multipart/mixed and multi-part principals to send such requests, for example:

content-type:multipart/form-data;boundary=[abcdefghijklmnopqrstuvwxyz]

The boundary parameter describes the string used to split the different parts of the body

The following example shows the Multipart/form-data encoding. Let's say we have a table like This:

<form action= "http://server.com/cgi/handle" enctype= "multipart/form-data" method= "post" ><p>what is your Name?<input type= "text" name= "submit-name" ><br> What files is you sending?<input type= "file" name= " Files "></p><input type=" submit "value=" Send "><input type=" Reset "></form>

If the user types Sally in the text entry field and selects the text file essayfile.txt, The user Agent agent may send back data such as the Following:

If the user also selected another (image) file imagefile.gif, the user Agent Agent might construct this section as Follows:

"multi-part Range response"

The HTTP response to a range request can also be multi-part. Such responses have content-type:multipart/byteranges headers and multi-part bodies with different scopes. Here is an example of a response to a request from a different range of documents:

Content encoding

HTTP applications sometimes need to encode content before they are Sent. For example, a server might compress a large HTML document before it is sent to a client over a slow connection, helping to reduce the time it takes to transfer the Entity. The server can also scramble or encrypt the content to prevent unauthorized third parties from seeing the contents of the document

This type of encoding is applied to the content on the sender Side. When content is encoded, the coded data is placed in the entity body and sent to the receiver as usual

"content encoding process"

The process of content encoding is described below

1, the website server generates the original response message, which has the original Content-type and Content-length header

2. The content encoding server (which may also be the original server or the downstream Agent) creates the encoded Message. The encoded message has the same content-type but the content-length may be different (for example, the subject is compressed). The content encoding server adds the content-encoding header to the encoded message so that the receiving application can decode the

3. The receiving program obtains the encoded message, decodes it, and obtains the original Message.

Gives an example of content coding

In this example, after the HTML page is processed by the gzip content-encoding function, a smaller, compressed body is Obtained. After the network is sent is the compression of the body, and the gzip compression marked. The received client uses the GZIP decoder to decompress the entity

The response fragment given below is another example of a coded response (a compressed image):

http/1.1 okdate:fri, 22:35:15 gmtserver:apache/1.2.4content-length:6096content-type:image/gifcontent- encoding:gzip[...]

Note that the Content-type header can and should also appear in the Message. It describes the original format of the entity, and once the entity is decoded, it may be necessary to display the Information. remember, the Content-length header now represents the length of the body after the Encoding.

"content encoding type"

HTTP defines a number of standard content encoding types, and allows additional encodings to be added in the form of extended encodings. The code is standardized by the Internet Number distribution agency (IANA), which assigns a unique code name to each content encoding Algorithm. The content-encoding header uses these standardized codes to illustrate the algorithm used in the Encoding.

The following table lists some of the commonly used content encoding codes

Content-encoding value       Description gzip                   indicates that the entity uses the GNU ZIP code compress               to indicate that the entity is using Unix's file compression program deflate                An identity that indicates that the entity is compressed in zlib format               indicates that the entity is not encoded. When there is no content-encoding header, it is implied that the situation

gzip, compress, and deflate codes are lossless compression algorithms that reduce the size of transmitted messages without causing loss of Information. Of these algorithms, gzip is usually the most efficient and the most widely used

"accept-encoding header"

There is no doubt that we do not want the server to encode the content in a way that the client cannot Decode. To prevent the server from using encoding that is not supported by the client, the client places a list of its supported content encodings in the requested accept-encoding Header. If the HTTP request does not contain the accept-encoding header, the server can assume that the client can accept any encoding (equivalent to sending accept-encoding:*)

displaying the accept-encoding header in an HTTP transaction

The Accept-encoding field contains a comma-delimited list of supported encodings, and here are some examples

accept-encoding:compress, gzipaccept-encoding: *accept-encoding:compress;q=0.5, gzip; q=1.0 accept-encoding:gzip;q=l.0, identity; q=0.5, *;q=0

The client can give each encoding a Q (quality parameter to indicate the priority of the encoding). The Q value ranges from 0.0 to 1.0, and 0.0 indicates that the client does not want to accept the encoded code, and 1.0 indicates the encoding that is most desirable to Use. "*" means "any other method". Deciding what to echo back to the client is a more general process, and choosing what content encoding to use is part of the process

The identity code designator can only appear in the accept-encoding header, which the client uses to describe the priority relative to other content encoding algorithms

Transfer encoding

Content coding is a reversible transformation of the main body of a message. Content encoding is closely related to the specific format details of the Content. For example, you might use gzip to compress text files, but not JPEG files, because JPEG is not good enough to compress with gzip

Transfer encodings are also reversible transformations that act on entity bodies, but they are used for architectural reasons, regardless of the format of the Content. Transfer encoding is used to change the way data in a message travels over the network

"reliable transmission"

In other protocols, transmission codes have long been used to ensure that messages are "reliably transmitted" over the Network. In the HTTP protocol, the focus of reliable transmission is different because the underlying transport infrastructure is standardized and fault-tolerant is Better. In http, in only a few cases, the transmitted message body may cause problems, two of which are described below

1, The Unknown size

Some gateway applications and content encoders will not be able to determine the final size of the message body if the content is not Mr. typically, These servers want to start transmitting data before they know the Size. Because the HTTP protocol requires the CONTENT-LENGTH header to be preceded by data, some servers use transport encoding to send the data, and the end of the data is indicated by a special end footnote

2. security

You can use transmission encoding to disrupt the contents of a message and then send it on a shared transport Network. however, because of the prevalence of transport Layer security systems like ssl, it is seldom necessary to rely on transmission encoding for Security.

"transfer-encoding header"

The HTTP protocol only defines the following two headers to describe and control the transfer encoding

Transfer-encoding         tells the receiver what encoding it has been given in order to reliably transmit the message Te                        used in the request header to tell the server which transport encoding to use to extend

In the following example, the request uses the TE header to tell the server that it can accept chunked encoding (which is necessary if it is a http/1.1 application) and is willing to accept a trailer attached to the end of a chunked encoded message:

get/new_products-html http/1.1host:www.joes-hardware.comuser-agent:mozilla/4.61 [en] (WinNT; I) te:trailers, chunked

The response to it contains the transfer-encoding header, which tells the receiver that the message has been encoded using chunked encoding:

http/1.1 oktransfer-encoding:chunkedserver:apache/3.0

After this initial header, the structure of the message will Change.

The value of the transfer encoding is Case-independent. The http/1.1 specifies that the transfer encoding values be used in the TE header and transfer-encoding header. The latest HTTP specification defines only one transfer encoding, which is the chunked encoding

Similar to the accept-encoding header, the TE header can also use the Q value to indicate the priority of the transmission encoding. however, the Q value associated with the block encoding is not set to 0.0 in the http/1.1 specification

Future extensions of HTTP may drive the need for more transmission encodings. If that's the case, then the chunk coding should still be on top of the other transmission encodings, so that the data can "penetrate" the http/1.1 applications that only understand the chunked encoding but do not understand the other transfer encodings.

"chunked encoding"

The chunked code divides the message into several known chunks of size. The blocks are sent next to each other, so that you don't need to know the size of the entire message before sending it.

It is important to note that chunked encoding is a transmission encoding and therefore a property of the message, not the principal

1, chunked and persistent connection

If the client and server are not persistent, the client does not need to know the length of the principal it is reading, but only to read until the server shuts down the principal Connection.

When using a persistent connection, you must know its size and send it in the Content-length header before the server writes the Principal. If the server dynamically creates content, it may not be able to know the length of the body before sending

chunked Coding provides a solution to this difficulty, as long as the server is allowed to send the principal block by chunk, indicating the size of each BLOCK. Because the principal is created dynamically, the server can buffer its part, send its size and the corresponding block, and then repeat the process before the body sends it OUT. The server can use a block of size 0 as a signal to end the body so that it can continue to stay connected and prepare for the next response

chunked coding is quite simple and shows the basic structure of a block coded message. It starts with the HTTP response header block, followed by a series of blocks. Each tile contains a length value and the data for that Tile. The length value is in hexadecimal form and separates the CRLF from the Data. The size of the data in the chunking is calculated in bytes, excluding the CRLF sequence between the length value and the data and the CRLF sequence at the end of the Block. The last block is a bit special, it has a length value of 0, which means "body end."

The client can also send chunked data to the Server. Because the client does not know beforehand whether the server accepts chunked encoding (this is because the server does not send the TE header in response to the client), the client must be ready for the server to reject block requests with 411 Length Required (requires a content-length header) response

2. Trailer for chunked messages

If the Client's te header says it can accept a trailer, it can add a trailer at the end of the chunked Message. The server that produces the original response can also add a trailer at the end of the chunked Message. Trailer content is optional no data, clients do not necessarily need to understand and use, the client can ignore and discard the content in the trailer

Trailers can contain the accompanying header fields whose values may not be determined at the beginning of the message (for example, the contents of the principal must be generated first). The CONTENT-MD5 header is a header that can be sent in a trailer because it is difficult to figure out the MD5 of a document before it is Generated. Shows how the trailer is Used. The header of the message contains a trailer header, which lists the first list followed by the Message. The header listed in the trailer header is immediately after the last block

In addition to transfer-encoding, trailer, and Content-length headers, Other HTTP headers can be sent as trailers

Content encoding and transmission encoding can be used at the same time. For example, shows how the sender compresses an HTML file with content encoding and then sends it using a transport-encoded chunk. Receiver "refactor" The body of the process and the sender opposite

"rules for Transport encoding"

When using transmission encoding for a message body, the following rules must be observed: "chunking" must be included in the transport encoding Collection. The only exception is to end the message with a closed connection, which must be the last action on the message body when using the chunked transfer encoding, and the chunked transmission encoding cannot be used on a message body multiple Times. These rules allow the receiver to determine the transmission length of the Message.

The transfer encoding is a relatively new feature introduced in version HTTP1.1. The server that implements the transfer encoding must pay particular attention to not sending the transmitted encoded message to the non-http/1.1 Application. similarly, If the server receives an incomprehensible transmitted encoded message, it should use the 501 unimplemented status code to REPLY. however, at a minimum, all http/1.1 applications must support chunked encoding

Instance manipulation

Site objects are not static. The same URLs point to different versions of the objects as they change over Time. On Cnn's homepage, for example, multiple visits to http://www.cnn.com on the same day may have a slightly different return page each Time.

You can think of Cnn's homepage as an object, and its different versions can be seen as different instances of this Object. in, the client requests the same resource (URL) multiple times, but gets a different instance of the resource because it changes over Time. Have the same instance at time (a) and time (b), and in time (c) are different instances

The HTTP protocol specifies a series of request and response operations called instance manipulation (instance Manipulations) to manipulate an instance of an Object. The two main instance manipulation methods are range request and differential encoding. Both of these methods require the client to identify a specific copy of the resource it owns (if any), and to request a new instance under certain conditions

"freshness"

Now again, the client does not initially have a copy of the resource, so it sends a request to the server to get One. The server responds with version 1 of the Resource. The client can now cache this copy, but how long will it be cached?

When a document is "expired" by the client (that is, the client no longer considers the copy to be valid), the client must request a new copy from the Server. however, if the document does not change on the server, the client does not need to receive it again--continue to use the cached copy

This special request, called a conditional request, requires the client to use a verification code (validator) to inform the server of the version number it currently owns and to request a new copy only if its current copy is no longer valid conditional

The server should tell the client how long it will be able to cache the content, which is fresh within this time. The server can use one of these two headers to provide this information: Expires (expired) and Cache-control (cache Control)

Expires the first time the document is "out of Date"-and should not be considered up-to-date thereafter. The syntax for the expires header is as Follows:

Expires:sun Mar 23:59:59 GMT 2016

The client and server must synchronize their clocks in order to properly use the expires Header. This is not always easy because they may not run a clock synchronization protocol like Networktimeprotocol (network time protocol, NTP). It is more useful to define outdated mechanisms with relative time. The Cache-control header can specify the maximum lifetime of a document in Seconds-the total time from the date the document leaves the Server. The usage period is not synchronized with the clock, so more accurate results can be given

In fact, the Cache-control first feature is very powerful. Both the server and the client can use it to describe freshness, and many instructions are available, in addition to the usage period or expiration Time. The following table lists some instructions for the Cache-control header

"verification code"

When a replica in the cache server is requested, if it is no longer fresh, the cache server needs to ensure that it has a fresh copy. The cache server can obtain the current copy from the original server. In many cases, however, the document on the original server is still the same as the expired copy in the Cache. The cached copy may have expired, but the content on the original server is still the same as the cached Content. If the document on the server is the same as the cached copy that has expired, and the cache server is still fetching documents from the original server, then the cache server is wasting network bandwidth, adding unnecessary load to the cache server and the original server, slowing everything Down.

To avoid this situation, HTTP provides a way for the client to request a copy only when the resource changes, a special request called a conditional request. A conditional request is a standard HTTP request message, but executes only if a certain condition is True. For example, a cache server might send the following conditional get message to the server and send it only if the file/announce.html has changed since June 29, 2016 (the time the cached document was last modified by the author):

get/announce.html http/1.0if-modified-since:sat, june, 14:30:00 GMT

A conditional request is implemented by a conditional header that begins with "if-". In the above example, the conditional header is if-modified-since (if-from ...). Later-modified). The conditional header causes the method to execute only if the condition is True. If the condition is not met, the server sends back an HTTP error code

Each conditional request works with a specific verification code. A verification code is a special property of a document instance that is used to test whether the condition is True. conceptually, you can think of a verification code as a File's serial number, version number, or a date and time when the last change Occurred.

The conditional header if-modified-since tests the date time that the document instance was last modified, so we say the last modified date time is the verification code. The conditional header If-none-match tests the ETag value of the document, which is a special keyword associated with the entity, or a version-aware tag. The last-modified and ETag are the two main types of authentication codes used by HTTP. The 4 HTTP headers for conditional requests are listed in the following Table. Each conditional header is followed by the type of verification code used for this header

HTTP divides the verification code into two categories: the weak authentication code (weak Validators) and the strong verification code (strong validators). A weak captcha does not necessarily uniquely identify an instance of a resource, which must be the case for strong authentication Codes. An example of a weak verification code is the size of the object in Bytes. It is possible that the content of the resource has changed, and the size remains the same, so the hypothetical byte count verification code is weakly correlated with the Change. The cryptographic checksum of the resource content (for example, MD5) is a strong verification code that always changes as the document changes

The last modification time is treated as a weak verification code, because although it illustrates the last time the resource was modified, it has a maximum accuracy of 1 seconds. Because the resource can be changed many times in 1 seconds, and the server can process thousands of requests per second, the last modification date time does not always reflect the change Situation. The ETag header is treated as a strong verification code because the server can place different values at the ETag header each time the resource content Changes. The version number and digest checksum are also good ETag header candidates, but they cannot have arbitrary text. The ETag header is flexible enough to carry arbitrary text values (in the form of TOKENS) that can be used to design a variety of client and server validation policies

sometimes, clients and servers may need to take a less precise approach to entity tag Validation. For example, a server might want to embellish a large, widely cached document, but not to generate significant traffic when the cache server is Re-authenticated. In this case, the server can precede the tag with a "w/" prefix to broadcast a "weak" entity Tag. For weak entity tags, The markup changes only when the associated entity has a significant semantic change. A strong entity tag is bound to change regardless of the nature of the associated Entity.

The following example shows how the client can request re-authentication to the server with a weak entity Token. The server returns the principal only when the contents of the document have changed significantly from version 4.0:

Get/announce.html http/1.1if-none-match:w/"v4.0"

When a client accesses the same resource multiple times, it first needs to determine if its current copy is still Fresh. If they are no longer fresh, they must obtain the latest version from the Server. To avoid receiving an identical copy of the resource without changes, the client can send a conditional request to the server stating the verification code that uniquely identifies the current copy of the Client. The server sends its copy only if the copy of the resource and the client is different

"scope request"

It has been clearly explained how the client requires the server to send its copy only if the client copy of the resource is no longer valid. HTTP also adds to the icing on the cake: it allows the client to actually request only part of the document, or a range

Assuming that the latest hot software is being downloaded over a slow modem connection, it's three-fourths, and suddenly the connection is interrupted because of a network failure. You've been waiting a long time for the download to finish, and now you're forced to start all over again, praying that it won't happen again.

With a scope request, the HTTP client can resume downloading the entity by requesting a range (or part of It) of the entity that Failed. There is, of course, a premise that the object has not changed since the last time the client requested the entity to the time of the request to make the Range.

get/bigfile.html http/1.1host:www.joes-hardware.com range:bytes=4000-user-agent:mozilla/4.61 [en] (WinNT; I)

In this case, the client is requesting a portion of the document that begins at the beginning of the 4000 bytes (no end-byte count is given, because the requester may not know the size of the document). This form of scope request can be used in cases where the client has failed after receiving the beginning of 4000 Bytes. You can also use the range header to request multiple ranges that can be given in any order or overlap each other.

For example, Suppose a client connects to multiple servers at the same time, downloading different parts of the same document from different servers in order to speed up downloading Documents. For cases where the client requests several different scopes within a request, the returned response is a single entity with a multipart body and a content-type:multipart/byteranges header

Not all servers accept range requests, but many servers Can. The server can explain to the client the acceptable range request by including the Accept-ranges header in the Response. The value of this header is the unit of the calculated range, usually in Bytes. For example:

http/1.1 0kdate:fri, 22:35:15 gmtserver:apache/1.2.4accept-ranges:bytes

The range header is widely used in popular point-to-point (peer-to-peer,p2p) file sharing client software, which simultaneously downloads different parts of a multimedia file from different peer entities

Note that scope requests also belong to a class of instance manipulation, because they exchange information between the client and the server for a particular object instance. That is, the Client's scope request only makes sense if the client and the server have the same version of the document

"diff code"

We have seen different versions of our site pages as different instances of the Page. If the client has an expired copy of a page, it is requested to request the most recent instance of the Page. If the server has an instance of the page update, it should be sent to the client, even if only a small part of the page has changed, but also to the full new page instance to the client

If there is less change, instead of sending a complete new page to the client, the client would prefer the server to send only the part of the page that changed, so that the most recent page can be obtained faster. Differential encoding is an extension of the HTTP protocol that optimizes transmission performance by exchanging portions of objects that change instead of the complete object. The differential encoding is also a class of instance manipulation because it relies on exchanging information between the client and server for a particular object instance. RFC 3229 describes the differential encoding

Clearly shows the structure of the differential coding, including the entire process of requesting, generating, receiving, and assembling Documents. The client must tell the server which version of the page it has, and it is willing to accept the difference (delta) of the latest version of the page, which knows which algorithms apply the differences to the existing Version. The server must check if it has an existing version of the client for this page, calculate the difference between the Client's existing version and the latest version (there are several algorithms that can calculate the difference between the two objects). The server must then calculate the difference, send it to the client, tell the client that the difference is being sent, and describe the new identity (ETag) of the latest version of the page, because the client will get this version after applying the difference to its old version

The client uses the unique identity of the page version it holds in the If-none-match header, which is sent in the ETag header before the server responds to the Client. The client is saying to the server: "if you have the most recent version of the page and this etag is different, send me the latest version of this page." "if only the If-none-match header is available, the server will send the latest version of the page to the client completely." (assuming that the latest version differs from the version that the client Holds)

however, If the client wants to tell the server that it is willing to accept the differences of the page, just send the A-im header. A-im is an abbreviation for accept-instance-manipulation (accepting instance manipulation). Image analogy, The client is equivalent to saying: "oh yes, I can accept some form of instance manipulation, if you will one of them, you do not have to send the full document to me." "in the A-im header, the client will show that it knows which algorithms can apply the difference to the old version and get the latest version." The server sends back the Following: a special response code--226 im used tells the client that it is sending an instance manipulation of the requested object, not the complete object itself; an IM (instance-manipulation abbreviation) header, Describes the algorithm used to calculate the variance, the new ETag header and the delta-base header, stating the etag of the baseline document used to calculate the variance (theoretically, it should be the same as the ETag in the If-none-match header in the Client's previous Request)

The following table summarizes the header used by the differential encoding

The client can use the A-im header to describe the types of instances that can be manipulated. The server describes what instance manipulation is used in the IM Header. But in the end, which instance manipulation types are acceptable? And what do they do? Some of the instance manipulation types registered in the IANA are listed in the following table

, the difference generator on the server side calculates the difference between the baseline document and the most recent instance of the document, using the algorithm specified by the client in the A-im header. The difference app on the client side is different, applies it to the baseline document, and gets the latest instance of the Document. For example, if the algorithm that makes the difference is the DIFF-E command of the UNIX system, the client can apply the difference using the functionality provided by the text editor in the Unix system, because diff-e <file1> <file2> produces a series of ED commands to <file1> conversion to <file2>. Ed is a very simple editor that supports a number of COMMANDS. example, 5c indicates that you want to delete the 5th line of the baseline document, and chisels.<cr>. description to add chisels., it's that Simple. For larger changes, more complex instructions are Generated. The DIFF-E algorithm for UNIX systems is to compare files on a line-by-row basis, which is not a problem for text files, but is not suitable for binary files. Vcdiff algorithm is more powerful, for non-text files are also applicable, and the resulting difference is smaller than the DIFF-E

The format of the a-im and IM headers is defined in detail in the specification of the differential encoding. here, as long as we know that these headers can show that multiple instance manipulations (and can have associated mass values) are sufficient, the document can be manipulated by multiple instances before being returned to the client, so that the maximum amount of compression can be achieved. For example, the differences resulting from the VCDIFF algorithm can then be compressed using the gzip algorithm. The Server's response contains the Im:vcdiff,gzip Header. The client should first gunzip the content and then apply the resulting differences to its own baseline page in order to generate the final Document

Differential coding can reduce the number of transfers, but can be cumbersome to Implement. Imagine the page changes frequently, and there are many different people who are accessing the Situation. A server that supports differential encoding must save all different versions of the page over time to indicate the difference between the latest version and any version held by the requested client

If documents change frequently, and many clients are requesting documents, they get different instances of the Document. They then request the difference between the version they hold and the latest version when they then make a request to the Server. In order to be able to send only the changed parts to them, the server must keep all the versions that the client has ever held

To reduce the delay time when the document is submitted, the server must increase disk space to hold various old instances of the Document. The additional disk space required to implement the differential encoding may quickly offset the benefits of reduced throughput

Frontend Learning HTTP entities and encodings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.