Skip to content

Instantly share code, notes, and snippets.

@keith-miller
Last active December 16, 2015 14:29
Show Gist options
  • Save keith-miller/5449287 to your computer and use it in GitHub Desktop.
Save keith-miller/5449287 to your computer and use it in GitHub Desktop.
Fixed the missing first chunk problem. This is working.
fire_http_request (URL, Method, ReqData, State) ->
%% ... prep code here
Options = [{connect_timeout, TimeoutMs},
{max_sessions, MaxConn},
{response_format, binary},
{stream_to, {self(), once}}],
case ibrowse:send_req(URL, ReqLHeadersHost, Method, ReqLBody, Options) of
{ibrowse_req_id, ReqId} ->
case ibrowse:stream_next(ReqId) of
ok ->
handle_stream_response(ReqId, ReqData, State);
Err ->
io:format("Server error! ~p~n", [Err]),
{{halt, {500, "Internal server error"}}, ReqData, State}
end;
Err ->
io:format("Server error! ~p~n", [Err]),
{{halt, {500, "Internal server error"}}, ReqData, State}
end.
handle_stream_response(ReqId, ReqData, State) ->
receive
{ibrowse_async_headers, ReqId, StatCode, Headers} ->
%% Got the headers, need to add them to the reponse
io:format("Got headers, starting streaming ~n"),
ResWHeaders = my_http_util:headers_l_to_m(Headers),
ReqDataWHeaders =
my_http_util:wrq_replace_resp_headers (ResWHeaders, ReqData),
%% Don't want to duplicate header
FixedReqData = wrq:remove_resp_header("Transfer-Encoding", ReqDataWHeaders),
%% Turn the Status Code into a Integer
SCode = case string:to_integer(StatCode) of
{error, Error} ->
io:format("Server error, incorrect Status Code! ~p~n", [Error]),
0;
{Int, _} ->
Int
end,
case SCode of
0 ->
{{halt, {500, "Internal server error"}}, ReqData, State};
_ ->
case ibrowse:stream_next(ReqId) of
ok ->
{true, wrq:set_resp_body({stream, return_chunks(ReqId)},
FixedReqData), State};
Err ->
io:format("Server error! ~p~n", [Err]),
{{halt, {500, "Internal server error"}}, ReqData, State}
end
end
end.
return_chunks(ReqId) ->
return_chunks(ReqId, <<>>).
return_chunks(ReqId, Body) ->
receive
{ibrowse_async_response, ReqId, {error, Err}} ->
%% There was an error during the transport
io:format("Server error! ~p~n", [Err]),
{Body, done};
{ibrowse_async_response, ReqId, Hunk} ->
case ibrowse:stream_next(ReqId) of
ok ->
{Hunk,
fun() ->
return_chunks(ReqId, Body)
end};
Err ->
%% shouldn't happen in a normal transport
io:format("Server error! ~p~n", [Err]),
{Body, done}
end;
{ibrowse_async_response_end, ReqId} ->
%% end of the transport
io:format("Streaming end ~n"),
{Body, done}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment