博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.Golang的Http源码阅读(http协议)
阅读量:6674 次
发布时间:2019-06-25

本文共 3658 字,大约阅读时间需要 12 分钟。

解释服务器的几个概念:

  1. Request 用户请求的信息
  2. Response 服务器返回的信息
  3. Conn 用户的每次请求链接
  4. Handler 处理请求和生成返回信息的处理逻辑

先写个小程序看看

func MyConn() {    // 监听9090端口    listener, _ := net.Listen("tcp", "localhost:9090")    defer listener.Close()        // 建立用户请求的连接    conn, _ := listener.Accept()    defer conn.Close()        // 读取Request的内容    buff := make([]byte, 10000)    len, _ := conn.Read(buff)    fmt.Fprintln(os.Stdout, string(buff[:len]))}

对于代码,要解释的是读取conn的内容,你可以把conn看做是数据流,先看下Golang中的源码,先不去掉注释,Golang的注释真的是简单好懂,看完了注释,你就应该能理解Conn是什么了。

// Multiple goroutines may invoke methods on a Conn simultaneously.type Conn interface {    // Read reads data from the connection.    // Read can be made to time out and return an Error with Timeout() == true    // after a fixed time limit; see SetDeadline and SetReadDeadline.    Read(b []byte) (n int, err error)    // Write writes data to the connection.    // Write can be made to time out and return an Error with Timeout() == true    // after a fixed time limit; see SetDeadline and SetWriteDeadline.    Write(b []byte) (n int, err error)    // Close closes the connection.    // Any blocked Read or Write operations will be unblocked and return errors.    Close() error    // LocalAddr returns the local network address.    LocalAddr() Addr    // RemoteAddr returns the remote network address.    RemoteAddr() Addr    // SetDeadline sets the read and write deadlines associated    // with the connection. It is equivalent to calling both    // SetReadDeadline and SetWriteDeadline.    //    // A deadline is an absolute time after which I/O operations    // fail with a timeout (see type Error) instead of    // blocking. The deadline applies to all future and pending    // I/O, not just the immediately following call to Read or    // Write. After a deadline has been exceeded, the connection    // can be refreshed by setting a deadline in the future.    //    // An idle timeout can be implemented by repeatedly extending    // the deadline after successful Read or Write calls.    //    // A zero value for t means I/O operations will not time out.    SetDeadline(t time.Time) error    // SetReadDeadline sets the deadline for future Read calls    // and any currently-blocked Read call.    // A zero value for t means Read will not time out.    SetReadDeadline(t time.Time) error    // SetWriteDeadline sets the deadline for future Write calls    // and any currently-blocked Write call.    // Even if write times out, it may return n > 0, indicating that    // some of the data was successfully written.    // A zero value for t means Write will not time out.    SetWriteDeadline(t time.Time) error}

在浏览器上输入 localhost:9090 ,按下回车键,你的命令行会有一下的内容

GET / HTTP/1.1            //请求行:请求方法(GET), 请求的URL(/),HTTP协议(HTTP/1.1)Host: localhost:9090        //服务器主机名User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0    //浏览器信息Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    //客户端能接受的mineAccept-Language: zh-CN,en-US;q=0.7,en;q=0.3    //请求头允许客户端声明它可以理解的自然语言,以及优先选择的区域方言Accept-Encoding: gzip, deflate //是否支持流压缩Connection: keep-alive        //控制当前传输结束后是否保持网络连接Upgrade-Insecure-Requests: 1    //Sends a signal to the server expressing the client’s preference for an encrypted and authenticated response, and that it can successfully handle the upgrade-insecure-requests directive.//空行,分割请求头和消息体//消息体,POST传递

关于请求头数据的更详细的资料可以看

目前比较重要的是这个数据的第一行

GET / HTTP/1.1

如果你在地址栏输入的网址是 localhost:9090/hello ,第一行会是这样

GET /hello HTTP/1.1

对于服务器,首先需要知道协议(是GET还是POST),然后需要知道你想要访问的地址。对于了解MVC的同学肯定了解Controller,在Golang中,也存在类似的路由机制,你可以将访问的URL跟Handler一一对应(感觉是不是有点像键值对),根据请求的URL就可以对请求内容进行操作并返回了,先写到这里,下一节再想想怎么写Handler.

转载地址:http://lmgxo.baihongyu.com/

你可能感兴趣的文章
十、cent OS开启APR模式报错:configure: error: Found APR 1.3.9. You need version 1.4.3 or newer installed...
查看>>
八、阻塞等待异步结果FutureTask
查看>>
中文字符按拼音首字母排序(转)
查看>>
【mysql】一次有意思的数据库查询分析。
查看>>
Spring 框架中注释驱动的事件监听器详解
查看>>
C++获取window临时路径
查看>>
Python(面向对象编程—1)
查看>>
自己封装的数据通信服务组件DotNet.ServiceModel
查看>>
Docker创建虚机和swarm
查看>>
JSON入门学习
查看>>
一个很好的UML工具
查看>>
[转]无需看到你的脸就能认出你——实现Beyond Frontal Faces: Improving Person Recognition Using Multiple Cues...
查看>>
函数Curry化
查看>>
二进制补码,原码,反码和移码
查看>>
Default Constructor 的建构操作
查看>>
函数中的不定长参数研究 *and**
查看>>
hive如何执行mr
查看>>
马士兵hadoop2.7.3_yarn笔记
查看>>
python自编程序实现——robert算子、sobel算子、Laplace算子进行图像边缘提取
查看>>
Extjs5项目进行中:打开新面板并加载数据(三)
查看>>