php - Socket accepts an http request. Where can we determine the end of the request by reading?
三叔
三叔 2017-07-03 11:40:16
0
1
938

I want to make a http proxy gadget using go:

   func main() {
        listener, err := net.Listen("tcp", ":8080")
        if err != nil {
            log.Panic(err)
        }
        for {
            client, err := listener.Accept()
            if err != nil {
                log.Panic(err)
            }
            go handleClientRequest(client)
        }
    }
    
    func handleClientRequest(client net.Conn) {
        if client == nil {
            return
        }
        defer client.Close()
    
        var buf [1024]byte
        n, err := client.Read(buf[:])
        if err != nil {
            log.Println(err)
            return
        }
        fmt.Println("recv msg:", string(buf[0:n]))
    }

1024 bytes may not be enough, there must be an end mark, right?

三叔
三叔

reply all(1)
三叔

Http protocol is based on TCP protocol. When the browser needs to obtain web page data from the server, it will issue an Http request. Http will establish a connection channel to the server through TCP. When the data required for this request is completed, Http will immediately disconnect the TCP connection

TCP protocol releases the connection by sending FIN packet, so the FIN packet is the end mark, which means that it is received until the connection is disconnected.

But

As time goes by, the HTML page becomes more complex, and there may be many pictures embedded in it. At this time, it is inefficient to establish a TCP connection every time to access the picture. Therefore, Keep-Alive was proposed to solve the problem of low efficiency. Starting from HTTP/1.1, Keep-Alive is enabled by default to keep the connection feature. Simply put, when a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. If the client When you visit the webpage on this server again, you will continue to use this established connection. Keep-Alive does not maintain the connection permanently. It has a retention time, which can be set in different server software (such as Apache). Although the TCP connection is maintained for a period of time here, this time is limited and will still be closed at the time point, so we also regard it as closing after each connection is completed.

So how to judge the end after turning on Keep-Alive?
In fact, the HTTP header has content-length, which is the length of the HTTP packet body

Of course, there is also a case where there is no content-length, that is, chunked encoding is used. The data is divided into a series of blocks and sent, and each block has a size description. Even if the server does not know the size of the entire entity when generating the header (usually because the entity is generated dynamically), chunked encoding can still be used to transmit a number of chunks of known size.

The encoding uses several Chunks and ends with a chunk marked with a length of 0. Each Chunk consists of two parts. The first part is the length and length unit of the Chunk (generally not written), and the second part specifies the length. Content, each part is separated by CRLF (full name carriage
return/line feed carriage return/line feed). The content in the last Chunk of length 0 is called footer, which is some additional Header information (usually can be ignored directly).

In addition, the HTTP message header and message body are separated by CRLF (carriage return and line feed), rnrn

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!