2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
//
|
|
|
|
// HTTP
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-28 12:42:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-06-05 21:22:27 +02:00
|
|
|
// MaybeWrapHTTPTransport wraps the HTTPTransport to save events if this Saver
|
|
|
|
// is not nil and otherwise just returns the given HTTPTransport. The snapshotSize
|
|
|
|
// argument is the maximum response body snapshot size to save per response.
|
|
|
|
func (s *Saver) MaybeWrapHTTPTransport(txp model.HTTPTransport, snapshotSize int64) model.HTTPTransport {
|
|
|
|
if s != nil {
|
|
|
|
txp = &HTTPTransportSaver{
|
|
|
|
HTTPTransport: txp,
|
|
|
|
Saver: s,
|
|
|
|
SnapshotSize: snapshotSize,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txp
|
|
|
|
}
|
|
|
|
|
2022-06-01 15:20:28 +02:00
|
|
|
// httpCloneRequestHeaders returns a clone of the headers where we have
|
fix(netxlite): do not mutate outgoing requests (#508)
I have recently seen a data race related our way of
mutating the outgoing request to set the host header.
Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.
Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:
1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;
2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);
3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.
Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
2021-09-27 13:35:47 +02:00
|
|
|
// also set the host header, which normally is not set by
|
|
|
|
// golang until it serializes the request itself.
|
2022-06-01 15:20:28 +02:00
|
|
|
func httpCloneRequestHeaders(req *http.Request) http.Header {
|
fix(netxlite): do not mutate outgoing requests (#508)
I have recently seen a data race related our way of
mutating the outgoing request to set the host header.
Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.
Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:
1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;
2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);
3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.
Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
2021-09-27 13:35:47 +02:00
|
|
|
header := req.Header.Clone()
|
|
|
|
if req.Host != "" {
|
|
|
|
header.Set("Host", req.Host)
|
|
|
|
} else {
|
|
|
|
header.Set("Host", req.URL.Host)
|
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// HTTPTransportSaver is a RoundTripper that saves
|
2021-02-02 12:05:47 +01:00
|
|
|
// events related to the HTTP transaction
|
2022-06-01 23:15:47 +02:00
|
|
|
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)
|
2022-06-01 19:27:47 +02:00
|
|
|
SnapshotSize int64
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 19:27:47 +02:00
|
|
|
// 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.
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *HTTPTransportSaver) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
|
2022-06-01 19:27:47 +02:00
|
|
|
started := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
txp.Saver.Write(&EventHTTPTransactionStart{&EventValue{
|
2022-06-01 19:27:47 +02:00
|
|
|
HTTPRequestHeaders: httpCloneRequestHeaders(req),
|
|
|
|
HTTPMethod: req.Method,
|
|
|
|
HTTPURL: req.URL.String(),
|
|
|
|
Transport: txp.HTTPTransport.Network(),
|
|
|
|
Time: started,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-06-01 19:27:47 +02:00
|
|
|
ev := &EventValue{
|
|
|
|
HTTPRequestHeaders: httpCloneRequestHeaders(req),
|
|
|
|
HTTPMethod: req.Method,
|
|
|
|
HTTPURL: req.URL.String(),
|
|
|
|
Transport: txp.HTTPTransport.Network(),
|
2022-09-02 15:10:57 +02:00
|
|
|
Time: time.Time{}, // see below
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-09-02 15:10:57 +02:00
|
|
|
defer func() {
|
|
|
|
ev.Time = time.Now() // https://github.com/ooni/probe/issues/2137
|
|
|
|
txp.Saver.Write(&EventHTTPTransactionDone{ev})
|
|
|
|
}()
|
2022-06-01 19:27:47 +02:00
|
|
|
|
2022-01-07 18:33:37 +01:00
|
|
|
resp, err := txp.HTTPTransport.RoundTrip(req)
|
2022-06-01 19:27:47 +02:00
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
2022-06-01 19:27:47 +02:00
|
|
|
ev.Duration = time.Since(started)
|
refactor(tracex): internally represent errors as strings (#786)
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).
2022-06-02 10:37:07 +02:00
|
|
|
ev.Err = NewFailureStr(err)
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-01 19:27:47 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
2022-06-01 19:27:47 +02:00
|
|
|
ev.Duration = time.Since(started)
|
refactor(tracex): internally represent errors as strings (#786)
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).
2022-06-02 10:37:07 +02:00
|
|
|
ev.Err = NewFailureStr(err)
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 19:27:47 +02:00
|
|
|
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
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *HTTPTransportSaver) CloseIdleConnections() {
|
|
|
|
txp.HTTPTransport.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txp *HTTPTransportSaver) Network() string {
|
|
|
|
return txp.HTTPTransport.Network()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (txp *HTTPTransportSaver) snapshotSize() int64 {
|
2022-06-01 19:27:47 +02:00
|
|
|
if txp.SnapshotSize > 0 {
|
|
|
|
return txp.SnapshotSize
|
|
|
|
}
|
|
|
|
return 1 << 17
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 19:27:47 +02:00
|
|
|
type httpReadableAgainBody struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
io.Reader
|
2022-06-01 19:27:47 +02:00
|
|
|
io.Closer
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.HTTPTransport = &HTTPTransportSaver{}
|