From 456dc6856de91cebaf6b5036d6007e632a2f88c8 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Sun, 9 Feb 2025 20:15:32 -0300 Subject: [PATCH] feat: https url return --- handlers.go | 4 ++-- helpers.go | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/handlers.go b/handlers.go index 2afeb89..9aa7358 100644 --- a/handlers.go +++ b/handlers.go @@ -180,7 +180,7 @@ func (app *Application) formHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - ResponseURLHandler(w, app.url, filename) + ResponseURLHandler(r, w, app.url, filename) } func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { @@ -220,7 +220,7 @@ func (app *Application) curlHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error parsing file: %s", err.Error()) } - ResponseURLHandler(w, app.url, filename) + ResponseURLHandler(r, w, app.url, filename) } func (app *Application) publicURL(file io.Reader, extension string, full bool) string { diff --git a/helpers.go b/helpers.go index 7a150a0..84e1a5a 100644 --- a/helpers.go +++ b/helpers.go @@ -123,12 +123,14 @@ func BasicAuth(next http.HandlerFunc, app *Application) http.HandlerFunc { }) } -func ResponseURLHandler(w http.ResponseWriter, url, filename string) { - pasteURL := fmt.Sprintf("http://%s/%s\n", url, filename) +func ResponseURLHandler(r *http.Request, w http.ResponseWriter, url, filename string) { + protocol := "http" + if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" { + protocol = "https" + } + pasteURL := fmt.Sprintf("%s://%s/%s\n", protocol, url, filename) w.Header().Set("Location", pasteURL) - w.WriteHeader(http.StatusCreated) - fmt.Fprintf(w, "%s", pasteURL) }