Skip to content

Commit

Permalink
minors
Browse files Browse the repository at this point in the history
  • Loading branch information
yashbudhia committed Oct 6, 2024
1 parent 97a84d9 commit 76464b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
20 changes: 8 additions & 12 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"fmt"
"log/slog"
"net/http"
"strings"
"sync"
"time"

"server/internal/middleware"
"server/internal/db"
"server/internal/middleware"
util "server/util"
)

Expand All @@ -21,8 +21,6 @@ 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 @@ -48,10 +46,8 @@ 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 @@ -110,11 +106,11 @@ func (s *HTTPServer) CliHandler(w http.ResponseWriter, r *http.Request) {
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
}
// 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 All @@ -126,7 +122,7 @@ if err := util.BlockListedCommand(diceCmd.Cmd); err != nil {
respStr, ok := resp.(string)
if !ok {
slog.Error("error: response is not a string", "error", slog.Any("err", err))
http.Error(w, errorResponse("internal Server Error"), http.StatusInternalServerError)
http.Error(w, errorResponse("internal server error"), http.StatusInternalServerError)
return
}

Expand Down
30 changes: 15 additions & 15 deletions util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import (

// Map of blocklisted commands
var blocklistedCommands = map[string]bool{
"FLUSHALL": true,
"FLUSHDB": true,
"DUMP": true,
"ABORT": true,
"FLUSHALL": true,
"FLUSHDB": true,
"DUMP": true,
"ABORT": true,
"AUTH": true,
"CONFIG": true,
"SAVE": true,
"BGSAVE": true,
"BGREWRITEAOF": true,
"CONFIG": true,
"SAVE": true,
"BGSAVE": true,
"BGREWRITEAOF": true,
"RESTORE": true,
"MULTI": true,
"EXEC": true,
"DISCARD": true,
"QWATCH": true,
"MULTI": true,
"EXEC": true,
"DISCARD": true,
"QWATCH": true,
"QUNWATCH": true,
"LATENCY": true,
"CLIENT": true,
"SLEEP": true,
"LATENCY": true,
"CLIENT": true,
"SLEEP": true,
"PERSIST": true,
}

Expand Down

0 comments on commit 76464b3

Please sign in to comment.