Skip to content

Commit

Permalink
Remove HTTP server usage of (*http.Request).PathValue and clarify req…
Browse files Browse the repository at this point in the history
…uirements in godoc

Signed-off-by: Ben Krieger <[email protected]>
  • Loading branch information
ben-krieger committed Dec 24, 2024
1 parent 17a03bf commit 8199547
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 53 deletions.
21 changes: 20 additions & 1 deletion http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down
10 changes: 0 additions & 10 deletions http/http_go_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package http_test

import (
"net/http"
"path"
"testing"

"github.com/fido-device-onboard/go-fdo"
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 0 additions & 10 deletions http/http_tinygo_test.go

This file was deleted.

11 changes: 0 additions & 11 deletions http/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions http/util_tinygo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8199547

Please sign in to comment.