From 34c894c5269872a81f7f6ee084ef2d1a366cd04c Mon Sep 17 00:00:00 2001 From: Michael Lynch Date: Fri, 12 Aug 2022 21:13:56 -0400 Subject: [PATCH 1/2] Add a plaintext response for guest uploads --- handlers/upload.go | 34 +++++++++++++++++++++++++++++----- handlers/upload_test.go | 1 + 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/handlers/upload.go b/handlers/upload.go index 9f4eb34b..9e66466d 100644 --- a/handlers/upload.go +++ b/handlers/upload.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "net/http" + "os" "time" "github.com/gorilla/mux" @@ -152,11 +153,18 @@ func (s Server) guestEntryPost() http.HandlerFunc { return } - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(EntryPostResponse{ - ID: string(id), - }); err != nil { - panic(err) + if clientAcceptsJson(r) { + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(EntryPostResponse{ + ID: string(id), + }); err != nil { + panic(err) + } + } else { + w.Header().Set("Content-Type", "text/plain") + if _, err := fmt.Fprintf(w, "%s/!%s\r\n", baseURLFromRequest(r), string(id)); err != nil { + log.Fatalf("failed to write HTTP response: %v", err) + } } } } @@ -302,3 +310,19 @@ func parseExpirationFromRequest(r *http.Request) (types.ExpirationTime, error) { func mibToBytes(i int64) int64 { return i << 20 } + +func clientAcceptsJson(r *http.Request) bool { + accepts := r.Header.Get("Accepts") + return accepts == "*/*" || accepts == "application/json" +} + +func baseURLFromRequest(r *http.Request) string { + var scheme string + // If we're running behind a proxy, assume that it's a TLS proxy. + if r.TLS != nil || os.Getenv("PS_BEHIND_PROXY") != "" { + scheme = "https" + } else { + scheme = "http" + } + return fmt.Sprintf("%s://%s", scheme, r.Host) +} diff --git a/handlers/upload_test.go b/handlers/upload_test.go index 1bf67e29..144fe1ef 100644 --- a/handlers/upload_test.go +++ b/handlers/upload_test.go @@ -391,6 +391,7 @@ func TestGuestUpload(t *testing.T) { t.Fatal(err) } req.Header.Add("Content-Type", contentType) + req.Header.Add("Accepts", "application/json") w := httptest.NewRecorder() s.Router().ServeHTTP(w, req) From 9f8c3ddb8db3a19f7282cd82874204cbc5499ac1 Mon Sep 17 00:00:00 2001 From: Michael Lynch Date: Fri, 12 Aug 2022 21:20:21 -0400 Subject: [PATCH 2/2] Fix Accept header name --- handlers/upload.go | 2 +- handlers/upload_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/upload.go b/handlers/upload.go index 9e66466d..2b92fb0a 100644 --- a/handlers/upload.go +++ b/handlers/upload.go @@ -312,7 +312,7 @@ func mibToBytes(i int64) int64 { } func clientAcceptsJson(r *http.Request) bool { - accepts := r.Header.Get("Accepts") + accepts := r.Header.Get("Accept") return accepts == "*/*" || accepts == "application/json" } diff --git a/handlers/upload_test.go b/handlers/upload_test.go index 144fe1ef..2cd871ee 100644 --- a/handlers/upload_test.go +++ b/handlers/upload_test.go @@ -391,7 +391,7 @@ func TestGuestUpload(t *testing.T) { t.Fatal(err) } req.Header.Add("Content-Type", contentType) - req.Header.Add("Accepts", "application/json") + req.Header.Add("Accept", "application/json") w := httptest.NewRecorder() s.Router().ServeHTTP(w, req)