1
diff --git a/couchdb-1.0.1/src/couchdb/couch_rep_changes_feed.erl b/couchdb-1.0.1/src/couchdb/couch_rep_changes_feed.erl
2
index 2d55778..958ee82 100644
3
--- a/couchdb-1.0.1/src/couchdb/couch_rep_changes_feed.erl
4
+++ b/couchdb-1.0.1/src/couchdb/couch_rep_changes_feed.erl
5
@@ -45,7 +45,7 @@ next(Server) ->
7
gen_server:call(Server, stop).
9
-init([_Parent, #http_db{}=Source, Since, PostProps] = Args) ->
10
+init([Parent, #http_db{}=Source, Since, PostProps]) ->
11
process_flag(trap_exit, true),
12
Feed = case couch_util:get_value(<<"continuous">>, PostProps, false) of
14
@@ -88,6 +88,7 @@ init([_Parent, #http_db{}=Source, Since, PostProps] = Args) ->
15
headers = Source#http_db.headers -- [{"Accept-Encoding", "gzip"}]
17
{ibrowse_req_id, ReqId} = couch_rep_httpc:request(Req),
18
+ Args = [Parent, Req, Since, PostProps],
21
{ibrowse_async_headers, ReqId, "200", _} ->
22
@@ -96,10 +97,13 @@ init([_Parent, #http_db{}=Source, Since, PostProps] = Args) ->
23
{ibrowse_async_headers, ReqId, Code, Hdrs} when Code=="301"; Code=="302" ->
24
catch ibrowse:stop_worker_process(Pid),
25
Url2 = couch_rep_httpc:redirect_url(Hdrs, Req#http_db.url),
26
- %% TODO use couch_httpc:request instead of start_http_request
27
- {Pid2, ReqId2} = start_http_request(Url2),
28
+ Req2 = couch_rep_httpc:redirected_request(Req, Url2),
29
+ Pid2 = couch_rep_httpc:spawn_link_worker_process(Req2),
30
+ Req3 = Req2#http_db{conn = Pid2},
31
+ {ibrowse_req_id, ReqId2} = couch_rep_httpc:request(Req3),
32
+ Args2 = [Parent, Req3, Since, PostProps],
33
receive {ibrowse_async_headers, ReqId2, "200", _} ->
34
- {ok, #state{conn=Pid2, last_seq=Since, reqid=ReqId2, init_args=Args}}
35
+ {ok, #state{conn=Pid2, last_seq=Since, reqid=ReqId2, init_args=Args2}}
37
{stop, changes_timeout}
39
@@ -258,13 +262,17 @@ handle_next_changes(_From, State) ->
40
handle_headers(200, _, State) ->
41
maybe_stream_next(State),
43
-handle_headers(301, Hdrs, #state{init_args = InitArgs} = State) ->
44
+handle_headers(Code, Hdrs, #state{init_args = InitArgs} = State)
45
+ when Code =:= 301 ; Code =:= 302 ->
46
catch ibrowse:stop_worker_process(State#state.conn),
47
- [_, #http_db{url = Url1} | _] = InitArgs,
48
+ [Parent, #http_db{url = Url1} = Source, Since, PostProps] = InitArgs,
49
Url = couch_rep_httpc:redirect_url(Hdrs, Url1),
50
- %% TODO use couch_httpc:request instead of start_http_request
51
- {Pid, ReqId} = start_http_request(Url),
52
- {noreply, State#state{conn=Pid, reqid=ReqId}};
53
+ Source2 = couch_rep_httpc:redirected_request(Source, Url),
54
+ Pid2 = couch_rep_httpc:spawn_link_worker_process(Source2),
55
+ Source3 = Source2#http_db{conn = Pid2},
56
+ {ibrowse_req_id, ReqId} = couch_rep_httpc:request(Source3),
57
+ InitArgs2 = [Parent, Source3, Since, PostProps],
58
+ {noreply, State#state{conn=Pid2, reqid=ReqId, init_args=InitArgs2}};
59
handle_headers(Code, Hdrs, State) ->
60
?LOG_ERROR("replicator changes feed failed with code ~s and Headers ~n~p",
62
@@ -374,15 +382,3 @@ maybe_stream_next(#state{complete=false, count=N} = S) when N < ?BUFFER_SIZE ->
63
ibrowse:stream_next(S#state.reqid);
64
maybe_stream_next(_) ->
65
timer:cancel(get(timeout)).
67
-start_http_request(RawUrl) ->
68
- Url = ibrowse_lib:parse_url(RawUrl),
69
- {ok, Pid} = ibrowse:spawn_link_worker_process(Url#url.host, Url#url.port),
71
- {stream_to, {self(), once}},
72
- {inactivity_timeout, 31000},
73
- {response_format, binary}
75
- {ibrowse_req_id, Id} =
76
- ibrowse:send_req_direct(Pid, RawUrl, [], get, [], Opts, infinity),