HTTP/WebSocket and httpwebsocket

Source: Internet
Author: User

HTTP/WebSocket and httpwebsocket
Http Definition

HTTP is a standard TCP protocol for client and server requests and responses.

HTTP request format

The HTTP request format consists of the request line, request header, empty line, and message body. Each part occupies one line.

<request-line><general-headers><request-headers><entity-headers><empty-line>[<message-body>]

Request Line: The request line is the first line of the request message. It consists of three parts: the request method (GET/POST/DELETE/PUT/HEAD) the URI path of the requested resource, and the HTTP Version Number.

GET /index.html HTTP/1.1

Request Header: The information in the Request Header has Cache-related headers (Cache-Control, If-Modified-Since), client identity information (User-Agent), and so on.

Cache-Control:max-age=0Cookie:gsScrollPos=; _ga=GA1.2.329038035.1465891024; _gat=1If-Modified-Since:Sun, 01 May 2016 11:19:03 GMTUser-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36

Message Body: The request body is the request data sent from the client to the server. This data is not required for each request.

HTTP Response format

After receiving and processing the request, the server returns an HTTP message to the client. The format of the HTTP Response Message includes the status line, Response Header, blank line, and message body. Each part occupies one line

<status-line><general-headers><response-headers><entity-headers><empty-line>[<message-body>]

Status line: The Status line is located in the first line of the corresponding message. It consists of the HTTP protocol version number, status code, and status description.

HTTP/1.1 200 OK

Response Header: The response header is the policy that the server sends to the client to describe the server information and continue to access the resource in the future.

Connection:keep-aliveContent-Encoding:gzipContent-Type:text/html; charset=utf-8Date:Fri, 24 Jun 2016 06:23:31 GMTServer:nginx/1.9.12Transfer-Encoding:chunked

Response body: The response body is the HTML text content that the server returns to the client, or data in other formats, such as video streams, images, or audio data.

HTTP protocol supplement

Request Line-Request Method
GET Request to GET the resource identified by Request-URI
POST attaches new data to the resource identified by Request-URI
HEAD Request to obtain the Response Message Header of the resource identified by Request-URI
The PUT Request server stores a resource and uses Request-URI as its identifier.
The DELETE Request server deletes the resource identified by Request-URI.
TRACE Request information received by the server for testing or diagnosis
CONNECT reserved for future use
OPTIONS requests query server performance, or query resource-related OPTIONS and requirements
Response line-status code
1xx: indicates that the request has been received and continues to be processed.
2xx: Success-indicates that the request has been successfully received, understood, and accepted
3xx: Redirection-further operations are required to complete the request
4xx: client error-the request has a syntax error or the request cannot be implemented
5xx: Server Error-the server fails to fulfill valid requests
Additional
Request = Response. This is always the case in HTTP. That is to say, a request can only have one response, and the response is also passive and cannot be actively initiated.

WebSocket

WebSocket is a new protocol in HTML5. It enables full duplex communication between browsers and servers, which can save server resources and bandwidth and achieve real-time communication, it transmits data through established TCP connections like HTTP

WebSocket request

Client request

GET /webfin/websocket/ HTTP/1.1Host: localhostUpgrade: websocketConnection: UpgradeSec-WebSocket-Key: xqBt3ImNzJbYqRINxEFlkg==Origin: http://localhost:8080Sec-WebSocket-Version: 13

Appendix: The WebSocket connection packet initiated by the client is similar to the traditional HTTP packet. The Upgrade: websocket parameter value indicates that this is a WebSocket type request, and the Sec-WebSocket-Key is a base64-encoded ciphertext sent by the WebSocket client, the server must return an encrypted Sec-WebSocket-Accept response. Otherwise, the client will throw the Error during WebSocket handshake and close the connection.
Server reply

HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: UpgradeSec-WebSocket-Accept: K7DJLdLooIwIG/MOpvWFB3y3FE8=

Appendix: The value of Sec-WebSocket-Accept is calculated by the server using the same key as the client and then returned to the client. HTTP/1.1 101 Switching Protocols indicates that the server accepts the WebSocket protocol client connection, after such a request-response process, the WebSocket connection at both ends of the handshake is successful, and TCP communication can be performed in the future.

Data Transmission

1. Transmission in frame format (Big Data sharding/support for real-time generation and transmission)
2. the data frames sent from the client to the server must be masked. When the server receives unmasked data frames, it must take the initiative to close the connection/the data sent from the server to the client must not be masked, when the client receives a masked actual frame, the client must close the connection or the client can send a close frame to close the connection.

Frame Type

The frame type is represented by a four-digit long value called Opcode.
1. Opcode = 0 continue
This indicates that this frame is a continuous frame and needs to be spliced after the previous received frame to form a complete message. Due to this parsing feature, the transmission and receipt of Non-Control Frames must be in the same order.
2. Opcode = 1 text frame
3. Opcode = 2 binary Frame
4. Opcode = 3-7 Future Use (non-Control Frame)
5. Opcode = 8 close the connection (Control Frame)
This frame may contain content to indicate the reason for closing the connection.
The communication Party sends this frame to disable the WebSocket connection. If the recipient of this frame has not previously sent this frame, it needs to send the same closing frame to confirm the closing, if both parties send this frame at the same time, both parties need to send a response to the close frame.
Ideally, the server closes the TCP connection after confirming that the WebSocket connection is closed. The client needs to wait for the server to close the TCP connection, but the client can also close the TCP connection in some cases.
6. Opcode = 9 Ping
Similar to heartbeat, one party receives Ping and should immediately send Pong as a response
7. Opcode = 10 Pong
If the communication party does not send Ping, but receives Pong, it does not require it to return any information. The Pong frame content should be the same as the Ping received, and the communication party may receive a lot of Ping requests, but you only need to respond to the latest one.

8. Opcode = 11-15 future use (Control Frame)

Saved question

Defects: HTTP/WebSocket connections often go through numerous routes, firewalls, and other intermediate network links, so it is easy to enter a half-dead state.
* Solution *: The server and client can send Ping/Pong Frame. This Frame is a special Data packet that only contains metadata and does not need real Data Payload, the connection status of the intermediate network can be maintained without affecting the Application.

WebSocket advantages over HTTP persistent connections

1. real full duplex mode. After a connection is established, the client and the server are completely equal and can actively request each other. However, HTTP persistent connections are based on HTTP, is a traditional Client-to-Server Request mode;
2. in the HTTP persistent connection, in addition to the real data, the server and the client need to exchange a large number of HTTP headers, which results in low information exchange efficiency, after a TCP connection is established through the first request through the Websocket protocol, the data exchanged afterwards can be exchanged without sending an HTTP header, this is obviously different from the original HTTP protocol. Therefore, it must be upgraded to both the server and client (HTML5 is supported by mainstream browsers ), in addition, multiplexing and different URLs can reuse the same WebSocket connection.

Supplementary Differentiation

Authenticity and persistent connection
Keep-aliveIn order to achieve the "negotiation" behavior of reusing tcp connections, the two parties have not established a positive connection session, and the server may not recognize it or be able to (after any request is completed) at any time) close;
WebSocketIt specifies a positive and duplex persistent connection. Both sides must maintain the connection status. In addition, the http protocol determines that the browser always initiates actively, the http Server Always passively accepts and responds to requests and never takes the initiative. However, after the WebSocket protocol is connected, the client and server are completely equal and there is no active or passive saying.
HTTP & WebSocket contact
When the client starts to establish a WebSocket connection, it must send an HTTP request with the header marked with Upgrade, indicating that the request protocol is upgraded. Therefore, the server responds, implement the WebSocket protocol directly on the existing HTTP server software and the existing port, reuse the existing code, and return an HTTP response with a status code of 101 to complete the handshake, in short, WebSocket requires HTTP to establish a WebSocket connection, and then runs the application protocol on the TCP transport layer independently.
Similarities and Differences
Similarities
1. TCP-based application layer protocol.
Both use the Request/Response Model for connection establishment.
3. The error handling method is the same during connection establishment. In this phase, WS may return the same return code as HTTP.
4. All data can be transmitted over the network.
Differences
1. WS uses HTTP to establish a connection, but defines a series of new header fields, which are not used in HTTP.
2. WS Connections cannot be forwarded through middlemen. They must be a direct connection.
3. After the WS connection is established, both parties can send data to the other party at any time.
4. After the WS connection is established, data transmission uses frames for transmission and no Request message is required.
5. Ordered WS data frames

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.