fix(netxlite): ensure HTTP errors are always wrapped (#584)
1. introduce implementations of HTTPTransport and HTTPClient that apply an error wrapping policy using the constructor for a generic top-level error wrapper 2. make sure we use the implementations in point 1 when we are constructing HTTPTransport and HTTPClient 3. make sure we apply error wrapping using the constructor for a generic top-level error wrapper when reading bodies 4. acknowledge that error wrapping would be broken if we do not return the same classification _and_ operation when we wrap an already wrapped error, so fix the to code to do that 5. acknowledge that the classifiers already deal with preserving the error string and explain why this is a quirk and why we cannot remove it right now and what needs to happen to safely remove this quirk from the codebase Closes https://github.com/ooni/probe/issues/1860
This commit is contained in:
parent
da34cfe6c9
commit
6a935d5407
9 changed files with 217 additions and 28 deletions
|
|
@ -19,6 +19,19 @@ type HTTPTransport interface {
|
|||
CloseIdleConnections()
|
||||
}
|
||||
|
||||
// httpTransportErrWrapper is an HTTPTransport with error wrapping.
|
||||
type httpTransportErrWrapper struct {
|
||||
HTTPTransport
|
||||
}
|
||||
|
||||
func (txp *httpTransportErrWrapper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
resp, err := txp.HTTPTransport.RoundTrip(req)
|
||||
if err != nil {
|
||||
return nil, NewTopLevelGenericErrWrapper(err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// httpTransportLogger is an HTTPTransport with logging.
|
||||
type httpTransportLogger struct {
|
||||
// HTTPTransport is the underlying HTTP transport.
|
||||
|
|
@ -141,12 +154,13 @@ func NewOOHTTPBaseTransport(dialer Dialer, tlsDialer TLSDialer) HTTPTransport {
|
|||
}
|
||||
}
|
||||
|
||||
// WrapHTTPTransport creates an HTTPTransport using the given logger.
|
||||
// WrapHTTPTransport creates an HTTPTransport using the given logger
|
||||
// and guarantees that returned errors are wrapped.
|
||||
//
|
||||
// This is a low level factory. Consider not using it directly.
|
||||
func WrapHTTPTransport(logger Logger, txp HTTPTransport) HTTPTransport {
|
||||
return &httpTransportLogger{
|
||||
HTTPTransport: txp,
|
||||
HTTPTransport: &httpTransportErrWrapper{txp},
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
|
@ -250,3 +264,26 @@ func NewHTTPTransportStdlib(logger Logger) HTTPTransport {
|
|||
tlsDialer := NewTLSDialer(dialer, NewTLSHandshakerStdlib(logger))
|
||||
return NewHTTPTransport(logger, dialer, tlsDialer)
|
||||
}
|
||||
|
||||
// HTTPClient is an http.Client-like interface.
|
||||
type HTTPClient interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
CloseIdleConnections()
|
||||
}
|
||||
|
||||
// WrapHTTPClient wraps an HTTP client to add error wrapping capabilities.
|
||||
func WrapHTTPClient(clnt HTTPClient) HTTPClient {
|
||||
return &httpClientErrWrapper{clnt}
|
||||
}
|
||||
|
||||
type httpClientErrWrapper struct {
|
||||
HTTPClient
|
||||
}
|
||||
|
||||
func (c *httpClientErrWrapper) Do(req *http.Request) (*http.Response, error) {
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, NewTopLevelGenericErrWrapper(err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue