Last active
December 16, 2015 14:29
-
-
Save keith-miller/5449287 to your computer and use it in GitHub Desktop.
Fixed the missing first chunk problem. This is working.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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