|
| 1 | +package cors |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// Option configures the CORS middleware. |
| 10 | +type Option func(*config) |
| 11 | + |
| 12 | +type config struct { |
| 13 | + allowedOrigins []string |
| 14 | + allowedMethods []string |
| 15 | + allowedHeaders []string |
| 16 | + maxAge int |
| 17 | +} |
| 18 | + |
| 19 | +// WithAllowedOrigins sets the allowed origins. Use "*" to allow all. |
| 20 | +func WithAllowedOrigins(origins ...string) Option { |
| 21 | + return func(c *config) { c.allowedOrigins = origins } |
| 22 | +} |
| 23 | + |
| 24 | +// WithAllowedMethods sets the allowed HTTP methods. |
| 25 | +func WithAllowedMethods(methods ...string) Option { |
| 26 | + return func(c *config) { c.allowedMethods = methods } |
| 27 | +} |
| 28 | + |
| 29 | +// WithAllowedHeaders sets the allowed request headers. |
| 30 | +func WithAllowedHeaders(headers ...string) Option { |
| 31 | + return func(c *config) { c.allowedHeaders = headers } |
| 32 | +} |
| 33 | + |
| 34 | +// WithMaxAge sets the max age (in seconds) for preflight cache. |
| 35 | +func WithMaxAge(seconds int) Option { |
| 36 | + return func(c *config) { c.maxAge = seconds } |
| 37 | +} |
| 38 | + |
| 39 | +// Defaults returns sensible CORS defaults for ConnectRPC services. |
| 40 | +// Includes Connect-specific headers. |
| 41 | +func Defaults() []Option { |
| 42 | + return []Option{ |
| 43 | + WithAllowedOrigins("*"), |
| 44 | + WithAllowedMethods("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"), |
| 45 | + WithAllowedHeaders( |
| 46 | + "Content-Type", |
| 47 | + "Connect-Protocol-Version", |
| 48 | + "Connect-Timeout-Ms", |
| 49 | + "Grpc-Timeout", |
| 50 | + "X-Grpc-Web", |
| 51 | + "X-User-Agent", |
| 52 | + "X-Request-ID", |
| 53 | + "Authorization", |
| 54 | + ), |
| 55 | + WithMaxAge(7200), |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func newConfig(opts []Option) *config { |
| 60 | + c := &config{} |
| 61 | + // Apply defaults first, then user overrides. |
| 62 | + for _, opt := range Defaults() { |
| 63 | + opt(c) |
| 64 | + } |
| 65 | + for _, opt := range opts { |
| 66 | + opt(c) |
| 67 | + } |
| 68 | + return c |
| 69 | +} |
| 70 | + |
| 71 | +// Middleware returns net/http CORS middleware. |
| 72 | +func Middleware(opts ...Option) func(http.Handler) http.Handler { |
| 73 | + cfg := newConfig(opts) |
| 74 | + return func(next http.Handler) http.Handler { |
| 75 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | + origin := r.Header.Get("Origin") |
| 77 | + if origin == "" { |
| 78 | + next.ServeHTTP(w, r) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + if isOriginAllowed(cfg.allowedOrigins, origin) { |
| 83 | + w.Header().Set("Access-Control-Allow-Origin", origin) |
| 84 | + } |
| 85 | + |
| 86 | + w.Header().Set("Access-Control-Allow-Methods", strings.Join(cfg.allowedMethods, ", ")) |
| 87 | + w.Header().Set("Access-Control-Allow-Headers", strings.Join(cfg.allowedHeaders, ", ")) |
| 88 | + |
| 89 | + if cfg.maxAge > 0 { |
| 90 | + w.Header().Set("Access-Control-Max-Age", strconv.Itoa(cfg.maxAge)) |
| 91 | + } |
| 92 | + |
| 93 | + w.Header().Set("Vary", "Origin") |
| 94 | + |
| 95 | + if r.Method == http.MethodOptions { |
| 96 | + w.WriteHeader(http.StatusNoContent) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + next.ServeHTTP(w, r) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func isOriginAllowed(allowed []string, origin string) bool { |
| 106 | + for _, a := range allowed { |
| 107 | + if a == "*" || a == origin { |
| 108 | + return true |
| 109 | + } |
| 110 | + } |
| 111 | + return false |
| 112 | +} |
0 commit comments