0fdc9cafb5
* 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
69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package dash
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
|
)
|
|
|
|
type negotiateDeps interface {
|
|
HTTPClient() *http.Client
|
|
JSONMarshal(v interface{}) ([]byte, error)
|
|
Logger() model.Logger
|
|
NewHTTPRequest(method string, url string, body io.Reader) (*http.Request, error)
|
|
ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error)
|
|
Scheme() string
|
|
UserAgent() string
|
|
}
|
|
|
|
func negotiate(
|
|
ctx context.Context, fqdn string, deps negotiateDeps) (negotiateResponse, error) {
|
|
var negotiateResp negotiateResponse
|
|
data, err := deps.JSONMarshal(negotiateRequest{DASHRates: defaultRates})
|
|
if err != nil {
|
|
return negotiateResp, err
|
|
}
|
|
deps.Logger().Debugf("dash: body: %s", string(data))
|
|
var URL url.URL
|
|
URL.Scheme = deps.Scheme()
|
|
URL.Host = fqdn
|
|
URL.Path = negotiatePath
|
|
req, err := deps.NewHTTPRequest("POST", URL.String(), bytes.NewReader(data))
|
|
if err != nil {
|
|
return negotiateResp, err
|
|
}
|
|
req.Header.Set("User-Agent", deps.UserAgent())
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "")
|
|
resp, err := deps.HTTPClient().Do(req.WithContext(ctx))
|
|
if err != nil {
|
|
return negotiateResp, err
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
return negotiateResp, errHTTPRequestFailed
|
|
}
|
|
defer resp.Body.Close()
|
|
data, err = deps.ReadAllContext(ctx, resp.Body)
|
|
if err != nil {
|
|
return negotiateResp, err
|
|
}
|
|
deps.Logger().Debugf("dash: body: %s", string(data))
|
|
err = json.Unmarshal(data, &negotiateResp)
|
|
if err != nil {
|
|
return negotiateResp, err
|
|
}
|
|
// Implementation oddity: Neubot is using an integer rather than a
|
|
// boolean for the unchoked, with obvious semantics. I wonder why
|
|
// I choose an integer over a boolean, given that Python does have
|
|
// support for booleans. I don't remember 🤷.
|
|
if negotiateResp.Authorization == "" || negotiateResp.Unchoked == 0 {
|
|
return negotiateResp, errServerBusy
|
|
}
|
|
return negotiateResp, nil
|
|
}
|