HTTP request and response

It seems that the nuggets gave me the id of the anchor point in the source code, so you can go directly here to view the use of the anchor point

Preface:

As a qualified front-end, I can deal with numerous network requests every day. So, knowing how the bottom layer of the network is sent from the source, and how the receiving source is processed, is a qualitative improvement in debugging your work.

Just recently, I chose the more common HTTP request and response in my work. There are mistakes can be through the following TWO-DIMENSIONAL code contact me, welcome to point out the error.

Background:

The associated protocols on the Internet are collectively known as the TCP/IP protocol family.

We only need to understand the HTTP protocol. So one of the more important aspects of this protocol family is layering. From top to bottom, it is the application layer, transmission layer, network layer and network data link layer. Refer to the “TCP/IP Reference Model “diagram.


TCP/IP reference model:


Directory:

  • The application layer
  • The transport layer
  • The network layer
  • The link layer

The HTTP request:

1. Application layer:

This layer contains many common protocols, such as FTP, SMTP, and HTTP.

When an HTTP request is made, usually by the sender (client), it reaches the application layer in the TCP/IP reference model. At work is the HTTP protocol, which generates AN HTTP request message (containing the request line, the request header, and sometimes the request body, as explained in more detail in the next article) to the target (that is, the receiver server) and sends it down to the next level. Note that the message is still complete.

Summary: The application layer is used to generate packets.

2. Transport Layer:

When an HTTP request passes through the application layer, it reaches the transport layer. The main function of this layer is to transmit packets, but the packets here have been tampered with.

In order to facilitate transmission, TCP divides a string of complete request packets into multiple packet segments by serial number and marks each packet with serial number and port number to form IP packets. Finally, the segmented packets are transmitted to the network layer. Notice, this is the clip.

Summary: The transport layer is used to slice messages.

3. Network Layer (IP layer)

The network here, not the network of signals as we normally understand it, is a structural model layer. First, let’s understand the difference between IP and MAC

Well, once you get to the network layer, that’s when the whole TCP/IP reference model architecture is key, because it determines where to send and in what order.

The underlying protocol is the Internet protocol, or IP (if you want to connect to the Internet). It connects the client and server through routing as a bridge and IP protocol as a communication tool.

The function of routing is to find the corresponding destination host (receiver) for the received processed request packets (packets). Click on me to see how routing works

In addition, the main protocol used for routing is IP protocol. Therefore, the network layer is also known as the IP layer.

Summary: The network layer can be regarded as a guidepost, that is, it looks for the address of the target server in the routing table according to the packet.

4. Link layer:

This layer is the closest layer to the device, the physical layer (OSI model). That is, closest to the server interface.

But, this layer is not working, why? Have you ever seen a leader write business code?

Of course, it wasn’t nothing. One of the functions of the link layer is to detect whether there are any errors in the packets you have processed. That’s testing. Test what? I don’t know how to test it, but I will give you my guess. I hope you can tell me. Thank you. What I think the link layer does is determine whether there is a physical address, whether there is a destination address, etc. balabala… When the confirmation is correct, it is handed over to the server. The server parses the received message and returns a response with response data, and the response begins. Summary: The link layer is used to detect and confirm packet data, and then acts as an interface to connect to servers or hardware.


The HTTP response:

The process is like retracing the road you just came to, but this time backwards.

Link layer -> network layer -> Transport layer -> application layer -> client, usually browser, and a single request and response is completed.


Doubt points:

Difference between IP and MAC

  • First, I’ll explain the two difficult distinctions between IP addresses and MAC addresses.
  • An IP address is an Internet protocol address, which is a logical address, which is abstract, which is given to each Internet packet, which is dynamic and can be different.
  • A MAC address is a physical address that exists and is provided by the device’s network card. It is usually fixed.
  • Colloquially speaking, AN IP address can be understood as your house number, while a MAC address is your personal ID card.
  • The two are not necessarily related.

Then go back to the network layer

At this point, you might be wondering, right?*

Why have a MAC address when you have an IP address?

Here is an answer based on Zhihu:

Because IP address is defined in the third layer – network layer, it has the meaning of the third layer; MAC addresses are defined at Layer 2 and have layer 2 meaning. Therefore, in the same network segment, that is, the LAN, only need to use the MAC address can be located; However, if the network segment is crossed (that is, the Internet environment), it is difficult to locate the network by MAC address alone, because the same MAC address may appear. So you need an IP address to address.

So why does the route know where the received message is going?

It turns out that the packet contains the IP address of the target host. An IP address consists of two parts, a network address and a host address. In addition, based on the IP address of the peer, you can not only determine whether the peer is in the same network with the host, but also obtain the host address of the peer.

So, there are two kinds of routing processing mechanisms,

  • The firstFor those that do not need to cross network segments, that is, within a local area network (LAN) : this is the time for the “switch” to handle.

This reflects the role of the MAC address, because the MAC address is a physical address, hardware address, provided by the network adapter, generally cannot be changed.

And the path through the network layer “ARP”, will be the IP address of the target host (target server), parsed into the target host with ARP table corresponding to the corresponding MAC address, and then tell the switch, then turn exchange opportunities according to their own published MAC address and MAC address of the corresponding port number one to one correspondence, then forward.

If the corresponding port number is found in the forwarding table, then congratulations, your express is SF, the switch directly to the target host in one step; If can’t find, ha ha, you estimate greedy, found the yunda… It will forward each port once, always once, very rough method, natural efficiency is low.

  • The secondFor the need to cross network segments, i.e. access to the Internet:

    This is then handed over to the gateway.

This time will need to use the IP address in the request message, to find the target host IP location, and then tell the gateway IP address, gateway matching corresponding routing, and then find the IP address of the corresponding in the routing table, and then according to the network address of the IP address, judgment is across the network or Intranet, and then make a decision to go to which network jump.

Then go back to the network layer – routing


Conclusion:

A complete HTTP request, from sent to received, goes through the application layer -> transport layer -> network layer -> link layer, even down to the physical layer.


In the next article, I’ll explain the message in an HTTP request in more detail, which means that if you want to know what goes into an HTTP request, check out my next article.


Personal ability is limited, there is a mistake to scan the following TWO-DIMENSIONAL code to contact me (reply to the public number I can receive), errata, common progress.