-
Notifications
You must be signed in to change notification settings - Fork 113
/
cors.go
130 lines (116 loc) · 3.34 KB
/
cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package cors
import (
"net/http"
"github.com/cs3org/reva/pkg/rhttp/global"
"github.com/mitchellh/mapstructure"
"github.com/rs/cors"
)
const (
defaultPriority = 200
)
func init() {
global.RegisterMiddleware("cors", New)
}
type config struct {
AllowCredentials bool `mapstructure:"allow_credentials"`
OptionsPassthrough bool `mapstructure:"options_passthrough"`
Debug bool `mapstructure:"debug"`
MaxAge int `mapstructure:"max_age"`
Priority int `mapstructure:"priority"`
AllowedMethods []string `mapstructure:"allowed_methods"`
AllowedHeaders []string `mapstructure:"allowed_headers"`
ExposedHeaders []string `mapstructure:"exposed_headers"`
AllowedOrigins []string `mapstructure:"allowed_origins"`
}
// New creates a new CORS middleware.
func New(m map[string]interface{}) (global.Middleware, int, error) {
conf := &config{}
if err := mapstructure.Decode(m, conf); err != nil {
return nil, 0, err
}
if conf.Priority == 0 {
conf.Priority = defaultPriority
}
// apply some defaults to reduce configuration boilerplate
if len(conf.AllowedOrigins) == 0 {
conf.AllowedOrigins = []string{"*"}
}
if len(conf.AllowedMethods) == 0 {
conf.AllowedMethods = []string{
http.MethodOptions,
http.MethodHead,
http.MethodGet,
http.MethodPut,
http.MethodPost,
"DELETE",
"MKCOL",
"PROPFIND",
"PROPPATCH",
"MOVE",
"COPY",
"REPORT",
"SEARCH",
}
}
if len(conf.AllowedHeaders) == 0 {
conf.AllowedHeaders = []string{
"Origin",
"Accept",
"Content-Type",
"Depth",
"Authorization",
"Ocs-Apirequest",
"If-None-Match",
"If-Match",
"Destination",
"Overwrite",
"X-Request-Id",
"X-Requested-With",
"Tus-Resumable",
"Tus-Checksum-Algorithm",
"Upload-Concat",
"Upload-Length",
"Upload-Metadata",
"Upload-Defer-Length",
"Upload-Expires",
"Upload-Checksum",
"Upload-Offset",
"X-HTTP-Method-Override",
}
}
if len(conf.ExposedHeaders) == 0 {
conf.ExposedHeaders = []string{
"Location",
}
}
// TODO(jfd): use log from request context, otherwise fmt will be used to log,
// preventing us from pinging the log to eg jq
c := cors.New(cors.Options{
AllowCredentials: conf.AllowCredentials,
AllowedHeaders: conf.AllowedHeaders,
AllowedMethods: conf.AllowedMethods,
AllowedOrigins: conf.AllowedOrigins,
ExposedHeaders: conf.ExposedHeaders,
MaxAge: conf.MaxAge,
OptionsPassthrough: conf.OptionsPassthrough,
Debug: conf.Debug,
})
return c.Handler, conf.Priority, nil
}