Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yashbudhia committed Oct 6, 2024
1 parent 9877cbd commit 66bea7e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"fmt"
"log/slog"
"net/http"
"strings"
Expand All @@ -21,6 +21,8 @@ type HTTPServer struct {
DiceClient *db.DiceDB
}

// HandlerMux wraps ServeMux and forces REST paths to lowercase
// and attaches a rate limiter with the handler
type HandlerMux struct {
mux *http.ServeMux
rateLimiter func(http.ResponseWriter, *http.Request, http.Handler)
Expand All @@ -46,8 +48,10 @@ func errorResponse(response string) string {
}

func (cim *HandlerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Convert the path to lowercase before passing to the underlying mux.
middleware.TrailingSlashMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
// Apply rate limiter
cim.rateLimiter(w, r, cim.mux)
})).ServeHTTP(w, r)
}
Expand Down Expand Up @@ -106,10 +110,11 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) {
return
}

if err := util.BlockListedCommand(diceCmd.Cmd); err != nil {
http.Error(w, errorResponse(fmt.Sprintf("ERR unknown command '%s'", diceCmd.Cmd)), http.StatusForbidden)
return
}
// Check if the command is blocklisted
if err := util.BlockListedCommand(diceCmd.Cmd); err != nil {
http.Error(w, errorResponse(fmt.Sprintf("ERR unknown command '%s'", diceCmd.Cmd)), http.StatusForbidden)
return
}

resp, err := s.DiceClient.ExecuteCommand(diceCmd)
if err != nil {
Expand Down

0 comments on commit 66bea7e

Please sign in to comment.