Skip to content

Instantly share code, notes, and snippets.

@luisbebop
Created July 29, 2010 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisbebop/499002 to your computer and use it in GitHub Desktop.
Save luisbebop/499002 to your computer and use it in GitHub Desktop.
From 991801b97297ac5047f42412e2d5ef3eb7bab0d8 Mon Sep 17 00:00:00 2001
From: luisbebop <luisbebop@gmail.com>
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
@cartazio
Copy link

i think i'm not the right person to do the pull request to for couchdb...

@davidcoallier
Copy link

You might want to direct your pull request to the original repo which is couchdb and not me either :)

@cartazio
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment