Skip to content

Commit 19f124c

Browse files
author
Hexawolf
committed
Send headers on OPTIONS request
Before making any requests with JavaScripts in browsers like Google Chrome, a OPTIONS request is being made to fetch CORS settings. Right now, this commit implements a simple patch to avoid 405 METHOD NO ALLOWED response while including all headers, similarily to HEAD. This can be optimized further, if needed/useful.
1 parent 2f80288 commit 19f124c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package filedrop
22

33
import (
4+
"bytes"
45
"database/sql"
56
"fmt"
67
"io"
@@ -337,6 +338,9 @@ func (s *Server) serveFile(w http.ResponseWriter, r *http.Request) {
337338
}
338339
w.Header().Set("ETag", fileUUID)
339340
w.Header().Set("Cache-Control", "public, immutable, max-age=31536000")
341+
if r.Method == http.MethodOptions {
342+
reader = bytes.NewReader([]byte{})
343+
}
340344
http.ServeContent(w, r, fileUUID, time.Time{}, reader)
341345
}
342346

@@ -347,7 +351,11 @@ func (s *Server) serveFile(w http.ResponseWriter, r *http.Request) {
347351
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
348352
if r.Method == http.MethodPost {
349353
s.acceptFile(w, r)
350-
} else if r.Method == http.MethodGet || r.Method == http.MethodHead {
354+
} else if
355+
r.Method == http.MethodGet ||
356+
r.Method == http.MethodHead ||
357+
r.Method == http.MethodOptions {
358+
351359
s.serveFile(w, r)
352360
} else {
353361
w.WriteHeader(http.StatusMethodNotAllowed)

0 commit comments

Comments
 (0)