栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Go服务器中设置HTTP预告片?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Go服务器中设置HTTP预告片?

使用bytes.Buffer,然后将其包装为散列,例如:

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.EnpreToString(h.h.Sum(nil)))    h.b.WriteTo(w)}//handlerfunc Handler(w http.ResponseWriter, r *http.Request) {    hb := NewHashedBuffer(sha256.New())    hb.Write([]byte("stuff"))    hb.Output(w)}

截至目前,您无法设置预告片标头,这是一个未解决的问题。

有一种解决方法,劫持连接(来自上述问题):

// 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 Bbuf.WriteString("0rn") // eoft.Write(buf)buf.WriteString("rn") // end of trailersbuf.Flush()conn.Close()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393317.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号