Skip to content

Commit

Permalink
Allows fetching custom non-UUID keys when support is enabled (prebid#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhjort authored Jan 31, 2019
1 parent de1b0d3 commit 65fc729
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ type RequestLimits struct {
}

func (cfg *RequestLimits) validateAndLog() {
log.Infof("config.request_limits.allow_setting_keys: %v", cfg.AllowSettingKeys)
log.Infof("config.request_limits.max_ttl_seconds: %d", cfg.MaxTTLSeconds)
log.Infof("config.request_limits.max_size_bytes: %d", cfg.MaxSize)
log.Infof("config.request_limits.max_num_values: %d", cfg.MaxNumValues)
}
Expand Down
21 changes: 11 additions & 10 deletions endpoints/get.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package endpoints

import (
"context"
"errors"
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
"time"
"strings"
"fmt"
"context"
"time"

"github.com/julienschmidt/httprouter"
"github.com/prebid/prebid-cache/backends"
"errors"
)

func NewGetHandler(backend backends.Backend) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func NewGetHandler(backend backends.Backend, allowKeys bool) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id, err := parseUUID(r)
id, err := parseUUID(r, allowKeys)
if err != nil {
if id == "" {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -47,15 +48,15 @@ type GetResponse struct {
Value interface{} `json:"value"`
}

func parseUUID(r *http.Request) (string, error) {
func parseUUID(r *http.Request, allowKeys bool) (string, error) {
id := r.URL.Query().Get("uuid")
var err error = nil
if id == "" {
err = errors.New("Missing required parameter uuid")
} else if len(id) != 36 {
} else if len(id) != 36 && (!allowKeys) {
// UUIDs are 36 characters long... so this quick check lets us filter out most invalid
// ones before even checking the backend.
err = fmt.Errorf("No content stored for uuid=%s", id)
}
return id, err
}
}
4 changes: 2 additions & 2 deletions endpoints/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func expectStored(t *testing.T, putBody string, expectedGet string, expectedMime
backend := backends.NewMemoryBackend()

router.POST("/cache", NewPutHandler(backend, 10, true))
router.GET("/cache", NewGetHandler(backend))
router.GET("/cache", NewGetHandler(backend, true))

uuid, putTrace := doMockPut(t, router, putBody)
if putTrace.Code != http.StatusOK {
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestXMLOther(t *testing.T) {
func TestGetInvalidUUIDs(t *testing.T) {
backend := backends.NewMemoryBackend()
router := httprouter.New()
router.GET("/cache", NewGetHandler(backend))
router.GET("/cache", NewGetHandler(backend, false))

getResults := doMockGet(t, router, "fdd9405b-ef2b-46da-a55a-2f526d338e16")
if getResults.Code != http.StatusNotFound {
Expand Down
2 changes: 1 addition & 1 deletion endpoints/routing/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewHandler(cfg config.Configuration, dataStore backends.Backend, appMetrics
router.GET("/", endpoints.Index) //Default route handler
router.GET("/status", endpoints.Status) // Determines whether the server is ready for more traffic.
router.POST("/cache", decorators.MonitorHttp(endpoints.NewPutHandler(dataStore, cfg.RequestLimits.MaxNumValues, cfg.RequestLimits.AllowSettingKeys), appMetrics.Puts))
router.GET("/cache", decorators.MonitorHttp(endpoints.NewGetHandler(dataStore), appMetrics.Gets))
router.GET("/cache", decorators.MonitorHttp(endpoints.NewGetHandler(dataStore, cfg.RequestLimits.AllowSettingKeys), appMetrics.Gets))

handler := handleCors(router)
handler = handleRateLimiting(handler, cfg.RateLimiting)
Expand Down

0 comments on commit 65fc729

Please sign in to comment.