简体   繁体   English

Golang TCP服务器:如何写入HTTP2数据

[英]Golang TCP server: how to write HTTP2 data

I'm new to HTTP/2.0, and I'm trying to set up a TCP server, written in Golang, which receives and writes HTTP/2.0 frames. 我是HTTP / 2.0的新手,我正在尝试建立一个用Golang编写的TCP服务器,该服务器可以接收和写入HTTP / 2.0帧。 I'm having trouble writing any data back to the client. 我无法将任何数据写回到客户端。

The following code snippet shows how the request is handled. 以下代码片段显示了如何处理请求。

conn, err := l.Accept()
if err != nil {
    log.Fatal("could not accept connection:", err)
}
defer conn.Close()

// Every connection starts with a connection preface send first, which has to be read prior
// to reading any frames (RFC 7540, section 3.5)
const preface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
b := make([]byte, len(preface))
if _, err := io.ReadFull(conn, b); err != nil {
    log.Fatal("could not read from connection:", err)
}
if string(b) != preface {
    log.Fatal("invalid preface")
}

framer := http2.NewFramer(conn, conn)

// Read client request (SETTINGS and HEADERS)
readFrames(framer)

// Send empty SETTINGS frame to the client
framer.WriteRawFrame(http2.FrameSettings, 0, 0, []byte{})

// Read clients response (contains empty SETTINGS with END_STREAM flag)
readFrames(framer)

// Prepare HEADERS
hbuf := bytes.NewBuffer([]byte{})
encoder := hpack.NewEncoder(hbuf)
encoder.WriteField(hpack.HeaderField{Name: ":status:", Value: "200"})
encoder.WriteField(hpack.HeaderField{Name: "date", Value: time.Now().UTC().Format(http.TimeFormat)})
encoder.WriteField(hpack.HeaderField{Name: "content-length", Value: strconv.Itoa(len("ok"))})
encoder.WriteField(hpack.HeaderField{Name: "content-type", Value: "text/html"})

// Write HEADERS frame 
err = framer.WriteHeaders(http2.HeadersFrameParam{StreamID: 2, BlockFragment: hbuf.Bytes(), EndHeaders: true})
if err != nil {
    log.Fatal("could not write headers: ", err)
}

// Clients response contains GOAWAY
readFrames(framer)

framer.WriteData(2, true, []byte("ok"))
conn.Close()

The full server can be found here: https://play.golang.org/p/ONA_OoyAMg- 完整的服务器可以在这里找到: https : //play.golang.org/p/ONA_OoyAMg-

It is called by doing a curl: 通过卷曲来调用它:

curl -kv https://127.0.0.1:8080 --http2

As far as I know, after the SETTINGS frames, the connection should be ready for traffic. 据我所知,在“设置”帧之后,连接应已准备就绪,可以进行通信了。 A stream should be opened by sending a HEADERS frame, after which a DATA frame can be send on the open stream. 应该通过发送HEADERS帧来打开流,然后可以在打开的流上发送DATA帧。 However, after sending the HEADERS frame I get the following error message: 但是,在发送HEADERS帧后,我收到以下错误消息:

curl: (16) Error in the HTTP2 framing layer

The clients responds with a GOAWAY. 客户以GOAWAY回应。

I found the issue myself, if fact there were two. 我发现了这个问题,如果实际上有两个。 I found both issues in this article: http://undertow.io/blog/2015/04/27/An-in-depth-overview-of-HTTP2.html 我在本文中发现了两个问题: http : //undertow.io/blog/2015/04/27/An-in-depth-overview-of-HTTP2.html

First of all the required response header containing the status code should be :status instead of :status: . 首先,所需的包含状态码的响应标头应为:status而不是:status:

And finally I didn't respond on the same stream ID from which the request was made and instead tried opening a new stream. 最后,我没有对发出请求的同一流ID进行响应,而是尝试打开一个新的流。

So I should loop over the request frames, look for the stream ID, and use it for writing out the response headers. 因此,我应该遍历请求帧,查找流ID,并将其用于写出响应头。

// Read client request (SETTINGS and HEADERS)
readFrames(framer)
var streamID uint32
for _, frame := range frames {
    if headersframe, ok := frame.(*http2.HeadersFrame); ok {
        streamID = headersframe.StreamID
    }
}

//... Prepare headers

// Write HEADERS frame 
err = framer.WriteHeaders(http2.HeadersFrameParam{StreamID: streamID, BlockFragment: hbuf.Bytes(), EndHeaders: true})
if err != nil {
    log.Fatal("could not write headers: ", err)
}

framer.WriteData(streamID, true, []byte("ok"))
conn.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM