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:
Simone Basso 2021-06-15 11:57:40 +02:00 committed by GitHub
commit 0fdc9cafb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
76 changed files with 440 additions and 223 deletions

View file

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-05-12 09:15:00.422051399 +0200 CEST m=+0.000129449
// 2021-06-15 10:55:55.967236 +0200 CEST m=+0.000374501
package ooapi
@ -59,7 +59,8 @@ func (api *simpleCheckReportIDAPI) Call(ctx context.Context, req *apimodel.Check
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleCheckInAPI implements the CheckIn API.
@ -109,7 +110,8 @@ func (api *simpleCheckInAPI) Call(ctx context.Context, req *apimodel.CheckInRequ
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleLoginAPI implements the Login API.
@ -159,7 +161,8 @@ func (api *simpleLoginAPI) Call(ctx context.Context, req *apimodel.LoginRequest)
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleMeasurementMetaAPI implements the MeasurementMeta API.
@ -209,7 +212,8 @@ func (api *simpleMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.Mea
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleRegisterAPI implements the Register API.
@ -259,7 +263,8 @@ func (api *simpleRegisterAPI) Call(ctx context.Context, req *apimodel.RegisterRe
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleTestHelpersAPI implements the TestHelpers API.
@ -309,7 +314,8 @@ func (api *simpleTestHelpersAPI) Call(ctx context.Context, req *apimodel.TestHel
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simplePsiphonConfigAPI implements the PsiphonConfig API.
@ -377,7 +383,8 @@ func (api *simplePsiphonConfigAPI) Call(ctx context.Context, req *apimodel.Psiph
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleTorTargetsAPI implements the TorTargets API.
@ -445,7 +452,8 @@ func (api *simpleTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTarge
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleURLsAPI implements the URLs API.
@ -495,7 +503,8 @@ func (api *simpleURLsAPI) Call(ctx context.Context, req *apimodel.URLsRequest) (
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleOpenReportAPI implements the OpenReport API.
@ -545,7 +554,8 @@ func (api *simpleOpenReportAPI) Call(ctx context.Context, req *apimodel.OpenRepo
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}
// simpleSubmitMeasurementAPI implements the SubmitMeasurement API.
@ -603,5 +613,6 @@ func (api *simpleSubmitMeasurementAPI) Call(ctx context.Context, req *apimodel.S
if api.UserAgent != "" {
httpReq.Header.Add("User-Agent", api.UserAgent)
}
return api.newResponse(api.httpClient().Do(httpReq))
httpResp, err := api.httpClient().Do(httpReq)
return api.newResponse(ctx, httpResp, err)
}