1. Basic concepts of protocol and protocol stack
1.1. What is an agreement
A protocol is a set of rules for communication between computers or devices in a network. Common protocols include IP, TCP, HTTP, POP3, and SMTP.
1.2. What is the protocol stack
In a network, multiple protocols over multiple layers must be used to complete communication. These protocols are grouped together in a hierarchical order to form the Protocol Stack, also known as the Protocol Suite.
1.3. Role of the Agreement
There are two main functions of a network protocol: one is to establish virtual communication between peer layers, the other is to achieve the irrelevance between layers.
1.4. Irrelevance between layers
The so-called inter-level independence means that when the higher level communicates with the adjacent lower level, it only uses the interfaces and services provided by the lower level without knowing the details of the algorithms and protocols adopted by the lower level to achieve this function. The lower levels only use parameters and control information transmitted from the higher level system, which is the irrelevance between the levels.
2. Network protocol family/stack composition
The role of network communication protocols is to establish communication channels on the network and the rules that control the flow of information through the channels. In order to carry out network communication, the communication parties must abide by the communication protocol
3. Common network protocols
TCP/IP: industry standard, open protocol, Internet standard
IPX/SPX: Protocol used by the Netware operating system developed by Novell. IPX is the Internet packet switching protocol, working at the network layer, and SPX is the sequential packet switching protocol, working at the transport layer.
NetBIOS/NetBEUI: A smaller stack of protocols used on IBM and earlier Windows systems and still supported by Windows.
AppleTalk: The network protocol used in Apple’s Mac OS.
(1) TCP/IP protocol
TCP/IP is a layered protocol, as shown in the layer diagram: From the bottom layer to the application layer, they are the physical layer, link layer, network layer, transport layer, and application layer respectively. Data is layered encapsulation, encapsulation is generally in front of the original data with a data control head.
(2) Telnet protocol
Telnet is an application protocol in TCP/IP, which supports terminal emulation. Enables the user to connect to the host so that the host responds as if it were directly connected to a terminal. Telnet uses TCP port 23 on the sender and receiver for dedicated communication.
(3) FTP protocol
FTP uses port TCP20 and port 21. Port 20 is used for data exchange, and port 21 is used for establishing connections. It allows directory and file access, upload and download, but cannot remotely execute files.
Trivial File Transfer Protocol (TFTP), TFTP is connectionless, uses UDP port 69, and is used to Transfer small files when data Transfer errors are Trivial and security is not required.
(4) SMTP protocol
SMTP is the Simple Mail Transfer Protocol (SMTP) designed for email exchange between network systems. Use port 25. SMTP requires only one email address on the receiving end to send messages. The POP3 protocol is used to receive emails. Using port 110
(5) DNS service
The DNS is the Domain Name Service (DNS). It translates a Domain Name into an IP address or an IP address into a Domain Name for resolving a fully qualified Domain Name (FQDN). Use port 53.
(6) DHCP service
DHCP is the Dynamic Host Configuration Protocol (DHCP). The server can provide the following information:
- The IP address
- Subnet Mask
- Domain name (domain name)
- Default Gateway
- DNS
2. HTTP protocol
-
HTTP is short for Hyper Text Transfer Protocol. It is used to Transfer hypertext from the World Wide Web server to the local browser.
-
HTTP is a TCP/ IP-based communication protocol to transfer data (HTML files, image files, query results, etc.).
-
HTTP is an object-oriented protocol belonging to the application layer. Because of its simple and fast way, it is suitable for distributed hypermedia information system. It was put forward in 1990. After several years of use and development, it has been constantly improved and expanded. Currently the sixth version of HTTP/1.0 is used in the WWW. The standardization of HTTP/1.1 is under way, and the proposal of HTTP-NG(Next Generation of HTTP) has been put forward.
-
The HTTP protocol works on a client-server architecture. As the HTTP client, the browser sends all requests to the HTTP server, namely the WEB server, through the URL. The Web server sends response information to the client based on the received request.
Main features of THE HTTP protocol
1, simple and fast: when customers request service to the server, only need to transmit the request method and path. The commonly used request methods are GET, HEAD and POST. Each method specifies a different type of contact between the client and the server. Because HTTP protocol is simple, the HTTP server program size is small, so the communication speed is very fast.
2. Flexibility: HTTP allows the transfer of data objects of any type. The Type being transferred is marked by content-Type.
3. Connectionless: Connectionless means that only one request can be processed per connection. The server disconnects from the customer after processing the request and receiving the reply from the customer. In this way, transmission time can be saved.
Stateless: HTTP is a stateless protocol. Stateless means that the protocol has no memory for transaction processing. The lack of state means that if the previous information is needed for subsequent processing, it must be retransmitted, which can result in an increase in the amount of data transferred per connection. On the other hand, the server responds faster when it doesn’t need the previous information. 5, support B/S and C/S mode.
The relationship between HTTP and URL
HTTP uses Uniform Resource Identifiers (URIs) to transfer data and establish connections. A URL is a special type of URI that contains enough information to find a resource.
URL is an IP address used to identify a resource on the Internet. Take the following URL as an example to describe the components of a common URL:
http://www.aspxfans.com:8080/news/index.asp?boardID=5&ID=24618&page=1#name
Copy the code
As you can see from the above URL, a complete URL consists of the following parts:
1. Protocol part: The protocol part of the URL is HTTP, which indicates that the web page uses HTTP. There are many protocols that can be used on the Internet, such as HTTP, FTP, and so on. In this example, HTTP is used. The “//” after “HTTP” is the delimiter.
2. Domain name: The domain name of the URL is www.aspxfans.com. In a URL, an IP address can also be used as a domain name.
3. Port: The domain name is followed by the port. The domain name and port are separated by colons (:). The port is not a required part of a URL, and the default port is used if the port part is omitted.
4. Virtual directory: The virtual directory starts from the first slash (/) after the domain name to the last slash (/). The virtual directory is also not a required part of a URL. The virtual directory in this example is “/news/”.
5. File name: start from the last slash (/) after the domain name to? Is the filename part, if there is no? Is the file part, if there is no “?” And “#”, then from the last “/” after the domain name to the end, is the filename part. In this case, the file name is index.asp. The file name portion is also not a required part of a URL, and if omitted, the default file name is used.
6. Anchor section: From the “#” to the end, it’s the anchor section. The anchor part in this case is “name”. The anchor part is also not a required part of a URL.
7. Parameter part: From “? The part between the beginning and “#” is the parameter part, also known as the search part, the query part. The parameter part in this example is “boardID=5&ID=24618&page=1”. A parameter can have multiple parameters separated by ampersand (&).
The difference between URIs and urls
A UNIFORM Resource Identifier (URI) is a uniform resource identifier that uniquely identifies a resource.
Each resource available on the Web, such as HTML documents, images, video clips, and programs, is a URI to locate the resource. The URI generally consists of three parts: (1) the naming mechanism for accessing the resource, (2) the host name for storing the resource, and (3) the name of the resource itself, which is represented by the path and emphasizes the resource.
A URL is a Uniform Resource locator. A URL is a specific URI that can be used to identify a resource and specify how to locate the resource.
A URL is a string of characters used to describe information resources on the Internet. It is used in various WWW client and server programs, especially the famous Mosaic program. Using URLS can use a unified format to describe various information resources, including files, server addresses and directories. A URL consists of three parts: (1) protocol (or service mode), (2) IP address (sometimes including port number) of the host where the resource resides, and (3) specific address of the host resource. Such as directory and file name
URN, Uniform resource Name, identifies the resource by name, for examplemailto:[email protected].
Uris are an abstract, high-level concept that defines a uniform resource identity, while urls and UrNs are ways of identifying specific resources. Urls and UrNs are both urIs. Broadly speaking, every URL is a URI, but not necessarily every URI is a URL. This is because URIs also include a subclass, the Uniform Resource Name (URN), which names resources but does not specify how to locate them. The mailto, news, and ISBN URIs above are examples of UrNs.
In Java URIs, an instance of a URI can represent either absolute or relative, as long as it follows the syntax rules for URIs. The URL class, on the other hand, not only conforms to semantics but also contains information to locate the resource, so it cannot be relative. In the Java class library, the URI class does not contain any methods to access resources; its only function is parsing. In contrast, the URL class opens a stream to the resource.
HTTP Request message Request
The client sends an HTTP request message to the server in the following format:
Request line, header, blank line and request data are composed of four parts.
- The request line begins with a method symbol, separated by a space, followed by the request URI and protocol version.
Example of a Get request, using a Charles fetch request:
GET / 562 f25980001b1b106000338. HTTP / 1.1 JPG Host img.mukewang.com the user-agent Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 Accept image/webp,image/*,*/*; Q =0.8 Referer http://www.imooc.com/ accept-encoding gzip, deflate, SDCH accept-language zh-cn,zh; Q = 0.8Copy the code
The first part: the request line, which specifies the type of request, the resource to access, and the HTTP version to use.
The GET request type for the GET, [/ 562 f25980001b1b106000338. JPG] to access resources, is the last part of the bank use HTTP1.1 version.
The second part: the request header, the part immediately after the request line (the first line), describes the additional information to be used by the server
From the second line, the request header, HOST will indicate the destination of the request. User-agent, which can be accessed by both server-side and client-side scripts, is an important basis for browser type detection logic. This information is defined by your browser, sent automatically on every request, etc
Part 3: Blank lines. Blank lines after the request header are required
There must be empty rows even if the request data in Part 4 is empty.
Part four: Request data, also known as the body, can be added to any other data.
The request data for this example is null.
Example of POST request, using Charles fetch request:
POST/HTTP1.1 Host:www.wrox.com user-agent :Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; The.net CLR 2.0.50727; The.net CLR 3.0.04506.648; .NET CLR 3.5.21022) Content-Type: Application/X-www-form-urlencoded Content-Length:40 Connection: Keep-Alive name=Professional%20Ajax&publisher=WileyCopy the code
Part 1: The request line, the first line is a post request, and the HTTP1.1 version. Part 2: Request header, lines 2 through 6. Part 3: blank line, blank line 7. Part 4: Requesting data, line 8.
HTTP Response message Response
Typically, the server will return an HTTP response message after receiving and processing a request from the client.
The HTTP response also consists of four parts: the status line, the message header, the blank line, and the response body.
example
HTTP/1.1 200 OK Date: Fri, 22 May 2009 06:07:21 GMT Content-Type: text/ HTML; charset=UTF-8 <html> <head></head> <body> <! --body goes here--> </body> </html>Copy the code
The first part: status line, which consists of HTTP protocol version number, status code and status message.
The first behavior status line, (HTTP/1.1) indicates HTTP version 1.1, status code 200, and status message (OK)
The second part: the message header, which describes some additional information to be used by the client
Line 2 and line 3 message headers, Date: the Date and time the response was generated; Content-type: Specifies the MIME Type of HTML(text/ HTML). The encoding Type is UTF-8
Part 3: Blank lines. Blank lines after the message header are required
Part four: Response body: the text message returned by the server to the client.
The HTML following the blank line is the response body.
HTTP status code
The status code consists of three digits. The first number defines the categories of the response. There are five categories:
1XX: Indicating message – indicating that the request has been received and processing continues
2xx: Success: The request is successfully received, understood, or accepted
3xx: Redirect – Further action must be taken to complete the request
4XX: Client error – The request has a syntax error or the request cannot be implemented
5xx: Server side error — the server failed to fulfill a valid request
Common status codes:
- 200 OK // The client request is successful
- 400 Bad Request // The client Request has a syntax error and cannot be understood by the server
- 401 Unauthorized // The request is Unauthorized. This status code must be used with the wwW-Authenticate header field using 403 Forbidden // The server receives the request but declines to provide service
- 404 Not Found // The requested resource does Not exist, eg: An incorrect URL was entered
- 500 Internal Server Error // An unexpected Error occurs on the Server
- 503 Server Unavailable // The Server cannot process client requests and may become normal in a period of time
More status codes www.runoob.com/http/http-s…
HTTP request methods
According to the HTTP standard, HTTP requests can use multiple request methods. HTTP1.0 defines three request methods: GET, POST, and HEAD. HTTP1.1 adds five new request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT.
GET requests the specified page information and returns the entity body. A HEAD is similar to a GET request, except that there is no concrete content in the response returned. It is used to retrieve the header and POST to submit data to a specified resource for processing the request (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources. PUT Transmits data from the client to the server instead of the content of the specified document. DELETE requests the server to DELETE the specified page. CONNECT Reserved in HTTP/1.1 for proxy servers that can pipe connections. OPTIONS allows clients to view server performance. TRACE displays the requests received by the server for testing or diagnosis. `
How HTTP works
The HTTP protocol defines how a Web client requests a Web page from a Web server and how the server delivers the Web page to the client. The HTTP protocol uses a request/response model. The client sends a request packet to the server containing the request method, URL, protocol version, request header, and request data. The server responds with a status line containing the protocol version, success or error code, server information, response headers, and response data.
Here are the steps for an HTTP request/response:
1. The client connects to the Web server
An HTTP client, typically a browser, establishes a TCP socket connection with the HTTP port of the Web server (default: 80). For example, www.oakcms.cn.
2. Send HTTP requests
Through the TCP socket, the client sends a text request packet to the Web server. A request packet consists of the request line, the request header, the blank line, and the request data.
3. The server accepts the request and returns an HTTP response
The Web server parses the request and locates the requested resource. The server writes the resource copy to the TCP socket, which is read by the client. A response consists of a status line, a response header, a blank line, and response data.
4. Release the connectionA TCP connection
If the connection mode is set to close, the server actively closes the TCP connection, and the client passively closes the connection to release the TCP connection. If the Connection mode is Keepalive, the connection is kept for a period of time, during which requests can be received.
5. The client browser parses the HTML content
The client browser first parses the status line to see the status code indicating whether the request was successful. Each response header is then parsed, and the response header tells the following several bytes of HTML document and the document’s character set. The client browser reads the response data HTML, formats it according to the HTML syntax, and displays it in the browser window.
For example, enter the URL in the browser address bar and press Enter. The following process occurs:
1. The browser requests the DNS server to resolve the IP address corresponding to the domain name in the URL.
2. After the IP address is resolved, establish a TCP connection with the server based on the IP address and port 80.
3. The browser sends an HTTP request to read the file (the file following the domain name in the URL). The request packet is sent to the server as the third packet of the TCP three-way handshake.
4. The server responds to the browser request and sends the corresponding HTML text to the browser;
5. Release TCP connections.
6. The browser will display the HTML text;
The difference between GET and POST requests
A GET request
GET /books/? Sex =man&name=Professional HTTP/1.1 Host: www.wrox.com user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; Rv :1.7.6) Gecko/20050225 Firefox/1.0.1 Connection: keep-aliveCopy the code
Notice that the last line is empty
A POST request
'POST/HTTP/1.1 Host: www.wrox.com user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; Rv :1.7.6) Gecko/20050225 Firefox/1.0.1 Content-Type: Application/X-www-form-urlencoded Content-Length: 40 Connection: 1.7.6) Gecko/20050225 Firefox/1.0.1 Content-Type: Application/X-www-form-urlencoded Content-Length: 40 Connection: Keep-AliveCopy the code
name=Professional%20Ajax&publisher=Wiley`
1, GET submit, the requested data will be attached to the URL (that is, put the data in the HTTP protocol header), to? Split URL and transfer data, multiple parameters with & link; Cases such as: the login. The action? Name = hyddd&password = idontknow&verify = A5 E5 A0 BD E4% % % % % % BD. If the data is English letters/digits, send it as is, if it is space, convert it to +, if it is Chinese/other characters, then the string is directly encrypted with BASE64, such as: %E4%BD%A0%E5%A5%BD, where XX in % XX is the HEXadecimal ASCII of the symbol.
POST submission: Puts submitted data in the body of an HTTP package. The actual data transferred is indicated in red in the example above
Therefore, data submitted by GET will be displayed in the address bar, while data submitted by POST will not change the address bar
2. Size of transmitted data: First of all, the HTTP protocol does not limit the size of transmitted data, and the HTTP protocol specification does not limit the length of URLS.
The main limitations in actual development are:
GET: The URL length is limited by specific browsers and servers. For example, the URL length is limited by 2083 bytes (2K+35) for Internet Explorer. For other browsers, such as Netscape, FireFox, etc., there is theoretically no length limit, which depends on operating system support.
Therefore, for GET submissions, the transmitted data is limited by the LENGTH of the URL.
POST: Since the value is not sent through the URL, the data is theoretically unlimited. However, each WEB server sets a limit on the size of the data submitted by post. Apache and IIS6 have their own configurations.
3. Security
POST is more secure than GET. For example, if you submit data through GET, the user name and password will appear in clear text on the URL, because (1) the login page may be cached by the browser; Other people can view your browser history, so they can GET your account and password. In addition, submitting data using GET can create a cross-site Request Forgery attack
Http Get, POST, and SOAP protocols all run over Http
(1) GET: The request parameter is attached to the URL as a sequence of key/value pairs (query string). The length of the query string is limited by web browsers and Web servers (for example, IE supports a maximum of 2048 characters). It is not suitable for the transmission of large data sets
(2) POST: Request parameters are transmitted in a different part of the HTTP title (named Entity Body), which is used to transmit form information, so the Content-Type must be set to: Application/X-www-form-urlencoded. Post is designed to support user fields on web forms, and its parameters are also transmitted as key/value pairs. But: It does not support complex data types, because POST does not define the semantics and rules for transferring data structures.
(3) SOAP: is a dedicated version of HTTP POST, which follows a special XML message format. The content-type is set to text/ XML. Any data can be xmlized.
The Http protocol defines many ways to interact with the server. The most basic four methods are GET,POST,PUT, and DELETE. A URL address is used to describe a resource on the network, and THE GET, POST, PUT, and DELETE operations in HTTP correspond to searching, modifying, adding, and deleting this resource. The most common ones are GET and POST. GET is used to obtain or query resource information, and POST is used to update resource information.
Let’s look at the difference between GET and POST
- The data submitted by GET is placed after the URL, ending with? Split URL and transfer data with & between parameters such as editposts.aspx? Name =test1&id=123456. The POST method places the submitted data in the Body of the HTTP package.
- The data submitted by GET is limited in size (because browsers have limits on the length of urls), while the data submitted by the POST method is not limited.
- GET uses Request.QueryString to GET the value of a variable, while POST uses Request.Form to GET the value of a variable.
- When submitting data through GET, the user name and password will appear in the URL. If the page can be cached or someone else can access the machine, the user’s account and password can be obtained from the history.
3. Node.js core module —- HTTP module
Starting a local server requires the HTTP core module in Node.js
- HTTP – the module provides an API for setting up local servers, which we first introduced in the project;
let http = require('http')
Copy the code
Once introduced, we use the http.createserver () method to get a server instance.
let server = http.createServer() The createServer() method returns a server instance, so we need a variable to receive it
Copy the code
- After the above two steps, we have set up a server instance, and then we bind the server instance to receive
request
Handler function, code as follows:
server.on('request'.(req, res) = > {
//console.log(req.url) // Get the requested path (request path always begins with "/")
res.end('hello,Node.js')})When the server receives the request sent by the client, it will call the following handler function. The handler function receives two parameters: request message object and response message object.
Copy the code
- Bind the listening port number and start the server. The code is as follows:
server.listen(3000.() = > {
console.log('Server started successfully, you can get data by visiting http://127.0.0.1:3000/ ~~')})// server.listen() is used to bind the listening port number. The second argument can be passed. When the server is successfully started, the following callback function is triggered
Copy the code
4. After this simple operation has completed the simple construction of a server, next we will realize a requirement:
- When we visit “http://127.0.0.1:3000/login”, the server returns “login page”
- When we visit “http://127.0.0.1:3000/register”, the server returns “register page”
- When we visit “http://127.0.0.1:3000/”, the server returns “Index Page”
- When we visit “http://127.0.0.1:3000/product”, the server returns the product information list
To achieve this requirement, we just need to get the user’s request path in the event handler function that the binding server listens to, and then return different data according to the different path. This is not difficult. See the code for details:
let http = require('http')
let server = http.createServer()
server.on('request'.(req, res) = > {
let url = req.url // Get the requested path (the requested path always begins with a '/')
if (url === '/') {
res.end('index page')}else if (url === '/login') {
res.end('login page')}else if (url === '/register') {
res.end('register page')}else if (url === '/product') {let arr = [
{
name: 'iphone X'.price: 8888
},
{
name: 'iphone 7'.price: 4320}]// The data type of the response must be string or binary
res.end(JSON.stringify(arr))
} else {
res.end('404 NOT found')
}
})
server.listen(3000.() = > {
console.log('Server started successfully, you can go to http://127.0.0.1:3000/')})Copy the code
Request different path, the server returned different content to us, and displayed in the web page.
Sets the status code and response header
response.writeHead(200, { 'Content-Type': 'text/plain' });
Copy the code
Setting the response header
response.setHeader('Content-Type'.'text/html');
Copy the code
Write content
response.write(fileData);
Copy the code
The end of the response
response.end();
Copy the code
Learn more about HTTP modules:Nodejs. Cn/API/HTTP. Ht…
Today’s study is mainly to understand the network and HTTP protocol, the use of HTTP module is relatively simple.
Date: 2021/11/18
Learning Reference Video: *www.bilibili.com/video/BV1i7…
Learning Reference document Reference section related video copywriting and courseware, only for personal learning and recording