-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http Trailer #22
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
一般HTTP请求或响应包含
Header
和Body
,如果有些信息是在Body发完才知道,比如Body的校验、数字签名、后期处理结果等希望在同一个请求里面延后发送,就需要用到Trailer
。传输格式
一个带
Trailer
的响应例子:使用
Trailer
有几个注意事项:Header
里面的Transfer-Encoding
必须是chunked
,也就是说不能指定Content-Length
。Trailer
的字段名字必须在Header
里面提前声明,比如上面的Trailer: Expires
。Trailer
在Body
发完之后再发,格式和Header
类似。实战
用Go实现一个HTTP客户端,对所发的
Body
计算MD5
并通过Trailer
传给服务端。服务端收到请求并对Body进行校验。
服务端程序:
客户端程序:
运行结果:
通过
nc
来看服务端收到的请求可以看到服务端在读完body之前只能知道有
Md5
这个Trailer
,值为空;读完body之后,能正常拿到Trailer
的Md5
值。Go语言使用
Trailer
也有几个注意事项:req.ContentLength
必须设置为0
或者-1
,这样body
才会以chunked
的形式传输。req.Trailer
需要在发请求之前声明所有的key字段,在body发完之后设置相应的value,如果客户端提前知道Trailer
的值的话也可以提前设置,比如上面例子里面的size
字段。body
之后Trailer
不允许再更改,否则可能会因为map并发读写,导致程序panic,同样的道理服务端在读body
的时候也不应该对Trailer
有引用。body
之后才能知道Trailer
的值。参考:
The text was updated successfully, but these errors were encountered: