refactor(oohelperd): flatten package hierarchy (#834)
In https://github.com/ooni/probe-cli/pull/832's initial diff, I mentioned it would be cool to flatten oohelperd's hier. I'm doing this now, and just for the master branch. This diff is mostly a mechanical refactoring with very light and apparently rather safe manual changes.
This commit is contained in:
parent
a4d17085f5
commit
535a5d3e00
12 changed files with 151 additions and 98 deletions
67
internal/cmd/oohelperd/handler.go
Normal file
67
internal/cmd/oohelperd/handler.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package main
|
||||
|
||||
//
|
||||
// HTTP handler
|
||||
//
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// handler implements the Web Connectivity test helper HTTP API.
|
||||
type handler struct {
|
||||
// MaxAcceptableBody is the MANDATORY maximum acceptable response body.
|
||||
MaxAcceptableBody int64
|
||||
|
||||
// NewClient is the MANDATORY factory to create a new HTTPClient.
|
||||
NewClient func() model.HTTPClient
|
||||
|
||||
// NewDialer is the MANDATORY factory to create a new Dialer.
|
||||
NewDialer func() model.Dialer
|
||||
|
||||
// NewResolver is the MANDATORY factory for creating a new resolver.
|
||||
NewResolver func() model.Resolver
|
||||
}
|
||||
|
||||
var _ http.Handler = &handler{}
|
||||
|
||||
// ServeHTTP implements http.Handler.ServeHTTP.
|
||||
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Add("Server", fmt.Sprintf(
|
||||
"oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version,
|
||||
))
|
||||
if req.Method != "POST" {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
reader := &io.LimitedReader{R: req.Body, N: h.MaxAcceptableBody}
|
||||
data, err := netxlite.ReadAllContext(req.Context(), reader)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
var creq ctrlRequest
|
||||
if err := json.Unmarshal(data, &creq); err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
cresp, err := measure(req.Context(), h, &creq)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
// We assume that the following call cannot fail because it's a
|
||||
// clearly-serializable data structure.
|
||||
data, err = json.Marshal(cresp)
|
||||
runtimex.PanicOnError(err, "json.Marshal failed")
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(data)
|
||||
}
|
||||
Loading…
Reference in a new issue