@@ -212,12 +212,12 @@ defmodule HTTP do
212212 | { { :http_version , integer ( ) , String . t ( ) } , [ { atom ( ) | String . t ( ) , String . t ( ) } ] ,
213213 binary ( ) }
214214
215-
216215 defp handle_response ( request_id , url ) do
217216 receive do
218217 { :http , { ^ request_id , response_from_httpc } } ->
219218 response = handle_httpc_response ( response_from_httpc , url )
220219 { :ok , response }
220+
221221 _ ->
222222 throw ( :request_interrupted_or_unexpected_message )
223223 after
@@ -238,23 +238,23 @@ defmodule HTTP do
238238 try do
239239 case Request . to_httpc_args ( request ) do
240240 [ method , request_tuple , options , client_options ] ->
241- # Configure httpc options
242- httpc_options = Keyword . put ( options , :body_format , :binary )
243-
244- # Send the request and get the RequestId (PID of the httpc client process)
245- case :httpc . request ( method , request_tuple , httpc_options , client_options ) do
246- { :ok , request_id } ->
247- # If an AbortController was provided, link it to this request_id
248- if abort_controller_pid && is_pid ( abort_controller_pid ) do
249- HTTP.AbortController . set_request_id ( abort_controller_pid , request_id )
250- end
241+ # Configure httpc options - body_format should be in client_opts (4th arg)
242+ httpc_client_opts = Keyword . put ( client_options , :body_format , :binary )
251243
252- # Handle response (simplified - streaming handled in handle_httpc_response)
253- handle_response ( request_id , request . url )
244+ # Send the request and get the RequestId (PID of the httpc client process)
245+ case :httpc . request ( method , request_tuple , options , httpc_client_opts ) do
246+ { :ok , request_id } ->
247+ # If an AbortController was provided, link it to this request_id
248+ if abort_controller_pid && is_pid ( abort_controller_pid ) do
249+ HTTP.AbortController . set_request_id ( abort_controller_pid , request_id )
250+ end
254251
255- { :error , reason } ->
256- throw ( reason )
257- end
252+ # Handle response (simplified - streaming handled in handle_httpc_response)
253+ handle_response ( request_id , request . url )
254+
255+ { :error , reason } ->
256+ throw ( reason )
257+ end
258258
259259 # Fallback for unexpected return from Request.to_httpc_args
260260 other_args ->
@@ -284,7 +284,14 @@ defmodule HTTP do
284284 if should_stream do
285285 # Create a streaming process
286286 { :ok , stream_pid } = start_httpc_stream_process ( url , response_headers )
287- % Response { status: status , headers: response_headers , body: nil , url: url , stream: stream_pid }
287+
288+ % Response {
289+ status: status ,
290+ headers: response_headers ,
291+ body: nil ,
292+ url: url ,
293+ stream: stream_pid
294+ }
288295 else
289296 # Non-streaming response - handle as before
290297 binary_body =
@@ -293,7 +300,14 @@ defmodule HTTP do
293300 else
294301 body
295302 end
296- % Response { status: status , headers: response_headers , body: binary_body , url: url , stream: nil }
303+
304+ % Response {
305+ status: status ,
306+ headers: response_headers ,
307+ body: binary_body ,
308+ url: url ,
309+ stream: nil
310+ }
297311 end
298312
299313 { :error , reason } ->
@@ -308,14 +322,17 @@ defmodule HTTP do
308322 # Stream responses larger than 100KB or when content-length is unknown
309323 case Integer . parse ( content_length || "" ) do
310324 { size , _ } when size > 100_000 -> true
311- _ -> content_length == nil # Stream when size is unknown
325+ # Stream when size is unknown
326+ _ -> content_length == nil
312327 end
313328 end
314329
315330 defp start_httpc_stream_process ( url , headers ) do
316- { :ok , pid } = Task . start_link ( fn ->
317- stream_httpc_response ( url , headers )
318- end )
331+ { :ok , pid } =
332+ Task . start_link ( fn ->
333+ stream_httpc_response ( url , headers )
334+ end )
335+
319336 { :ok , pid }
320337 end
321338
@@ -325,21 +342,22 @@ defmodule HTTP do
325342 _host = uri . host
326343 _port = uri . port || 80
327344 _path = uri . path || "/"
328-
345+
329346 # Build headers for the request
330- request_headers =
347+ request_headers =
331348 headers . headers
332349 |> Enum . map ( fn { name , value } -> { String . to_charlist ( name ) , String . to_charlist ( value ) } end )
333-
350+
334351 # Start the HTTP request with streaming
335352 case :httpc . request (
336- :get ,
337- { String . to_charlist ( url ) , request_headers } ,
338- [ ] ,
339- [ sync: false , body_format: :binary ]
340- ) do
353+ :get ,
354+ { String . to_charlist ( url ) , request_headers } ,
355+ [ ] ,
356+ sync: false
357+ ) do
341358 { :ok , request_id } ->
342359 stream_loop ( request_id , self ( ) )
360+
343361 { :error , reason } ->
344362 send ( self ( ) , { :stream_error , self ( ) , reason } )
345363 end
@@ -349,30 +367,29 @@ defmodule HTTP do
349367 receive do
350368 { :http , { ^ request_id , { :http_response , _http_version , _status , _reason } } } ->
351369 stream_loop ( request_id , caller )
352-
370+
353371 { :http , { ^ request_id , { :http_header , _ , _header_name , _ , _header_value } } } ->
354372 stream_loop ( request_id , caller )
355-
373+
356374 { :http , { ^ request_id , :http_eoh } } ->
357375 stream_loop ( request_id , caller )
358-
376+
359377 { :http , { ^ request_id , { :http_error , reason } } } ->
360378 send ( caller , { :stream_error , self ( ) , reason } )
361-
379+
362380 { :http , { ^ request_id , :stream_end } } ->
363381 send ( caller , { :stream_end , self ( ) } )
364-
382+
365383 { :http , { ^ request_id , { :http_chunk , chunk } } } ->
366384 send ( caller , { :stream_chunk , self ( ) , to_string ( chunk ) } )
367385 stream_loop ( request_id , caller )
368-
386+
369387 { :http , { ^ request_id , { :http_body , body } } } ->
370388 send ( caller , { :stream_chunk , self ( ) , to_string ( body ) } )
371389 send ( caller , { :stream_end , self ( ) } )
372-
373- after 60_000 ->
390+ after
391+ 60_000 ->
374392 send ( caller , { :stream_error , self ( ) , :timeout } )
375393 end
376394 end
377-
378395end
0 commit comments