couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbra...@apache.org
Subject chttpd commit: updated refs/heads/2080-port-cors to 20e437a
Date Fri, 14 Nov 2014 22:21:27 GMT
Repository: couchdb-chttpd
Updated Branches:
  refs/heads/2080-port-cors 2b3aaa775 -> 20e437a47


Use binaries instead of lists


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

Branch: refs/heads/2080-port-cors
Commit: 20e437a4714fcb013b18910888126901c221747a
Parents: 2b3aaa7
Author: Russell Branca <chewbranca@apache.org>
Authored: Fri Nov 14 14:21:21 2014 -0800
Committer: Russell Branca <chewbranca@apache.org>
Committed: Fri Nov 14 14:21:21 2014 -0800

----------------------------------------------------------------------
 src/chttpd_cors.erl       | 34 ++++++++++++++++++++++------------
 test/chttpd_cors_test.erl | 14 +++++++-------
 2 files changed, 29 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/20e437a4/src/chttpd_cors.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_cors.erl b/src/chttpd_cors.erl
index fdaa6dc..f0a4d0e 100644
--- a/src/chttpd_cors.erl
+++ b/src/chttpd_cors.erl
@@ -71,7 +71,7 @@ preflight_request(Req, Config) ->
             not_preflight;
         Origin ->
             AcceptedOrigins = get_accepted_origins(Req, Config),
-            AcceptAll = lists:member("*", AcceptedOrigins),
+            AcceptAll = lists:member(<<"*">>, AcceptedOrigins),
 
             HandlerFun = fun() ->
                 handle_preflight_request(Req, Config, Origin)
@@ -107,14 +107,19 @@ handle_preflight_request(Req, Config, Origin) ->
         %% http://www.w3.org/TR/cors/#resource-preflight-requests
         not_preflight;
     Method ->
-        SupportedMethods = get_origin_config(Config, Origin,
-                <<"allow_methods">>, ?SUPPORTED_METHODS),
+        SupportedMethods = case get_origin_config(Config, Origin,
+                <<"allow_methods">>, undefined) of
+            undefined ->
+                ?SUPPORTED_METHODS;
+            Methods ->
+                [binary_to_list(M) || M <- Methods]
+        end,
 
         %% get max age
         MaxAge = couch_util:get_value("max_age", Config, ?CORS_DEFAULT_MAX_AGE),
 
         PreflightHeaders0 = maybe_add_credentials(Config, Origin, [
-            {"Access-Control-Allow-Origin", Origin},
+            {"Access-Control-Allow-Origin", binary_to_list(Origin)},
             {"Access-Control-Max-Age", MaxAge},
             {"Access-Control-Allow-Methods",
                 string:join(SupportedMethods, ", ")}]),
@@ -169,8 +174,9 @@ headers(Req, RequestHeaders) ->
 
 headers(_Req, RequestHeaders, undefined, _Config) ->
     RequestHeaders;
-headers(Req, RequestHeaders, Origin0, Config) ->
-    Origin = string:to_lower(Origin0),
+headers(Req, RequestHeaders, Origin, Config) when is_list(Origin) ->
+    headers(Req, RequestHeaders, ?l2b(string:to_lower(Origin)), Config);
+headers(Req, RequestHeaders, Origin, Config) ->
     case is_cors_enabled(Config) of
         true ->
             AcceptedOrigins = get_accepted_origins(Req, Config),
@@ -223,7 +229,7 @@ simple_headers(Headers) ->
 handle_headers(_Config, _Origin, []) ->
     [];
 handle_headers(Config, Origin, AcceptedOrigins) ->
-    AcceptAll = lists:member("*", AcceptedOrigins),
+    AcceptAll = lists:member(<<"*">>, AcceptedOrigins),
     case AcceptAll orelse lists:member(Origin, AcceptedOrigins) of
     true ->
         make_cors_header(Config, Origin);
@@ -238,7 +244,7 @@ handle_headers(Config, Origin, AcceptedOrigins) ->
 
 
 make_cors_header(Config, Origin) ->
-    Headers = [{"Access-Control-Allow-Origin", Origin}],
+    Headers = [{"Access-Control-Allow-Origin", binary_to_list(Origin)}],
     maybe_add_credentials(Config, Origin, Headers).
 
 
@@ -254,7 +260,7 @@ maybe_add_credentials(Config, Origin, Headers) ->
     end.
 
 
-allow_credentials(_Config, "*") ->
+allow_credentials(_Config, <<"*">>) ->
     false;
 allow_credentials(Config, Origin) ->
     get_origin_config(Config, Origin, <<"allow_credentials">>,
@@ -267,13 +273,13 @@ get_cors_config(_Req) ->
         undefined ->
             ?SUPPORTED_HEADERS;
         AllowHeaders0 ->
-            split_list(AllowHeaders0)
+            binary_split_list(AllowHeaders0)
     end,
     AllowMethods = case config:get("cors", "methods", undefined) of
         undefined ->
             ?SUPPORTED_METHODS;
         AllowMethods0 ->
-            split_list(AllowMethods0)
+            binary_split_list(AllowMethods0)
     end,
     Origins = split_list(config:get("cors", "origins", [])),
     [
@@ -339,7 +345,7 @@ get_origin(Req) ->
         undefined ->
             undefined;
         Origin ->
-            string:to_lower(Origin)
+            list_to_binary(string:to_lower(Origin))
     end.
 
 
@@ -351,5 +357,9 @@ split_list(S) ->
     re:split(S, "\\s*,\\s*", [trim, {return, list}]).
 
 
+binary_split_list(S) ->
+    [list_to_binary(E) || E <- split_list(S)].
+
+
 split_headers(H) ->
     re:split(H, ",\\s*", [{return,list}, trim]).

http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/20e437a4/test/chttpd_cors_test.erl
----------------------------------------------------------------------
diff --git a/test/chttpd_cors_test.erl b/test/chttpd_cors_test.erl
index 12f2d6b..6ad807a 100644
--- a/test/chttpd_cors_test.erl
+++ b/test/chttpd_cors_test.erl
@@ -43,7 +43,7 @@ simple_cors_config() ->
     [
         {<<"enable_cors">>, true},
         {<<"origins">>, {[
-            {?DEFAULT_ORIGIN, {[]}}
+            {list_to_binary(?DEFAULT_ORIGIN), {[]}}
         ]}}
     ].
 
@@ -52,7 +52,7 @@ wildcard_cors_config() ->
     [
         {<<"enable_cors">>, true},
         {<<"origins">>, {[
-            {"*", {[]}}
+            {<<"*">>, {[]}}
         ]}}
     ].
 
@@ -62,7 +62,7 @@ access_control_cors_config(AllowCredentials) ->
         {<<"enable_cors">>, true},
         {<<"allow_credentials">>, AllowCredentials},
         {<<"origins">>, {[
-            {?DEFAULT_ORIGIN, {[]}}
+            {list_to_binary(?DEFAULT_ORIGIN), {[]}}
         ]}}].
 
 
@@ -70,10 +70,10 @@ multiple_cors_config() ->
     [
         {<<"enable_cors">>, true},
         {<<"origins">>, {[
-            {?DEFAULT_ORIGIN, {[]}},
-            {"https://example.com", {[]}},
-            {"http://example.com:5984", {[]}},
-            {"https://example.com:5984", {[]}}
+            {list_to_binary(?DEFAULT_ORIGIN), {[]}},
+            {<<"https://example.com">>, {[]}},
+            {<<"http://example.com:5984">>, {[]}},
+            {<<"https://example.com:5984">>, {[]}}
         ]}}
     ].
 


Mime
View raw message