fix(all): introduce and use iox.ReadAllContext (#379)
* fix(all): introduce and use iox.ReadAllContext This improvement over the ioutil.ReadAll utility returns early if the context expires. This enables us to unblock stuck code in case there's censorship confounding the TCP stack. See https://github.com/ooni/probe/issues/1417. Compared to the functionality postulated in the above mentioned issue, I choose to be more generic and separate limiting the maximum body size (not implemented here) from using the context to return early when reading a body (or any other reader). After implementing iox.ReadAllContext, I made sure we always use it everywhere in the tree instead of ioutil.ReadAll. This includes many parts of the codebase where in theory we don't need iox.ReadAllContext. Though, changing all the places makes checking whether we're not using ioutil.ReadAll where we should not be using it easy: `git grep` should return no lines. * Update internal/iox/iox_test.go * fix(ndt7): treat context errors as non-errors The rationale is explained by the comment documenting reduceErr. * Update internal/engine/experiment/ndt7/download.go
This commit is contained in:
@@ -63,7 +63,7 @@ func (bw *bodyWrapper) Read(b []byte) (n int, err error) {
|
||||
// bytes read (0 <= n <= len(p)) and any error encountered."
|
||||
Data: b[:n],
|
||||
Error: err,
|
||||
DurationSinceBeginning: time.Now().Sub(bw.root.Beginning),
|
||||
DurationSinceBeginning: time.Since(bw.root.Beginning),
|
||||
TransactionID: bw.tid,
|
||||
},
|
||||
})
|
||||
@@ -74,7 +74,7 @@ func (bw *bodyWrapper) Close() (err error) {
|
||||
err = bw.ReadCloser.Close()
|
||||
bw.root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPResponseDone: &modelx.HTTPResponseDoneEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(bw.root.Beginning),
|
||||
DurationSinceBeginning: time.Since(bw.root.Beginning),
|
||||
TransactionID: bw.tid,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package oldhttptransport
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
func TestBodyTracerSuccess(t *testing.T) {
|
||||
@@ -15,7 +17,7 @@ func TestBodyTracerSuccess(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
_, err = iox.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package oldhttptransport
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
func TestGood(t *testing.T) {
|
||||
@@ -15,7 +17,7 @@ func TestGood(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
_, err = iox.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package oldhttptransport
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"sync"
|
||||
@@ -16,21 +16,22 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/transactionid"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
// TraceTripper performs single HTTP transactions.
|
||||
type TraceTripper struct {
|
||||
readAllErrs *atomicx.Int64
|
||||
readAll func(r io.Reader) ([]byte, error)
|
||||
roundTripper http.RoundTripper
|
||||
readAllErrs *atomicx.Int64
|
||||
readAllContext func(ctx context.Context, r io.Reader) ([]byte, error)
|
||||
roundTripper http.RoundTripper
|
||||
}
|
||||
|
||||
// NewTraceTripper creates a new Transport.
|
||||
func NewTraceTripper(roundTripper http.RoundTripper) *TraceTripper {
|
||||
return &TraceTripper{
|
||||
readAllErrs: &atomicx.Int64{},
|
||||
readAll: ioutil.ReadAll,
|
||||
roundTripper: roundTripper,
|
||||
readAllErrs: &atomicx.Int64{},
|
||||
readAllContext: iox.ReadAllContext,
|
||||
roundTripper: roundTripper,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,10 +58,10 @@ func (c *readCloseWrapper) Close() error {
|
||||
}
|
||||
|
||||
func readSnap(
|
||||
source *io.ReadCloser, limit int64,
|
||||
readAll func(r io.Reader) ([]byte, error),
|
||||
ctx context.Context, source *io.ReadCloser, limit int64,
|
||||
readAllContext func(ctx context.Context, r io.Reader) ([]byte, error),
|
||||
) (data []byte, err error) {
|
||||
data, err = readAll(io.LimitReader(*source, limit))
|
||||
data, err = readAllContext(ctx, io.LimitReader(*source, limit))
|
||||
if err == nil {
|
||||
*source = newReadCloseWrapper(
|
||||
io.MultiReader(bytes.NewReader(data), *source),
|
||||
@@ -79,7 +80,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPRoundTripStart: &modelx.HTTPRoundTripStartEvent{
|
||||
DialID: dialid.ContextDialID(req.Context()),
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
Method: req.Method,
|
||||
TransactionID: tid,
|
||||
URL: req.URL.String(),
|
||||
@@ -98,7 +99,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
|
||||
// Save a snapshot of the request body
|
||||
if req.Body != nil {
|
||||
requestBody, err = readSnap(&req.Body, snapSize, t.readAll)
|
||||
requestBody, err = readSnap(req.Context(), &req.Body, snapSize, t.readAllContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -114,7 +115,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// configured in the http.Transport
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
TLSHandshakeStart: &modelx.TLSHandshakeStartEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
TransactionID: tid,
|
||||
},
|
||||
})
|
||||
@@ -127,7 +128,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
Operation: errorx.TLSHandshakeOperation,
|
||||
TransactionID: tid,
|
||||
}.MaybeBuild()
|
||||
durationSinceBeginning := time.Now().Sub(root.Beginning)
|
||||
durationSinceBeginning := time.Since(root.Beginning)
|
||||
// Event emitted by net/http when DialTLS is not
|
||||
// configured in the http.Transport
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
@@ -149,7 +150,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
info.Conn.LocalAddr().Network(),
|
||||
info.Conn.LocalAddr().String(),
|
||||
),
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
TransactionID: tid,
|
||||
},
|
||||
})
|
||||
@@ -165,7 +166,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
requestHeadersMu.Unlock()
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPRequestHeader: &modelx.HTTPRequestHeaderEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
Key: key,
|
||||
TransactionID: tid,
|
||||
Value: values,
|
||||
@@ -175,7 +176,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
WroteHeaders: func() {
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPRequestHeadersDone: &modelx.HTTPRequestHeadersDoneEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
Headers: requestHeaders, // [*]
|
||||
Method: req.Method, // [*]
|
||||
TransactionID: tid,
|
||||
@@ -193,7 +194,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
}.MaybeBuild()
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPRequestDone: &modelx.HTTPRequestDoneEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
Error: err,
|
||||
TransactionID: tid,
|
||||
},
|
||||
@@ -202,7 +203,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
GotFirstResponseByte: func() {
|
||||
root.Handler.OnMeasurement(modelx.Measurement{
|
||||
HTTPResponseStart: &modelx.HTTPResponseStartEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
TransactionID: tid,
|
||||
},
|
||||
})
|
||||
@@ -233,7 +234,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
// [*] Require less event joining work by providing info that
|
||||
// makes this event alone actionable for OONI
|
||||
event := &modelx.HTTPRoundTripDoneEvent{
|
||||
DurationSinceBeginning: time.Now().Sub(root.Beginning),
|
||||
DurationSinceBeginning: time.Since(root.Beginning),
|
||||
Error: err,
|
||||
RequestBodySnap: requestBody,
|
||||
RequestHeaders: requestHeaders, // [*]
|
||||
@@ -248,7 +249,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
event.ResponseProto = resp.Proto
|
||||
// Save a snapshot of the response body
|
||||
var data []byte
|
||||
data, err = readSnap(&resp.Body, snapSize, t.readAll)
|
||||
data, err = readSnap(req.Context(), &resp.Body, snapSize, t.readAllContext)
|
||||
if err != nil {
|
||||
t.readAllErrs.Add(1)
|
||||
resp = nil // this is how net/http likes it
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"sync"
|
||||
@@ -14,6 +13,7 @@ import (
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
func TestTraceTripperSuccess(t *testing.T) {
|
||||
@@ -25,7 +25,7 @@ func TestTraceTripperSuccess(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
_, err = iox.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func (h *roundTripHandler) OnMeasurement(m modelx.Measurement) {
|
||||
|
||||
func TestTraceTripperReadAllFailure(t *testing.T) {
|
||||
transport := NewTraceTripper(http.DefaultTransport)
|
||||
transport.readAll = func(r io.Reader) ([]byte, error) {
|
||||
transport.readAllContext = func(ctx context.Context, r io.Reader) ([]byte, error) {
|
||||
return nil, io.EOF
|
||||
}
|
||||
client := &http.Client{Transport: transport}
|
||||
@@ -156,7 +156,7 @@ func TestTraceTripperWithCorrectSnaps(t *testing.T) {
|
||||
|
||||
// Read the whole response body, parse it as valid DNS
|
||||
// reply and verify we obtained what we expected
|
||||
replyData, err := ioutil.ReadAll(resp.Body)
|
||||
replyData, err := iox.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -230,7 +230,7 @@ func TestTraceTripperWithReadAllFailingForBody(t *testing.T) {
|
||||
// use such transport to configure an ordinary client
|
||||
transport := NewTraceTripper(http.DefaultTransport)
|
||||
errorMocked := errors.New("mocked error")
|
||||
transport.readAll = func(r io.Reader) ([]byte, error) {
|
||||
transport.readAllContext = func(ctx context.Context, r io.Reader) ([]byte, error) {
|
||||
return nil, errorMocked
|
||||
}
|
||||
const snapSize = 15
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package oldhttptransport
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/transactionid"
|
||||
"github.com/ooni/probe-cli/v3/internal/iox"
|
||||
)
|
||||
|
||||
type transactionerCheckTransactionID struct {
|
||||
@@ -33,7 +34,7 @@ func TestTransactionerSuccess(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
_, err = ioutil.ReadAll(resp.Body)
|
||||
_, err = iox.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user