云原生CAx软件: HTTP基础知识汇总

云原生(Cloud Native)的兴起,面向服务架构(Service-Oriented Architecture,SOA)、微服务(Microservice)容器(Container)等相关概念与技术正在逐渐影响CAx(CAD/CAE/CAM)软件的架构设计与开发。

云原生CAx软件中,首先需要把系统按照功能划分成不同的服务单元然后再根据业务流程完成各种服务的集成(Enterprise Application IntegrationEAI)。Web ServicesMicroservice是将应用程序转换成服务的常用方法,而这些技术实际上大多数都是以HTTP为基础的。

因此,非常有必要对HTTP的概念、原理等基础知识进行总结,以期能够更好的进行云原生CAx软件的设计与开发。

注1:限于研究水平,分析难免不当,欢迎批评指正

注2:文章内容会不定期更新

一、HTTP基础

HTTP(HyperText Transfer Protocol, 超文本传输协议)是一种采用"请求-应答"通信模式、运行在TCP协议之上的应用层协议,早期主要应用于Web服务器与客户端之间HTML数据传输。

Ref. from Hypertext Transfer Protocol -- HTTP/1.1

The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. It is a generic, stateless, protocol which can be used for many tasks beyond its use for hypertext, such as name servers and distributed object management systems, through extension of its request methods, error codes and headers. A feature of HTTP is the typing and negotiation of data representation, allowing systems to be built independently of the data being transferred.

  1.1 HTTP工作原理

HTTP采用请求-应答通信模式,一般用于B/S架构中HTTP Client与HTTP Server之间的网络通信。

Ref. from HTTP/1.1 Overall Operation

The HTTP protocol is a request/response protocol. A client sends a request to the server in the form of a request method, URI, and protocol version, followed by a MIME-like message containing request modifiers, client information, and possible body content over a connection with a server. The server responds with a status line, including the message's protocol version and a success or error code, followed by a MIME-like message containing server information, entity metainformation, and possible entity-body content.

One or more intermediaries are present in the request/response chain. There are three common forms of intermediary: proxy, gateway, and tunnel.A proxy is a forwarding agent, receiving requests for a URI in its absolute form, rewriting all or part of the message, and forwarding the reformatted request toward the server identified by the URI. A gateway is a receiving agent, acting as a layer above some other server(s) and, if necessary, translating the requests to the underlying server's protocol. A tunnel acts as a relay point between two connections without changing the messages; tunnels are used when the communication needs to pass through an intermediary (such as a firewall) even when the intermediary cannot understand the contents of the messages.

首先,HTTP Client(通常是浏览器)创建一个到HTTP Server指定端口(默认为80端口)的TCP连接,然后HTTP Client向HTTP Server发送HTTP请求报文。

HTTP server收到HTTP请求报文之后,完成HTTP请求报文数据报文解析,执行对应的业务逻辑,然后将状态码、处理结果等写入到一个HTTP响应报文,并将该HTTP响应报文返还给HTTP Client。

HTTP Client收到HTTP响应报文之后,根据返还状态、处理结果等完成显示、数据存储等工作。

  1.2 HTTP数据报文

HTTP请求报文由请求行、请求头部、空行、请求数据等四个部分组成。

 HTTP响应报文由状态行、消息报头、空行、响应正文等四个部分组成。

  1.3 浏览器架构

  1.4 HTTP会话保持

HTTP协议本身是无状态的。对于浏览器,一般一个连接处理一个请求,完成一次“请求-应答”之后一般会断开连接。对于服务器,请求彼此之间是独立的,服务器无法知道请求之间的关联。

Ref. from HTTP/1.1 Connections

A significant difference between HTTP/1.1 and earlier versions of HTTP is that persistent connections are the default behavior of any HTTP connection. That is, unless otherwise indicated, the client SHOULD assume that the server will maintain a persistent connection, even after error responses from the server.

Cookie、Session和Token是常用的会话保持技术。

关于此部分的内容请关注笔者博文:云原生CAx软件:多租户的认证

二、实现与衍生

  2.1 Boost

Module Description Scenario
Beast Portable HTTP, WebSocket, and network operations using only C++11 and Boost.Asio

  2.2 Qt

Module Description Scenario
Qt Network

Classes to make network programming easier and more portable.

Currently HTTP, FTP and local file URLs are supported for uploading and downloading.

封装了套接字,也实现了HTTP、FTP等常见网络协议
Qt WebSockets Provides WebSocket communication compliant with RFC 6455.  It provides an implementation for the WebSocket protocol, which is offered by IETF (Internet Engineering Task Force) as a better alternative for bidirectional communication using the existing web infrastructure. 服务端主动向客户端推送数据
Qt WebChannel Provides access to QObject or QML objects from HTML clients for seamless integration of Qt applications with HTML/JavaScript clients. 基于Qt的Server应用
Qt WebKit The QtWebKit module provides a web browser engine as well as classes to render and interact with web content 在Qt5中,被Qt WebEngine替换
Qt WebEngine Classes and functions for embedding web content in applications using the Chromium browser project. 作为Qt WebEngine替换者,在应用中显示Web内容
Qt WebView Displays web content in a QML application by using APIs native to the platform, without the need to include a full web browser stack. 在QML程序中显示Web内容
Qt for WebAssembly A platform target to compile Qt applications for the web browsers using WebAssembly. 将Qt应用嵌入到Web页面

  2.3 MIME

  2.4 DIME

  2.5 WebSocket

Ref. from The WebSocket Protocol

The WebSocket Protocol enables two-way communication between a client running untrusted code in a controlled environment to a remote host that has opted-in to communications from that code.  The security model used for this is the origin-based security model commonly used by web browsers.  The protocol consists of an opening handshake followed by basic message framing, layered over TCP.  The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections.

The protocol has two parts: a handshake and the data transfer.

The handshake from the client looks as follows:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

The handshake from the server looks as follows:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol: chat

After a successful handshake, clients and servers transfer data back and forth in conceptual units referred to in this specification as "messages".  On the wire, a message is composed of one or more frames. The WebSocket message does not necessarily correspond to a particular network layer framing, as a fragmented message may be coalesced or split by an intermediary.

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len |    Extended payload length    |
|I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
|N|V|V|V|       |S|             |   (if payload len==126/127)   |
| |1|2|3|       |K|             |                               |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
|     Extended payload length continued, if payload len == 127  |
+ - - - - - - - - - - - - - - - +-------------------------------+
|                               |Masking-key, if MASK set to 1  |
+-------------------------------+-------------------------------+
| Masking-key (continued)       |          Payload Data         |
+-------------------------------- - - - - - - - - - - - - - - - +
:                     Payload Data continued ...                :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
|                     Payload Data continued ...                |
+---------------------------------------------------------------+

  2.6 Web Services 

  2.7 ESB

  2.8 API GateWay

  2.9 WebGL and Three.js

参考文献

罗军舟. TCP/IP协议及网络编程技术. 清华大学, 2004.

Joseph Ingeno. Handbook of Software Architecture.

顾宁. Web Services原理与研发实践. 机械工业出版社, 2005.

网络资料

大型CAx(CAD/CAE/CAM)工业软件开发中的关键组件https://blog.csdn.net/qq_26221775/article/details/123193318?spm=1001.2014.3001.5501

云原生CAx软件:多租户的认证https://blog.csdn.net/qq_26221775/article/details/130441983?spm=1001.2014.3001.5502

Hypertext Transfer Protocol -- HTTP/1.0 https://datatracker.ietf.org/doc/html/rfc1945

Hypertext Transfer Protocol -- HTTP/1.1https://datatracker.ietf.org/doc/html/rfc2616

HTTP State Management Mechanism draft-abarth-cookie-07https://datatracker.ietf.org/doc/html/draft-abarth-cookie-07

JSON Web Tokenhttps://datatracker.ietf.org/doc/html/rfc7519

Google Chromehttps://www.google.cn/intl/en_uk/chrome/

The Chromium Projectshttps://www.chromium.org/Home/

gSOAPhttps://www.genivia.com/

Boosthttps://www.boost.org/doc/libs/1_80_0/

Qt5 All Moduleshttps://doc.qt.io/qt-5/qtmodules.html

Qt5 Porting Guidehttps://doc.qt.io/qt-5/portingguide.html

Qt4.8 WebKithttps://doc.qt.io/archives/qt-4.8/qtwebkit-module.html

WebSockethttps://datatracker.ietf.org/doc/html/rfc6455

Qt for WebAssemblyhttps://doc.qt.io/qt-5/wasm.html

Spring Web Serviceshttps://spring.io/projects/spring-ws

OpenESBhttp://www.open-esb.net/

Pattern: API Gateway / Backends for Frontends Contexthttps://microservices.io/patterns/apigateway.html

Spring Cloud Gatewayhttps://spring.io/projects/spring-cloud-gateway

Netflix Zuulhttps://github.com/Netflix/zuul

Nginx http://nginx.org/en/index.html

Konghttps://github.com/Kong/kong

WebGL https://www.khronos.org/webgl/

Three.js https://threejs.org/

猜你喜欢

转载自blog.csdn.net/qq_26221775/article/details/130372879