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

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

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

问题

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

代码:

  1. stra := `GET / HTTP/1.1
  2. Host: google.com
  3. Upgrade-Insecure-Requests: 1
  4. 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
  5. 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
  6. Accept-Encoding: gzip, deflate
  7. Accept-Language: en-US,en;q=0.9
  8. Connection: close`
  9. r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
  10. if err != nil {
  11. fmt.Println(err.Error())
  12. os.Exit(1)
  13. }
  14. u, err := url.Parse("https://" + r.Host + r.URL.String())
  15. if err != nil {
  16. panic(err)
  17. }
  18. req.URL = u
  19. transport := http.Transport{}
  20. transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  21. client := &http.Client{Transport: &transport}
  22. resp, err := client.Do(req)

错误:

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

  1. stra := `GET / HTTP/1.1
  2. Host: google.com
  3. Upgrade-Insecure-Requests: 1
  4. 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
  5. 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
  6. Accept-Encoding: gzip, deflate
  7. Accept-Language: en-US,en;q=0.9
  8. Connection: close`
  9. r, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
  10. if err != nil {
  11. fmt.Println(err.Error())
  12. os.Exit(1)
  13. }
  14. u, err := url.Parse("https://" + r.Host + r.URL.String())
  15. if err != nil {
  16. panic(err)
  17. }
  18. req.URL = u
  19. transport := http.Transport{}
  20. transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
  21. client := &http.Client{Transport: &transport}
  22. resp, err := client.Do(req)

Error:

  1. unexpected EOF

Kindly help.

答案1

得分: 2

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

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. )
  9. func main() {
  10. stra := `GET / HTTP/1.1
  11. Host: google.com
  12. Upgrade-Insecure-Requests: 1
  13. 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
  14. 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
  15. Accept-Encoding: gzip, deflate
  16. Accept-Language: en-US,en;q=0.9
  17. Connection: close
  18. `
  19. req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
  20. if err != nil {
  21. fmt.Println(err.Error())
  22. os.Exit(1)
  23. }
  24. fmt.Println(req)
  25. }
英文:

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

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. )
  9. func main() {
  10. stra := `GET / HTTP/1.1
  11. Host: google.com
  12. Upgrade-Insecure-Requests: 1
  13. 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
  14. 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
  15. Accept-Encoding: gzip, deflate
  16. Accept-Language: en-US,en;q=0.9
  17. Connection: close
  18. `
  19. req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(stra)))
  20. if err != nil {
  21. fmt.Println(err.Error())
  22. os.Exit(1)
  23. }
  24. fmt.Println(req)
  25. }

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:

确定