1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/controller/api"
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/auth"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "git.arvados.org/arvados.git/sdk/go/httpserver"
19 "github.com/gorilla/mux"
20 "github.com/sirupsen/logrus"
30 // Return an error if request body exceeds this size. 0 means
34 // If wrapCalls is not nil, it is called once for each API
35 // method, and the returned method is used in its place. This
36 // can be used to install hooks before and after each API call
37 // and alter responses; see localdb.WrapCallsInTransaction for
39 WrapCalls func(api.RoutableFunc) api.RoutableFunc
42 // New returns a new router (which implements the http.Handler
43 // interface) that serves requests by calling Arvados API methods on
45 func New(backend arvados.API, config Config) *router {
55 func (rtr *router) addRoutes() {
56 for _, route := range []struct {
57 endpoint arvados.APIEndpoint
58 defaultOpts func() interface{}
62 arvados.EndpointConfigGet,
63 func() interface{} { return &struct{}{} },
64 func(ctx context.Context, opts interface{}) (interface{}, error) {
65 return rtr.backend.ConfigGet(ctx)
69 arvados.EndpointVocabularyGet,
70 func() interface{} { return &struct{}{} },
71 func(ctx context.Context, opts interface{}) (interface{}, error) {
72 return rtr.backend.VocabularyGet(ctx)
76 arvados.EndpointLogin,
77 func() interface{} { return &arvados.LoginOptions{} },
78 func(ctx context.Context, opts interface{}) (interface{}, error) {
79 return rtr.backend.Login(ctx, *opts.(*arvados.LoginOptions))
83 arvados.EndpointLogout,
84 func() interface{} { return &arvados.LogoutOptions{} },
85 func(ctx context.Context, opts interface{}) (interface{}, error) {
86 return rtr.backend.Logout(ctx, *opts.(*arvados.LogoutOptions))
90 arvados.EndpointCollectionCreate,
91 func() interface{} { return &arvados.CreateOptions{} },
92 func(ctx context.Context, opts interface{}) (interface{}, error) {
93 return rtr.backend.CollectionCreate(ctx, *opts.(*arvados.CreateOptions))
97 arvados.EndpointCollectionUpdate,
98 func() interface{} { return &arvados.UpdateOptions{} },
99 func(ctx context.Context, opts interface{}) (interface{}, error) {
100 return rtr.backend.CollectionUpdate(ctx, *opts.(*arvados.UpdateOptions))
104 arvados.EndpointCollectionGet,
105 func() interface{} { return &arvados.GetOptions{} },
106 func(ctx context.Context, opts interface{}) (interface{}, error) {
107 return rtr.backend.CollectionGet(ctx, *opts.(*arvados.GetOptions))
111 arvados.EndpointCollectionList,
112 func() interface{} { return &arvados.ListOptions{Limit: -1} },
113 func(ctx context.Context, opts interface{}) (interface{}, error) {
114 return rtr.backend.CollectionList(ctx, *opts.(*arvados.ListOptions))
118 arvados.EndpointCollectionProvenance,
119 func() interface{} { return &arvados.GetOptions{} },
120 func(ctx context.Context, opts interface{}) (interface{}, error) {
121 return rtr.backend.CollectionProvenance(ctx, *opts.(*arvados.GetOptions))
125 arvados.EndpointCollectionUsedBy,
126 func() interface{} { return &arvados.GetOptions{} },
127 func(ctx context.Context, opts interface{}) (interface{}, error) {
128 return rtr.backend.CollectionUsedBy(ctx, *opts.(*arvados.GetOptions))
132 arvados.EndpointCollectionDelete,
133 func() interface{} { return &arvados.DeleteOptions{} },
134 func(ctx context.Context, opts interface{}) (interface{}, error) {
135 return rtr.backend.CollectionDelete(ctx, *opts.(*arvados.DeleteOptions))
139 arvados.EndpointCollectionTrash,
140 func() interface{} { return &arvados.DeleteOptions{} },
141 func(ctx context.Context, opts interface{}) (interface{}, error) {
142 return rtr.backend.CollectionTrash(ctx, *opts.(*arvados.DeleteOptions))
146 arvados.EndpointCollectionUntrash,
147 func() interface{} { return &arvados.UntrashOptions{} },
148 func(ctx context.Context, opts interface{}) (interface{}, error) {
149 return rtr.backend.CollectionUntrash(ctx, *opts.(*arvados.UntrashOptions))
153 arvados.EndpointContainerCreate,
154 func() interface{} { return &arvados.CreateOptions{} },
155 func(ctx context.Context, opts interface{}) (interface{}, error) {
156 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
160 arvados.EndpointContainerUpdate,
161 func() interface{} { return &arvados.UpdateOptions{} },
162 func(ctx context.Context, opts interface{}) (interface{}, error) {
163 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
167 arvados.EndpointContainerGet,
168 func() interface{} { return &arvados.GetOptions{} },
169 func(ctx context.Context, opts interface{}) (interface{}, error) {
170 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
174 arvados.EndpointContainerList,
175 func() interface{} { return &arvados.ListOptions{Limit: -1} },
176 func(ctx context.Context, opts interface{}) (interface{}, error) {
177 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
181 arvados.EndpointContainerDelete,
182 func() interface{} { return &arvados.DeleteOptions{} },
183 func(ctx context.Context, opts interface{}) (interface{}, error) {
184 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
188 arvados.EndpointContainerRequestCreate,
189 func() interface{} { return &arvados.CreateOptions{} },
190 func(ctx context.Context, opts interface{}) (interface{}, error) {
191 return rtr.backend.ContainerRequestCreate(ctx, *opts.(*arvados.CreateOptions))
195 arvados.EndpointContainerRequestUpdate,
196 func() interface{} { return &arvados.UpdateOptions{} },
197 func(ctx context.Context, opts interface{}) (interface{}, error) {
198 return rtr.backend.ContainerRequestUpdate(ctx, *opts.(*arvados.UpdateOptions))
202 arvados.EndpointContainerRequestGet,
203 func() interface{} { return &arvados.GetOptions{} },
204 func(ctx context.Context, opts interface{}) (interface{}, error) {
205 return rtr.backend.ContainerRequestGet(ctx, *opts.(*arvados.GetOptions))
209 arvados.EndpointContainerRequestList,
210 func() interface{} { return &arvados.ListOptions{Limit: -1} },
211 func(ctx context.Context, opts interface{}) (interface{}, error) {
212 return rtr.backend.ContainerRequestList(ctx, *opts.(*arvados.ListOptions))
216 arvados.EndpointContainerRequestDelete,
217 func() interface{} { return &arvados.DeleteOptions{} },
218 func(ctx context.Context, opts interface{}) (interface{}, error) {
219 return rtr.backend.ContainerRequestDelete(ctx, *opts.(*arvados.DeleteOptions))
223 arvados.EndpointContainerLock,
225 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
227 func(ctx context.Context, opts interface{}) (interface{}, error) {
228 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
232 arvados.EndpointContainerUnlock,
234 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
236 func(ctx context.Context, opts interface{}) (interface{}, error) {
237 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
241 arvados.EndpointContainerSSH,
242 func() interface{} { return &arvados.ContainerSSHOptions{} },
243 func(ctx context.Context, opts interface{}) (interface{}, error) {
244 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
248 // arvados-client built before commit
249 // bdc29d3129f6d75aa9ce0a24ffb849a272b06f08
250 // used GET with params in headers instead of
252 arvados.APIEndpoint{"GET", "arvados/v1/connect/{uuid}/ssh", ""},
253 func() interface{} { return &arvados.ContainerSSHOptions{} },
254 func(ctx context.Context, opts interface{}) (interface{}, error) {
255 return nil, httpError(http.StatusGone, fmt.Errorf("API endpoint is obsolete -- please upgrade your arvados-client program"))
259 arvados.EndpointContainerGatewayTunnel,
260 func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
261 func(ctx context.Context, opts interface{}) (interface{}, error) {
262 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
266 arvados.EndpointGroupCreate,
267 func() interface{} { return &arvados.CreateOptions{} },
268 func(ctx context.Context, opts interface{}) (interface{}, error) {
269 return rtr.backend.GroupCreate(ctx, *opts.(*arvados.CreateOptions))
273 arvados.EndpointGroupUpdate,
274 func() interface{} { return &arvados.UpdateOptions{} },
275 func(ctx context.Context, opts interface{}) (interface{}, error) {
276 return rtr.backend.GroupUpdate(ctx, *opts.(*arvados.UpdateOptions))
280 arvados.EndpointGroupList,
281 func() interface{} { return &arvados.ListOptions{Limit: -1} },
282 func(ctx context.Context, opts interface{}) (interface{}, error) {
283 return rtr.backend.GroupList(ctx, *opts.(*arvados.ListOptions))
287 arvados.EndpointGroupContents,
288 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
289 func(ctx context.Context, opts interface{}) (interface{}, error) {
290 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
294 arvados.EndpointGroupContentsUUIDInPath,
295 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
296 func(ctx context.Context, opts interface{}) (interface{}, error) {
297 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
301 arvados.EndpointGroupShared,
302 func() interface{} { return &arvados.ListOptions{Limit: -1} },
303 func(ctx context.Context, opts interface{}) (interface{}, error) {
304 return rtr.backend.GroupShared(ctx, *opts.(*arvados.ListOptions))
308 arvados.EndpointGroupGet,
309 func() interface{} { return &arvados.GetOptions{} },
310 func(ctx context.Context, opts interface{}) (interface{}, error) {
311 return rtr.backend.GroupGet(ctx, *opts.(*arvados.GetOptions))
315 arvados.EndpointGroupDelete,
316 func() interface{} { return &arvados.DeleteOptions{} },
317 func(ctx context.Context, opts interface{}) (interface{}, error) {
318 return rtr.backend.GroupDelete(ctx, *opts.(*arvados.DeleteOptions))
322 arvados.EndpointGroupTrash,
323 func() interface{} { return &arvados.DeleteOptions{} },
324 func(ctx context.Context, opts interface{}) (interface{}, error) {
325 return rtr.backend.GroupTrash(ctx, *opts.(*arvados.DeleteOptions))
329 arvados.EndpointGroupUntrash,
330 func() interface{} { return &arvados.UntrashOptions{} },
331 func(ctx context.Context, opts interface{}) (interface{}, error) {
332 return rtr.backend.GroupUntrash(ctx, *opts.(*arvados.UntrashOptions))
336 arvados.EndpointLinkCreate,
337 func() interface{} { return &arvados.CreateOptions{} },
338 func(ctx context.Context, opts interface{}) (interface{}, error) {
339 return rtr.backend.LinkCreate(ctx, *opts.(*arvados.CreateOptions))
343 arvados.EndpointLinkUpdate,
344 func() interface{} { return &arvados.UpdateOptions{} },
345 func(ctx context.Context, opts interface{}) (interface{}, error) {
346 return rtr.backend.LinkUpdate(ctx, *opts.(*arvados.UpdateOptions))
350 arvados.EndpointLinkList,
351 func() interface{} { return &arvados.ListOptions{Limit: -1} },
352 func(ctx context.Context, opts interface{}) (interface{}, error) {
353 return rtr.backend.LinkList(ctx, *opts.(*arvados.ListOptions))
357 arvados.EndpointLinkGet,
358 func() interface{} { return &arvados.GetOptions{} },
359 func(ctx context.Context, opts interface{}) (interface{}, error) {
360 return rtr.backend.LinkGet(ctx, *opts.(*arvados.GetOptions))
364 arvados.EndpointLinkDelete,
365 func() interface{} { return &arvados.DeleteOptions{} },
366 func(ctx context.Context, opts interface{}) (interface{}, error) {
367 return rtr.backend.LinkDelete(ctx, *opts.(*arvados.DeleteOptions))
371 arvados.EndpointLogCreate,
372 func() interface{} { return &arvados.CreateOptions{} },
373 func(ctx context.Context, opts interface{}) (interface{}, error) {
374 return rtr.backend.LogCreate(ctx, *opts.(*arvados.CreateOptions))
378 arvados.EndpointLogUpdate,
379 func() interface{} { return &arvados.UpdateOptions{} },
380 func(ctx context.Context, opts interface{}) (interface{}, error) {
381 return rtr.backend.LogUpdate(ctx, *opts.(*arvados.UpdateOptions))
385 arvados.EndpointLogList,
386 func() interface{} { return &arvados.ListOptions{Limit: -1} },
387 func(ctx context.Context, opts interface{}) (interface{}, error) {
388 return rtr.backend.LogList(ctx, *opts.(*arvados.ListOptions))
392 arvados.EndpointLogGet,
393 func() interface{} { return &arvados.GetOptions{} },
394 func(ctx context.Context, opts interface{}) (interface{}, error) {
395 return rtr.backend.LogGet(ctx, *opts.(*arvados.GetOptions))
399 arvados.EndpointLogDelete,
400 func() interface{} { return &arvados.DeleteOptions{} },
401 func(ctx context.Context, opts interface{}) (interface{}, error) {
402 return rtr.backend.LogDelete(ctx, *opts.(*arvados.DeleteOptions))
406 arvados.EndpointSpecimenCreate,
407 func() interface{} { return &arvados.CreateOptions{} },
408 func(ctx context.Context, opts interface{}) (interface{}, error) {
409 return rtr.backend.SpecimenCreate(ctx, *opts.(*arvados.CreateOptions))
413 arvados.EndpointSpecimenUpdate,
414 func() interface{} { return &arvados.UpdateOptions{} },
415 func(ctx context.Context, opts interface{}) (interface{}, error) {
416 return rtr.backend.SpecimenUpdate(ctx, *opts.(*arvados.UpdateOptions))
420 arvados.EndpointSpecimenGet,
421 func() interface{} { return &arvados.GetOptions{} },
422 func(ctx context.Context, opts interface{}) (interface{}, error) {
423 return rtr.backend.SpecimenGet(ctx, *opts.(*arvados.GetOptions))
427 arvados.EndpointSpecimenList,
428 func() interface{} { return &arvados.ListOptions{Limit: -1} },
429 func(ctx context.Context, opts interface{}) (interface{}, error) {
430 return rtr.backend.SpecimenList(ctx, *opts.(*arvados.ListOptions))
434 arvados.EndpointSpecimenDelete,
435 func() interface{} { return &arvados.DeleteOptions{} },
436 func(ctx context.Context, opts interface{}) (interface{}, error) {
437 return rtr.backend.SpecimenDelete(ctx, *opts.(*arvados.DeleteOptions))
441 arvados.EndpointAPIClientAuthorizationCreate,
442 func() interface{} { return &arvados.CreateOptions{} },
443 func(ctx context.Context, opts interface{}) (interface{}, error) {
444 return rtr.backend.APIClientAuthorizationCreate(ctx, *opts.(*arvados.CreateOptions))
448 arvados.EndpointAPIClientAuthorizationUpdate,
449 func() interface{} { return &arvados.UpdateOptions{} },
450 func(ctx context.Context, opts interface{}) (interface{}, error) {
451 return rtr.backend.APIClientAuthorizationUpdate(ctx, *opts.(*arvados.UpdateOptions))
455 arvados.EndpointAPIClientAuthorizationDelete,
456 func() interface{} { return &arvados.DeleteOptions{} },
457 func(ctx context.Context, opts interface{}) (interface{}, error) {
458 return rtr.backend.APIClientAuthorizationDelete(ctx, *opts.(*arvados.DeleteOptions))
462 arvados.EndpointAPIClientAuthorizationList,
463 func() interface{} { return &arvados.ListOptions{Limit: -1} },
464 func(ctx context.Context, opts interface{}) (interface{}, error) {
465 return rtr.backend.APIClientAuthorizationList(ctx, *opts.(*arvados.ListOptions))
469 arvados.EndpointAPIClientAuthorizationCurrent,
470 func() interface{} { return &arvados.GetOptions{} },
471 func(ctx context.Context, opts interface{}) (interface{}, error) {
472 return rtr.backend.APIClientAuthorizationCurrent(ctx, *opts.(*arvados.GetOptions))
476 arvados.EndpointAPIClientAuthorizationGet,
477 func() interface{} { return &arvados.GetOptions{} },
478 func(ctx context.Context, opts interface{}) (interface{}, error) {
479 return rtr.backend.APIClientAuthorizationGet(ctx, *opts.(*arvados.GetOptions))
483 arvados.EndpointUserCreate,
484 func() interface{} { return &arvados.CreateOptions{} },
485 func(ctx context.Context, opts interface{}) (interface{}, error) {
486 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
490 arvados.EndpointUserMerge,
491 func() interface{} { return &arvados.UserMergeOptions{} },
492 func(ctx context.Context, opts interface{}) (interface{}, error) {
493 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
497 arvados.EndpointUserActivate,
498 func() interface{} { return &arvados.UserActivateOptions{} },
499 func(ctx context.Context, opts interface{}) (interface{}, error) {
500 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
504 arvados.EndpointUserSetup,
505 func() interface{} { return &arvados.UserSetupOptions{} },
506 func(ctx context.Context, opts interface{}) (interface{}, error) {
507 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
511 arvados.EndpointUserUnsetup,
512 func() interface{} { return &arvados.GetOptions{} },
513 func(ctx context.Context, opts interface{}) (interface{}, error) {
514 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
518 arvados.EndpointUserGetCurrent,
519 func() interface{} { return &arvados.GetOptions{} },
520 func(ctx context.Context, opts interface{}) (interface{}, error) {
521 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
525 arvados.EndpointUserGetSystem,
526 func() interface{} { return &arvados.GetOptions{} },
527 func(ctx context.Context, opts interface{}) (interface{}, error) {
528 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
532 arvados.EndpointUserGet,
533 func() interface{} { return &arvados.GetOptions{} },
534 func(ctx context.Context, opts interface{}) (interface{}, error) {
535 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
539 arvados.EndpointUserUpdate,
540 func() interface{} { return &arvados.UpdateOptions{} },
541 func(ctx context.Context, opts interface{}) (interface{}, error) {
542 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
546 arvados.EndpointUserList,
547 func() interface{} { return &arvados.ListOptions{Limit: -1} },
548 func(ctx context.Context, opts interface{}) (interface{}, error) {
549 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
553 arvados.EndpointUserBatchUpdate,
554 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
555 func(ctx context.Context, opts interface{}) (interface{}, error) {
556 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
560 arvados.EndpointUserDelete,
561 func() interface{} { return &arvados.DeleteOptions{} },
562 func(ctx context.Context, opts interface{}) (interface{}, error) {
563 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
567 arvados.EndpointUserAuthenticate,
568 func() interface{} { return &arvados.UserAuthenticateOptions{} },
569 func(ctx context.Context, opts interface{}) (interface{}, error) {
570 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
575 if rtr.config.WrapCalls != nil {
576 exec = rtr.config.WrapCalls(exec)
578 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
580 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
581 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
583 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
584 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
588 var altMethod = map[string]string{
589 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
590 "GET": "HEAD", // Accept HEAD at any GET route
593 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
594 methods := []string{endpoint.Method}
595 if alt, ok := altMethod[endpoint.Method]; ok {
596 methods = append(methods, alt)
598 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
599 logger := ctxlog.FromContext(req.Context())
600 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey)
602 logger.WithFields(logrus.Fields{
604 "method": endpoint.Method,
605 "endpoint": endpoint,
606 }).WithError(err).Debug("error loading request params")
607 rtr.sendError(w, err)
610 opts := defaultOpts()
611 err = rtr.transcode(params, opts)
613 logger.WithField("params", params).WithError(err).Debugf("error transcoding params to %T", opts)
614 rtr.sendError(w, err)
617 respOpts, err := rtr.responseOptions(opts)
619 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
620 rtr.sendError(w, err)
624 creds := auth.CredentialsFromRequest(req)
625 err = creds.LoadTokensFromHTTPRequestBody(req)
627 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
630 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
631 for _, t := range rt {
632 if t, ok := t.(string); ok {
633 creds.Tokens = append(creds.Tokens, t)
637 ctx := auth.NewContext(req.Context(), creds)
638 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
639 logger.WithFields(logrus.Fields{
640 "apiEndpoint": endpoint,
641 "apiOptsType": fmt.Sprintf("%T", opts),
644 // Extract the token UUIDs (or a placeholder for v1 tokens)
645 var tokenUUIDs []string
646 for _, t := range creds.Tokens {
647 if strings.HasPrefix(t, "v2/") {
648 tokenParts := strings.Split(t, "/")
649 if len(tokenParts) >= 3 {
650 tokenUUIDs = append(tokenUUIDs, tokenParts[1])
657 tokenUUIDs = append(tokenUUIDs, "v1 token ending in "+end)
660 httpserver.SetResponseLogFields(req.Context(), logrus.Fields{"tokenUUIDs": tokenUUIDs})
661 resp, err := exec(ctx, opts)
663 logger.WithError(err).Debugf("returning error type %T", err)
664 rtr.sendError(w, err)
667 rtr.sendResponse(w, req, resp, respOpts)
671 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
672 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
673 case "login", "logout", "auth":
675 w.Header().Set("Access-Control-Allow-Origin", "*")
676 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, PUT, POST, PATCH, DELETE")
677 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Http-Method-Override")
678 w.Header().Set("Access-Control-Max-Age", "86486400")
680 if r.Method == "OPTIONS" {
684 // Wrap r.Body in a http.MaxBytesReader(), otherwise
685 // r.ParseForm() uses a default max request body size
686 // of 10 megabytes. Note we rely on the Nginx
687 // configuration to enforce the real max body size.
688 max := int64(rtr.config.MaxRequestSize)
690 max = math.MaxInt64 - 1
692 r.Body = http.MaxBytesReader(w, r.Body, max)
694 if r.Method == "POST" {
697 if err.Error() == "http: request body too large" {
698 err = httpError(http.StatusRequestEntityTooLarge, err)
700 rtr.sendError(w, err)
703 if m := r.FormValue("_method"); m != "" {
707 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
713 rtr.mux.ServeHTTP(w, r)