aboutsummaryrefslogtreecommitdiff
path: root/internal/node/handler
diff options
context:
space:
mode:
Diffstat (limited to 'internal/node/handler')
-rw-r--r--internal/node/handler/handler.go91
-rw-r--r--internal/node/handler/handler_test.go113
-rw-r--r--internal/node/handler/metric.go19
3 files changed, 223 insertions, 0 deletions
diff --git a/internal/node/handler/handler.go b/internal/node/handler/handler.go
new file mode 100644
index 0000000..2871c5d
--- /dev/null
+++ b/internal/node/handler/handler.go
@@ -0,0 +1,91 @@
+package handler
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "time"
+
+ "git.sigsum.org/sigsum-go/pkg/log"
+ "git.sigsum.org/sigsum-go/pkg/types"
+)
+
+type Config interface {
+ Prefix() string
+ LogID() string
+ Deadline() time.Duration
+}
+
+// Handler implements the http.Handler interface
+type Handler struct {
+ Config
+ Fun func(context.Context, Config, http.ResponseWriter, *http.Request) (int, error)
+ Endpoint types.Endpoint
+ Method string
+}
+
+// Path returns a path that should be configured for this handler
+func (h Handler) Path() string {
+ if len(h.Prefix()) == 0 {
+ return h.Endpoint.Path("", "sigsum", "v0")
+ }
+ return h.Endpoint.Path("", h.Prefix(), "sigsum", "v0")
+}
+
+// ServeHTTP is part of the http.Handler interface
+func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ start := time.Now()
+ code := 0
+ defer func() {
+ end := time.Now().Sub(start).Seconds()
+ sc := fmt.Sprintf("%d", code)
+
+ rspcnt.Inc(h.LogID(), string(h.Endpoint), sc)
+ latency.Observe(end, h.LogID(), string(h.Endpoint), sc)
+ }()
+ reqcnt.Inc(h.LogID(), string(h.Endpoint))
+
+ code = h.verifyMethod(w, r)
+ if code != 0 {
+ return
+ }
+ h.handle(w, r)
+}
+
+// verifyMethod checks that an appropriate HTTP method is used and
+// returns 0 if so, or an HTTP status code if not. Error handling is
+// based on RFC 7231, see Sections 6.5.5 (Status 405) and 6.5.1
+// (Status 400).
+func (h Handler) verifyMethod(w http.ResponseWriter, r *http.Request) int {
+ checkHTTPMethod := func(m string) bool {
+ return m == http.MethodGet || m == http.MethodPost
+ }
+
+ if h.Method == r.Method {
+ return 0
+ }
+
+ code := http.StatusBadRequest
+ if ok := checkHTTPMethod(r.Method); ok {
+ w.Header().Set("Allow", h.Method)
+ code = http.StatusMethodNotAllowed
+ }
+
+ http.Error(w, fmt.Sprintf("error=%s", http.StatusText(code)), code)
+ return code
+}
+
+// handle handles an HTTP request for which the HTTP method is already verified
+func (h Handler) handle(w http.ResponseWriter, r *http.Request) {
+ deadline := time.Now().Add(h.Deadline())
+ ctx, cancel := context.WithDeadline(r.Context(), deadline)
+ defer cancel()
+
+ code, err := h.Fun(ctx, h.Config, w, r)
+ if err != nil {
+ log.Debug("%s/%s: %v", h.Prefix(), h.Endpoint, err)
+ http.Error(w, fmt.Sprintf("error=%s", err.Error()), code)
+ } else if code != 200 {
+ w.WriteHeader(code)
+ }
+}
diff --git a/internal/node/handler/handler_test.go b/internal/node/handler/handler_test.go
new file mode 100644
index 0000000..dfd27bd
--- /dev/null
+++ b/internal/node/handler/handler_test.go
@@ -0,0 +1,113 @@
+package handler
+
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "git.sigsum.org/sigsum-go/pkg/types"
+)
+
+type dummyConfig struct {
+ prefix string
+}
+
+func (c dummyConfig) Prefix() string { return c.prefix }
+func (c dummyConfig) LogID() string { return "dummyLogID" }
+func (c dummyConfig) Deadline() time.Duration { return time.Nanosecond }
+
+// TestPath checks that Path works for an endpoint (add-leaf)
+func TestPath(t *testing.T) {
+ testFun := func(_ context.Context, _ Config, _ http.ResponseWriter, _ *http.Request) (int, error) {
+ return 0, nil
+ }
+ for _, table := range []struct {
+ description string
+ prefix string
+ want string
+ }{
+ {
+ description: "no prefix",
+ want: "/sigsum/v0/add-leaf",
+ },
+ {
+ description: "a prefix",
+ prefix: "test-prefix",
+ want: "/test-prefix/sigsum/v0/add-leaf",
+ },
+ } {
+ testConfig := dummyConfig{
+ prefix: table.prefix,
+ }
+ h := Handler{testConfig, testFun, types.EndpointAddLeaf, http.MethodPost}
+ if got, want := h.Path(), table.want; got != want {
+ t.Errorf("got path %v but wanted %v", got, want)
+ }
+ }
+}
+
+// func TestServeHTTP(t *testing.T) {
+// h.ServeHTTP(w http.ResponseWriter, r *http.Request)
+// }
+
+func TestVerifyMethod(t *testing.T) {
+ badMethod := http.MethodHead
+ for _, h := range []Handler{
+ {
+ Endpoint: types.EndpointAddLeaf,
+ Method: http.MethodPost,
+ },
+ {
+ Endpoint: types.EndpointGetTreeHeadToCosign,
+ Method: http.MethodGet,
+ },
+ } {
+ for _, method := range []string{
+ http.MethodGet,
+ http.MethodPost,
+ badMethod,
+ } {
+ url := h.Endpoint.Path("http://log.example.com", "fixme")
+ req, err := http.NewRequest(method, url, nil)
+ if err != nil {
+ t.Fatalf("must create HTTP request: %v", err)
+ }
+
+ w := httptest.NewRecorder()
+ code := h.verifyMethod(w, req)
+ if got, want := code == 0, h.Method == method; got != want {
+ t.Errorf("%s %s: got %v but wanted %v: %v", method, url, got, want, err)
+ continue
+ }
+ if code == 0 {
+ continue
+ }
+
+ if method == badMethod {
+ if got, want := code, http.StatusBadRequest; got != want {
+ t.Errorf("%s %s: got status %d, wanted %d", method, url, got, want)
+ }
+ if _, ok := w.Header()["Allow"]; ok {
+ t.Errorf("%s %s: got Allow header, wanted none", method, url)
+ }
+ continue
+ }
+
+ if got, want := code, http.StatusMethodNotAllowed; got != want {
+ t.Errorf("%s %s: got status %d, wanted %d", method, url, got, want)
+ } else if methods, ok := w.Header()["Allow"]; !ok {
+ t.Errorf("%s %s: got no allow header, expected one", method, url)
+ } else if got, want := len(methods), 1; got != want {
+ t.Errorf("%s %s: got %d allowed method(s), wanted %d", method, url, got, want)
+ } else if got, want := methods[0], h.Method; got != want {
+ t.Errorf("%s %s: got allowed method %s, wanted %s", method, url, got, want)
+ }
+ }
+ }
+}
+
+// func TestHandle(t *testing.T) {
+// h.handle(w http.ResponseWriter, r *http.Request)
+// }
diff --git a/internal/node/handler/metric.go b/internal/node/handler/metric.go
new file mode 100644
index 0000000..ced0096
--- /dev/null
+++ b/internal/node/handler/metric.go
@@ -0,0 +1,19 @@
+package handler
+
+import (
+ "github.com/google/trillian/monitoring"
+ "github.com/google/trillian/monitoring/prometheus"
+)
+
+var (
+ reqcnt monitoring.Counter // number of incoming http requests
+ rspcnt monitoring.Counter // number of valid http responses
+ latency monitoring.Histogram // request-response latency
+)
+
+func init() {
+ mf := prometheus.MetricFactory{}
+ reqcnt = mf.NewCounter("http_req", "number of http requests", "logid", "endpoint")
+ rspcnt = mf.NewCounter("http_rsp", "number of http requests", "logid", "endpoint", "status")
+ latency = mf.NewHistogram("http_latency", "http request-response latency", "logid", "endpoint", "status")
+}