CORSMiddleware returns a middleware handler to support CORS request. This handler will write following header into response: Access-Control-Allow-Origin [*] Access-Control-Allow-Headers [*] Access-Control-Allow-Methods [*] Access-Control-Max-Age [0]
(next http.Handler)
| 244 | // Access-Control-Allow-Methods [*] |
| 245 | // Access-Control-Max-Age [0] |
| 246 | func (o *ObjectNode) corsMiddleware(next http.Handler) http.Handler { |
| 247 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 248 | var err error |
| 249 | param := ParseRequestParam(r) |
| 250 | if param.Bucket() == "" { |
| 251 | next.ServeHTTP(w, r) |
| 252 | return |
| 253 | } |
| 254 | |
| 255 | var vol *Volume |
| 256 | if param.action == proto.OSSCreateBucketAction { |
| 257 | if vol, err = o.vm.VolumeWithoutBlacklist(param.Bucket(), false); err != nil { |
| 258 | next.ServeHTTP(w, r) |
| 259 | return |
| 260 | } |
| 261 | } else { |
| 262 | if vol, err = o.vm.Volume(param.Bucket()); err != nil { |
| 263 | next.ServeHTTP(w, r) |
| 264 | return |
| 265 | } |
| 266 | } |
| 267 | mux.Vars(r)[ContextKeyOwner] = vol.GetOwner() |
| 268 | |
| 269 | if IsAccountLevelApi(param.apiName) { |
| 270 | next.ServeHTTP(w, r) |
| 271 | return |
| 272 | } |
| 273 | |
| 274 | isPreflight := param.apiName == OPTIONS_OBJECT |
| 275 | w.Header().Add("Vary", "Origin,Access-Control-Request-Method,Access-Control-Request-Headers") |
| 276 | cors, err := vol.metaLoader.loadCORS() |
| 277 | if err != nil { |
| 278 | log.LogErrorf("get cors fail: requestID(%v) err(%v)", GetRequestID(r), err) |
| 279 | InternalErrorCode(err).ServeResponse(w, r) |
| 280 | return |
| 281 | } |
| 282 | |
| 283 | if isPreflight { |
| 284 | errCode := preflightProcess(cors, w, r) |
| 285 | if errCode != nil { |
| 286 | errCode.ServeResponse(w, r) |
| 287 | return |
| 288 | } |
| 289 | w.WriteHeader(http.StatusOK) |
| 290 | return |
| 291 | } |
| 292 | |
| 293 | errCode := simpleProcess(cors, w, r) |
| 294 | if errCode != nil { |
| 295 | errCode.ServeResponse(w, r) |
| 296 | return |
| 297 | } |
| 298 | next.ServeHTTP(w, r) |
| 299 | }) |
| 300 | } |
| 301 | |
| 302 | func isMatchAndSetupCORSHeader(cors *CORSConfiguration, writer http.ResponseWriter, request *http.Request, isPreflight bool) (match bool) { |
| 303 | origin := request.Header.Get(Origin) |
nothing calls this directly
no test coverage detected