Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Options struct {
MaxFileSize int
HTTP1Only bool
MaxDumpBodySize int
CORS bool
HTTPHeaders HTTPHeaders
}

Expand Down Expand Up @@ -64,6 +65,7 @@ func ParseOptions() *Options {
flag.BoolVar(&options.HTTP1Only, "http1", false, "Enable only HTTP1")
flag.IntVar(&options.MaxFileSize, "max-file-size", 50, "Max Upload File Size")
flag.IntVar(&options.MaxDumpBodySize, "max-dump-body-size", -1, "Max Dump Body Size")
flag.BoolVar(&options.CORS, "cors", false, "Enable Cross-Origin Resource Sharing (CORS)")
flag.Var(&options.HTTPHeaders, "header", "Add HTTP Response Header (name: value), can be used multiple times")
flag.Parse()

Expand Down
1 change: 1 addition & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func New(options *Options) (*Runner, error) {
MaxFileSize: r.options.MaxFileSize,
HTTP1Only: r.options.HTTP1Only,
MaxDumpBodySize: unit.ToMb(r.options.MaxDumpBodySize),
CORS: r.options.CORS,
HTTPHeaders: r.options.HTTPHeaders,
})
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions pkg/httpserver/corslayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package httpserver

import (
"net/http"
"strings"
)

func (t *HTTPServer) corslayer(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers := w.Header()
headers.Set("Access-Control-Allow-Origin", "*")
if r.Method != http.MethodOptions {
handler.ServeHTTP(w, r)
return
}

headers.Add("Vary", "Origin")
headers.Add("Vary", "Access-Control-Request-Method")
headers.Add("Vary", "Access-Control-Request-Headers")

reqMethod := r.Header.Get("Access-Control-Request-Method")
if reqMethod != "" {
headers.Set("Access-Control-Allow-Methods", strings.ToUpper(reqMethod))
}

w.WriteHeader(http.StatusOK)
})
}
5 changes: 5 additions & 0 deletions pkg/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options struct {
HTTP1Only bool
MaxFileSize int // 50Mb
MaxDumpBodySize int64
CORS bool
HTTPHeaders []HTTPHeader
}

Expand Down Expand Up @@ -72,6 +73,10 @@ func New(options *Options) (*HTTPServer, error) {
addHandler(h.basicauthlayer)
}

if options.CORS {
addHandler(h.corslayer)
}

httpHandler = h.loglayer(httpHandler)
httpHandler = h.headerlayer(httpHandler, options.HTTPHeaders)

Expand Down