This is the sixth article in our series on the Web, with more to come. Stay tuned! Let’s ride the waves together!

preface

HTTP packets are a communication mode used by the client and server to communicate with each other only when both sides can understand each other. TCP uses TCP packets to communicate. So what is the format of HTTP packets? Let’s find out! In this article, you’ll learn about:

  • Flow mode of packets
  • Component of a packet
  • Difference between request packets and response packets
  • Various functions of request messages
  • Meaning of the status code
  • What is the HTTP header used for

Understand the concept of packet flow

The transfer of HTTP packets between clients, servers, and proxies can be termed flow. The terms “incoming”, “outgoing”, “upstream”, and “downstream” are defined based on the flow and describe the direction of packets.

  • Incoming indicates that packets arrive from the client to the server. “Outflow” is the opposite of inflow. As you can see, these terms refer to servers.
  • Upstream and downstream: All packets, whether request packets or response packets, flow downstream. The sender of all packets is upstream of the receiver.

Format of the message

The first picture illustrates the basic format of HTTP messages.

/// Request packet format <method> space < request-URL > space <version> <headers> Empty line <entity-body> // Response packet format <version> space <status> space <reason-phrase> < headers > empty line < entity - body >Copy the code

The functions of each part are as follows:

  • The beginning and beginning lines are line-split ASCII text, each ending with a sequence of carriage return (\n) and a newline (\r). This sequence can be writtenCRLF.
  • method, the action that the client wants the server to perform.
  • request-url, the requested resource path.
  • version, HTTP version, for exampleHTTP / 1.0. Different versions have different features.
  • status, a three-digit number that describes the situation in the request process. Details will be introduced later.
  • reason-phrase, andstatusCorresponding is its description.
  • headers, can have no or more. Details will be introduced later.
  • entity-body, data block, body is optional, can be binary or text.

Let’s take a look at the methods, status codes, and headers.

methods

HTTP method list:

HTTP method describe
GET Tell the server that naming resources need to be sent from the server to the client
HEAD Send only the HTTP header in the named resource response
PUT Stores the client’s data in a named server resource
POST Send client data to a server application
TRACE Tracing a request
OPTIONS View the operations supported by the server for resources
DELETE Deletes resources from the server

Although HTTP specifies these methods, it is up to the server to determine whether it supports them.

  • GETHTTP/1.1 requires a way to achieve this.
  • HEAD, andGETThe method is similar, but the response message does not contain the body part. Using this feature, you can do this without actually obtaining the resource:
    • Determining resource Types
    • Check whether the corresponding resource exists
    • The HTTP/1.1 specification requires this method to be implemented, and for the same resource, the method response header should be andGETMethod returns the same.
  • PUT, andGETInstead, the request server creates a file at the specified location with the contents of the request body. If the corresponding resource exists, replace it.
  • POST, the client sends data to the server. Often used to submit forms.
  • TRACEAfter the client sends a request, it may pass through the intermediate gateway, proxy, etc. The original request may be modified and usedTRACEYou can see exactly what the request looks like when it finally reaches the server (the server contains the request it received in the body of the response message). Here is an example:

    TRACEUsually used to diagnose whether a request can reach the server without a body part.

  • OPTIONSTo view the methods supported by the server for a particular resource. This parameter is used in request packets*Instead of urls, this means viewing the server’s common approach to all resources.
  • DELETE, asking the server to delete the specified resource. Of course, whether to delete is up to the server.

HTTP extension methods

HTTP extension methods are methods that are not defined in the HTTP specification. For example, here are the methods in the WebDAV HTTP extension:

Extension method name describe
LOCK Tells the server to lock the specified resource to prevent others from making changes to it
MKCOL Allows users to create resources
COPY Allows users to copy resources
MOVE Mobile Server Resources

Status code

100 to 199 Informational status code

These status codes are relatively new and were introduced in HTTP/1.1. Here are the defined status codes:

Code The reason the phrase describe
100 Continue The server received the initial portion of the request, asking the client to continue.
101 Switching Protocols The server is changing the protocol to the one listed in the Update header as specified by the client.

For status code 100, the original design was that the client wanted to send an entity to the server, but wanted to see if the server would accept it before sending it. What you need to know:

  • For the client, if the client wants to send an entity and is willing to wait for the response with the server status code 100, the client request message should contain the value100 ContinuetheExpectThe first. Instead of waiting for the request to be sent, the client can send the entity after a certain amount of time.
  • For the server, if the server receives an include value of100 ContinueAt the beginning of the100 ContinueOr other corresponding error status code response. The server should not contain to no100 ContinueRequest response at header100 ContinueStatus code. If the server is responding100 ContinueThe state has previously received the entity part sent by the client and can skip the response100 ContinueBut in response to the final state.
  • For an agent, if the agent receives client contains100 ContinueBefore the agent knows that the next hop of the request only supports HTTP/1.0 (or earlier), it should respond417 Expectation Failed; When the proxy knows that the next hop supports HTTP/1.1 or has no clear status, it should forward the request. If the proxy forwards the upstream server response to the client knowing that the client does not support HTTP/1.1, it should not respond100 Continue.

200 to 299 Success status code

When the request is successful, the server returns a status code indicating success. The status code may differ for different request methods. The known success status codes are as follows:

Code The reason the phrase describe
200 OK The most common. The request succeeds, and the response body contains concrete data.
201 Created Respond to requests to create resources on the server, such as PUT. The server should ensure that the resource is created and include the resource URL in the response message.
202 Accepted The server has received the request, but no action has been taken.
203 Non-Authoritative Information The entity header (also known as meta information) contains information not from the server, but a copy of the resource. This can occur if there is a copy of a resource on an intermediate node, but it cannot or does not validate the resource-related meta-information it emits. Of course, this state is not necessary; if the entity header comes from the server, returning 200 is fine.
204 No Content The response packet contains no body part. It is used to update the browser without converting it to display a new document.
205 Reset Content Responsible for telling the browser to remove all HTML elements from the current page.
206 Partial Content A partial or Range request was successfully executed. The client can request files in a range specified in the header. The status response header must contain content-range, Date, and either ETag or Content-Location.

300 to 399 Redirection status code

List of known status codes:

Code The reason the phrase describe
300 Multiple Choices The client requests urls that actually point to multiple resources. The client can find a list of resources in the response.
301 Moved Permanently The requested URL has been removed. The Location header of the response contains the current Location.
302 Found Similar to 301, the client should use the temporary URL in the response this time and use the previous URL for future requests.
303 See Other Tell the client to use a different URL to get the resource. Its main purpose is to allow the response to a POST request to direct the client to a resource.
304 Not Modified If the client sends a conditional GET request but the resource is not modified, you can use this status code to indicate that the resource is not modified.
305 Use Proxy This resource must be accessed through a proxy, which is given in the Location header. It is important to note that when the client receives this state, it should not assume that all requests go through the broker.
306 Don’t use Not currently in use.
307 Temporary Redirect Same as 302.

Description of 302, 303 and 307 status codes: As shown in the table above, the three status codes have overlapping situations. In HTTP/1.0, there was only 302, and after the server wanted to respond to the 302 POST request, the client sent a GET request to the follow-directed URL. 303 and 307 are added in HTTP/1.1. When 303 is added, the browser still performs HTTP/1.0 302. 307, only instead of converting the original POST to GET, the user is asked. That’s all normative rhetoric, but that’s not what’s going on in practice. Did you see a lot of 307?

400 to 499 Client error status code

Sometimes the client sends something that the server can’t handle, causing an error. List of known status codes:

Code The reason the phrase describe
400 Bad Request Tell the client that it sent an incorrect request.
401 Unauthorized Returns with the appropriate header, telling the client to authenticate before requesting it.
402 Payment Required Reserve unused.
403 Forbidden The request was denied.
404 Not Found The server could not find the requested URL.
405 Method Not Allowed The client requested the URL using an unsupported method. Allow should be used in the header to inform the client of the correct method.
406 Not Acceptable The client is using the specified parameter to specify what type of entity it is willing to receive, but the server has no corresponding resource.
407 Proxy Authentication Required Proxy servers require client authentication.
408 Request Timeout The server can close the link if the client takes too long to complete the request.
409 Conflict The server thinks the request may cause a conflict. The response body should contain a description of the conflicting principal.
410 Gone This is similar to 404, except that the server once owned the resource and has since been removed.
411 Length Required The server requires that the request packet contain the Content-Length header.
412 Precondition Failed The client initiates a conditional request, and some of the conditions fail.
413 Request Entity Too Large The client sends a larger body than the server can hope to handle.
414 Request URI Too Long The URL is too long.
415 Unsupported Media Type The server could not understand or support the type of content sent by the client.
416 Requested Range Not Satisfiable The requested scope is invalid or cannot be satisfied.
417 Expectation Failed The request header contains Expect expectations, but the server cannot meet them.

500 to 599 Server error status code

The client sent a valid request, and the server itself failed. Here is a list of known status codes:

Code The reason the phrase describe
500 Internal Server Error The server encountered an error that prevented it from serving.
501 Not Implemented A client initiated a request that was beyond the server’s capabilities, for example using an unsupported method.
502 Bad Gateway Invalid gateway. Usually it is not the upstream server that is down, but the protocol used by the upstream server does not agree to exchange data.
503 Service Unavailable The server is temporarily unavailable. If the server knows when the service will be available, it can add a retry-after header to the response header.
504 Gateway Timeout Similar to 408, except that the response comes from a gateway or proxy that timed out while waiting for another server response to respond to its request.
505 HTTP Version Not Support The server received a request using a protocol version it did not support.

The first

The header and method work together to determine what the client and server can do. The types of headers are as follows:

  • Common header. Both request and response packets can be used. Including but not limited to:

    The first describe
    Connection The client and server specify links related options
    Date Date time indicates when the packet is created
    MIME-Version MIME version used by the sender
    Trailer If the packet is encoded in blocks, you can use this header to list the header set in the drag-and-drop part of the packet.
    Transfer-Encoding Inform the receiver of the encoding mode used to ensure reliable transmission of packets.
    Update The sender may want to upgrade to a new version or protocol
    Via Intermediate node through which the packet passes
    Cache-Control Cache directives
  • The request header, which is only meaningful in the request message, tells you about the client. Including but not limited to:

    The first describe
    Client-IP IP address of the client
    From Email address of a client user
    Host The host name and port number of the server receiving the request
    Referer The URL of the document that contains the currently requested URI. That is, the URL of the page from which the currently requested URL is located.
    User-Agent Information about the application that initiated the request
    Ua-color, UA-CPU, UA-DISP, UA-OS, UA-Pixels Respectively represents the client display color information, CPU information, display information, operating system information, display pixel information
    Accept, accept-charset, accept-encoding, accept-language, TE Represents the media type, character set, encoding mode, language, and extended encoding that can be accepted by the client
    Expect Allows the client to list the behavior required of the server
    If-Match If the entity tag matches the document’s current entity tagmatchingTo get the document
    If-None-Match If the entity tag matches the document’s current entity tagDon't matchTo get the document
    If-Modified-Since Resources unless after the specified dateHas been modifiedOtherwise, restrict the request
    If-Unmodified-Since Resources unless after the specified dateIt hasn't been modifiedOtherwise, restrict the request
    If-Range Make a conditional request for a range of documents
    Range Requests resources within the specified scope
    Authorization Data that the client provides to the server for authentication
    Cookie The token that the client sends to the server
    Cookie2 Indicates the cookie version supported by the client
    Max-Forward Used with the TRACE method to control the maximum number of times a request can be forwarded
    Proxy-Authorization And proxy authentication is used
    Proxy-Connect Control links while establishing links with agents
  • Response header The response header provides the client with additional information so that it can respond better. Including but not limited to:

    The first describe
    Age Response duration from initial creation
    Public List of request methods supported by the server for its resources
    Retry-After If the resource is unavailable, try again after this date
    Server Server application software information
    Title Title of HTML document
    Warning Warning message more detailed than cause phrase
    Accept-Ranges The type of range that the server can receive
    vary Cache information
    Proxy-Authenticate List of proxy challenges to clients
    Set-Cookie Token set by the server on the client side
    WWW-Authenticate List of server – to – client challenges
  • Entity header, which describes information about the entity. Including but not limited to:

    The first describe
    Allow The request method supported by this entity
    Location Informs the client of the actual location of the resource
    Content-base, content-Encoding, content-Language, Content-Length, content-location, content-MD5, content-range, and content-Type Represents the underlying URL of the principal, encoding, language used, length or size, actual location, MD5 checksum, byte range of the entity in the entire scope, and object type, respectively
    BTag Entity tags
    Expires Entities are no longer valid
    Last-Modified Date of last modification

conclusion

The main purpose of this passage is to broaden one’s horizon without rote memorization. See you next. Have a great weekend!

note

  • Some pictures come from the network, if there is infringement, please inform.
  • Please point out any mistakes. ‘!
  • Your liking is the greatest compliment.