Created
July 29, 2010 19:32
-
-
Save luisbebop/499002 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 991801b97297ac5047f42412e2d5ef3eb7bab0d8 Mon Sep 17 00:00:00 2001 | |
From: luisbebop <[email protected]> | |
Date: Thu, 29 Jul 2010 15:02:58 -0300 | |
Subject: [PATCH] Implemented W3C CORS on httpd interface. Closes COUCHDB-832 | |
--- | |
src/couchdb/couch_httpd.erl | 27 ++++++++++++++++++++------- | |
1 files changed, 20 insertions(+), 7 deletions(-) | |
diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl | |
index 23ff7f9..c1aba59 100644 | |
--- a/src/couchdb/couch_httpd.erl | |
+++ b/src/couchdb/couch_httpd.erl | |
@@ -277,11 +277,16 @@ handle_request_int(MochiReq, DefaultFun, | |
{ok, Resp} = | |
try | |
- case authenticate_request(HttpReq, AuthenticationSrcs) of | |
- #httpd{} = Req -> | |
- HandlerFun(Req); | |
- Response -> | |
- Response | |
+ case Method =:= 'OPTIONS' of | |
+ true -> | |
+ send_json(HttpReq, 200, [{"Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, COPY, OPTIONS"},{"Access-Control-Max-Age", "86400"}], {[]}); | |
+ false -> | |
+ case authenticate_request(HttpReq, AuthenticationSrcs) of | |
+ #httpd{} = Req -> | |
+ HandlerFun(Req); | |
+ Response -> | |
+ Response | |
+ end | |
end | |
catch | |
throw:{http_head_abort, Resp0} -> | |
@@ -631,9 +636,13 @@ send_json(Req, Code, Value) -> | |
send_json(Req, Code, [], Value). | |
send_json(Req, Code, Headers, Value) -> | |
+ Origin = header_value(Req, "Origin", "*"), | |
DefaultHeaders = [ | |
{"Content-Type", negotiate_content_type(Req)}, | |
- {"Cache-Control", "must-revalidate"} | |
+ {"Cache-Control", "must-revalidate"}, | |
+ {"Access-Control-Allow-Origin", Origin}, | |
+ {"Access-Control-Allow-Headers", "Content-Type, Authorization"}, | |
+ {"Access-Control-Allow-Credentials", "true"} | |
], | |
Body = list_to_binary( | |
[start_jsonp(Req), ?JSON_ENCODE(Value), end_jsonp(), $\n] | |
@@ -644,9 +653,13 @@ start_json_response(Req, Code) -> | |
start_json_response(Req, Code, []). | |
start_json_response(Req, Code, Headers) -> | |
+ Origin = header_value(Req, "Origin", "*"), | |
DefaultHeaders = [ | |
{"Content-Type", negotiate_content_type(Req)}, | |
- {"Cache-Control", "must-revalidate"} | |
+ {"Cache-Control", "must-revalidate"}, | |
+ {"Access-Control-Allow-Origin", Origin}, | |
+ {"Access-Control-Allow-Headers", "Content-Type, Authorization"}, | |
+ {"Access-Control-Allow-Credentials", "true"} | |
], | |
start_jsonp(Req), % Validate before starting chunked. | |
%start_chunked_response(Req, Code, DefaultHeaders ++ Headers). | |
-- | |
1.6.4.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
even better, go to apache's github page, and then go to the couchdb/ page there
or better yet, go read the how to contribute docs that are no doubt on the couchdb website