couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From beno...@apache.org
Subject [1/3] git commit: check if "*" is accepted.
Date Mon, 05 Nov 2012 05:02:38 GMT
Updated Branches:
  refs/heads/431-feature_cors e70e4faa9 -> 95e5c72df


check if "*" is accepted.

Now we are checking if the node can accept all origins. This patch add a
test from @daleharvey.


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/95e5c72d
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/95e5c72d
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/95e5c72d

Branch: refs/heads/431-feature_cors
Commit: 95e5c72df89bff955b2be725d9408d765a3dc0e2
Parents: 84b2d15
Author: benoitc <bchesneau@gmail.com>
Authored: Mon Nov 5 05:59:21 2012 +0100
Committer: benoitc <bchesneau@gmail.com>
Committed: Mon Nov 5 05:59:21 2012 +0100

----------------------------------------------------------------------
 src/couchdb/couch_httpd.erl      |    1 +
 src/couchdb/couch_httpd_cors.erl |   55 +++++++++++++++++---------------
 2 files changed, 30 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/95e5c72d/src/couchdb/couch_httpd.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl
index 0024609..54381da 100644
--- a/src/couchdb/couch_httpd.erl
+++ b/src/couchdb/couch_httpd.erl
@@ -30,6 +30,7 @@
 -export([send_response/4,send_method_not_allowed/2,send_error/4, send_redirect/2,send_chunked_error/2]).
 -export([send_json/2,send_json/3,send_json/4,last_chunk/1,parse_multipart_request/3]).
 -export([accepted_encodings/1,handle_request_int/5,validate_referer/1,validate_ctype/2]).
+-export([http_1_0_keep_alive/2]).
 
 start_link() ->
     start_link(http).

http://git-wip-us.apache.org/repos/asf/couchdb/blob/95e5c72d/src/couchdb/couch_httpd_cors.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd_cors.erl b/src/couchdb/couch_httpd_cors.erl
index 4f8bf48..879fee0 100644
--- a/src/couchdb/couch_httpd_cors.erl
+++ b/src/couchdb/couch_httpd_cors.erl
@@ -37,7 +37,7 @@ is_preflight_request(#httpd{mochi_req=MochiReq}=Req) ->
         true ->
             case preflight_request(MochiReq) of
                 {ok, PreflightHeaders} ->
-                    couch_httpd:send_response(Req, 204, PreflightHeaders, <<>>);
+                    send_preflight_response(Req, PreflightHeaders);
                 _ ->
                     Req
             end;
@@ -56,10 +56,6 @@ cors_headers(#httpd{mochi_req=MochiReq}) ->
             case MochiReq:get_header_value("Origin") of
                 undefined ->
                     [];
-                <<"*">> ->
-                    handle_cors_headers("*", Host, AcceptedOrigins);
-                <<"null">> ->
-                    handle_cors_headers("*", Host, AcceptedOrigins);
                 Origin ->
                     handle_cors_headers(couch_util:to_list(Origin),
                                         Host, AcceptedOrigins)
@@ -68,47 +64,45 @@ cors_headers(#httpd{mochi_req=MochiReq}) ->
             []
     end.
 
-handle_cors_headers("*", _Host, _AcceptedOrigins) ->
-    [{"Access-Control-Allow-Origin", "*"}];
 handle_cors_headers(Origin, Host, []) ->
+    make_cors_header(Origin, Host);
+handle_cors_headers(Origin, Host, AcceptedOrigins) ->
+    AcceptAll = lists:member("*", AcceptedOrigins),
+    case {AcceptAll, lists:member(Origin, AcceptedOrigins)} of
+        {true, _} ->
+            make_cors_header(Origin, Host);
+        {false, true}  ->
+            make_cors_header(Origin, Host);
+        _ ->
+            []
+    end.
+
+make_cors_header(Origin, Host) ->
     case credentials(Origin, Host) of
         true ->
             [{"Access-Control-Allow-Origin", Origin},
              {"Access-Control-Allow-Credentials", "true"}];
         false ->
             [{"Access-Control-Allow-Origin", Origin}]
-    end;
-handle_cors_headers(Origin, Host, AcceptedOrigins) ->
-    AllowCredentials = credentials(Origin, Host),
-    case lists:member(Origin, AcceptedOrigins) of
-        true when AllowCredentials =:= true ->
-            [{"Access-Control-Allow-Origin", Origin},
-             {"Access-Control-Allow-Credentials", "true"}];
-        true ->
-            [{"Access-Control-Allow-Origin", Origin}];
-        _ ->
-            []
     end.
 
-
 preflight_request(MochiReq) ->
     Host = couch_httpd_vhost:host(MochiReq),
     case MochiReq:get_header_value("Origin") of
         undefined ->
             MochiReq;
-        <<"*">>  ->
-            handle_preflight_request("*", Host, MochiReq);
-        <<"null">> ->
-            handle_preflight_request("*", Host, MochiReq);
+
         Origin ->
             AcceptedOrigins = re:split(cors_config(Host, "origins", []),
                                        "\\s*,\\s*",
                                        [trim, {return, list}]),
-            case AcceptedOrigins of
-                [] ->
+            AcceptAll = lists:member("*", AcceptedOrigins),
+
+            case {AcceptAll, AcceptedOrigins} of
+                {true, _} ->
                     handle_preflight_request(couch_util:to_list(Origin),
                                              Host, MochiReq);
-                _ ->
+                {false, _} ->
                     case lists:member(Origin, AcceptedOrigins) of
                         true ->
                             handle_preflight_request(couch_util:to_list(Origin),
@@ -183,6 +177,15 @@ handle_preflight_request(Origin, Host, MochiReq) ->
     end.
 
 
+send_preflight_response(#httpd{mochi_req=MochiReq}=Req, Headers) ->
+    couch_httpd:log_request(Req, 204),
+    couch_stats_collector:increment({httpd_status_codes, 204}),
+    Headers1 = couch_httpd:http_1_0_keep_alive(MochiReq, Headers),
+    Headers2 = Headers1 ++ couch_httpd:server_header() ++
+               couch_httpd_auth:cookie_auth_header(Req, Headers1),
+    {ok, MochiReq:respond({204, Headers2, <<>>})}.
+
+
 credentials("*", _Host) ->
     false;
 credentials(_Origin, Host) ->


Mime
View raw message