"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 first ~

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

Simple Introduction

Hypertext Transfer Protocol (hypertext Transfer Protocol. HTTP) is an application-level protocol, which is a request/response protocol that sends a request to the server after a client has established a connection with the server, after the server receives the request. Give the appropriate 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 3 parts of the Method field, the URL field, and the HTTP protocol version number field, separated by a space. Frequently used HTTP Request methods are get, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;

Examples:
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. Sent back to the client. That is, the client requests a resource from the server. When using the Get method, the request parameters and corresponding values are appended to the URL, using a question mark ("?") Represents the end of the URL and the start of the request. The length of the pass parameter is restricted.

Like,/index.jsp?

Id=100&op=bind.

Post: You can use the Post method to submit data to the server when the client provides more information to the server. For example, complete the submission of form data. Submits 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 change a content.

Delete: Deletes a content.

CONNECT: For the agent to transfer. If using SSL;

Options: Ask what methods you can run;

PATCH: Partial document changes;

PROPFIND, (WEDAV): View properties;

PROPPATCH, (WEDAV): Set properties;

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

Copy, (WEDAV): copy;

Move, (WEDAV): mobile;

Lock, (WEDAV): Add lock.

UNLOCK (WEDAV): Unlocked.

TRACE: For remote diagnostics server.

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

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

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

Accept:client a list of responsive content types that can be recognized; the asterisk "*" is used to group types by range. Use "*/*" to indicate that all types are acceptable, and "type/*" indicates that all sub-types of type are acceptable;

Accept-language:client acceptable natural language;

Accept-encoding:client acceptable encoding compression format;

Accept-charset: An acceptable set of responses;

Host: The hostname of the request, consent to multiple domain names in the same IP address, that is, virtual host;

Connection: Connection method (Close or keepalive);

Close: Tells the WEB server or proxy server that after the response of this request has been completed, the connection is disconnected without waiting for a request for this connection.
KeepAlive: Tell webserver or proxy server to remain connected after the response of this request has been completed. A request to wait for this connection;

Cookies: stored in the Client extension field. Sends a cookie belonging to that domain to the server of the same domain name;

"Blank line": The last request header is followed by a blank line. Send a carriage return and newline character, notifying the server that there are no more request headers;

Request Package Body: The request package body is not used in the GET method. Instead, it is used in the Post method. The POST method is useful for situations where customers need 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 3 parts of the description text of the HTTP Protocol version Number field, the status code and the status code, separating them with a space;

The status code consists of three digits. The first digit indicates the type of response, and frequently used status codes have five categories such as the following:

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: The client request has illegal content;

5XX: Unexpected error occurred while server failed to process client's request properly;

The state code descriptive narration text has for example the following values:

OK: Indicates the client request is successful;

Bad Request: Indicates a syntax error for client requests 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 receives the request, but refuses to provide the service, generally in the response body gives the reason that does not provide the service;

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 that could result in the client's request not being completed;

503 Service Unavailable: Indicates that the server is currently not able to process client requests, and 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 rows, keyword and values separated by the English colon ":". Some examples include the following:

Location: Used to redirect the recipient to a new position. For example: the page requested by the client does not already exist, in order for the client to redirect to the new location of the page. The server side can send back the location response header and use the redirect statement, allowing the client to access resources on the server corresponding to the new domain name;

The Server:server response header field includes the software information that the Server uses to process the request and its version number. 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: Connection is maintained, waiting for a request for this connection; )

Keep-alive: Suppose the browser requests to remain connected. The header indicates how long the Web server is expected to remain connected (in seconds); for example: keep-alive:300;

The Www-authenticate:www-authenticate response header field must be included in the 401 (unauthorized) response message, which is related to the header field of the authorization request mentioned earlier. When the client receives a 401 response message, it decides whether to request the server to validate it. Assume that the server is required to validate it. is able to send a request that includes 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 there is no longer a response header.

Response Package Body: The text information that the server returns to the client;

HTTP Stateless

The HTTP protocol is stateless (stateless). Other words. When the same client visits a page on the same server for the second time, the server cannot know that the client was previously interviewed and the server was unable to distinguish between different clients.

The stateless nature of HTTP simplifies the design of the server. Make the server easier 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, each request for a document will have twice times the cost of RTT. Because multiple objects may exist on the same page. So a non-persistent connection can make a page download very slow, and such a short connection adds to the burden of network transmission.

HTTP1.1 uses persistent connection keepalive. The so-called persistent connection is that the server still maintains the connection for a period of time after the response is sent, agreeing that there are multiple data requests and responses in the same connection, that is, in the case of persistent connections, 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 is able to make the next request after receiving the previous response;

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 join the function 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

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.