anchor.diff:      do_http_request(Url, Action, Headers, []).
   
   do_http_request(Url, Action, Headers, JsonBody) ->
  -    do_http_request(Url, Action, Headers, JsonBody, 10).
  -
  -do_http_request(Url, Action, Headers, Body, Retries) when is_binary(Url) ->
  -    do_http_request(?b2l(Url), Action, Headers, Body, Retries);
  -do_http_request(Url, Action, _Headers, _JsonBody, 0) ->
  +    do_http_request(Url, Action, Headers, JsonBody, 10, 1000).
  +
  +do_http_request(Url, Action, Headers, Body, Retries, Pause) when is_binary(Url) ->
  +    do_http_request(?b2l(Url), Action, Headers, Body, Retries, Pause);
  +do_http_request(Url, Action, _Headers, _JsonBody, 0, _Pause) ->
       ?LOG_ERROR("couch_rep HTTP ~p request failed after 10 retries: ~s", 
           [Action, Url]),
       exit({http_request_failed, ?l2b(["failed to replicate ", Url])});
  -do_http_request(Url, Action, Headers, JsonBody, Retries) ->
  +do_http_request(Url, Action, Headers, JsonBody, Retries, Pause) ->
       ?LOG_DEBUG("couch_rep HTTP ~p request: ~s", [Action, Url]),
       Body =
       case JsonBody of
  
positive.code:     Opts = [{stream_to, Pid}, {response_format, binary}],
      ReqId = 
      case ibrowse:send_req_direct(Conn, Url, Headers, get, [], Opts, infinity) of
          {ibrowse_req_id, X} -> X;
          {error, Reason} ->
              exit({attachment_request_failed,
                  ?l2b(["ibrowse error on ", Url, " : ", atom_to_list(Reason)])})
      end,
      
      %% tell our receiver about the ReqId it needs to look for
      Pid ! {self(), {set_req_id, ReqId}},
      receive {Pid, {ok, ReqId}} -> ok end,
      
      %% wait for headers to ensure that we have a 200 status code
      %% this is where we follow redirects etc
      Pid ! {self(), gimme_status}, 
      receive
      {'EXIT', Pid, attachment_request_failed} ->
          make_attachment_stub_receiver(Url, Headers, Name, Type, Length, Retries-1);
      {Pid, {status, StreamStatus, StreamHeaders}} -> 
          ?LOG_DEBUG("streaming attachment Status ~p Headers ~p",
              [StreamStatus, StreamHeaders]),
  
negative.code:             exit(attachment_request_failed);
          ResponseCode == 500 ->
              % an error... log and retry
              ?LOG_INFO("retrying couch_rep attachment request in ~p " ++ 
                  "seconds due to 500 response: ~s", [Pause/1000, Url]),
              Pid ! {self(), fail},
              catch ibrowse:stop_worker_process(Conn),
              timer:sleep(Pause),
              make_attachment_stub_receiver(Url, Headers, Name, Type, Length, 
                  Retries - 1, 2*Pause)
          end
      end.
  
  