Skip to content

Commit

Permalink
Merge pull request #1 from ccamel/fix-uri-encoding
Browse files Browse the repository at this point in the history
Fix possible URI encoding issues on Location
  • Loading branch information
rchakode authored Jun 2, 2020
2 parents cdd3fc5 + f3a62cd commit 162ee58
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions sendmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net"
"net/http"
"net/smtp"
"net/url"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -258,14 +259,20 @@ func SendMail(httpResp http.ResponseWriter, httpReq *http.Request) {
contactResponse.Message = "Invalid request, please review your input and try again."
}

refererURL := strings.Split(httpReq.Header["Referer"][0], "?")[0]
refererURL, err := url.Parse(httpReq.Header.Get("Referer"))
if err != nil {
log.Infof("error: %s", err.Error())
refererURL = &url.URL{} // continue with default (empty) url
}

q := refererURL.Query()
q.Set("status", contactResponse.Status)
q.Set("message", contactResponse.Message)
refererURL.RawQuery = q.Encode()

respRawData, _ := json.Marshal(contactResponse)

httpResp.Header().Set("Location",
fmt.Sprintf("%s?status=%s&message=%s",
refererURL,
contactResponse.Status,
contactResponse.Message))
httpResp.Header().Set("Location", refererURL.String())
httpResp.WriteHeader(http.StatusSeeOther)
httpResp.Header().Set("Content-Type", "application/json; charset=UTF-8")
httpResp.Write(respRawData)
Expand Down

0 comments on commit 162ee58

Please sign in to comment.