diff --git a/http/handler.go b/http/handler.go index 3e01f3b..46138b2 100644 --- a/http/handler.go +++ b/http/handler.go @@ -24,7 +24,8 @@ import ( const bearerPrefix = "Bearer " // Handler implements http.Handler and responds to all DI, TO1, and TO2 message -// types. +// types. It is expected that the request will use the POST method and the path +// will be of the form "/fdo/$VER/msg/$MSG". type Handler struct { Tokens protocol.TokenService @@ -38,6 +39,24 @@ type Handler struct { MaxContentLength int64 } +func msgTypeFromPath(w http.ResponseWriter, r *http.Request) (uint8, bool) { + if r.Method != http.MethodPost { + w.WriteHeader(http.StatusMethodNotAllowed) + return 0, false + } + path := strings.TrimPrefix(r.URL.Path, "/fdo/101/msg/") + if strings.Contains(path, "/") { + w.WriteHeader(http.StatusNotFound) + return 0, false + } + typ, err := strconv.ParseUint(path, 10, 8) + if err != nil { + writeErr(w, 0, fmt.Errorf("invalid message type")) + return 0, false + } + return uint8(typ), true +} + func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if h.Tokens == nil { panic("token service not set") diff --git a/http/http_go_test.go b/http/http_go_test.go deleted file mode 100644 index 910a883..0000000 --- a/http/http_go_test.go +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-FileCopyrightText: (C) 2024 Intel Corporation -// SPDX-License-Identifier: Apache 2.0 - -//go:build !tinygo - -package http_test - -import "net/http" - -func setPathValue(req *http.Request, name, value string) { req.SetPathValue(name, value) } diff --git a/http/http_test.go b/http/http_test.go index 915fe29..bb71abd 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -5,7 +5,6 @@ package http_test import ( "net/http" - "path" "testing" "github.com/fido-device-onboard/go-fdo" @@ -51,7 +50,6 @@ type transport struct { // Assume request is well-formed and ignore timeouts, retries, etc. func (tr *transport) RoundTrip(req *http.Request) (*http.Response, error) { - setPathValue(req, "msg", path.Base(req.URL.Path)) rr := new(httputil.ResponseRecorder) tr.Handler.ServeHTTP(rr, req) resp := rr.Result() diff --git a/http/http_tinygo_test.go b/http/http_tinygo_test.go deleted file mode 100644 index 17f6a7f..0000000 --- a/http/http_tinygo_test.go +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-FileCopyrightText: (C) 2024 Intel Corporation -// SPDX-License-Identifier: Apache 2.0 - -//go:build tinygo - -package http_test - -import "net/http" - -func setPathValue(req *http.Request, name, value string) {} diff --git a/http/util.go b/http/util.go index 9165efe..c5ef706 100644 --- a/http/util.go +++ b/http/util.go @@ -7,24 +7,13 @@ package http import ( "bytes" - "fmt" "io" "log/slog" "net/http" "net/http/httptest" "net/http/httputil" - "strconv" ) -func msgTypeFromPath(w http.ResponseWriter, r *http.Request) (uint8, bool) { - typ, err := strconv.ParseUint(r.PathValue("msg"), 10, 8) - if err != nil { - writeErr(w, 0, fmt.Errorf("invalid message type")) - return 0, false - } - return uint8(typ), true -} - func debugRequest(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc) { if !debugEnabled() { handler.ServeHTTP(w, r) diff --git a/http/util_tinygo.go b/http/util_tinygo.go index f124ee7..6f483f9 100644 --- a/http/util_tinygo.go +++ b/http/util_tinygo.go @@ -11,30 +11,11 @@ import ( "io" "log/slog" "net/http" - "strconv" "strings" "github.com/fido-device-onboard/go-fdo/http/internal/httputil" ) -func msgTypeFromPath(w http.ResponseWriter, r *http.Request) (uint8, bool) { - if r.Method != http.MethodPost { - w.WriteHeader(http.StatusMethodNotAllowed) - return 0, false - } - path := strings.TrimPrefix(r.URL.Path, "/fdo/101/msg/") - if strings.Contains(path, "/") { - w.WriteHeader(http.StatusNotFound) - return 0, false - } - typ, err := strconv.ParseUint(path, 10, 8) - if err != nil { - writeErr(w, 0, fmt.Errorf("invalid message type")) - return 0, false - } - return uint8(typ), true -} - func debugRequest(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc) { if !debugEnabled() { handler(w, r)