解析HTTP/2原始响应的Golang实现
时间:2024-02-28
浏览:223
作者:佚名
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《解析HTTP/2原始响应的Golang实现》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
我有一个特定的情况,我需要将 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) ,它完全符合我的需要,但它无法使用 格式错误的 http 版本 http/2 解析 http/2,但是它适用于 http/1.1 和 http/1.0。另外,使用 http.defaultclient.do() 进行请求后,可以看到响应的字段 proto 包含 http/2.0,这意味着 http/2 没有问题。
有什么想法如何解析此响应吗?
解决方案
总结一下,解析错误的原因是http.parsehttpversion没有解析http/2,将前缀7字节替换成http/2.0来修复,或者为net/http提供pr支持来解析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>以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
