couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fdman...@apache.org
Subject svn commit: r1040489 - /couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl
Date Tue, 30 Nov 2010 12:22:17 GMT
Author: fdmanana
Date: Tue Nov 30 12:22:16 2010
New Revision: 1040489

URL: http://svn.apache.org/viewvc?rev=1040489&view=rev
Log:
Backport COUCHDB-918: add support to follow HTTP 303 redirects to the new replicator.

Modified:
    couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl

Modified: couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl?rev=1040489&r1=1040488&r2=1040489&view=diff
==============================================================================
--- couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl (original)
+++ couchdb/branches/new_replicator/src/couchdb/couch_api_wrap_httpc.erl Tue Nov 30 12:22:16
2010
@@ -26,6 +26,7 @@
     ]).
 
 -define(RETRY_LATER_WAIT, 100).
+-define(replace(L, K, V), lists:keystore(K, 1, L, {K, V})).
 
 
 setup(#httpdb{url = Url} = Db) ->
@@ -48,7 +49,7 @@ send_req(#httpdb{headers = BaseHeaders} 
     Headers1 = case (Body =:= [] orelse Body =:= <<>>) andalso
         (Method =:= put orelse Method =:= post) of
     true ->
-        lists:keystore("Content-Length", 1, Headers, {"Content-Length", 0});
+        ?replace(Headers, "Content-Length", 0);
     false ->
         Headers
     end,
@@ -97,8 +98,8 @@ process_response({ok, Code, Headers, Bod
             ?JSON_DECODE(Json)
         end,
         Callback(Ok, Headers, EJson);
-    R when R =:= 301 ; R =:= 302 ->
-        do_redirect(Worker, Headers, HttpDb, Params, Callback);
+    R when R =:= 301 ; R =:= 302 ; R =:= 303 ->
+        do_redirect(Worker, R, Headers, HttpDb, Params, Callback);
     Error ->
         maybe_retry({code, Error}, Worker, HttpDb, Params, Callback)
     end;
@@ -118,8 +119,8 @@ process_stream_response(ReqId, Worker, H
             Ret = Callback(Ok, Headers, StreamDataFun),
             stop_worker(Worker),
             Ret;
-        R when R =:= 301 ; R =:= 302 ->
-            do_redirect(Worker, Headers, HttpDb, Params, Callback);
+        R when R =:= 301 ; R =:= 302 ; R =:= 303 ->
+            do_redirect(Worker, R, Headers, HttpDb, Params, Callback);
         Error ->
             report_error(Worker, HttpDb, Params, {code, Error})
         end;
@@ -228,10 +229,10 @@ oauth_header(#httpdb{url = BaseUrl, oaut
         "OAuth " ++ oauth_uri:params_to_header_string(OAuthParams)}].
 
 
-do_redirect(Worker, RespHeaders, #httpdb{url = Url} = HttpDb, Params, Cb) ->
+do_redirect(Worker, Code, Headers, #httpdb{url = Url} = HttpDb, Params, Cb) ->
     stop_worker(Worker),
-    RedirectUrl = redirect_url(RespHeaders, Url),
-    {HttpDb2, Params2} = after_redirect(RedirectUrl, HttpDb, Params),
+    RedirectUrl = redirect_url(Headers, Url),
+    {HttpDb2, Params2} = after_redirect(RedirectUrl, Code, HttpDb, Params),
     send_req(HttpDb2, Params2, Cb).
 
 
@@ -257,6 +258,11 @@ redirect_url(RespHeaders, OrigUrl) ->
     atom_to_list(Proto) ++ "://" ++ Creds ++ Base ++ ":" ++
         integer_to_list(Port) ++ Path.
 
+after_redirect(RedirectUrl, 303, HttpDb, Params) ->
+    after_redirect(RedirectUrl, HttpDb, ?replace(Params, method, get));
+after_redirect(RedirectUrl, _Code, HttpDb, Params) ->
+    after_redirect(RedirectUrl, HttpDb, Params).
+
 after_redirect(RedirectUrl, HttpDb, Params) ->
     Params2 = lists:keydelete(path, 1, lists:keydelete(qs, 1, Params)),
     {HttpDb#httpdb{url = RedirectUrl}, Params2}.



Mime
View raw message