nuclei/v2/pkg/protocols/offlinehttp/read_response.go

38 lines
1.0 KiB
Go
Raw Normal View History

package offlinehttp
import (
"bufio"
2021-02-08 19:08:35 +05:30
"errors"
"net/http"
2021-10-02 14:30:40 +07:00
"regexp"
"strings"
)
// readResponseFromString reads a raw http response from a string.
func readResponseFromString(data string) (*http.Response, error) {
var final string
2021-10-02 14:30:40 +07:00
noMinor := regexp.MustCompile(`HTTP\/[0-9] `)
if strings.HasPrefix(data, "HTTP/") {
2021-10-02 14:30:40 +07:00
// Go could not parse http version with no minor version
if noMinor.MatchString(data) {
// add minor version
data = strings.Replace(data, "HTTP/2", "HTTP/2.0", 1)
data = strings.Replace(data, "HTTP/3", "HTTP/3.0", 1)
}
final = data
} else {
2021-02-08 19:08:35 +05:30
lastIndex := strings.LastIndex(data, "HTTP/")
if lastIndex == -1 {
return nil, errors.New("malformed raw http response")
}
2021-02-27 12:31:17 +05:30
final = data[lastIndex:] // choose last http/ in case of it being later.
2021-10-02 14:30:40 +07:00
if noMinor.MatchString(final) {
final = strings.ReplaceAll(final, "HTTP/2", "HTTP/2.0")
final = strings.ReplaceAll(final, "HTTP/3", "HTTP/3.0")
}
}
return http.ReadResponse(bufio.NewReader(strings.NewReader(final)), nil)
}