Analog HTTP requests via Golang based on TCP Sockets (cont.)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Formatting problems with HTTP messages

Then yesterday's write. Yesterday's article has an uncertain place, that is, after the establishment of a TCP connection, the data sent to the server, including the command, header and body format. These three sections are how to split, I refer to the format of the Postman Preview and the format of the telnet sent guess, the original thought line is directly through the \n distinction, and the header and the subject is two \n to differentiate. This send is also able to parse the normal, today went to read the "HTTP authoritative guide" and Golang net/http package, the specific understanding of the next exactly how to distinguish.

In Chapter 3.2 of the HTTP authoritative guide, the components of the message refer to:

Each line ends with a two-character line termination sequence, including a carriage return \ R and a newline character \ n, which can be written CRLF. Although the HTTP specification should use CRLF to indicate termination, a robust application should also accept a single newline character \ n as the termination of a row.

This also explains the question I left yesterday. To put it simply, my guess is that I can send success because the server is awesome. The standard notation is two CRLF, not a \n . The HTTP write is in the net/http/request.go No. 365 line of the Write function. As you can see, each write is \r\n ended. Here is the write command and the write header User-Agent .

// Header linesfmt.Fprintf(w, "Host: %s\r\n", host)// Use the defaultUserAgent unless the Header contains one, which// may be blank to not send the header.userAgent := defaultUserAgentif req.Header != nil {if ua := req.Header["User-Agent"]; len(ua) > 0 {userAgent = ua[0]}}if userAgent != "" {fmt.Fprintf(w, "User-Agent: %s\r\n", userAgent)}

HTTP time-out issues

By reading Golang source code, and then combined with yesterday's understanding, understand the github.com/astaxie/beego/httplib package, set the request timeout is two parameters.

func (b *BeegoHttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *BeegoHttpRequest

The HTTP protocol first establishes a TCP connection to the server and then sends an HTTP message to the server. This involves two and server-side steps, and requires two time-out judgments. One is to determine if the TCP connection is timed out, and the other is to determine if the HTTP message is sent and the response timed out.

The Send time net/http/client.go -out occurs in a doFollowingRedirects function in line No. 278.

if c.Timeout > 0 {type canceler interface {CancelRequest(*Request)}tr, ok := c.transport().(canceler)if !ok {return nil, fmt.Errorf("net/http: Client Transport of type %T doesn't support CancelRequest; Timeout not supported", c.transport())}timer = time.AfterFunc(c.Timeout, func() {reqmu.Lock()defer reqmu.Unlock()tr.CancelRequest(req)})}

TCP establishes a connection timeout that is net/http/transport.go handled in the 495-line dialConn function.

if d := t.TLSHandshakeTimeout; d != 0 {timer = time.AfterFunc(d, func() {errc <- tlsHandshakeTimeoutError{}})}

The judgment for the timeout is a time package-based AfterFunc function that calls the function after the specified time. If you have not received a response or established a connection over the specified time, cancel the request.

Reference documents

    • "1" http authoritative guide
    • "2" "Computer network-Shehiren"

Original link: Based on TCP sockets, through the Golang analog HTTP request (continued), reprint please indicate the source!

Related Article

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.