"SLIGHTTPD" server project based on LIGHTTPD Architecture Combat (6)-HTTP for pre-knowledge

Source: Internet
Author: User
Tags rfc

Next, we start the development of the HTTP section. Before this, it is necessary to learn the HTTP protocol ~

RFC documentation for HTTP1.1: Http://www.ietf.org/rfc/rfc2616.txt

Brief introduction

Hypertext Transfer Protocol (hypertext Transfer Protocol, referred to as HTTP) is the application layer protocol, is a request/response protocol, that is, after a client establishes a connection with the server, sends a request to the server, the server receives the request, gives the corresponding response information.

HTTP Request Message

The HTTP request message consists of 4 parts: The request line, the request header, the blank line, and the request body.

|  请求方法 | URL | 协议版本 |     ->请求行|  请求头部(Request Header) ||  空行                     ||  body                    |

Request Line: Consists of the Method field, the URL field, and the HTTP protocol version field 3 parts, separated by a space. Common HTTP Request methods are get, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;

Example:
Get/index.jsp?id=100&op=bind http/1.1

Get: Use the Get method when the client wants to read a resource from the server. The GET method requires the server to place the URL-positioned resource in the data portion of the response message, which is sent back to the client, the client requesting a resource from the server. When using the Get method, the request parameter and the corresponding value are appended to the URL, using a question mark ("?") Represents the end of the URL and the start of the request parameter, which is limited by the length of the pass parameter. For example,/index.jsp?id=100&op=bind.

Post: When the client provides more information to the server, the Post method can be used to submit data to the server, such as completing the submission of form data and submitting the data to the server for processing. Get is typically used to get/query resource information, and POST comes with user data, which is typically used to update resource information. The POST method encapsulates the request parameters in the HTTP request data, appears as a name/value, and can transmit large amounts of data;

PUT: Used to modify a content;

Delete: Deletes a content;

CONNECT: For the agent to transfer, such as the use of SSL;

Options: Ask what methods you can implement;

PATCH: Partial document changes;

PROPFIND, (WEDAV): View properties;

PROPPATCH, (WEDAV): Set properties;

Mkcol, (WEDAV): Create a collection (folder);

Copy, (WEDAV): copy;

Move, (WEDAV): mobile;

LOCK, (WEDAV): locking;

UNLOCK (WEDAV): unlocked;

TRACE: for remote diagnostics server;

HEAD: Similar to get, but does not return body information, to check whether the object exists, and to get the object's metadata;

"Request Header": The request header is composed of key/value pairs, each pair of rows, keywords and values separated by the English colon ":". The request header notifies the server that there is information about the client request, and the typical request headers are:

User-agent: The type of browser that generated the request;

Accept: A list of response content types that are recognized by the client; the asterisk "*" is used to group types by scope, with "*/*" indicating that all types are acceptable, and "type/*" indicates that all subtypes of type types are acceptable;

Accept-language: Natural language acceptable to the client;

Accept-encoding: Acceptable encoding compression format for the client;

Accept-charset: An acceptable set of responses;

Host: The hostname of the request, allowing multiple domain names to be located in the same IP address, i.e., the virtual host;

Connection: Connection method (Close or keepalive);

Close: Tells the WEB server or proxy server, after completing the response of this request, disconnects, does not wait for the subsequent request of this connection;
KeepAlive: Tell the Web server or proxy server, after completing the response of this request, keep the connection and wait for the subsequent request of this connection;

Cookie: stored in the Client Extension field, sending a cookie belonging to that domain to the server of the same domain name;

"Blank line": After the last request header is a blank line, send a carriage return and newline character, notify the server no longer have the request header;

Request Package Body: The request package body is not used in the GET method, but is used in the Post method. The POST method is useful for situations where a customer needs to fill out a form. The most commonly used is the package body type Content-type and the package body length content-length associated with the request package body.

HTTP response Messages

An HTTP response message consists of 4 parts: A status line, a response header, a blank line, and a response package body.

|  协议版本 | 状态码 | 状态码描述 |  ->状态行|  响应头(Response Header)      ||  空行                         ||  响应正文                     |

"Status line": The status line consists of the HTTP Protocol version field, the status code, and the description text of the status Code 3 parts, separated by a space;

The status code consists of three digits, the first digit indicates the type of response, and the commonly used status code has five main classes as follows:

1XX: Indicates that the server has received the client request, the client can continue to send the request;

2XX: Indicates that the server has successfully received and processed the request;

3XX: Indicates that the server requires client redirection;

4XX: Indicates that the client's request has illegal content;

5XX: An unexpected error occurred when the server failed to process the client's request properly;

The status Code description text has the following values:

OK: Indicates the client request was successful;

Bad Request: Indicates that the client requests have syntax errors and cannot be understood by the server;

401 unauthonzed: Indicates that the request was not authorized and that the status code must be used with the www-authenticate header domain;

403 Forbidden: Indicates that the server received the request, but refused to provide the service, usually in the response body given the reasons for not providing services;

404 Not Found: The requested resource does not exist, for example, the wrong URL was entered;

Internal Server Error: Indicates that the server has unexpected errors, resulting in the inability to complete the client's request;

503 Service Unavailable: Indicates that the server is currently not able to process the client's request, after a period of time, the server may return to normal;

"Response Header": The request header is composed of key/value pairs, each pair of lines, keywords and values separated by the English colon ":". Sections are as follows:

Location: Used to redirect the recipient to a new position. For example: the client requested the page no longer exists in the original location, in order to redirect the client to the new location of the page, the server can send back to the address of the response header after the use of redirection statements, so that the client to access the new domain name of the corresponding server resources;

The Server:server response header field contains the software information that the server uses to process the request and its version. It corresponds to the user-agent request header domain, which sends information about the server-side software, which sends the client software (browser) and the operating system.

Vary: Indicates a list of non-cacheable request headers;

Connection: Connection mode;
(Close: The connection has been closed;
KeepAlive: The connection is maintained, waiting for subsequent requests for this connection; )

Keep-alive: If the browser requests to remain connected, the header indicates how long (in seconds) you want the Web server to remain connected; for example: keep-alive:300;

The Www-authenticate:www-authenticate response header field must be contained in a 401 (unauthorized) response message, and the header domain is related to the authorization Request header field mentioned earlier when the client receives a 401 response message, Decide whether to request the server to validate it. If the server is required to verify it, it can send a request containing the authorization header domain;

"Blank line": The last response header is followed by a blank line that sends a carriage return and a newline character, notifying the server that no longer responds to the header.

"Response Package Body": Text information returned by the server to the client;

HTTP Stateless

The HTTP protocol is stateless (stateless). That is, when the same client accesses a page on the same server for the second time, the server cannot know that the client has visited and the server cannot distinguish between different clients. The stateless nature of HTTP simplifies the design of servers, making it easier for servers to support a large number of concurrent HTTP requests.

HTTP Persistent Connection

HTTP1.0 uses a non-persistent connection, the main drawback is that the client must establish and maintain a new connection for each object to be requested, that is, twice times the cost of the RTT per request for a document. Because there may be multiple objects on the same page, non-persistent connections can make a page download very slow, and this short connection increases the burden of network transmission.

HTTP1.1 uses persistent connection keepalive, the so-called persistent connection, that is, the server after sending the response still for a period of time to maintain the connection, allowing multiple data requests and responses in the same connection, that is, in the case of persistent connection, the server does not close the TCP connection after sending the response, The client can continue to request other objects through this connection.

There are two ways to http/1.1 a persistent connection to a protocol:

Non-pipelined mode: The customer receives the previous response before issuing the next request;

Pipelining: A customer can then send a new request message before receiving an HTTP response message.

All right, ~http's basic knowledge is introduced here, next we will be for HTTP to add functionality for our server, for the parsing part of HTTP, I do not intend to write myself, will use a third-party parsing library Http-parser.

"SLIGHTTPD" server project based on LIGHTTPD Architecture Combat (6)-HTTP for pre-knowledge

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.