From 66d5aae4988322e2715e8fd560088b78e36b683a Mon Sep 17 00:00:00 2001 From: Dominik George Date: Thu, 28 Apr 2022 23:30:56 +0200 Subject: [PATCH] Do not redirect if no originURI is set --- sendmail.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/sendmail.go b/sendmail.go index f66401f..2e9f0b4 100644 --- a/sendmail.go +++ b/sendmail.go @@ -297,21 +297,26 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) { contactResponse.Message = "Invalid request, please review your input and try again." } - originURL, err := url.Parse(contactRequest.OriginURI) - if err != nil { - log.Printf("error parsing the origin URL %s (%s)", originURL, err.Error()) - originURL = &url.URL{} // continue with default (empty) url - } + if contactRequest.OriginURI != "" { + originURL, err := url.Parse(contactRequest.OriginURI) + if err != nil { + log.Printf("error parsing the origin URL %s (%s)", originURL, err.Error()) + originURL = &url.URL{} // continue with default (empty) url + } - q := originURL.Query() - q.Set("status", contactResponse.Status) - q.Set("message", contactResponse.Message) - originURL.RawQuery = q.Encode() + q := originURL.Query() + q.Set("status", contactResponse.Status) + q.Set("message", contactResponse.Message) + originURL.RawQuery = q.Encode() + + httpResp.Header().Set("Location", originURL.String()) + httpResp.WriteHeader(http.StatusSeeOther) + } else { + httpResp.WriteHeader(http.StatusOK) + } respRawData, _ := json.Marshal(contactResponse) - httpResp.Header().Set("Location", originURL.String()) - httpResp.WriteHeader(http.StatusSeeOther) httpResp.Header().Set("Content-Type", "application/json; charset=UTF-8") httpResp.Write(respRawData) }