2021-02-02 12:05:47 +01:00
|
|
|
package oldhttptransport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-15 11:57:40 +02:00
|
|
|
"context"
|
2021-02-02 12:05:47 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptrace"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
2021-09-07 17:52:42 +02:00
|
|
|
errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
2021-09-07 17:09:30 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
2021-09-05 14:49:38 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TraceTripper performs single HTTP transactions.
|
|
|
|
type TraceTripper struct {
|
2021-06-15 11:57:40 +02:00
|
|
|
readAllErrs *atomicx.Int64
|
|
|
|
readAllContext func(ctx context.Context, r io.Reader) ([]byte, error)
|
|
|
|
roundTripper http.RoundTripper
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTraceTripper creates a new Transport.
|
|
|
|
func NewTraceTripper(roundTripper http.RoundTripper) *TraceTripper {
|
|
|
|
return &TraceTripper{
|
2021-06-15 11:57:40 +02:00
|
|
|
readAllErrs: &atomicx.Int64{},
|
|
|
|
readAllContext: iox.ReadAllContext,
|
|
|
|
roundTripper: roundTripper,
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readCloseWrapper struct {
|
|
|
|
closer io.Closer
|
|
|
|
reader io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func newReadCloseWrapper(
|
|
|
|
reader io.Reader, closer io.ReadCloser,
|
|
|
|
) *readCloseWrapper {
|
|
|
|
return &readCloseWrapper{
|
|
|
|
closer: closer,
|
|
|
|
reader: reader,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *readCloseWrapper) Read(p []byte) (int, error) {
|
|
|
|
return c.reader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *readCloseWrapper) Close() error {
|
|
|
|
return c.closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSnap(
|
2021-06-15 11:57:40 +02:00
|
|
|
ctx context.Context, source *io.ReadCloser, limit int64,
|
|
|
|
readAllContext func(ctx context.Context, r io.Reader) ([]byte, error),
|
2021-02-02 12:05:47 +01:00
|
|
|
) (data []byte, err error) {
|
2021-06-15 11:57:40 +02:00
|
|
|
data, err = readAllContext(ctx, io.LimitReader(*source, limit))
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil {
|
|
|
|
*source = newReadCloseWrapper(
|
|
|
|
io.MultiReader(bytes.NewReader(data), *source),
|
|
|
|
*source,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip executes a single HTTP transaction, returning
|
|
|
|
// a Response for the provided Request.
|
|
|
|
func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
root := modelx.ContextMeasurementRootOrDefault(req.Context())
|
|
|
|
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPRoundTripStart: &modelx.HTTPRoundTripStartEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
Method: req.Method,
|
|
|
|
URL: req.URL.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
2021-07-01 16:34:36 +02:00
|
|
|
majorOp = errorsx.HTTPRoundTripOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
majorOpMu sync.Mutex
|
|
|
|
requestBody []byte
|
|
|
|
requestHeaders = http.Header{}
|
|
|
|
requestHeadersMu sync.Mutex
|
|
|
|
snapSize = modelx.ComputeBodySnapSize(root.MaxBodySnapSize)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Save a snapshot of the request body
|
|
|
|
if req.Body != nil {
|
2021-06-15 11:57:40 +02:00
|
|
|
requestBody, err = readSnap(req.Context(), &req.Body, snapSize, t.readAllContext)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare a tracer for delivering events
|
|
|
|
tracer := &httptrace.ClientTrace{
|
|
|
|
TLSHandshakeStart: func() {
|
|
|
|
majorOpMu.Lock()
|
2021-07-01 16:34:36 +02:00
|
|
|
majorOp = errorsx.TLSHandshakeOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
majorOpMu.Unlock()
|
|
|
|
// Event emitted by net/http when DialTLS is not
|
|
|
|
// configured in the http.Transport
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
TLSHandshakeStart: &modelx.TLSHandshakeStartEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
TLSHandshakeDone: func(state tls.ConnectionState, err error) {
|
|
|
|
// Wrapping the error even if we're not returning it because it may
|
|
|
|
// less confusing to users to see the wrapped name
|
2021-09-07 17:09:30 +02:00
|
|
|
err = errorsxlegacy.SafeErrWrapperBuilder{
|
2021-06-23 13:36:45 +02:00
|
|
|
Error: err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.TLSHandshakeOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
2021-06-15 11:57:40 +02:00
|
|
|
durationSinceBeginning := time.Since(root.Beginning)
|
2021-02-02 12:05:47 +01:00
|
|
|
// Event emitted by net/http when DialTLS is not
|
|
|
|
// configured in the http.Transport
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
TLSHandshakeDone: &modelx.TLSHandshakeDoneEvent{
|
|
|
|
ConnectionState: modelx.NewTLSConnectionState(state),
|
|
|
|
Error: err,
|
|
|
|
DurationSinceBeginning: durationSinceBeginning,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
GotConn: func(info httptrace.GotConnInfo) {
|
|
|
|
majorOpMu.Lock()
|
2021-07-01 16:34:36 +02:00
|
|
|
majorOp = errorsx.HTTPRoundTripOperation
|
2021-02-02 12:05:47 +01:00
|
|
|
majorOpMu.Unlock()
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPConnectionReady: &modelx.HTTPConnectionReadyEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
WroteHeaderField: func(key string, values []string) {
|
|
|
|
requestHeadersMu.Lock()
|
|
|
|
// Important: do not set directly into the headers map using
|
|
|
|
// the [] operator because net/http expects to be able to
|
|
|
|
// perform normalization of header names!
|
|
|
|
for _, value := range values {
|
|
|
|
requestHeaders.Add(key, value)
|
|
|
|
}
|
|
|
|
requestHeadersMu.Unlock()
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPRequestHeader: &modelx.HTTPRequestHeaderEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
Key: key,
|
|
|
|
Value: values,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
WroteHeaders: func() {
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPRequestHeadersDone: &modelx.HTTPRequestHeadersDoneEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
Headers: requestHeaders, // [*]
|
|
|
|
Method: req.Method, // [*]
|
2021-06-23 13:36:45 +02:00
|
|
|
URL: req.URL, // [*]
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
WroteRequest: func(info httptrace.WroteRequestInfo) {
|
|
|
|
// Wrapping the error even if we're not returning it because it may
|
|
|
|
// less confusing to users to see the wrapped name
|
2021-09-07 17:09:30 +02:00
|
|
|
err := errorsxlegacy.SafeErrWrapperBuilder{
|
2021-06-23 13:36:45 +02:00
|
|
|
Error: info.Err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.HTTPRoundTripOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPRequestDone: &modelx.HTTPRequestDoneEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
GotFirstResponseByte: func() {
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPResponseStart: &modelx.HTTPResponseStartEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have already a tracer this is a toplevel request, so just
|
|
|
|
// set the tracer. Otherwise, we're doing DoH. We cannot set anothert trace
|
|
|
|
// because they'd be merged. Instead, replace the existing trace content
|
|
|
|
// with the new trace and then remember to reset it.
|
|
|
|
origtracer := httptrace.ContextClientTrace(req.Context())
|
|
|
|
if origtracer != nil {
|
|
|
|
bkp := *origtracer
|
|
|
|
*origtracer = *tracer
|
|
|
|
defer func() {
|
|
|
|
*origtracer = bkp
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), tracer))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := t.roundTripper.RoundTrip(req)
|
2021-09-07 17:09:30 +02:00
|
|
|
err = errorsxlegacy.SafeErrWrapperBuilder{
|
2021-06-23 13:36:45 +02:00
|
|
|
Error: err,
|
|
|
|
Operation: majorOp,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
// [*] Require less event joining work by providing info that
|
|
|
|
// makes this event alone actionable for OONI
|
|
|
|
event := &modelx.HTTPRoundTripDoneEvent{
|
2021-06-15 11:57:40 +02:00
|
|
|
DurationSinceBeginning: time.Since(root.Beginning),
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
|
|
|
RequestBodySnap: requestBody,
|
|
|
|
RequestHeaders: requestHeaders, // [*]
|
|
|
|
RequestMethod: req.Method, // [*]
|
|
|
|
RequestURL: req.URL.String(), // [*]
|
|
|
|
MaxBodySnapSize: snapSize,
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
event.ResponseHeaders = resp.Header
|
|
|
|
event.ResponseStatusCode = int64(resp.StatusCode)
|
|
|
|
event.ResponseProto = resp.Proto
|
|
|
|
// Save a snapshot of the response body
|
|
|
|
var data []byte
|
2021-06-15 11:57:40 +02:00
|
|
|
data, err = readSnap(req.Context(), &resp.Body, snapSize, t.readAllContext)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.readAllErrs.Add(1)
|
|
|
|
resp = nil // this is how net/http likes it
|
|
|
|
} else {
|
|
|
|
event.ResponseBodySnap = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
root.Handler.OnMeasurement(modelx.Measurement{
|
|
|
|
HTTPRoundTripDone: event,
|
|
|
|
})
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseIdleConnections closes the idle connections.
|
|
|
|
func (t *TraceTripper) CloseIdleConnections() {
|
|
|
|
// Adapted from net/http code
|
|
|
|
type closeIdler interface {
|
|
|
|
CloseIdleConnections()
|
|
|
|
}
|
|
|
|
if tr, ok := t.roundTripper.(closeIdler); ok {
|
|
|
|
tr.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
}
|