Skip to content

Commit

Permalink
cleanup: merge httpheader and httpfailure into model
Browse files Browse the repository at this point in the history
These two small packages could easily be merged into the model
package, since they're clearly model-like packages.

Part of ooni/probe#2115
  • Loading branch information
bassosimone committed May 25, 2022
1 parent 928de50 commit 2bc1a24
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 90 deletions.
5 changes: 3 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ This repository contains core OONI tools written in Go:

Every top-level directory in this repository contains an explanatory README file. You
may also notice that some internal packages live under [internal/engine](internal/engine)
while most others are top-level. This is part of a long-standing refactoring started
when we merged https://github.com/ooni/probe-engine into this repository. We'll slowly
while most others are top-level. This is part of [a long-standing refactoring](
https://github.com/ooni/probe/issues/2115) started when we merged
https://github.com/ooni/probe-engine into this repository. We'll slowly
ensure that all packages inside `engine` are moved out of it and inside `internal`.

## Semantic versioning policy
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/oohelper/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
Expand Down Expand Up @@ -105,9 +105,9 @@ func (oo OOClient) Do(ctx context.Context, config OOConfig) (*CtrlResponse, erro
creq := ctrlRequest{
HTTPRequest: config.TargetURL,
HTTPRequestHeaders: map[string][]string{
"Accept": {httpheader.Accept()},
"Accept-Language": {httpheader.AcceptLanguage()},
"User-Agent": {httpheader.UserAgent()},
"Accept": {model.HTTPHeaderAccept},
"Accept-Language": {model.HTTPHeaderAcceptLanguage},
"User-Agent": {model.HTTPHeaderUserAgent},
},
TCPConnect: endpoints,
}
Expand Down
7 changes: 3 additions & 4 deletions internal/engine/experiment/hhfm/hhfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"time"

"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
Expand Down Expand Up @@ -121,12 +120,12 @@ func (m Measurer) Run(
return err
}
headers := map[string]string{
randx.ChangeCapitalization("Accept"): httpheader.Accept(),
randx.ChangeCapitalization("Accept"): model.HTTPHeaderAccept,
randx.ChangeCapitalization("Accept-Charset"): "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
randx.ChangeCapitalization("Accept-Encoding"): "gzip,deflate,sdch",
randx.ChangeCapitalization("Accept-Language"): httpheader.AcceptLanguage(),
randx.ChangeCapitalization("Accept-Language"): model.HTTPHeaderAcceptLanguage,
randx.ChangeCapitalization("Host"): randx.Letters(15) + ".com",
randx.ChangeCapitalization("User-Agent"): httpheader.UserAgent(),
randx.ChangeCapitalization("User-Agent"): model.HTTPHeaderUserAgent,
}
for key, value := range headers {
// Implementation note: Golang will normalize the header names. We will use
Expand Down
8 changes: 4 additions & 4 deletions internal/engine/experiment/urlgetter/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"net/http/cookiejar"
"net/url"

"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
Expand Down Expand Up @@ -55,7 +55,7 @@ func (r Runner) Run(ctx context.Context) error {
// returns httpheader.RandomUserAgent().
func MaybeUserAgent(ua string) string {
if ua == "" {
ua = httpheader.UserAgent()
ua = model.HTTPHeaderUserAgent
}
return ua
}
Expand All @@ -65,8 +65,8 @@ func (r Runner) httpGet(ctx context.Context, url string) error {
req, err := http.NewRequest(r.Config.Method, url, nil)
runtimex.PanicOnError(err, "http.NewRequest failed")
req = req.WithContext(ctx)
req.Header.Set("Accept", httpheader.Accept())
req.Header.Set("Accept-Language", httpheader.AcceptLanguage())
req.Header.Set("Accept", model.HTTPHeaderAccept)
req.Header.Set("Accept-Language", model.HTTPHeaderAcceptLanguage)
req.Header.Set("User-Agent", MaybeUserAgent(r.Config.UserAgent))
if r.Config.HTTPHost != "" {
req.Host = r.Config.HTTPHost
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/experiment/urlgetter/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
)

func TestRunnerWithInvalidURLScheme(t *testing.T) {
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestRunnerWeCanForceUserAgent(t *testing.T) {
}

func TestRunnerDefaultUserAgent(t *testing.T) {
expected := httpheader.UserAgent()
expected := model.HTTPHeaderUserAgent
found := &atomicx.Int64{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") == expected {
Expand Down
7 changes: 3 additions & 4 deletions internal/engine/experiment/webconnectivity/webconnectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity/internal"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
"github.com/ooni/probe-cli/v3/internal/model"
)
Expand Down Expand Up @@ -174,9 +173,9 @@ func (m Measurer) Run(
tk.Control, err = Control(ctx, sess, testhelper.Address, ControlRequest{
HTTPRequest: URL.String(),
HTTPRequestHeaders: map[string][]string{
"Accept": {httpheader.Accept()},
"Accept-Language": {httpheader.AcceptLanguage()},
"User-Agent": {httpheader.UserAgent()},
"Accept": {model.HTTPHeaderAccept},
"Accept-Language": {model.HTTPHeaderAcceptLanguage},
"User-Agent": {model.HTTPHeaderUserAgent},
},
TCPConnect: epnts.Endpoints(),
})
Expand Down
7 changes: 3 additions & 4 deletions internal/engine/experiment/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpfailure"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
Expand Down Expand Up @@ -109,11 +108,11 @@ func (tk *TestKeys) Update(v urlgetter.MultiOutput) {
if failure != nil {
// nothing to do here
} else if v.TestKeys.HTTPResponseStatus != 302 {
failure = &httpfailure.UnexpectedStatusCode
failure = &model.HTTPUnexpectedStatusCode
} else if len(v.TestKeys.HTTPResponseLocations) != 1 {
failure = &httpfailure.UnexpectedRedirectURL
failure = &model.HTTPUnexpectedRedirectURL
} else if v.TestKeys.HTTPResponseLocations[0] != WebHTTPSURL {
failure = &httpfailure.UnexpectedRedirectURL
failure = &model.HTTPUnexpectedRedirectURL
}
tk.WhatsappHTTPFailure = failure
}
Expand Down
9 changes: 4 additions & 5 deletions internal/engine/experiment/whatsapp/whatsapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
"github.com/ooni/probe-cli/v3/internal/engine/experiment/whatsapp"
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpfailure"
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
"github.com/ooni/probe-cli/v3/internal/model"
)
Expand Down Expand Up @@ -414,7 +413,7 @@ func TestTestKeysOnlyWebHTTPFailureNo302(t *testing.T) {
if tk.WhatsappEndpointsStatus != "ok" {
t.Fatal("invalid WhatsappEndpointsStatus")
}
if *tk.WhatsappWebFailure != httpfailure.UnexpectedStatusCode {
if *tk.WhatsappWebFailure != model.HTTPUnexpectedStatusCode {
t.Fatal("invalid WhatsappWebFailure")
}
if tk.WhatsappWebStatus != "blocked" {
Expand Down Expand Up @@ -459,7 +458,7 @@ func TestTestKeysOnlyWebHTTPFailureNoLocations(t *testing.T) {
if tk.WhatsappEndpointsStatus != "ok" {
t.Fatal("invalid WhatsappEndpointsStatus")
}
if *tk.WhatsappWebFailure != httpfailure.UnexpectedRedirectURL {
if *tk.WhatsappWebFailure != model.HTTPUnexpectedRedirectURL {
t.Fatal("invalid WhatsappWebFailure")
}
if tk.WhatsappWebStatus != "blocked" {
Expand Down Expand Up @@ -504,7 +503,7 @@ func TestTestKeysOnlyWebHTTPFailureNotExpectedURL(t *testing.T) {
if tk.WhatsappEndpointsStatus != "ok" {
t.Fatal("invalid WhatsappEndpointsStatus")
}
if *tk.WhatsappWebFailure != httpfailure.UnexpectedRedirectURL {
if *tk.WhatsappWebFailure != model.HTTPUnexpectedRedirectURL {
t.Fatal("invalid WhatsappWebFailure")
}
if tk.WhatsappWebStatus != "blocked" {
Expand Down Expand Up @@ -549,7 +548,7 @@ func TestTestKeysOnlyWebHTTPFailureTooManyURLs(t *testing.T) {
if tk.WhatsappEndpointsStatus != "ok" {
t.Fatal("invalid WhatsappEndpointsStatus")
}
if *tk.WhatsappWebFailure != httpfailure.UnexpectedRedirectURL {
if *tk.WhatsappWebFailure != model.HTTPUnexpectedRedirectURL {
t.Fatal("invalid WhatsappWebFailure")
}
if tk.WhatsappWebStatus != "blocked" {
Expand Down
3 changes: 1 addition & 2 deletions internal/engine/geolocate/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"
"strings"

"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/httpx"
"github.com/ooni/probe-cli/v3/internal/model"
)
Expand All @@ -21,7 +20,7 @@ func cloudflareIPLookup(
BaseURL: "https://www.cloudflare.com",
HTTPClient: httpClient,
Logger: logger,
UserAgent: httpheader.CLIUserAgent(),
UserAgent: model.HTTPHeaderUserAgent,
}).WithBodyLogging().Build().FetchResource(ctx, "/cdn-cgi/trace")
if err != nil {
return DefaultProbeIP, err
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/geolocate/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
)

func TestIPLookupWorksUsingcloudlflare(t *testing.T) {
ip, err := cloudflareIPLookup(
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
model.HTTPHeaderUserAgent,
)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/geolocate/stun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/pion/stun"
)

Expand Down Expand Up @@ -128,7 +128,7 @@ func TestIPLookupWorksUsingSTUNEkiga(t *testing.T) {
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
model.HTTPHeaderUserAgent,
)
if err != nil {
t.Fatal(err)
Expand All @@ -143,7 +143,7 @@ func TestIPLookupWorksUsingSTUNGoogle(t *testing.T) {
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
model.HTTPHeaderUserAgent,
)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions internal/engine/geolocate/ubuntu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
)

func TestUbuntuParseError(t *testing.T) {
Expand All @@ -22,7 +22,7 @@ func TestUbuntuParseError(t *testing.T) {
},
}},
log.Log,
httpheader.UserAgent(),
model.HTTPHeaderUserAgent,
)
if err == nil || !strings.HasPrefix(err.Error(), "XML syntax error") {
t.Fatalf("not the error we expected: %+v", err)
Expand All @@ -37,7 +37,7 @@ func TestIPLookupWorksUsingUbuntu(t *testing.T) {
context.Background(),
http.DefaultClient,
log.Log,
httpheader.UserAgent(),
model.HTTPHeaderUserAgent,
)
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 0 additions & 6 deletions internal/engine/httpheader/accept.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/engine/httpheader/acceptlanguage.go

This file was deleted.

16 changes: 0 additions & 16 deletions internal/engine/httpheader/useragent.go

This file was deleted.

15 changes: 0 additions & 15 deletions internal/engine/internal/httpfailure/httpfailure.go

This file was deleted.

7 changes: 3 additions & 4 deletions internal/measurex/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"unicode/utf8"

"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/runtimex"
Expand Down Expand Up @@ -289,9 +288,9 @@ func NewCookieJar() http.CookieJar {
// the headers are the ones we use for measuring.
func NewHTTPRequestHeaderForMeasuring() http.Header {
h := http.Header{}
h.Set("Accept", httpheader.Accept())
h.Set("Accept-Language", httpheader.AcceptLanguage())
h.Set("User-Agent", httpheader.UserAgent())
h.Set("Accept", model.HTTPHeaderAccept)
h.Set("Accept-Language", model.HTTPHeaderAcceptLanguage)
h.Set("User-Agent", model.HTTPHeaderUserAgent)
return h
}

Expand Down
26 changes: 26 additions & 0 deletions internal/model/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package model

//
// Common HTTP definitions and errors.
//

const (
// HTTPHeaderAccept is the Accept header used for measuring.
HTTPHeaderAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

// HTTPHeaderAcceptLanguage is the Accept-Language header used for measuring.
HTTPHeaderAcceptLanguage = "en-US,en;q=0.9"

// HTTPHeaderUserAgent is the User-Agent header used for measuring.
HTTPHeaderUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
)

var (
// HTTPUnexpectedStatusCode indicates that we re not getting
// the expected (range of) HTTP status code(s).
HTTPUnexpectedStatusCode = "http_unexpected_status_code"

// HTTPUnexpectedRedirectURL indicates that the redirect URL
// returned by the server is not the expected one.
HTTPUnexpectedRedirectURL = "http_unexpected_redirect_url"
)
3 changes: 1 addition & 2 deletions internal/netxlite/dnsoverhttps.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"time"

"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
"github.com/ooni/probe-cli/v3/internal/model"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func (t *DNSOverHTTPSTransport) RoundTrip(ctx context.Context, query []byte) ([]
return nil, err
}
req.Host = t.HostOverride
req.Header.Set("user-agent", httpheader.UserAgent())
req.Header.Set("user-agent", model.HTTPHeaderUserAgent)
req.Header.Set("content-type", "application/dns-message")
var resp *http.Response
resp, err = t.Client.Do(req.WithContext(ctx))
Expand Down
Loading

0 comments on commit 2bc1a24

Please sign in to comment.