@cryptotaxi247 / kubo / commits / 865114334

Gateway: add support for HTTP OPTIONS request type

OPTIONS is a noop request that is used by the browsers to check if server will accept cross-site XMLHttpRequest (indicated by the presence of CORS headers) Before this fix user could enable CORS headers in the Gateway config, but XHR failed due to the lack of support for OPTIONS request type (as described in https://git.io/vzgGe) License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>

Marcin Rataj committed Jan 21, 2016 at 23:05 UTC 8651143344fc14e5157635b5be84c86f63002440
1 file changed +14
core/corehttp/gateway_handler.go
+14
@@ -82,6 +82,11 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
82 return
83 }
84
85 + if r.Method == "OPTIONS" {
86 + i.optionsHandler(w, r)
87 + return
88 + }
89 +
90 errmsg := "Method " + r.Method + " not allowed: "
91 if !i.config.Writable {
92 w.WriteHeader(http.StatusMethodNotAllowed)
@@ -94,6 +99,15 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
99 log.Error(errmsg) // TODO(cryptix): log errors until we have a better way to expose these (counter metrics maybe)
100 }
101
102 +func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) {
103 + /*
104 + OPTIONS is a noop request that is used by the browsers to check
105 + if server accepts cross-site XMLHttpRequest (indicated by the presence of CORS headers)
106 + https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
107 + */
108 + i.addUserHeaders(w) // return all custom headers (including CORS ones, if set)
109 +}
110 +
111 func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) {
112 ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour)
113 // the hour is a hard fallback, we don't expect it to happen, but just in case