@@ -806,7 +806,8 @@ handle_request(Method, Url,
806
806
request_options = Options },
807
807
case httpc_manager :request (Request , profile_name (Profile )) of
808
808
{ok , RequestId } ->
809
- handle_answer (RequestId , Sync , Options );
809
+ handle_answer (RequestId , Receiver , Sync , Options ,
810
+ element (# http_options .timeout , HTTPOptions ));
810
811
{error , Reason } ->
811
812
{error , Reason }
812
813
end
@@ -862,20 +863,41 @@ mk_chunkify_fun(ProcessBody) ->
862
863
end .
863
864
864
865
865
- handle_answer (RequestId , false , _ ) ->
866
+ handle_answer (RequestId , _ , false , _ , _ ) ->
866
867
{ok , RequestId };
867
- handle_answer (RequestId , true , Options ) ->
868
+ handle_answer (RequestId , ClientAlias , true , Options , Timeout ) ->
868
869
receive
869
870
{http , {RequestId , {ok , saved_to_file }}} ->
871
+ unalias (ClientAlias ),
870
872
{ok , saved_to_file };
871
873
{http , {RequestId , {error , Reason }}} ->
874
+ unalias (ClientAlias ),
872
875
{error , Reason };
873
876
{http , {RequestId , {ok , {StatusLine ,Headers ,BinBody }}}} ->
877
+ unalias (ClientAlias ),
874
878
Body = maybe_format_body (BinBody , Options ),
875
879
{ok , {StatusLine , Headers , Body }};
876
880
{http , {RequestId , {ok , {StatusCode ,BinBody }}}} ->
881
+ unalias (ClientAlias ),
877
882
Body = maybe_format_body (BinBody , Options ),
878
883
{ok , {StatusCode , Body }}
884
+ after Timeout ->
885
+ cancel_request (RequestId ),
886
+ unalias (ClientAlias ),
887
+ receive
888
+ {http , {RequestId , {ok , saved_to_file }}} ->
889
+ {ok , saved_to_file };
890
+ {http , {RequestId , {error , Reason }}} ->
891
+ {error , Reason };
892
+ {http , {RequestId , {ok , {StatusLine ,Headers ,BinBody }}}} ->
893
+ Body = maybe_format_body (BinBody , Options ),
894
+ {ok , {StatusLine , Headers , Body }};
895
+ {http , {RequestId , {ok , {StatusCode ,BinBody }}}} ->
896
+ Body = maybe_format_body (BinBody , Options ),
897
+ {ok , {StatusCode , Body }}
898
+ after 0 ->
899
+ {error , timeout }
900
+ end
879
901
end .
880
902
881
903
maybe_format_body (BinBody , Options ) ->
@@ -1064,6 +1086,8 @@ request_options_defaults() ->
1064
1086
ok ;
1065
1087
(Value ) when is_function (Value , 1 ) ->
1066
1088
ok ;
1089
+ (Value ) when is_reference (Value ) ->
1090
+ ok ;
1067
1091
(_ ) ->
1068
1092
error
1069
1093
end ,
@@ -1085,7 +1109,7 @@ request_options_defaults() ->
1085
1109
{body_format , string , VerifyBodyFormat },
1086
1110
{full_result , true , VerifyFullResult },
1087
1111
{headers_as_is , false , VerifyHeaderAsIs },
1088
- {receiver , self (), VerifyReceiver },
1112
+ {receiver , alias (), VerifyReceiver },
1089
1113
{socket_opts , undefined , VerifySocketOpts },
1090
1114
{ipv6_host_with_brackets , false , VerifyBrackets }
1091
1115
].
@@ -1139,6 +1163,7 @@ request_options([{Key, DefaultVal, Verify} | Defaults], Options, Acc) ->
1139
1163
BodyFormat :: string () | binary () | atom (),
1140
1164
SocketOpt :: term (),
1141
1165
Receiver :: pid ()
1166
+ | reference ()
1142
1167
| fun ((term ()) -> term ())
1143
1168
| { ReceiverModule :: atom ()
1144
1169
, ReceiverFunction :: atom ()
@@ -1149,6 +1174,8 @@ request_options_sanity_check(Opts) ->
1149
1174
case proplists :get_value (receiver , Opts ) of
1150
1175
Pid when is_pid (Pid ) andalso (Pid =:= self ()) ->
1151
1176
ok ;
1177
+ Reference when is_reference (Reference ) ->
1178
+ ok ;
1152
1179
BadReceiver ->
1153
1180
throw ({error , {bad_options_combo ,
1154
1181
[{sync , true }, {receiver , BadReceiver }]}})
0 commit comments