发生了意外的EOF错误,尝试从解析的原始HTTP请求发送请求时。

huangapple go评论83阅读模式
英文:

Unexpected EOF while trying to send a request from a parsed Raw HTTP request

问题

我正在使用golang解析原始的HTTP请求,然后发送一个请求到URL并打印输出。

代码:

stra := `GET / HTTP/1.1
Host: google.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close`
r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
if err != nil {
    fmt.Println(err.Error())
    os.Exit(1)
}
u, err := url.Parse("https://" + r.Host + r.URL.String())
if err != nil {
    panic(err)
}
req.URL = u
transport := http.Transport{}
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{Transport: &transport}
resp, err := client.Do(req)

错误:

unexpected EOF

请帮忙看看。

英文:

I'm using golang to parse a raw HTTP request and then send a request to the URL and print the output.

Code:

stra := `GET / HTTP/1.1
Host: google.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close`
	r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	u, err := url.Parse("https://" + r.Host + r.URL.String())
	if err != nil {
		panic(err)
	}
	req.URL = u
	transport := http.Transport{}
	transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
	client := &http.Client{Transport: &transport}
	resp, err := client.Do(req)

Error:

unexpected EOF

Kindly help.

答案1

得分: 2

http头部必须以\r\n\r\n结尾,但是对于http.ReadRequest来说,似乎\n也可以。下面的代码确实有效。

package main

import (
	"bufio"
	"fmt"
	"net/http"
	"os"
	"strings"
)

func main() {
	stra := `GET / HTTP/1.1
Host: google.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close

`
	req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	fmt.Println(req)
}
英文:

http header must ends with \r\n\r\n, but it seems that \n is ok for http.ReadRequest? the code below really works.

package main

import (
	"bufio"
	"fmt"
	"net/http"
	"os"
	"strings"
)

func main() {
	stra := `GET / HTTP/1.1
Host: google.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Connection: close

`
	req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
	fmt.Println(req)
}

huangapple
  • 本文由 发表于 2021年10月7日 22:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69482532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定