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