summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/couchdb/couch_rep.erl5
-rw-r--r--src/couchdb/couch_rep_httpc.erl17
2 files changed, 13 insertions, 9 deletions
diff --git a/src/couchdb/couch_rep.erl b/src/couchdb/couch_rep.erl
index 8805577e..fa4d33bd 100644
--- a/src/couchdb/couch_rep.erl
+++ b/src/couchdb/couch_rep.erl
@@ -416,10 +416,7 @@ open_db({Props}, _UserCtx) ->
auth = AuthProps,
headers = lists:ukeymerge(1, Headers, DefaultHeaders)
},
- case couch_rep_httpc:db_exists(Db) of
- true -> Db;
- false -> throw({db_not_found, ?l2b(Url)})
- end;
+ couch_rep_httpc:db_exists(Db);
open_db(<<"http://",_/binary>>=Url, _) ->
open_db({[{<<"url">>,Url}]}, []);
open_db(<<"https://",_/binary>>=Url, _) ->
diff --git a/src/couchdb/couch_rep_httpc.erl b/src/couchdb/couch_rep_httpc.erl
index fdc640dd..9e8bfb42 100644
--- a/src/couchdb/couch_rep_httpc.erl
+++ b/src/couchdb/couch_rep_httpc.erl
@@ -56,20 +56,27 @@ do_request(Req) ->
process_response(Resp, Req).
db_exists(Req) ->
+ db_exists(Req, Req#http_db.url).
+
+db_exists(Req, CanonicalUrl) ->
#http_db{
url = Url,
headers = Headers
} = Req,
case catch ibrowse:send_req(Url, Headers, head) of
{ok, "200", _, _} ->
- true;
- {ok, "301", Headers, _} ->
- MochiHeaders = mochiweb_headers:make(Headers),
+ Req#http_db{url = CanonicalUrl};
+ {ok, "301", RespHeaders, _} ->
+ MochiHeaders = mochiweb_headers:make(RespHeaders),
+ RedirectUrl = mochiweb_headers:get_value("Location", MochiHeaders),
+ db_exists(Req#http_db{url = RedirectUrl}, RedirectUrl);
+ {ok, "302", RespHeaders, _} ->
+ MochiHeaders = mochiweb_headers:make(RespHeaders),
RedirectUrl = mochiweb_headers:get_value("Location", MochiHeaders),
- db_exists(Req#http_db{url = RedirectUrl});
+ db_exists(Req#http_db{url = RedirectUrl}, CanonicalUrl);
Error ->
?LOG_DEBUG("DB at ~s could not be found because ~p", [Url, Error]),
- false
+ throw({db_not_found, ?l2b(Url)})
end.
full_url(#http_db{url=Url} = Req) when is_binary(Url) ->