2021-02-06 00:36:43 +05:30
|
|
|
package offlinehttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
2021-02-08 19:08:35 +05:30
|
|
|
"errors"
|
2021-02-06 00:36:43 +05:30
|
|
|
"net/http"
|
2021-10-02 14:30:40 +07:00
|
|
|
"regexp"
|
2021-02-06 00:36:43 +05:30
|
|
|
"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] `)
|
|
|
|
|
|
2021-02-06 00:36:43 +05:30
|
|
|
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)
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
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")
|
|
|
|
|
}
|
2021-02-06 00:36:43 +05:30
|
|
|
}
|
|
|
|
|
return http.ReadResponse(bufio.NewReader(strings.NewReader(final)), nil)
|
|
|
|
|
}
|