forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
88 lines (76 loc) · 2.64 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
//
// HTTP handler
//
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"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 {
// BaseLogger is the MANDATORY logger to use.
BaseLogger model.Logger
// Indexer is the MANDATORY atomic integer used to assign an index to requests.
Indexer *atomicx.Int64
// MaxAcceptableBody is the MANDATORY maximum acceptable response body.
MaxAcceptableBody int64
// NewClient is the MANDATORY factory to create a new HTTPClient.
NewClient func(model.Logger) model.HTTPClient
// NewDialer is the MANDATORY factory to create a new Dialer.
NewDialer func(model.Logger) model.Dialer
// NewResolver is the MANDATORY factory for creating a new resolver.
NewResolver func(model.Logger) model.Resolver
// NewTLSHandshaker is the MANDATORY factory for creating a new TLS handshaker.
NewTLSHandshaker func(model.Logger) model.TLSHandshaker
}
var _ http.Handler = &handler{}
// ServeHTTP implements http.Handler.ServeHTTP.
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
metricRequestsInflight.Inc()
defer metricRequestsInflight.Dec()
w.Header().Add("Server", fmt.Sprintf(
"oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version,
))
if req.Method != "POST" {
metricRequestsCount.WithLabelValues("400", "bad_request_method").Inc()
w.WriteHeader(400)
return
}
reader := &io.LimitedReader{R: req.Body, N: h.MaxAcceptableBody}
data, err := netxlite.ReadAllContext(req.Context(), reader)
if err != nil {
metricRequestsCount.WithLabelValues("400", "request_body_too_large").Inc()
w.WriteHeader(400)
return
}
var creq ctrlRequest
if err := json.Unmarshal(data, &creq); err != nil {
metricRequestsCount.WithLabelValues("400", "cannot_unmarshal_request_body").Inc()
w.WriteHeader(400)
return
}
started := time.Now()
cresp, err := measure(req.Context(), h, &creq)
elapsed := time.Since(started)
metricWCTaskDurationSeconds.Observe(float64(elapsed.Seconds()))
if err != nil {
metricRequestsCount.WithLabelValues("400", "wctask_failed").Inc()
w.WriteHeader(400)
return
}
metricRequestsCount.WithLabelValues("200", "ok").Inc()
// 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)
}