Skip to content

Commit 9231327

Browse files
author
Hexawolf
committed
Implement CORS headers.
In modern world, OPTIONS request is used primarily as a "pre-flight" request for CORS headers. https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it
1 parent 19f124c commit 9231327

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type Config struct {
4747
// Overridden by X-HTTPS-Downstream header.
4848
HTTPSDownstream bool `yaml:"https_downstream"`
4949

50+
// AllowedOrigins specifies Access-Control-Allow-Origin header.
51+
AllowedOrigins string `yaml:"allowed_origins"`
52+
5053
// Internal, used only for testing. Always 60 secs in production.
5154
CleanupIntervalSecs int `yaml:"-"`
5255
}

filedropd/filedropd.example.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ storage_dir: /var/lib/filedrop
3535
# Specifies whether filedrop should return links with https scheme or not.
3636
# Overridden by X-HTTPS-Downstream header.
3737
https_downstream: true
38+
39+
# Specifies Access-Control-Allow-Origin header.
40+
allowed_origins: "*"

server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,17 @@ func (s *Server) serveFile(w http.ResponseWriter, r *http.Request) {
349349
// Note that filedrop code is URL prefix-agnostic, so request URI doesn't
350350
// matters much.
351351
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
352+
w.Header().Set("Access-Control-Allowed-Origin", s.Conf.AllowedOrigins)
352353
if r.Method == http.MethodPost {
353354
s.acceptFile(w, r)
354355
} else if
355356
r.Method == http.MethodGet ||
356-
r.Method == http.MethodHead ||
357-
r.Method == http.MethodOptions {
357+
r.Method == http.MethodHead {
358358

359359
s.serveFile(w, r)
360+
} else if r.Method == http.MethodOptions {
361+
w.Header().Set("Access-Control-Allow-Methods", "HEAD, GET, POST, DELETE")
362+
w.WriteHeader(http.StatusNoContent)
360363
} else {
361364
w.WriteHeader(http.StatusMethodNotAllowed)
362365
w.Write([]byte("405 method not allowed"))

0 commit comments

Comments
 (0)