ooni-probe-cli/internal/engine/experiment/dash/download.go
Simone Basso 0fdc9cafb5
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
2021-06-15 11:57:40 +02:00

75 lines
2.0 KiB
Go

package dash
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
type downloadDeps interface {
HTTPClient() *http.Client
NewHTTPRequest(method string, url string, body io.Reader) (*http.Request, error)
ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error)
Scheme() string
UserAgent() string
}
type downloadConfig struct {
authorization string
begin time.Time
currentRate int64
deps downloadDeps
elapsedTarget int64
fqdn string
}
type downloadResult struct {
elapsed float64
received int64
requestTicks float64
serverURL string
timestamp int64
}
func download(ctx context.Context, config downloadConfig) (downloadResult, error) {
nbytes := (config.currentRate * 1000 * config.elapsedTarget) >> 3
var URL url.URL
URL.Scheme = config.deps.Scheme()
URL.Host = config.fqdn
URL.Path = fmt.Sprintf("%s%d", downloadPath, nbytes)
req, err := config.deps.NewHTTPRequest("GET", URL.String(), nil)
var result downloadResult
if err != nil {
return result, err
}
result.serverURL = URL.String()
req.Header.Set("User-Agent", config.deps.UserAgent())
req.Header.Set("Authorization", config.authorization)
savedTicks := time.Now()
resp, err := config.deps.HTTPClient().Do(req.WithContext(ctx))
if err != nil {
return result, err
}
if resp.StatusCode != 200 {
return result, errHTTPRequestFailed
}
defer resp.Body.Close()
data, err := config.deps.ReadAllContext(ctx, resp.Body)
if err != nil {
return result, err
}
// Implementation note: MK contains a comment that says that Neubot uses
// the elapsed time since when we start receiving the response but it
// turns out that Neubot and MK do the same. So, we do what they do. At
// the same time, we are currently not able to include the overhead that
// is caused by HTTP headers etc. So, we're a bit less precise.
result.elapsed = time.Since(savedTicks).Seconds()
result.received = int64(len(data))
result.requestTicks = savedTicks.Sub(config.begin).Seconds()
result.timestamp = time.Now().Unix()
return result, nil
}