feat(webconnectivity): try all the available THs (#980)
We introduce a fork of internal/httpx, named internal/httpapi, where there is a clear split between the concept of an API endpoint (such as https://0.th.ooni.org/) and of an API descriptor (such as using `GET` to access /api/v1/test-list/url). Additionally, httpapi allows to create a SequenceCaller that tries to call a given API descriptor using multiple API endpoints. The SequenceCaller will stop once an endpoint works or when all the available endpoints have been tried unsuccessfully. The definition of "success" is the following: we consider "failure" any error that occurs during the HTTP round trip or when reading the response body. We DO NOT consider "failure" errors (1) when parsing the input URL; (2) when the server returns >= 400; (3) when the server returns a string that does not parse as valid JSON. The idea of this classification of failures is that we ONLY want to retry when we see what looks like a network error that may be caused by (collateral or targeted) censorship. We take advantage of the availability of this new package and we refactor web_connectivity@v0.4 and web_connectivity@v0.5 to use a SequenceCaller for calling the web connectivity TH API. This means that we will now try all the available THs advertised by the backend rather than just selecting and using the first one provided by the backend. Because this diff is designed to be backported to the `release/3.16` branch, we have omitted additional changes to always use httpapi where we are currently using httpx. Yet, to remind ourselves about the need to do that, we have deprecated the httpx package. We will rewrite all the code currently using httpx to use httpapi as part of future work. It is also worth noting that httpapi will allow us to refactor the backend code such that (1) we remove code to select a backend URL endpoint at the beginning and (2) we try several endpoints. The design of the code is such that we can add to the mix some endpoints using as `http.Client` a special client using a tunnel. This will allow us to automatically fallback backend queries. Closes https://github.com/ooni/probe/issues/2353. Related to https://github.com/ooni/probe/issues/1519.
This commit is contained in:
parent
28aabe0947
commit
c2ea0b4704
|
@ -4,9 +4,10 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/geoipx"
|
||||
"github.com/ooni/probe-cli/v3/internal/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/httpapi"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// Redirect to types defined inside the model package
|
||||
|
@ -21,22 +22,23 @@ type (
|
|||
// Control performs the control request and returns the response.
|
||||
func Control(
|
||||
ctx context.Context, sess model.ExperimentSession,
|
||||
thAddr string, creq ControlRequest) (out ControlResponse, err error) {
|
||||
clnt := &httpx.APIClientTemplate{
|
||||
BaseURL: thAddr,
|
||||
HTTPClient: sess.DefaultHTTPClient(),
|
||||
Logger: sess.Logger(),
|
||||
UserAgent: sess.UserAgent(),
|
||||
}
|
||||
testhelpers []model.OOAPIService, creq ControlRequest) (ControlResponse, *model.OOAPIService, error) {
|
||||
seqCaller := httpapi.NewSequenceCaller(
|
||||
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor(sess.Logger(), "/", creq).WithBodyLogging(true),
|
||||
httpapi.NewEndpointList(sess.DefaultHTTPClient(), sess.UserAgent(), testhelpers...)...,
|
||||
)
|
||||
sess.Logger().Infof("control for %s...", creq.HTTPRequest)
|
||||
// make sure error is wrapped
|
||||
err = clnt.WithBodyLogging().Build().PostJSON(ctx, "/", creq, &out)
|
||||
if err != nil {
|
||||
err = netxlite.NewTopLevelGenericErrWrapper(err)
|
||||
}
|
||||
var out ControlResponse
|
||||
idx, err := seqCaller.CallWithJSONResponse(ctx, &out)
|
||||
sess.Logger().Infof("control for %s... %+v", creq.HTTPRequest, model.ErrorToStringOrOK(err))
|
||||
if err != nil {
|
||||
// make sure error is wrapped
|
||||
err = netxlite.NewTopLevelGenericErrWrapper(err)
|
||||
return ControlResponse{}, nil, err
|
||||
}
|
||||
fillASNs(&out.DNS)
|
||||
return
|
||||
runtimex.Assert(idx >= 0 && idx < len(testhelpers), "idx out of bounds")
|
||||
return out, &testhelpers[idx], nil
|
||||
}
|
||||
|
||||
// fillASNs fills the ASNs array of ControlDNSResult. For each Addr inside
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
|
||||
const (
|
||||
testName = "web_connectivity"
|
||||
testVersion = "0.4.1"
|
||||
testVersion = "0.4.2"
|
||||
)
|
||||
|
||||
// Config contains the experiment config.
|
||||
|
@ -145,19 +145,9 @@ func (m Measurer) Run(
|
|||
}
|
||||
// 1. find test helper
|
||||
testhelpers, _ := sess.GetTestHelpersByName("web-connectivity")
|
||||
var testhelper *model.OOAPIService
|
||||
for _, th := range testhelpers {
|
||||
if th.Type == "https" {
|
||||
testhelper = &th
|
||||
break
|
||||
}
|
||||
}
|
||||
if testhelper == nil {
|
||||
if len(testhelpers) < 1 {
|
||||
return ErrNoAvailableTestHelpers
|
||||
}
|
||||
measurement.TestHelpers = map[string]interface{}{
|
||||
"backend": testhelper,
|
||||
}
|
||||
// 2. perform the DNS lookup step
|
||||
dnsBegin := time.Now()
|
||||
dnsResult := DNSLookup(ctx, DNSLookupConfig{
|
||||
|
@ -167,10 +157,11 @@ func (m Measurer) Run(
|
|||
tk.Queries = append(tk.Queries, dnsResult.TestKeys.Queries...)
|
||||
tk.DNSExperimentFailure = dnsResult.Failure
|
||||
epnts := NewEndpoints(URL, dnsResult.Addresses())
|
||||
sess.Logger().Infof("using control: %s", testhelper.Address)
|
||||
sess.Logger().Infof("using control: %+v", testhelpers)
|
||||
// 3. perform the control measurement
|
||||
thBegin := time.Now()
|
||||
tk.Control, err = Control(ctx, sess, testhelper.Address, ControlRequest{
|
||||
var usedTH *model.OOAPIService
|
||||
tk.Control, usedTH, err = Control(ctx, sess, testhelpers, ControlRequest{
|
||||
HTTPRequest: URL.String(),
|
||||
HTTPRequestHeaders: map[string][]string{
|
||||
"Accept": {model.HTTPHeaderAccept},
|
||||
|
@ -179,6 +170,11 @@ func (m Measurer) Run(
|
|||
},
|
||||
TCPConnect: epnts.Endpoints(),
|
||||
})
|
||||
if usedTH != nil {
|
||||
measurement.TestHelpers = map[string]interface{}{
|
||||
"backend": usedTH,
|
||||
}
|
||||
}
|
||||
tk.THRuntime = time.Since(thBegin)
|
||||
tk.ControlFailure = tracex.NewFailure(err)
|
||||
// 4. analyze DNS results
|
||||
|
|
|
@ -21,7 +21,7 @@ func TestNewExperimentMeasurer(t *testing.T) {
|
|||
if measurer.ExperimentName() != "web_connectivity" {
|
||||
t.Fatal("unexpected name")
|
||||
}
|
||||
if measurer.ExperimentVersion() != "0.4.1" {
|
||||
if measurer.ExperimentVersion() != "0.4.2" {
|
||||
t.Fatal("unexpected version")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -285,7 +285,7 @@ func (t *CleartextFlow) maybeFollowRedirects(ctx context.Context, resp *http.Res
|
|||
WaitGroup: t.WaitGroup,
|
||||
Referer: resp.Request.URL.String(),
|
||||
Session: nil, // no need to issue another control request
|
||||
THAddr: "", // ditto
|
||||
TestHelpers: nil, // ditto
|
||||
UDPAddress: t.UDPAddress,
|
||||
}
|
||||
resolvers.Start(ctx)
|
||||
|
|
|
@ -8,10 +8,11 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
||||
"github.com/ooni/probe-cli/v3/internal/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/httpapi"
|
||||
"github.com/ooni/probe-cli/v3/internal/measurexlite"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// EndpointMeasurementsStarter is used by Control to start extra
|
||||
|
@ -51,8 +52,8 @@ type Control struct {
|
|||
// Session is the MANDATORY session to use.
|
||||
Session model.ExperimentSession
|
||||
|
||||
// THAddr is the MANDATORY TH's URL.
|
||||
THAddr string
|
||||
// TestHelpers is the MANDATORY list of test helpers.
|
||||
TestHelpers []model.OOAPIService
|
||||
|
||||
// URL is the MANDATORY URL we are measuring.
|
||||
URL *url.URL
|
||||
|
@ -102,26 +103,20 @@ func (c *Control) Run(parentCtx context.Context) {
|
|||
// create logger for this operation
|
||||
ol := measurexlite.NewOperationLogger(
|
||||
c.Logger,
|
||||
"control for %s using %s",
|
||||
"control for %s using %+v",
|
||||
creq.HTTPRequest,
|
||||
c.THAddr,
|
||||
c.TestHelpers,
|
||||
)
|
||||
|
||||
// create an API client
|
||||
clnt := (&httpx.APIClientTemplate{
|
||||
Accept: "",
|
||||
Authorization: "",
|
||||
BaseURL: c.THAddr,
|
||||
HTTPClient: c.Session.DefaultHTTPClient(),
|
||||
Host: "", // use the one inside the URL
|
||||
LogBody: true,
|
||||
Logger: c.Logger,
|
||||
UserAgent: c.Session.UserAgent(),
|
||||
}).Build()
|
||||
// create an httpapi sequence caller
|
||||
seqCaller := httpapi.NewSequenceCaller(
|
||||
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor(c.Logger, "/", creq).WithBodyLogging(true),
|
||||
httpapi.NewEndpointList(c.Session.DefaultHTTPClient(), c.Session.UserAgent(), c.TestHelpers...)...,
|
||||
)
|
||||
|
||||
// issue the control request and wait for the response
|
||||
var cresp webconnectivity.ControlResponse
|
||||
err := clnt.PostJSON(opCtx, "/", creq, &cresp)
|
||||
idx, err := seqCaller.CallWithJSONResponse(opCtx, &cresp)
|
||||
if err != nil {
|
||||
// make sure error is wrapped
|
||||
err = netxlite.NewTopLevelGenericErrWrapper(err)
|
||||
|
@ -134,6 +129,10 @@ func (c *Control) Run(parentCtx context.Context) {
|
|||
c.TestKeys.SetControl(&cresp)
|
||||
ol.Stop(nil)
|
||||
|
||||
// record the specific TH that worked
|
||||
runtimex.Assert(idx >= 0 && idx < len(c.TestHelpers), "idx out of bounds")
|
||||
c.TestKeys.setTestHelper(&c.TestHelpers[idx])
|
||||
|
||||
// if the TH returned us addresses we did not previously were
|
||||
// aware of, make sure we also measure them
|
||||
c.maybeStartExtraMeasurements(parentCtx, cresp.DNS.Addrs)
|
||||
|
|
|
@ -67,8 +67,9 @@ type DNSResolvers struct {
|
|||
// always follow the redirect chain caused by the provided URL.
|
||||
Session model.ExperimentSession
|
||||
|
||||
// THAddr is the OPTIONAL test helper address.
|
||||
THAddr string
|
||||
// TestHelpers is the OPTIONAL list of test helpers. If the list is
|
||||
// empty, we are not going to try to contact any test helper.
|
||||
TestHelpers []model.OOAPIService
|
||||
|
||||
// UDPAddress is the OPTIONAL address of the UDP resolver to use. If this
|
||||
// field is not set we use a default one (e.g., `8.8.8.8:53`).
|
||||
|
@ -498,15 +499,15 @@ func (t *DNSResolvers) startSecureFlows(
|
|||
}
|
||||
}
|
||||
|
||||
// maybeStartControlFlow starts the control flow iff .Session and .THAddr are set.
|
||||
// maybeStartControlFlow starts the control flow iff .Session and .TestHelpers are set.
|
||||
func (t *DNSResolvers) maybeStartControlFlow(
|
||||
ctx context.Context,
|
||||
ps *prioritySelector,
|
||||
addresses []DNSEntry,
|
||||
) {
|
||||
// note: for subsequent requests we don't set .Session and .THAddr hence
|
||||
// note: for subsequent requests we don't set .Session and .TestHelpers hence
|
||||
// we are not going to query the test helper more than once
|
||||
if t.Session != nil && t.THAddr != "" {
|
||||
if t.Session != nil && len(t.TestHelpers) > 0 {
|
||||
var addrs []string
|
||||
for _, addr := range addresses {
|
||||
addrs = append(addrs, addr.Addr)
|
||||
|
@ -518,7 +519,7 @@ func (t *DNSResolvers) maybeStartControlFlow(
|
|||
PrioSelector: ps,
|
||||
TestKeys: t.TestKeys,
|
||||
Session: t.Session,
|
||||
THAddr: t.THAddr,
|
||||
TestHelpers: t.TestHelpers,
|
||||
URL: t.URL,
|
||||
WaitGroup: t.WaitGroup,
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ func (m *Measurer) ExperimentName() string {
|
|||
|
||||
// ExperimentVersion implements model.ExperimentMeasurer.
|
||||
func (m *Measurer) ExperimentVersion() string {
|
||||
return "0.5.18"
|
||||
return "0.5.19"
|
||||
}
|
||||
|
||||
// Run implements model.ExperimentMeasurer.
|
||||
|
@ -89,17 +89,7 @@ func (m *Measurer) Run(ctx context.Context, sess model.ExperimentSession,
|
|||
|
||||
// obtain the test helper's address
|
||||
testhelpers, _ := sess.GetTestHelpersByName("web-connectivity")
|
||||
var thAddr string
|
||||
for _, th := range testhelpers {
|
||||
if th.Type == "https" {
|
||||
thAddr = th.Address
|
||||
measurement.TestHelpers = map[string]any{
|
||||
"backend": &th,
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if thAddr == "" {
|
||||
if len(testhelpers) < 1 {
|
||||
sess.Logger().Warnf("continuing without a valid TH address")
|
||||
tk.SetControlFailure(webconnectivity.ErrNoAvailableTestHelpers)
|
||||
}
|
||||
|
@ -120,7 +110,7 @@ func (m *Measurer) Run(ctx context.Context, sess model.ExperimentSession,
|
|||
CookieJar: jar,
|
||||
Referer: "",
|
||||
Session: sess,
|
||||
THAddr: thAddr,
|
||||
TestHelpers: testhelpers,
|
||||
UDPAddress: "",
|
||||
}
|
||||
resos.Start(ctx)
|
||||
|
@ -137,6 +127,16 @@ func (m *Measurer) Run(ctx context.Context, sess model.ExperimentSession,
|
|||
// perform any deferred computation on the test keys
|
||||
tk.Finalize(sess.Logger())
|
||||
|
||||
// set the test helper we used
|
||||
// TODO(bassosimone): it may be more informative to know about all the
|
||||
// test helpers we _tried_ to use, however the data format does not have
|
||||
// support for that as far as I can tell...
|
||||
if th := tk.getTestHelper(); th != nil {
|
||||
measurement.TestHelpers = map[string]interface{}{
|
||||
"backend": th,
|
||||
}
|
||||
}
|
||||
|
||||
// return whether there was a fundamental failure, which would prevent
|
||||
// the measurement from being submitted to the OONI collector.
|
||||
return tk.fundamentalFailure
|
||||
|
|
|
@ -337,7 +337,7 @@ func (t *SecureFlow) maybeFollowRedirects(ctx context.Context, resp *http.Respon
|
|||
WaitGroup: t.WaitGroup,
|
||||
Referer: resp.Request.URL.String(),
|
||||
Session: nil, // no need to issue another control request
|
||||
THAddr: "", // ditto
|
||||
TestHelpers: nil, // ditto
|
||||
UDPAddress: t.UDPAddress,
|
||||
}
|
||||
resolvers.Start(ctx)
|
||||
|
|
|
@ -134,6 +134,10 @@ type TestKeys struct {
|
|||
|
||||
// mu provides mutual exclusion for accessing the test keys.
|
||||
mu *sync.Mutex
|
||||
|
||||
// testHelper is used to communicate the TH that worked to the main
|
||||
// goroutine such that we can fill measurement.TestHelpers.
|
||||
testHelper *model.OOAPIService
|
||||
}
|
||||
|
||||
// ConnPriorityLogEntry is an entry in the TestKeys.ConnPriorityLog slice.
|
||||
|
@ -302,6 +306,21 @@ func (tk *TestKeys) AppendConnPriorityLogEntry(entry *ConnPriorityLogEntry) {
|
|||
tk.mu.Unlock()
|
||||
}
|
||||
|
||||
// setTestHelper sets .testHelper in a thread safe way
|
||||
func (tk *TestKeys) setTestHelper(th *model.OOAPIService) {
|
||||
tk.mu.Lock()
|
||||
tk.testHelper = th
|
||||
tk.mu.Unlock()
|
||||
}
|
||||
|
||||
// getTestHelper gets .testHelper in a thread safe way
|
||||
func (tk *TestKeys) getTestHelper() (th *model.OOAPIService) {
|
||||
tk.mu.Lock()
|
||||
th = tk.testHelper
|
||||
tk.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// NewTestKeys creates a new instance of TestKeys.
|
||||
func NewTestKeys() *TestKeys {
|
||||
return &TestKeys{
|
||||
|
@ -348,6 +367,7 @@ func NewTestKeys() *TestKeys {
|
|||
ControlRequest: nil,
|
||||
fundamentalFailure: nil,
|
||||
mu: &sync.Mutex{},
|
||||
testHelper: nil,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
181
internal/httpapi/call.go
Normal file
181
internal/httpapi/call.go
Normal file
|
@ -0,0 +1,181 @@
|
|||
package httpapi
|
||||
|
||||
//
|
||||
// Calling HTTP APIs.
|
||||
//
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// joinURLPath appends |resourcePath| to |urlPath|.
|
||||
func joinURLPath(urlPath, resourcePath string) string {
|
||||
if resourcePath == "" {
|
||||
if urlPath == "" {
|
||||
return "/"
|
||||
}
|
||||
return urlPath
|
||||
}
|
||||
if !strings.HasSuffix(urlPath, "/") {
|
||||
urlPath += "/"
|
||||
}
|
||||
resourcePath = strings.TrimPrefix(resourcePath, "/")
|
||||
return urlPath + resourcePath
|
||||
}
|
||||
|
||||
// newRequest creates a new http.Request from the given |ctx|, |endpoint|, and |desc|.
|
||||
func newRequest(ctx context.Context, endpoint *Endpoint, desc *Descriptor) (*http.Request, error) {
|
||||
URL, err := url.Parse(endpoint.BaseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// BaseURL and resource URL are joined if they have a path
|
||||
URL.Path = joinURLPath(URL.Path, desc.URLPath)
|
||||
if len(desc.URLQuery) > 0 {
|
||||
URL.RawQuery = desc.URLQuery.Encode()
|
||||
} else {
|
||||
URL.RawQuery = "" // as documented we only honour desc.URLQuery
|
||||
}
|
||||
var reqBody io.Reader
|
||||
if len(desc.RequestBody) > 0 {
|
||||
reqBody = bytes.NewReader(desc.RequestBody)
|
||||
desc.Logger.Debugf("httpapi: request body length: %d", len(desc.RequestBody))
|
||||
if desc.LogBody {
|
||||
desc.Logger.Debugf("httpapi: request body: %s", string(desc.RequestBody))
|
||||
}
|
||||
}
|
||||
request, err := http.NewRequestWithContext(ctx, desc.Method, URL.String(), reqBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request.Host = endpoint.Host // allow cloudfronting
|
||||
if desc.Authorization != "" {
|
||||
request.Header.Set("Authorization", desc.Authorization)
|
||||
}
|
||||
if desc.ContentType != "" {
|
||||
request.Header.Set("Content-Type", desc.ContentType)
|
||||
}
|
||||
if desc.Accept != "" {
|
||||
request.Header.Set("Accept", desc.Accept)
|
||||
}
|
||||
if endpoint.UserAgent != "" {
|
||||
request.Header.Set("User-Agent", endpoint.UserAgent)
|
||||
}
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// ErrHTTPRequestFailed indicates that the server returned >= 400.
|
||||
type ErrHTTPRequestFailed struct {
|
||||
// StatusCode is the status code that failed.
|
||||
StatusCode int
|
||||
}
|
||||
|
||||
// Error implements error.
|
||||
func (err *ErrHTTPRequestFailed) Error() string {
|
||||
return fmt.Sprintf("httpapi: http request failed: %d", err.StatusCode)
|
||||
}
|
||||
|
||||
// errMaybeCensorship indicates that there was an error at the networking layer
|
||||
// including, e.g., DNS, TCP connect, TLS. When we see this kind of error, we
|
||||
// will consider retrying with another endpoint under the assumption that it
|
||||
// may be that the current endpoint is censored.
|
||||
type errMaybeCensorship struct {
|
||||
// Err is the underlying error
|
||||
Err error
|
||||
}
|
||||
|
||||
// Error implements error
|
||||
func (err *errMaybeCensorship) Error() string {
|
||||
return err.Err.Error()
|
||||
}
|
||||
|
||||
// Unwrap allows to get the underlying error
|
||||
func (err *errMaybeCensorship) Unwrap() error {
|
||||
return err.Err
|
||||
}
|
||||
|
||||
// docall calls the API represented by the given request |req| on the given |endpoint|
|
||||
// and returns the response and its body or an error.
|
||||
func docall(endpoint *Endpoint, desc *Descriptor, request *http.Request) (*http.Response, []byte, error) {
|
||||
// Implementation note: remember to mark errors for which you want
|
||||
// to retry with another endpoint using errMaybeCensorship.
|
||||
response, err := endpoint.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
return nil, nil, &errMaybeCensorship{err}
|
||||
}
|
||||
defer response.Body.Close()
|
||||
// Implementation note: always read and log the response body since
|
||||
// it's quite useful to see the response JSON on API error.
|
||||
r := io.LimitReader(response.Body, DefaultMaxBodySize)
|
||||
data, err := netxlite.ReadAllContext(request.Context(), r)
|
||||
if err != nil {
|
||||
return response, nil, &errMaybeCensorship{err}
|
||||
}
|
||||
desc.Logger.Debugf("httpapi: response body length: %d bytes", len(data))
|
||||
if desc.LogBody {
|
||||
desc.Logger.Debugf("httpapi: response body: %s", string(data))
|
||||
}
|
||||
if response.StatusCode >= 400 {
|
||||
return response, nil, &ErrHTTPRequestFailed{response.StatusCode}
|
||||
}
|
||||
return response, data, nil
|
||||
}
|
||||
|
||||
// call is like Call but also returns the response.
|
||||
func call(ctx context.Context, desc *Descriptor, endpoint *Endpoint) (*http.Response, []byte, error) {
|
||||
timeout := desc.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = DefaultCallTimeout // as documented
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
request, err := newRequest(ctx, endpoint, desc)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return docall(endpoint, desc, request)
|
||||
}
|
||||
|
||||
// Call invokes the API described by |desc| on the given HTTP |endpoint| and
|
||||
// returns the response body (as a slice of bytes) or an error.
|
||||
//
|
||||
// Note: this function returns ErrHTTPRequestFailed if the HTTP status code is
|
||||
// greater or equal than 400. You could use errors.As to obtain a copy of the
|
||||
// error that was returned and see for yourself the actual status code.
|
||||
func Call(ctx context.Context, desc *Descriptor, endpoint *Endpoint) ([]byte, error) {
|
||||
_, rawResponseBody, err := call(ctx, desc, endpoint)
|
||||
return rawResponseBody, err
|
||||
}
|
||||
|
||||
// goodContentTypeForJSON tracks known-good content-types for JSON. If the content-type
|
||||
// is not in this map, |CallWithJSONResponse| emits a warning message.
|
||||
var goodContentTypeForJSON = map[string]bool{
|
||||
applicationJSON: true,
|
||||
}
|
||||
|
||||
// CallWithJSONResponse is like Call but also assumes that the response is a
|
||||
// JSON body and attempts to parse it into the |response| field.
|
||||
//
|
||||
// Note: this function returns ErrHTTPRequestFailed if the HTTP status code is
|
||||
// greater or equal than 400. You could use errors.As to obtain a copy of the
|
||||
// error that was returned and see for yourself the actual status code.
|
||||
func CallWithJSONResponse(ctx context.Context, desc *Descriptor, endpoint *Endpoint, response any) error {
|
||||
httpResp, rawRespBody, err := call(ctx, desc, endpoint)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ctype := httpResp.Header.Get("Content-Type"); !goodContentTypeForJSON[ctype] {
|
||||
desc.Logger.Warnf("httpapi: unexpected content-type: %s", ctype)
|
||||
// fallthrough
|
||||
}
|
||||
return json.Unmarshal(rawRespBody, response)
|
||||
}
|
1163
internal/httpapi/call_test.go
Normal file
1163
internal/httpapi/call_test.go
Normal file
File diff suppressed because it is too large
Load Diff
155
internal/httpapi/descriptor.go
Normal file
155
internal/httpapi/descriptor.go
Normal file
|
@ -0,0 +1,155 @@
|
|||
package httpapi
|
||||
|
||||
//
|
||||
// HTTP API descriptor (e.g., GET /api/v1/test-list/urls)
|
||||
//
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// Descriptor contains the parameters for calling a given HTTP
|
||||
// API (e.g., GET /api/v1/test-list/urls).
|
||||
//
|
||||
// The zero value of this struct is invalid. Please, fill all the
|
||||
// fields marked as MANDATORY for correct initialization.
|
||||
type Descriptor struct {
|
||||
// Accept contains the OPTIONAL accept header.
|
||||
Accept string
|
||||
|
||||
// Authorization is the OPTIONAL authorization.
|
||||
Authorization string
|
||||
|
||||
// ContentType is the OPTIONAL content-type header.
|
||||
ContentType string
|
||||
|
||||
// LogBody OPTIONALLY enables logging bodies.
|
||||
LogBody bool
|
||||
|
||||
// Logger is the MANDATORY logger to use.
|
||||
//
|
||||
// For example, model.DiscardLogger.
|
||||
Logger model.Logger
|
||||
|
||||
// MaxBodySize is the OPTIONAL maximum response body size. If
|
||||
// not set, we use the |DefaultMaxBodySize| constant.
|
||||
MaxBodySize int64
|
||||
|
||||
// Method is the MANDATORY request method.
|
||||
Method string
|
||||
|
||||
// RequestBody is the OPTIONAL request body.
|
||||
RequestBody []byte
|
||||
|
||||
// Timeout is the OPTIONAL timeout for this call. If no timeout
|
||||
// is specified we will use the |DefaultCallTimeout| const.
|
||||
Timeout time.Duration
|
||||
|
||||
// URLPath is the MANDATORY URL path.
|
||||
URLPath string
|
||||
|
||||
// URLQuery is the OPTIONAL query.
|
||||
URLQuery url.Values
|
||||
}
|
||||
|
||||
// WithBodyLogging returns a SHALLOW COPY of |Descriptor| with LogBody set to |value|. You SHOULD
|
||||
// only use this method when initializing the descriptor you want to use.
|
||||
func (desc *Descriptor) WithBodyLogging(value bool) *Descriptor {
|
||||
out := &Descriptor{}
|
||||
*out = *desc
|
||||
out.LogBody = value
|
||||
return out
|
||||
}
|
||||
|
||||
// DefaultMaxBodySize is the default value for the maximum
|
||||
// body size you can fetch using the httpapi package.
|
||||
const DefaultMaxBodySize = 1 << 22
|
||||
|
||||
// DefaultCallTimeout is the default timeout for an httpapi call.
|
||||
const DefaultCallTimeout = 60 * time.Second
|
||||
|
||||
// NewGETJSONDescriptor is a convenience factory for creating a new descriptor
|
||||
// that uses the GET method and expects a JSON response.
|
||||
func NewGETJSONDescriptor(logger model.Logger, urlPath string) *Descriptor {
|
||||
return NewGETJSONWithQueryDescriptor(logger, urlPath, url.Values{})
|
||||
}
|
||||
|
||||
// applicationJSON is the content-type for JSON
|
||||
const applicationJSON = "application/json"
|
||||
|
||||
// NewGETJSONWithQueryDescriptor is like NewGETJSONDescriptor but it also
|
||||
// allows you to provide |query| arguments. Leaving |query| nil or empty
|
||||
// is equivalent to calling NewGETJSONDescriptor directly.
|
||||
func NewGETJSONWithQueryDescriptor(logger model.Logger, urlPath string, query url.Values) *Descriptor {
|
||||
return &Descriptor{
|
||||
Accept: applicationJSON,
|
||||
Authorization: "",
|
||||
ContentType: "",
|
||||
LogBody: false,
|
||||
Logger: logger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodGet,
|
||||
RequestBody: nil,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: urlPath,
|
||||
URLQuery: query,
|
||||
}
|
||||
}
|
||||
|
||||
// NewPOSTJSONWithJSONResponseDescriptor creates a descriptor that POSTs a JSON document
|
||||
// and expects to receive back a JSON document from the API.
|
||||
//
|
||||
// This function ONLY fails if we cannot serialize the |request| to JSON. So, if you know
|
||||
// that |request| is JSON-serializable, you can safely call MustNewPostJSONWithJSONResponseDescriptor instead.
|
||||
func NewPOSTJSONWithJSONResponseDescriptor(logger model.Logger, urlPath string, request any) (*Descriptor, error) {
|
||||
rawRequest, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
desc := &Descriptor{
|
||||
Accept: applicationJSON,
|
||||
Authorization: "",
|
||||
ContentType: applicationJSON,
|
||||
LogBody: false,
|
||||
Logger: logger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodPost,
|
||||
RequestBody: rawRequest,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: urlPath,
|
||||
URLQuery: nil,
|
||||
}
|
||||
return desc, nil
|
||||
}
|
||||
|
||||
// MustNewPOSTJSONWithJSONResponseDescriptor is like NewPOSTJSONWithJSONResponseDescriptor except that
|
||||
// it panics in case it's not possible to JSON serialize the |request|.
|
||||
func MustNewPOSTJSONWithJSONResponseDescriptor(logger model.Logger, urlPath string, request any) *Descriptor {
|
||||
desc, err := NewPOSTJSONWithJSONResponseDescriptor(logger, urlPath, request)
|
||||
runtimex.PanicOnError(err, "NewPOSTJSONWithJSONResponseDescriptor failed")
|
||||
return desc
|
||||
}
|
||||
|
||||
// NewGETResourceDescriptor creates a generic descriptor for GETting a
|
||||
// resource of unspecified type using the given |urlPath|.
|
||||
func NewGETResourceDescriptor(logger model.Logger, urlPath string) *Descriptor {
|
||||
return &Descriptor{
|
||||
Accept: "",
|
||||
Authorization: "",
|
||||
ContentType: "",
|
||||
LogBody: false,
|
||||
Logger: logger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodGet,
|
||||
RequestBody: nil,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: urlPath,
|
||||
URLQuery: url.Values{},
|
||||
}
|
||||
}
|
248
internal/httpapi/descriptor_test.go
Normal file
248
internal/httpapi/descriptor_test.go
Normal file
|
@ -0,0 +1,248 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
func TestDescriptor_WithBodyLogging(t *testing.T) {
|
||||
type fields struct {
|
||||
Accept string
|
||||
Authorization string
|
||||
ContentType string
|
||||
LogBody bool
|
||||
Logger model.Logger
|
||||
MaxBodySize int64
|
||||
Method string
|
||||
RequestBody []byte
|
||||
Timeout time.Duration
|
||||
URLPath string
|
||||
URLQuery url.Values
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want *Descriptor
|
||||
}{{
|
||||
name: "with empty fields",
|
||||
fields: fields{}, // LogBody defaults to false
|
||||
want: &Descriptor{
|
||||
LogBody: true,
|
||||
},
|
||||
}, {
|
||||
name: "with nonempty fields",
|
||||
fields: fields{
|
||||
Accept: "xx",
|
||||
Authorization: "y",
|
||||
ContentType: "zzz",
|
||||
LogBody: false, // obviously must be false
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: 123,
|
||||
Method: "POST",
|
||||
RequestBody: []byte("123"),
|
||||
Timeout: 15555,
|
||||
URLPath: "/",
|
||||
URLQuery: map[string][]string{
|
||||
"a": {"b"},
|
||||
},
|
||||
},
|
||||
want: &Descriptor{
|
||||
Accept: "xx",
|
||||
Authorization: "y",
|
||||
ContentType: "zzz",
|
||||
LogBody: true,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: 123,
|
||||
Method: "POST",
|
||||
RequestBody: []byte("123"),
|
||||
Timeout: 15555,
|
||||
URLPath: "/",
|
||||
URLQuery: map[string][]string{
|
||||
"a": {"b"},
|
||||
},
|
||||
},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
desc := &Descriptor{
|
||||
Accept: tt.fields.Accept,
|
||||
Authorization: tt.fields.Authorization,
|
||||
ContentType: tt.fields.ContentType,
|
||||
LogBody: tt.fields.LogBody,
|
||||
Logger: tt.fields.Logger,
|
||||
MaxBodySize: tt.fields.MaxBodySize,
|
||||
Method: tt.fields.Method,
|
||||
RequestBody: tt.fields.RequestBody,
|
||||
Timeout: tt.fields.Timeout,
|
||||
URLPath: tt.fields.URLPath,
|
||||
URLQuery: tt.fields.URLQuery,
|
||||
}
|
||||
got := desc.WithBodyLogging(true)
|
||||
if diff := cmp.Diff(tt.want, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewGetJSONDescriptor(t *testing.T) {
|
||||
expected := &Descriptor{
|
||||
Accept: "application/json",
|
||||
Authorization: "",
|
||||
ContentType: "",
|
||||
LogBody: false,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodGet,
|
||||
RequestBody: nil,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: "/robots.txt",
|
||||
URLQuery: url.Values{},
|
||||
}
|
||||
got := NewGETJSONDescriptor(model.DiscardLogger, "/robots.txt")
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewGetJSONWithQueryDescriptor(t *testing.T) {
|
||||
query := url.Values{
|
||||
"a": {"b"},
|
||||
"c": {"d"},
|
||||
}
|
||||
expected := &Descriptor{
|
||||
Accept: "application/json",
|
||||
Authorization: "",
|
||||
ContentType: "",
|
||||
LogBody: false,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodGet,
|
||||
RequestBody: nil,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: "/robots.txt",
|
||||
URLQuery: query,
|
||||
}
|
||||
got := NewGETJSONWithQueryDescriptor(model.DiscardLogger, "/robots.txt", query)
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPOSTJSONWithJSONResponseDescriptor(t *testing.T) {
|
||||
type request struct {
|
||||
Name string
|
||||
Age int64
|
||||
}
|
||||
|
||||
t.Run("with failure", func(t *testing.T) {
|
||||
request := make(chan int64)
|
||||
got, err := NewPOSTJSONWithJSONResponseDescriptor(model.DiscardLogger, "/robots.txt", request)
|
||||
if err == nil || err.Error() != "json: unsupported type: chan int64" {
|
||||
log.Fatal("unexpected err", err)
|
||||
}
|
||||
if got != nil {
|
||||
log.Fatal("expected to get a nil Descriptor")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with success", func(t *testing.T) {
|
||||
request := request{
|
||||
Name: "sbs",
|
||||
Age: 99,
|
||||
}
|
||||
expected := &Descriptor{
|
||||
Accept: "application/json",
|
||||
Authorization: "",
|
||||
ContentType: "application/json",
|
||||
LogBody: false,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodPost,
|
||||
RequestBody: []byte(`{"Name":"sbs","Age":99}`),
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: "/robots.txt",
|
||||
URLQuery: nil,
|
||||
}
|
||||
got, err := NewPOSTJSONWithJSONResponseDescriptor(model.DiscardLogger, "/robots.txt", request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestMustNewPOSTJSONWithJSONResponseDescriptor(t *testing.T) {
|
||||
type request struct {
|
||||
Name string
|
||||
Age int64
|
||||
}
|
||||
|
||||
t.Run("with failure", func(t *testing.T) {
|
||||
var panicked bool
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
panicked = true
|
||||
}
|
||||
}()
|
||||
request := make(chan int64)
|
||||
_ = MustNewPOSTJSONWithJSONResponseDescriptor(model.DiscardLogger, "/robots.txt", request)
|
||||
}()
|
||||
if !panicked {
|
||||
t.Fatal("did not panic")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with success", func(t *testing.T) {
|
||||
request := request{
|
||||
Name: "sbs",
|
||||
Age: 99,
|
||||
}
|
||||
expected := &Descriptor{
|
||||
Accept: "application/json",
|
||||
Authorization: "",
|
||||
ContentType: "application/json",
|
||||
LogBody: false,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodPost,
|
||||
RequestBody: []byte(`{"Name":"sbs","Age":99}`),
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: "/robots.txt",
|
||||
URLQuery: nil,
|
||||
}
|
||||
got := MustNewPOSTJSONWithJSONResponseDescriptor(model.DiscardLogger, "/robots.txt", request)
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewGetResourceDescriptor(t *testing.T) {
|
||||
expected := &Descriptor{
|
||||
Accept: "",
|
||||
Authorization: "",
|
||||
ContentType: "",
|
||||
LogBody: false,
|
||||
Logger: model.DiscardLogger,
|
||||
MaxBodySize: DefaultMaxBodySize,
|
||||
Method: http.MethodGet,
|
||||
RequestBody: nil,
|
||||
Timeout: DefaultCallTimeout,
|
||||
URLPath: "/robots.txt",
|
||||
URLQuery: url.Values{},
|
||||
}
|
||||
got := NewGETResourceDescriptor(model.DiscardLogger, "/robots.txt")
|
||||
if diff := cmp.Diff(expected, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
15
internal/httpapi/doc.go
Normal file
15
internal/httpapi/doc.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Package httpapi contains code for calling HTTP APIs.
|
||||
//
|
||||
// We model HTTP APIs as follows:
|
||||
//
|
||||
// 1. |Endpoint| is an API endpoint (e.g., https://api.ooni.io);
|
||||
//
|
||||
// 2. |Descriptor| describes the specific API you want to use (e.g.,
|
||||
// GET /api/v1/test-list/urls with JSON response body).
|
||||
//
|
||||
// Generally, you use |Call| to call the API identified by a |Descriptor|
|
||||
// on the specified |Endpoint|. However, there are cases where you
|
||||
// need more complex calling patterns. For example, with |SequenceCaller|
|
||||
// you can invoke the same API |Descriptor| with multiple equivalent
|
||||
// API |Endpoint|s until one of them succeeds or all fail.
|
||||
package httpapi
|
76
internal/httpapi/endpoint.go
Normal file
76
internal/httpapi/endpoint.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package httpapi
|
||||
|
||||
//
|
||||
// HTTP API Endpoint (e.g., https://api.ooni.io)
|
||||
//
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/model"
|
||||
|
||||
// Endpoint models an HTTP endpoint on which you can call
|
||||
// several HTTP APIs (e.g., https://api.ooni.io) using a
|
||||
// given HTTP client potentially using a circumvention tunnel
|
||||
// mechanism such as psiphon or torsf.
|
||||
//
|
||||
// The zero value of this struct is invalid. Please, fill all the
|
||||
// fields marked as MANDATORY for correct initialization.
|
||||
type Endpoint struct {
|
||||
// BaseURL is the MANDATORY endpoint base URL. We will honour the
|
||||
// path of this URL and prepend it to the actual path specified inside
|
||||
// a |Descriptor.URLPath|. However, we will always discard any query
|
||||
// that may have been set inside the BaseURL. The only query string
|
||||
// will be composed from the |Descriptor.URLQuery| values.
|
||||
//
|
||||
// For example, https://api.ooni.io.
|
||||
BaseURL string
|
||||
|
||||
// HTTPClient is the MANDATORY HTTP client to use.
|
||||
//
|
||||
// For example, http.DefaultClient. You can introduce circumvention
|
||||
// here by using an HTTPClient bound to a specific tunnel.
|
||||
HTTPClient model.HTTPClient
|
||||
|
||||
// Host is the OPTIONAL host header to use.
|
||||
//
|
||||
// If this field is empty we use the BaseURL's hostname. A specific
|
||||
// host header may be needed when using cloudfronting.
|
||||
Host string
|
||||
|
||||
// User-Agent is the OPTIONAL user-agent to use. If empty,
|
||||
// we'll use the stdlib's default user-agent string.
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
// NewEndpointList constructs a list of API endpoints from |services|
|
||||
// returned by the OONI backend (or known in advance).
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// - httpClient is the HTTP client to use for accessing the endpoints;
|
||||
//
|
||||
// - userAgent is the user agent you would like to use;
|
||||
//
|
||||
// - service is the list of services gathered from the backend.
|
||||
func NewEndpointList(httpClient model.HTTPClient,
|
||||
userAgent string, services ...model.OOAPIService) (out []*Endpoint) {
|
||||
for _, svc := range services {
|
||||
switch svc.Type {
|
||||
case "https":
|
||||
out = append(out, &Endpoint{
|
||||
BaseURL: svc.Address,
|
||||
HTTPClient: httpClient,
|
||||
Host: "",
|
||||
UserAgent: userAgent,
|
||||
})
|
||||
case "cloudfront":
|
||||
out = append(out, &Endpoint{
|
||||
BaseURL: svc.Address,
|
||||
HTTPClient: httpClient,
|
||||
Host: svc.Front,
|
||||
UserAgent: userAgent,
|
||||
})
|
||||
default:
|
||||
// nothing!
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
69
internal/httpapi/endpoint_test.go
Normal file
69
internal/httpapi/endpoint_test.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestNewEndpointList(t *testing.T) {
|
||||
type args struct {
|
||||
httpClient model.HTTPClient
|
||||
userAgent string
|
||||
services []model.OOAPIService
|
||||
}
|
||||
defaultHTTPClient := &mocks.HTTPClient{}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantOut []*Endpoint
|
||||
}{{
|
||||
name: "with no services",
|
||||
args: args{
|
||||
httpClient: defaultHTTPClient,
|
||||
userAgent: model.HTTPHeaderUserAgent,
|
||||
services: nil,
|
||||
},
|
||||
wantOut: nil,
|
||||
}, {
|
||||
name: "common cases",
|
||||
args: args{
|
||||
httpClient: defaultHTTPClient,
|
||||
userAgent: model.HTTPHeaderUserAgent,
|
||||
services: []model.OOAPIService{{
|
||||
Address: "https://www.example.com/",
|
||||
Type: "https",
|
||||
Front: "",
|
||||
}, {
|
||||
Address: "https://www.example.org/",
|
||||
Type: "cloudfront",
|
||||
Front: "example.org.it",
|
||||
}, {
|
||||
Address: "https://nonexistent.onion/",
|
||||
Type: "onion",
|
||||
Front: "",
|
||||
}},
|
||||
},
|
||||
wantOut: []*Endpoint{{
|
||||
BaseURL: "https://www.example.com/",
|
||||
HTTPClient: defaultHTTPClient,
|
||||
Host: "",
|
||||
UserAgent: model.HTTPHeaderUserAgent,
|
||||
}, {
|
||||
BaseURL: "https://www.example.org/",
|
||||
HTTPClient: defaultHTTPClient,
|
||||
Host: "example.org.it",
|
||||
UserAgent: model.HTTPHeaderUserAgent,
|
||||
}},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotOut := NewEndpointList(tt.args.httpClient, tt.args.userAgent, tt.args.services...)
|
||||
if diff := cmp.Diff(tt.wantOut, gotOut); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
92
internal/httpapi/sequence.go
Normal file
92
internal/httpapi/sequence.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package httpapi
|
||||
|
||||
//
|
||||
// Sequentially call available API endpoints until one succeed
|
||||
// or all of them fail. A future implementation of this code may
|
||||
// (probably should?) take into account knowledge of what is
|
||||
// working and what is not working to optimize the order with
|
||||
// which to try different alternatives.
|
||||
//
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
)
|
||||
|
||||
// SequenceCaller calls the API specified by |Descriptor| once for each of
|
||||
// the available |Endpoints| until one of them succeeds.
|
||||
//
|
||||
// CAVEAT: this code will ONLY retry API calls with subsequent endpoints when
|
||||
// the error originates in the HTTP round trip or while reading the body.
|
||||
type SequenceCaller struct {
|
||||
// Descriptor is the API |Descriptor|.
|
||||
Descriptor *Descriptor
|
||||
|
||||
// Endpoints is the list of |Endpoint| to use.
|
||||
Endpoints []*Endpoint
|
||||
}
|
||||
|
||||
// NewSequenceCaller is a factory for creating a |SequenceCaller|.
|
||||
func NewSequenceCaller(desc *Descriptor, endpoints ...*Endpoint) *SequenceCaller {
|
||||
return &SequenceCaller{
|
||||
Descriptor: desc,
|
||||
Endpoints: endpoints,
|
||||
}
|
||||
}
|
||||
|
||||
// ErrAllEndpointsFailed indicates that all endpoints failed.
|
||||
var ErrAllEndpointsFailed = errors.New("httpapi: all endpoints failed")
|
||||
|
||||
// shouldRetry returns true when we should try with another endpoint given the
|
||||
// value of |err| which could (obviously) be nil in case of success.
|
||||
func (sc *SequenceCaller) shouldRetry(err error) bool {
|
||||
var kind *errMaybeCensorship
|
||||
belongs := errors.As(err, &kind)
|
||||
return belongs
|
||||
}
|
||||
|
||||
// Call calls |Call| for each |Endpoint| and |Descriptor| until one endpoint succeeds. The
|
||||
// return value is the response body and the selected endpoint index or the error.
|
||||
//
|
||||
// CAVEAT: this code will ONLY retry API calls with subsequent endpoints when
|
||||
// the error originates in the HTTP round trip or while reading the body.
|
||||
func (sc *SequenceCaller) Call(ctx context.Context) ([]byte, int, error) {
|
||||
var selected int
|
||||
merr := multierror.New(ErrAllEndpointsFailed)
|
||||
for _, epnt := range sc.Endpoints {
|
||||
respBody, err := Call(ctx, sc.Descriptor, epnt)
|
||||
if sc.shouldRetry(err) {
|
||||
merr.Add(err)
|
||||
selected++
|
||||
continue
|
||||
}
|
||||
// Note: some errors will lead us to return
|
||||
// early as documented for this method
|
||||
return respBody, selected, err
|
||||
}
|
||||
return nil, -1, merr
|
||||
}
|
||||
|
||||
// CallWithJSONResponse is like |SequenceCaller.Call| except that it invokes the
|
||||
// underlying |CallWithJSONResponse| rather than invoking |Call|.
|
||||
//
|
||||
// CAVEAT: this code will ONLY retry API calls with subsequent endpoints when
|
||||
// the error originates in the HTTP round trip or while reading the body.
|
||||
func (sc *SequenceCaller) CallWithJSONResponse(ctx context.Context, response any) (int, error) {
|
||||
var selected int
|
||||
merr := multierror.New(ErrAllEndpointsFailed)
|
||||
for _, epnt := range sc.Endpoints {
|
||||
err := CallWithJSONResponse(ctx, sc.Descriptor, epnt, response)
|
||||
if sc.shouldRetry(err) {
|
||||
merr.Add(err)
|
||||
selected++
|
||||
continue
|
||||
}
|
||||
// Note: some errors will lead us to return
|
||||
// early as documented for this method
|
||||
return selected, err
|
||||
}
|
||||
return -1, merr
|
||||
}
|
358
internal/httpapi/sequence_test.go
Normal file
358
internal/httpapi/sequence_test.go
Normal file
|
@ -0,0 +1,358 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestSequenceCaller(t *testing.T) {
|
||||
t.Run("Call", func(t *testing.T) {
|
||||
t.Run("first success", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(strings.NewReader("deadbeef")),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
data, idx, err := sc.Call(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if idx != 0 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if diff := cmp.Diff([]byte("deadbeef"), data); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("first HTTP failure and we immediately stop", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 403, // should cause us to return early
|
||||
Body: io.NopCloser(strings.NewReader("deadbeef")),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
data, idx, err := sc.Call(context.Background())
|
||||
var failure *ErrHTTPRequestFailed
|
||||
if !errors.As(err, &failure) || failure.StatusCode != 403 {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if idx != 0 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if len(data) > 0 {
|
||||
t.Fatal("expected to see no response body")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("first network failure, second success", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to cycle to the second entry
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(strings.NewReader("abad1dea")),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
data, idx, err := sc.Call(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if idx != 1 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if diff := cmp.Diff([]byte("abad1dea"), data); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("all network failure", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to cycle to the next entry
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to cycle to the next entry
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
data, idx, err := sc.Call(context.Background())
|
||||
if !errors.Is(err, ErrAllEndpointsFailed) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if idx != -1 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if len(data) > 0 {
|
||||
t.Fatal("expected zero-length data")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("CallWithJSONResponse", func(t *testing.T) {
|
||||
type response struct {
|
||||
Name string
|
||||
Age int64
|
||||
}
|
||||
|
||||
t.Run("first success", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(strings.NewReader(`{"Name":"sbs","Age":99}`)),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(strings.NewReader(`{}`)), // different
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
expect := response{
|
||||
Name: "sbs",
|
||||
Age: 99,
|
||||
}
|
||||
var got response
|
||||
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if idx != 0 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if diff := cmp.Diff(expect, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("first HTTP failure and we immediately stop", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 403, // should be enough to cause us fail immediately
|
||||
Body: io.NopCloser(strings.NewReader(`{"Age": 155, "Name": "sbs"}`)),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
// even though there is a JSON body we don't care about reading it
|
||||
// and so we expect to see in output the zero-value struct
|
||||
expect := response{
|
||||
Name: "",
|
||||
Age: 0,
|
||||
}
|
||||
var got response
|
||||
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
|
||||
var failure *ErrHTTPRequestFailed
|
||||
if !errors.As(err, &failure) || failure.StatusCode != 403 {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if idx != 0 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if diff := cmp.Diff(expect, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("first network failure, second success", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to try the next entry
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
resp := &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: io.NopCloser(strings.NewReader(`{"Age":155}`)),
|
||||
}
|
||||
return resp, nil
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
expect := response{
|
||||
Name: "",
|
||||
Age: 155,
|
||||
}
|
||||
var got response
|
||||
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if idx != 1 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
if diff := cmp.Diff(expect, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("all network failure", func(t *testing.T) {
|
||||
sc := NewSequenceCaller(
|
||||
&Descriptor{
|
||||
Logger: model.DiscardLogger,
|
||||
Method: http.MethodGet,
|
||||
URLPath: "/",
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://a.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to try the next entry
|
||||
},
|
||||
},
|
||||
},
|
||||
&Endpoint{
|
||||
BaseURL: "https://b.example.com/",
|
||||
HTTPClient: &mocks.HTTPClient{
|
||||
MockDo: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, io.EOF // should cause us to try the next entry
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
var got response
|
||||
idx, err := sc.CallWithJSONResponse(context.Background(), &got)
|
||||
if !errors.Is(err, ErrAllEndpointsFailed) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if idx != -1 {
|
||||
t.Fatal("invalid idx")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
|
@ -1,4 +1,8 @@
|
|||
// Package httpx contains http extensions.
|
||||
//
|
||||
// Deprecated: new code should use httpapi instead. While this package and httpapi
|
||||
// are basically using the same implementation, the API exposed by httpapi allows
|
||||
// us to try the same request with multiple HTTP endpoints.
|
||||
package httpx
|
||||
|
||||
import (
|
||||
|
|
Loading…
Reference in New Issue
Block a user