2021-02-05 19:06:43 +00:00
|
|
|
package offlinehttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-02-08 13:38:35 +00:00
|
|
|
"errors"
|
2021-02-05 19:06:43 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// readResponseFromString reads a raw http response from a string.
|
|
|
|
func readResponseFromString(data string) (*http.Response, error) {
|
|
|
|
var final string
|
|
|
|
if strings.HasPrefix(data, "HTTP/") {
|
|
|
|
final = data
|
|
|
|
} else {
|
2021-02-08 13:38:35 +00:00
|
|
|
lastIndex := strings.LastIndex(data, "HTTP/")
|
|
|
|
if lastIndex == -1 {
|
|
|
|
return nil, errors.New("malformed raw http response")
|
|
|
|
}
|
|
|
|
final = data[:] // choose last http/ in case of it being later.
|
2021-02-05 19:06:43 +00:00
|
|
|
}
|
|
|
|
return http.ReadResponse(bufio.NewReader(strings.NewReader(final)), nil)
|
|
|
|
}
|