Fix redirection on success when sending webmention

kcoram/uplift
Jan-Lukas Else 2019-12-12 16:52:29 +01:00
parent 88c17bacb8
commit 988ff42365
1 changed files with 19 additions and 10 deletions

View File

@ -101,9 +101,8 @@ func HandleWebmention(w http.ResponseWriter, r *http.Request) {
if respCode < 200 || respCode >= 300 {
if respCode == 410 {
// Delete mention, because source is gone
// TODO: Implement deleteion
w.Header().Add("Location", targetUrl.String())
w.WriteHeader(http.StatusOK)
// TODO: Implement deletion
returnSuccess(targetUrl.String(), w, r)
return
} else {
err = errors.New("source returned error")
@ -130,13 +129,7 @@ func HandleWebmention(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(err.Error()))
return
}
w.Header().Add("Location", targetUrl.String())
w.WriteHeader(http.StatusOK)
// Purge BunnyCDN in 10 seconds
go func() {
time.Sleep(10 * time.Second)
Purge(targetUrl.String())
}()
returnSuccess(targetUrl.String(), w, r)
return
}
@ -172,3 +165,19 @@ func saveWebmention(mention *Mention) (err error) {
err = UpdateFile(filePath, string(bytesRepresentation), "New webmention from "+mention.Source)
return
}
func returnSuccess(target string, w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Accept"), "text/html") {
// Redirect browser
w.Header().Add("Location", target)
w.WriteHeader(http.StatusSeeOther)
} else {
w.WriteHeader(http.StatusOK)
}
// Purge BunnyCDN in 10 seconds
go func() {
time.Sleep(10 * time.Second)
Purge(target)
}()
return
}