@@ -806,7 +806,8 @@ handle_request(Method, Url,
806806 request_options = Options },
807807 case httpc_manager :request (Request , profile_name (Profile )) of
808808 {ok , RequestId } ->
809- handle_answer (RequestId , Sync , Options );
809+ handle_answer (RequestId , Receiver , Sync , Options ,
810+ element (# http_options .timeout , HTTPOptions ));
810811 {error , Reason } ->
811812 {error , Reason }
812813 end
@@ -862,20 +863,41 @@ mk_chunkify_fun(ProcessBody) ->
862863 end .
863864
864865
865- handle_answer (RequestId , false , _ ) ->
866+ handle_answer (RequestId , _ , false , _ , _ ) ->
866867 {ok , RequestId };
867- handle_answer (RequestId , true , Options ) ->
868+ handle_answer (RequestId , ClientAlias , true , Options , Timeout ) ->
868869 receive
869870 {http , {RequestId , {ok , saved_to_file }}} ->
871+ unalias (ClientAlias ),
870872 {ok , saved_to_file };
871873 {http , {RequestId , {error , Reason }}} ->
874+ unalias (ClientAlias ),
872875 {error , Reason };
873876 {http , {RequestId , {ok , {StatusLine ,Headers ,BinBody }}}} ->
877+ unalias (ClientAlias ),
874878 Body = maybe_format_body (BinBody , Options ),
875879 {ok , {StatusLine , Headers , Body }};
876880 {http , {RequestId , {ok , {StatusCode ,BinBody }}}} ->
881+ unalias (ClientAlias ),
877882 Body = maybe_format_body (BinBody , Options ),
878883 {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
879901 end .
880902
881903maybe_format_body (BinBody , Options ) ->
@@ -1064,6 +1086,8 @@ request_options_defaults() ->
10641086 ok ;
10651087 (Value ) when is_function (Value , 1 ) ->
10661088 ok ;
1089+ (Value ) when is_reference (Value ) ->
1090+ ok ;
10671091 (_ ) ->
10681092 error
10691093 end ,
@@ -1085,7 +1109,7 @@ request_options_defaults() ->
10851109 {body_format , string , VerifyBodyFormat },
10861110 {full_result , true , VerifyFullResult },
10871111 {headers_as_is , false , VerifyHeaderAsIs },
1088- {receiver , self (), VerifyReceiver },
1112+ {receiver , alias (), VerifyReceiver },
10891113 {socket_opts , undefined , VerifySocketOpts },
10901114 {ipv6_host_with_brackets , false , VerifyBrackets }
10911115 ].
@@ -1139,6 +1163,7 @@ request_options([{Key, DefaultVal, Verify} | Defaults], Options, Acc) ->
11391163 BodyFormat :: string () | binary () | atom (),
11401164 SocketOpt :: term (),
11411165 Receiver :: pid ()
1166+ | reference ()
11421167 | fun ((term ()) -> term ())
11431168 | { ReceiverModule :: atom ()
11441169 , ReceiverFunction :: atom ()
@@ -1149,6 +1174,8 @@ request_options_sanity_check(Opts) ->
11491174 case proplists :get_value (receiver , Opts ) of
11501175 Pid when is_pid (Pid ) andalso (Pid =:= self ()) ->
11511176 ok ;
1177+ Reference when is_reference (Reference ) ->
1178+ ok ;
11521179 BadReceiver ->
11531180 throw ({error , {bad_options_combo ,
11541181 [{sync , true }, {receiver , BadReceiver }]}})
0 commit comments