douwo1517 2018-08-24 10:33
浏览 99
已采纳

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

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. 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

SM

"
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-

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. However, after sending the HEADERS frame I get the following error message:

curl: (16) Error in the HTTP2 framing layer

The clients responds with a GOAWAY.

  • 写回答

1条回答 默认 最新

  • duanposhi3641 2018-08-24 14:29
    关注

    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

    First of all the required response header containing the status code should be :status instead of :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.

    So I should loop over the request frames, look for the stream ID, and use it for writing out the response headers.

    // 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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面