Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package restful |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // Copyright 2013 Ernest Micklei. All rights reserved. |
| 6 | // Use of this source code is governed by a license |
| 7 | // that can be found in the LICENSE file. |
| 8 | |
| 9 | // OPTIONSFilter is a filter function that inspects the Http Request for the OPTIONS method |
| 10 | // and provides the response with a set of allowed methods for the request URL Path. |
| 11 | // As for any filter, you can also install it for a particular WebService within a Container. |
| 12 | // Note: this filter is not needed when using CrossOriginResourceSharing (for CORS). |
| 13 | func (c *Container) OPTIONSFilter(req *Request, resp *Response, chain *FilterChain) { |
| 14 | if "OPTIONS" != req.Request.Method { |
| 15 | chain.ProcessFilter(req, resp) |
| 16 | return |
| 17 | } |
| 18 | |
| 19 | archs := req.Request.Header.Get(HEADER_AccessControlRequestHeaders) |
| 20 | methods := strings.Join(c.computeAllowedMethods(req), ",") |
| 21 | origin := req.Request.Header.Get(HEADER_Origin) |
| 22 | |
| 23 | resp.AddHeader(HEADER_Allow, methods) |
| 24 | resp.AddHeader(HEADER_AccessControlAllowOrigin, origin) |
| 25 | resp.AddHeader(HEADER_AccessControlAllowHeaders, archs) |
| 26 | resp.AddHeader(HEADER_AccessControlAllowMethods, methods) |
| 27 | } |
| 28 | |
| 29 | // OPTIONSFilter is a filter function that inspects the Http Request for the OPTIONS method |
| 30 | // and provides the response with a set of allowed methods for the request URL Path. |
| 31 | // Note: this filter is not needed when using CrossOriginResourceSharing (for CORS). |
| 32 | func OPTIONSFilter() FilterFunction { |
| 33 | return DefaultContainer.OPTIONSFilter |
| 34 | } |