如何在Go服务器中设置HTTP预告片? [英] How to set HTTP trailers in Go server?
问题描述
我想通过对响应体进行散列来计算响应体的实体标签。当我计算实体标签时,将实体标签添加到响应标头为时已晚。我想将实体标签添加到预告片。我看到net / http软件包支持编写预告片,但我无法弄清楚如何使用它们。
预告片代码位于 https://golang.org/src/pkg/net/http/transfer.go 。如何从我的应用程序中设置预告片?
使用bytes.Buffer,并在同一时间,类似于:
类型HashedBuffer结构{
h hash.Hash
b bytes.Buffer
}
$ b $ func NewHashedBuffer(h hash.Hash)* HashedBuffer {
return& HashedBuffer {h:h}
}
func(h * HashedBuffer)Write(p [] byte)(n int,err error){
n,err = hbWrite(p)
hhWrite(p)
return
}
func(h * HashedBuffer)Output(w http.ResponseWriter){
w.Header()。Set(ETag,hex.EncodeToString(hhSum(nil)))
hbWriteTo(w)
}
//处理程序
func处理程序(w http.ResponseWriter,r * http.Request){
hb:= NewHashedBuffer sha256.New())
hb.Write([] byte(stuff))
hb.Output(w)
}
截至目前,您无法设置预告片标题,还有一个开放的 issue 。
有一个解决方法,劫持连接(从上面的问题):
// TODO:服务器还没有办法设置预告片
//不劫持,所以现在就做,只是为了测试客户端。
//稍后,在Go 1.4中,应该隐含的是,在初始写入之后,任何突变
//到w.Header()都是后缀为
//的发送,if并且仅当它们之前被声明为
// w.Header()。Set(Trailer,..keys ..)
w。(Flusher).Flush()
conn ,buf,_:= w。(Hijacker).Hijack()
t:= Header {}
t.Set(Server-Trailer-A,valuea)
t。 Set(Server-Trailer-C,valuec)//跳过B
buf.WriteString(0 \r\\\
)// eof
t.Write(buf)
buf.WriteString(\r\\\
)//尾部结尾
buf.Flush()
conn.Close()
I want to compute the entity tag for a response by hashing the response body as it's written out. By the time I compute the entity tag, it's too late to add the entity tag to the response header. I'd like to add the entity tag to the trailer. I see the the net/http package has support for writing trailers, but I cannot figure out how to use them.
The trailer code is in https://golang.org/src/pkg/net/http/transfer.go. How do I set a trailer from my application?
use a bytes.Buffer, and wrap it to hash in the same time, something like:
type HashedBuffer struct {
h hash.Hash
b bytes.Buffer
}
func NewHashedBuffer(h hash.Hash) *HashedBuffer {
return &HashedBuffer{h: h}
}
func (h *HashedBuffer) Write(p []byte) (n int, err error) {
n, err = h.b.Write(p)
h.h.Write(p)
return
}
func (h *HashedBuffer) Output(w http.ResponseWriter) {
w.Header().Set("ETag", hex.EncodeToString(h.h.Sum(nil)))
h.b.WriteTo(w)
}
//handler
func Handler(w http.ResponseWriter, r *http.Request) {
hb := NewHashedBuffer(sha256.New())
hb.Write([]byte("stuff"))
hb.Output(w)
}
As of right now, you can't set trailer headers, there's an open issue about it.
There's a workaround, hijack the connection (from the above issue) :
// TODO: There's no way yet for the server to set trailers
// without hijacking, so do that for now, just to test the client.
// Later, in Go 1.4, it should be be implicit that any mutations
// to w.Header() after the initial write are the trailers to be
// sent, if and only if they were previously declared with
// w.Header().Set("Trailer", ..keys..)
w.(Flusher).Flush()
conn, buf, _ := w.(Hijacker).Hijack()
t := Header{}
t.Set("Server-Trailer-A", "valuea")
t.Set("Server-Trailer-C", "valuec") // skipping B
buf.WriteString("0\r\n") // eof
t.Write(buf)
buf.WriteString("\r\n") // end of trailers
buf.Flush()
conn.Close()
这篇关于如何在Go服务器中设置HTTP预告片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!