ooni-probe-cli/internal/engine/experiment/webconnectivity/httpget.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

71 lines
1.9 KiB
Go

package webconnectivity
import (
"context"
"fmt"
"net"
"net/url"
"strings"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
"github.com/ooni/probe-cli/v3/internal/model"
)
// HTTPGetConfig contains the config for HTTPGet
type HTTPGetConfig struct {
Addresses []string
Begin time.Time
Session model.ExperimentSession
TargetURL *url.URL
}
// TODO(bassosimone): we should normalize the timings
// HTTPGetResult contains the results of HTTPGet
type HTTPGetResult struct {
TestKeys urlgetter.TestKeys
Failure *string
}
// TODO(bassosimone): Web Connectivity uses too much external testing
// and we should actually expose much less to the outside by using
// internal testing and by making _many_ functions private.
// HTTPGetMakeDNSCache constructs the DNSCache option for HTTPGet
// by combining domain and addresses into a single string. As a
// corner case, if the domain is an IP address, we return an empty
// string. This corner case corresponds to Web Connectivity
// inputs like https://1.1.1.1.
func HTTPGetMakeDNSCache(domain, addresses string) string {
if net.ParseIP(domain) != nil {
return ""
}
return fmt.Sprintf("%s %s", domain, addresses)
}
// HTTPGet performs the HTTP/HTTPS part of Web Connectivity.
func HTTPGet(ctx context.Context, config HTTPGetConfig) (out HTTPGetResult) {
addresses := strings.Join(config.Addresses, " ")
if addresses == "" {
// TODO(bassosimone): what to do in this case? We clearly
// cannot fill the DNS cache...
return
}
target := config.TargetURL.String()
config.Session.Logger().Infof("GET %s...", target)
domain := config.TargetURL.Hostname()
result, err := urlgetter.Getter{
Begin: config.Begin,
Config: urlgetter.Config{
DNSCache: HTTPGetMakeDNSCache(domain, addresses),
},
Session: config.Session,
Target: target,
}.Get(ctx)
config.Session.Logger().Infof("GET %s... %+v", target, err)
out.Failure = result.Failure
out.TestKeys = result
return
}