Introduction to the HTTP protocol

Source: Internet
Author: User
Tags http file upload ranges

Introduction to the HTTP protocol Blog Category:
    • ACL Development--http Protocol Chapter
Network Protocol HTTP Protocol

I. Introduction of TCP/IP protocol

Before introducing the HTTP protocol, let's briefly talk about the contents of the TCP/IP protocol. The TCP/IP protocol is layered, from the underlying to the application layer: the physical layer, the link layer, the network layer, the transport layer, and the application layer, as shown in:

From the application layer to the physical layer, the data is a layer of packaging, packaging is generally in front of the original data and a data control head, the format of the data package is as follows:

For the TCP transport protocol, the client is required to undergo a TCP three-level handshake before the server establishes the connection, as follows:

Second, the HTTP protocol

2.1 Introduction

Hypertext Transfer Protocol (hypertext Transfer Protocol, referred to as HTTP) is the application layer protocol, since 1990, HTTP has been applied to the WWW Global information Service system.
HTTP is a request-and-response protocol. After a client has established a connection with the server, a request is sent to the server, and the server receives the request, giving the corresponding response information.
The first version of HTTP http/0.9 is a simple protocol for raw data transfer between networks;
http/1.0 is defined by RFC 1945 and, on the basis of the original http/0.9, has been further improved to allow messages to exist in the class MIME information format, including information on transmitted data and modifiers in the request/response paradigm;
http/1.1 (RFC2616) is more stringent to ensure service reliability, enhancing the performance of http/1.0 not fully consider the layered proxy server, high-speed buffer memory, persistent connection requirements or virtual host;
Security-Enhanced HTTP (that is, s-http or HTTPS) is the combination of the HTTP protocol and the Secure Sockets Layer (SSL), which makes HTTP protocol data more secure during transmission.

2.2 Protocol Structure

HTTP protocol format is also relatively simple, the format is as follows:

2.3 HTTP Protocol Example

Here is an example of an HTTP request and response:

2.4 Request Header Format

A) General purpose head (General-header):
Cache-control: The client wants the server to cache its own request data, such as "Cache-control:no-cache", "cache-control:max-age=0";
Connection: Whether the client wants to maintain a long connection with the server, such as "Connection:close", "connection:keep-alive";
Date: The client may have some fields only if the request method is a post or Put method;
Pragma: Contains some special request information of the client, such as "Pragma:no-cache" client wants the proxy or application server should not cache the result data related to the request;
Via: Typically used in a request header sent by the proxy gateway to the application server, indicating that the request from the client passed through the gateway proxy,
The format is: "Via: Request protocol version Gateway Identity [additional information]",
such as: "via:1.1 webcache_250_199.hexun.com:80 (Squid)"

B) Request header (Request-header):
Accept: A list of media type ranges that indicate acceptable request responses from the customer. The asterisk "*" is used to group types by range, with "*/*" indicating that all types are acceptable, and "type/*" indicates that all subtypes of type types are acceptable, such as "Accept:image/gif, Image/jpeg, */*";
Accept-charset: The character set encoding format that the client can recognize, format: "Accept-charset: Character Set 1[: weight], Character set 2[: weight]", such as: "Accept-charset:iso-8859-5, unicode-1-1;q=0.8 ";
Accept-language: The language that the client can recognize, format:" Accept-language: Language 1[: Weight], language 2[: weight] ", such as:" Accept-language:zh , en;q=0.7 ";
Host: The host domain name or host IP requested by the client, format:" Host: Domain name or ip[: Port number] ", such as:" host:www.hexun.com:80 ", if there is a http/1.1 in the request line must have the request header;
User-agent: Indicates the browser identifier used by the user, primarily for statistical purposes;
Referer: Indicates which association connection the request was made from;

Accept-encoding: The client can recognize the encoding compression format, such as: "Accept-encoding:gzip, deflate";
If-modified-since: This field is related to client-side caching, the URL accessed by the client has been modified on the server since the specified date, if modified, the service side returns the new modified information, if it has not been modified, then 304 indicates that the URL referred to by this request has not been modified. such as: "If-modified-since:fri, 2 Sep 2006 19:37:36 GMT";
If-none-match: This field is related to client-side caching, and the client sends the field and identity while sending a URL request, and if the identity of the server side is consistent with the client's identity, 304 indicates that the URL has not been modified, and the server returns full data information if it is inconsistent, such as: " if-none-match:0f0a893aad8c61:253, 0f0a893aad8c61:252, 0f0a893aad8c61:251 ";
Cookies: For the extended field, stored in the client, to the same domain name on the server to send a Cookie belonging to the domain, such as: "Cookie:mailusername=whouse";

c) Entity header (Entity-header): (The data body is required when such a header exists)
Content-encoding: The client can recognize the encoding compression format, such as: "Content-encoding:gzip, deflate";
Content-length: The content length of the data body part when the client uploads data by post method, for example: "Content-length:24";
Content-type: The content type of the data body sent by the client, for example: "Content-type:application/x-www-form-urlencoded" is the data sent with the normal post method; "Content-type: Multipart/form-data; boundary=---------------------------5169208281820 "indicates that the data body is made up of multiple parts and the delimiter is"----------------------------- 5169208281820 ";

2.5) Response Format

A) General purpose head (General-header):
Cache-control: The server requires intermediate agents and clients how to cache their own response data, such as "Cache-control:no-cache", such as: "Cache-control:private" do not want to be cached, " Cache-control:public "can be cached;
Connection: Whether the server wants to keep a long connection with the client, such as "Connection:close", "connection:keep-alive";
Date: The client may have some fields only if the request method is a post or Put method;
Pragma: Contains some special response information of the service side, such as "Pragma:no-cache" server want agent or client should not cache the result data;
Transfer-encoding: The transport mode used by the server to transmit data to the client (only in HTTP1.1), such as: "transfer-encoding:chunked", note: This field has a higher priority than "Content-length" The priority of the field;

b) Response Head (Response-header):
Accept-ranges: Indicates the unit of data received by the server, such as: "Accept-ranges:bytes",;
Location: The server returns this information to the client to redirect the client, such as: "location:http://www.hexun.com";
Server: Some information returned by the service to identify itself, such as: "server:microsoft-iis/6.0";
ETag: The identity field of the response data returned by the server, where the client can send a URL whether or not the information is updated based on the value of this field;

c) Entity header (Entity-header): (The data body is required when such a header exists)
Content-encoding: The encoding format of the response data of the server, such as: "Content-encoding:gzip";
Content-length: The content length of the data body portion of the data returned by the server, for example: "Content-length:24";
Content-type: The content type of the data body returned by the server, such as: "content-type:text/html; charset=gb2312 ";
Set-cookie: Cookie data returned by the server to the client, such as "SET-COOKIE:ASP.NET_SESSIONID=ICNH2KU2DQLMKCIYOBGVZL55; path=/"

2.6) Server return status code

1XX: Indicates that the server receives the client request and the client continues to send the request;
2XX: The request sent by the client was successfully received by the server and processed successfully;
3XX: The server returns information to the client for redirection;
4XX: The client's request has illegal content;
5XX: Unexpected error occurred when the server failed to process the client's request properly.

Example:

"100"; The service side wants the client to continue;
"200"; The client's request was successfully received and processed by the server;
"301"; The URL requested by the client has been moved, requiring the client to redirect to another URL;
"304"; The URL requested by the client has not changed;
"400"; Client request error;
"403"; Client requests are prohibited by the server;
"404"; The URL requested by the client does not exist on the server;
"500"; The server encountered an exception while processing the client request;
"501"; The method or content of the client request is not implemented by the server;
"502"; This is an error message returned by the intermediary agent to the client, indicating an error occurred when the server returned to the agent.
"503"; The server is unable to respond to client requests due to high load or other errors;
"504"; This is an error message returned by the intermediary agent to the client, indicating a timeout on the proxy connection server.

2.7) chunked Transmission

The encoding consists of several chunk consisting of a chunk with a length of 0, each of which consists of two parts, the first part of which is the length of the chunk (in hexadecimal notation) and the length unit (generally not written), and the second part is the content of the specified length, each section is separated by CRLF. In the last chunk of length 0 is the content called footer, which is something that is not written in the header content. In addition, the HTTP header must contain the following: "Transfer-encoding:chunked" for the common head field. The format is as follows:

2.8) HTTP Request method

GET, POST, HEAD, CONNECT, PUT, DELETE, TRACE

2.9) Example

a) GET request

HTML code
  1. GET Http://photo.test.com/inc/global.js http/1.1
  2. Host:photo.test.com
  3. user-agent:mozilla/5.0 (Windows; U Windows NT 5.0; ZH-CN; rv:1.8.1) gecko/20061010 firefox/2.0
  4. accept:text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain; q=0.8,image/png,*/*; q=0.5
  5. ACCEPT-LANGUAGE:EN-US,ZH-CN; q=0.7,zh; q=0.3
  6. Accept-encoding:gzip,deflate
  7. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  8. keep-alive:300
  9. Proxy-connection:keep-alive
  10. Cookie: asp.net_sessionid=ey5drq45lsomio55hoydzc45
  11. Cache-control: max-age=0

b) Post request

HTML code
    1. post/http/1.1
    2. Accept:image/gif, Image/x-xbitmap, Image/jpeg, Application/vnd.ms-powerpoint, Application/msword, */*
    3. Accept-language:zh-cn
    4. content-type:application/x-www-form-urlencoded
    5. Accept-encoding:gzip, deflate
    6. user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)
    7. Host:www.test.com
    8. Content-length:24
    9. Connection:keep-alive
    10. Cache-control:no-cache
    11. Name=value&submitsubmit=submit

c) Send a GET request over an HTTP proxy

HTML code
  1. GET http://mail.test.com/HTTP/1.1
  2. Host:mail.test.com
  3. user-agent:mozilla/5.0 (Windows; U Windows NT 5.0; ZH-CN; rv:1.8.1) gecko/20061010 firefox/2.0
  4. accept:text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain; q=0.8,image/png,*/*; q=0.5
  5. ACCEPT-LANGUAGE:EN-US,ZH-CN; q=0.7,zh; q=0.3
  6. Accept-encoding:gzip,deflate
  7. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  8. keep-alive:300
  9. Proxy-connection:keep-alive

D) Post mode upload file

HTML code
  1. POST Http://www.test.comt/upload_attach? uidl=%3c http/1.1
  2. Host:www.test.com
  3. user-agent:mozilla/5.0 (Windows; U Windows NT 5.0; ZH-CN; rv:1.8.1) gecko/20061010 firefox/2.0
  4. accept:text/xml,application/xml,application/xhtml+xml,text/html; q=0.9,text/plain; q=0.8,image/png,*/*; q=0.5
  5. ACCEPT-LANGUAGE:EN-US,ZH-CN; q=0.7,zh; q=0.3
  6. Accept-charset:gb2312,utf-8; q=0.7,*; q=0.7
  7. Content-type:multipart/form-data; boundary=---------------------------5169208281820
  8. content-length:449
  9. -----------------------------5169208281820
  10. Content-disposition:form-data; name="File_1"; filename=""
  11. Content-type:application/octet-stream
  12. -----------------------------5169208281820
  13. Content-disposition:form-data; name="File_0"; filename="test.txt"
  14. Content-type:text/plain
  15. Hello world!
  16. -----------------------------5169208281820
  17. Content-disposition:form-data; name="oper"
  18. Upload
  19. -----------------------------5169208281820--

E) Connect Example

HTML code
    1. CONNECT mail.test.com:80 http/1.1
    2. user-agent:mozilla/5.0 (Windows; U Windows NT 5.0; ZH-CN; rv:1.8.1) gecko/20061010 firefox/2.0
    3. Proxy-connection:keep-alive
    4. Host:mail.test.com:80

3.0) test in Telnet mode at terminal

A) Turn on Echo feature (for Windows)
Windows 2000: Enter DOS mode, input Telnet->set local_echo-> exit: quit->telnet IP 80
Windows XP: Enter DOS mode, input telnet->set local echo->open IP 80
b) Enter the GET request, head request, and POST request in the HTTP protocol format.

Personal micro-Blog: http://weibo.com/zsxxsz

Reference:

The article, "Implementing a programming interface similar to Java HttpServlet in C + +" gives examples of WEB programming provided in the Acl_cpp library.

The article "Writing Web Apps using Acl_cpp's HttpServlet class and Google's Ctemplate library" gives examples of web libraries and Google's page template libraries that use ACL _cpp.

The article "server instance of implementing file uploading in Web programming" gives an example of using Acl_cpp Web Library to handle HTTP file upload.

The article "Writing Web server programs using Acl_cpp's HttpServlet class and server framework" gives a Web application instance using the ACL server framework.

The article "using ACL libraries to develop an HTTP download Client" gives an HTTP download client written using the ACL library.

An example of HTTP client download is given in the article, "Using the HTTP protocol library to write HTTP download client for the lower ACL."

-
Top -
Step to share: Use ACL to lower the HTTP protocol library to write http ... | Developing an HTTP download client using the ACL library
    • 2010-01-12 11:12
    • Browse 30735
    • Comments (23)
    • Category: Internet
    • Related recommendations
Reviews 23 Floor bin_1715575332 2013-07-14 very good, just need this knowledge, preserved, hehe. 22/F Rox studied at the university in 2012-05-20 and forgot. Thanks, man. 21 Floor Xiaowangzaixian 2012-04-25 elder brother, hello to strength ah. 20 Floor Fl

Introduction to the HTTP protocol

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.