Respond to HEAD requests

Fixes #21
This commit is contained in:
Daan Sprenkels 2019-12-15 11:43:49 +05:30
parent ba08aca622
commit 40a32fa535
3 changed files with 14 additions and 5 deletions

View File

@ -67,6 +67,10 @@ func (rl *rushlink) uploadFileGetHandler(w http.ResponseWriter, r *http.Request)
panic(err) panic(err)
} }
w.Header().Set("Content-Type", fu.ContentType) w.Header().Set("Content-Type", fu.ContentType)
w.WriteHeader(http.StatusOK)
if r.Method == "HEAD" {
return
}
io.Copy(w, file) io.Copy(w, file)
} }

View File

@ -79,14 +79,14 @@ func StartMainServer(addr string, db *db.Database, fs *db.FileStore) {
router := mux.NewRouter() router := mux.NewRouter()
router.Use(recoveryMiddleware) router.Use(recoveryMiddleware)
router.Use(metricsMiddleware) router.Use(metricsMiddleware)
router.HandleFunc("/", rl.indexGetHandler).Methods("GET") router.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/", rl.newPasteHandler).Methods("POST") router.HandleFunc("/", rl.newPasteHandler).Methods("POST")
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.viewPasteHandler).Methods("GET") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.viewPasteHandler).Methods("GET", "HEAD")
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", rl.viewPasteHandlerNoRedirect).Methods("GET") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", rl.viewPasteHandlerMeta).Methods("GET") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.deletePasteHandler).Methods("DELETE") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}", rl.deletePasteHandler).Methods("DELETE")
router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/delete", rl.deletePasteHandler).Methods("POST") router.HandleFunc("/{key:[A-Za-z0-9-_]{4,}}/delete", rl.deletePasteHandler).Methods("POST")
router.HandleFunc("/uploads/{id:[A-Za-z0-9-_]+}/{filename:.+}", rl.uploadFileGetHandler).Methods("GET") router.HandleFunc("/uploads/{id:[A-Za-z0-9-_]+}/{filename:.+}", rl.uploadFileGetHandler).Methods("GET", "HEAD")
srv := &http.Server{ srv := &http.Server{
Handler: router, Handler: router,

View File

@ -115,6 +115,11 @@ func render(w http.ResponseWriter, r *http.Request, tmplName string, data map[st
} }
return buf.String() return buf.String()
} }
w.WriteHeader(http.StatusOK)
if r.Method == "HEAD" {
return
}
err = tmpl.Execute(w, data) err = tmpl.Execute(w, data)
default: default:
// Fall back to plain text without template // Fall back to plain text without template