при попытке отправить более одного HTTP-запроса, сгенерированного http.ReadRequest, я получаю следующую ошибку:
Post http://192.168.x.x:8000/dir1: http: invalid Read on closed Body
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5ec4cc]
Мой код берет файл и помещает его в bufio.NewReader
.Затем он использует http.ReadRequest
на том, что прочитал, что является HTTP POST
запросом.наконец, он открывает другой файл «wordlist.txt», который содержит каталоги, и каждый раз перебирает исходный URI HTTP с другим каталогом из списка.По какой-то причине - второй запрос приводит к тому, что программа выдает ошибку (http: invalid Read on closed Body
).Вот рабочий пример (просто измените путь к своему):
main.go
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"net/url"
"os"
)
func main() {
//Read HTTP request from text file
rdr, err := os.Open("/opt/gohttp/tests/req2.txt")
if err != nil {
log.Println(err)
}
//Parse to a web request to work with using ReadRequest
req, err := http.ReadRequest(bufio.NewReader(rdr))
if err != nil {
log.Println(err)
}
// fix for "requestURI can't be sent in client requests"
req.RequestURI = ""
//Open wordlist
file, err := os.Open("/opt/gohttp/tests/wordlist.txt")
if err != nil {
log.Println(err)
}
defer file.Close()
//Scan wordlist line by line and append each to the directory
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var i string = scanner.Text()
fmt.Println(i)
//this part appends the wordlist word to the request sent e.g. /dir1
u, err := url.Parse("http://" + req.Host + "/" + i) //todo: https? other protocols?
if err != nil {
log.Println("scanner parse error: ", err)
}
req.URL = u
//Send the request
fmt.Println("Sending request.." + "\n")
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
log.Println(err) // invalid read on closed body error
}
fmt.Println(*response)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
пример файла HTTP-запроса
POST /hi HTTP/1.1
Host: 192.168.x.x:8000
Content-Length: 41
{"email":"aa","password":"secret"}
пример файла списка слов
0
1
dir1
dir2
dir3
выход приложения
go run tests/main.go
0
Sending request..
{404 Not Found 404 HTTP/1.1 1 1...}
1
Sending request..
2019/01/28 04:27:52 Post http://192.168.x.x:8000/1: http: invalid Read on closed Body
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x5ec4cc]
goroutine 1 [running]:
main.main()
/opt/gohttp/tests/main.go:53 +0x35c
exit status 2
Обратите внимание, что первый HTTP-запрос отправленуспешно, и только второй возвращает ошибку выше.Как я могу это исправить?