Skip to content

Commit

Permalink
Accept extra parameters at the HTTP handlers
Browse files Browse the repository at this point in the history
The HTTP handlers now accept multiple parameters without invalidating
the signature token.

Issue: CW-1315
  • Loading branch information
diegobernardes committed Feb 5, 2025
1 parent f14e5ff commit 8a86d6f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 6 additions & 1 deletion internal/service/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ func (w *Worker) Process(
return fmt.Errorf("fail to fetch the file: %w", err)
}

if len(payload) == 0 {
return fmt.Errorf("empty payload")
}

storage := bytes.NewBuffer([]byte{})
err = lazypdf.SaveToPNG(ctx, uint16(page), uint16(width), scale, bytes.NewBuffer(payload), storage)
dpi := 96
err = lazypdf.SaveToPNG(ctx, uint16(page), uint16(width), scale, dpi, bytes.NewBuffer(payload), storage)
if err != nil {
return fmt.Errorf("fail to extract the PNG from the PDF: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestWorkerProcess(t *testing.T) {
client.On("GetObjectWithContext", mock.Anything, &input).Return(&output, nil)
return &client
},
expectedError: "fail to extract the PNG from the PDF: failure at the C/MuPDF layer: cannot tell in file",
expectedError: "empty payload",
},
{
message: "process and return a page",
Expand Down
17 changes: 15 additions & 2 deletions internal/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (h handler) document(w http.ResponseWriter, r *http.Request) {

path := strings.TrimPrefix(r.URL.Path, "/documents/")
buf := bytes.NewBuffer([]byte{})
err = h.documentService.Process(r.Context(), r.URL.String(), path, page, width, float32(scale), buf)
err = h.documentService.Process(r.Context(), h.urlToVerify(r), path, page, width, float32(scale), buf)
if ctxErr := r.Context().Err(); ctxErr != nil {
logger.Err(ctxErr).Str("requestID", reqID).Msg("Context error")
if ctxErr == context.Canceled {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (h handler) metadata(w http.ResponseWriter, r *http.Request) {
}

path := strings.TrimPrefix(r.URL.Path, "/documents/")
fileName, pageCount, err := h.documentService.Metadata(r.Context(), r.URL.String(), path)
fileName, pageCount, err := h.documentService.Metadata(r.Context(), h.urlToVerify(r), path)
if ctxErr := r.Context().Err(); ctxErr != nil {
logger.Err(ctxErr).Str("requestID", reqID).Msg("Context error")
if ctxErr == context.Canceled {
Expand All @@ -150,3 +150,16 @@ func (h handler) metadata(w http.ResponseWriter, r *http.Request) {
}
h.writer.response(r.Context(), w, result, http.StatusOK)
}

// Remove all the parameters, but the token and page, from the path. Other parameters can then be passed to the service
// without making the url signature invalid.
func (h handler) urlToVerify(r *http.Request) string {
q := r.URL.Query()
for key := range q {
if key == "page" || key == "token" {
continue
}
q.Del(key)
}
return r.URL.String()
}

0 comments on commit 8a86d6f

Please sign in to comment.