MCPcopy Create free account
hub / github.com/cubefs/cubefs / corsMiddleware

Method corsMiddleware

objectnode/api_middleware.go:246–300  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

244// Access-Control-Allow-Methods [*]
245// Access-Control-Max-Age [0]
246func (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
302func isMatchAndSetupCORSHeader(cors *CORSConfiguration, writer http.ResponseWriter, request *http.Request, isPreflight bool) (match bool) {
303 origin := request.Header.Get(Origin)

Callers

nothing calls this directly

Calls 15

GetOwnerMethod · 0.95
LogErrorfFunction · 0.92
ParseRequestParamFunction · 0.85
IsAccountLevelApiFunction · 0.85
GetRequestIDFunction · 0.85
InternalErrorCodeFunction · 0.85
preflightProcessFunction · 0.85
simpleProcessFunction · 0.85
ServeResponseMethod · 0.80
loadCORSMethod · 0.65
BucketMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected