Golang解析原始HTTP / 2响应

问题描述 投票:1回答:1

我有一个特殊的情况,我需要将HTTP / 2响应输出解析为Go的http.Response。响应本身具有默认结构:

$ curl --include https://google.com

HTTP/2 301
location: https://www.google.com/
content-type: text/html; charset=UTF-8
date: Mon, 15 Jun 2020 11:08:39 GMT
expires: Wed, 15 Jul 2020 11:08:39 GMT
cache-control: public, max-age=2592000
server: gws
content-length: 220

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

状态和主体本身无关紧要,仅是示例。

http库具有功能ReadResponse(r *bufio.Reader, req *Request) (*Response, error),该功能完全可以满足我的需要,但是它无法解析具有malformed HTTP version HTTP/2的HTTP / 2,但是它对于HTTP / 1.1和HTTP / 1.0正常工作。另外,在使用http.DefaultClient.Do()进行请求之后,您可以看到响应的字段Proto包含HTTP/2.0,这意味着HTTP / 2没有问题。

任何想法如何解析此响应?

http go http2
1个回答
0
投票

总结讨论,解析错误的原因是http.ParseHTTPVersion不解析HTTP/2,将前缀7个字节替换为HTTP/2.0以进行修复,或者为net/http提供解析支持HTTP/2

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "io"
    "net/http"
)

func main() {
    fmt.Println("Hello, playground")
    req, _ := http.NewRequest("GET", "https://google.com", nil)
    {
        resp, err := http.ReadResponse(bufio.NewReader(bytes.NewBuffer(httpbody)), req)
        fmt.Println(resp, err)
        // err is malformed HTTP version "HTTP/2", because http.ParseHTTPVersion not parse "HTTP/2"
    }

    {
        body := bytes.NewBuffer(httpbody)
        prefix := make([]byte, 7)
        n, err := io.ReadFull(body, prefix)
        if err != nil {
            panic("handler err")
        }
        fmt.Println(n, err, string(prefix))
        if string(prefix[:n]) == "HTTP/2 " {
            // fix HTTP/2 proto
            resp, err := http.ReadResponse(bufio.NewReader(io.MultiReader(bytes.NewBufferString("HTTP/2.0 "), body)), req)
            fmt.Println(resp, err)
        } else {
            // other proto
            resp, err := http.ReadResponse(bufio.NewReader(io.MultiReader(bytes.NewBuffer(prefix[:n]), body)), req)
            fmt.Println(resp, err)
        }
    }
}

var httpbody = []byte(`HTTP/2 301
location: https://www.google.com/
content-type: text/html; charset=UTF-8
date: Mon, 15 Jun 2020 11:08:39 GMT
expires: Wed, 15 Jul 2020 11:08:39 GMT
cache-control: public, max-age=2592000
server: gws
content-length: 220

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>`)

输出:

Hello, playground
<nil> malformed HTTP version "HTTP/2"
7 <nil> HTTP/2 
&{301 301 HTTP/2.0 2 0 map[Cache-Control:[public, max-age=2592000] Content-Length:[220] Content-Type:[text/html; charset=UTF-8] Date:[Mon, 15 Jun 2020 11:08:39 GMT] Expires:[Wed, 15 Jul 2020 11:08:39 GMT] Location:[https://www.google.com/] Server:[gws]] 0xc0000902c0 220 [] false false map[] 0xc0000f2000 <nil>} <nil>
© www.soinside.com 2019 - 2024. All rights reserved.