83e3167ce2
There are two reasons why this is beneficial: 1. github.com/google/go-cmp is more annoying to use for comparing data structures when there are interfaces to compare. Sure, there's a recipe for teaching it to compare errors, but how about making the errors trivially comparable instead? 2. if we want to send errors over the network, JSON serialization works but we cannot unmarshal the resulting string back to an error, so how about making this representation trivial to serialize (we are not going this now, but we need this property for websteps and it may be sensible to try to avoid to have duplicate code because of that -- measurex currently duplicates many tracex functionality and this is quite unfortunate because it slows development down) Additionally, if an error is a string: 3. we can very easily use a switch for comparing its possible values with "" representing the absence of errors, while it is more complex to do the same when using a nullable string or even an error (i.e., an interface) 4. if a type is not nullable, it's easier to write safe code for it and we may want to refactor experiments to use the internal representation of measurements for more robust processing code For all these reasons, let's internally use strings in tracex. The overall aim here is to reduce the duplicated code between pre and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package tracex
|
|
|
|
//
|
|
// HTTP
|
|
//
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
)
|
|
|
|
// httpCloneRequestHeaders returns a clone of the headers where we have
|
|
// also set the host header, which normally is not set by
|
|
// golang until it serializes the request itself.
|
|
func httpCloneRequestHeaders(req *http.Request) http.Header {
|
|
header := req.Header.Clone()
|
|
if req.Host != "" {
|
|
header.Set("Host", req.Host)
|
|
} else {
|
|
header.Set("Host", req.URL.Host)
|
|
}
|
|
return header
|
|
}
|
|
|
|
// HTTPTransportSaver is a RoundTripper that saves
|
|
// events related to the HTTP transaction
|
|
type HTTPTransportSaver struct {
|
|
// HTTPTransport is the MANDATORY underlying HTTP transport.
|
|
HTTPTransport model.HTTPTransport
|
|
|
|
// Saver is the MANDATORY saver to use.
|
|
Saver *Saver
|
|
|
|
// SnapshotSize is the OPTIONAL maximum body snapshot size (if not set, we'll
|
|
// use 1<<17, which we've been using since the ooni/netx days)
|
|
SnapshotSize int64
|
|
}
|
|
|
|
// HTTPRoundTrip performs the round trip with the given transport and
|
|
// the given arguments and saves the results into the saver.
|
|
//
|
|
// The maxBodySnapshotSize argument controls the maximum size of the
|
|
// body snapshot that we collect along with the HTTP round trip.
|
|
func (txp *HTTPTransportSaver) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
// TODO(bassosimone): we're currently using the started time for
|
|
// the transaction done event, which contrasts with what we do for
|
|
// every other event. What does the spec say?
|
|
|
|
started := time.Now()
|
|
txp.Saver.Write(&EventHTTPTransactionStart{&EventValue{
|
|
HTTPRequestHeaders: httpCloneRequestHeaders(req),
|
|
HTTPMethod: req.Method,
|
|
HTTPURL: req.URL.String(),
|
|
Transport: txp.HTTPTransport.Network(),
|
|
Time: started,
|
|
}})
|
|
ev := &EventValue{
|
|
HTTPRequestHeaders: httpCloneRequestHeaders(req),
|
|
HTTPMethod: req.Method,
|
|
HTTPURL: req.URL.String(),
|
|
Transport: txp.HTTPTransport.Network(),
|
|
Time: started,
|
|
}
|
|
defer txp.Saver.Write(&EventHTTPTransactionDone{ev})
|
|
|
|
resp, err := txp.HTTPTransport.RoundTrip(req)
|
|
|
|
if err != nil {
|
|
ev.Duration = time.Since(started)
|
|
ev.Err = NewFailureStr(err)
|
|
return nil, err
|
|
}
|
|
|
|
ev.HTTPStatusCode = resp.StatusCode
|
|
ev.HTTPResponseHeaders = resp.Header.Clone()
|
|
|
|
maxBodySnapshotSize := txp.snapshotSize()
|
|
r := io.LimitReader(resp.Body, maxBodySnapshotSize)
|
|
body, err := netxlite.ReadAllContext(req.Context(), r)
|
|
|
|
if err != nil {
|
|
ev.Duration = time.Since(started)
|
|
ev.Err = NewFailureStr(err)
|
|
return nil, err
|
|
}
|
|
|
|
resp.Body = &httpReadableAgainBody{ // allow for reading again the whole body
|
|
Reader: io.MultiReader(bytes.NewReader(body), resp.Body),
|
|
Closer: resp.Body,
|
|
}
|
|
|
|
ev.Duration = time.Since(started)
|
|
ev.HTTPResponseBody = body
|
|
ev.HTTPResponseBodyIsTruncated = int64(len(body)) >= maxBodySnapshotSize
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func (txp *HTTPTransportSaver) CloseIdleConnections() {
|
|
txp.HTTPTransport.CloseIdleConnections()
|
|
}
|
|
|
|
func (txp *HTTPTransportSaver) Network() string {
|
|
return txp.HTTPTransport.Network()
|
|
}
|
|
|
|
func (txp *HTTPTransportSaver) snapshotSize() int64 {
|
|
if txp.SnapshotSize > 0 {
|
|
return txp.SnapshotSize
|
|
}
|
|
return 1 << 17
|
|
}
|
|
|
|
type httpReadableAgainBody struct {
|
|
io.Reader
|
|
io.Closer
|
|
}
|
|
|
|
var _ model.HTTPTransport = &HTTPTransportSaver{}
|