Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/adapter/httpc.ex b/lib/tesla/adapter/httpc.ex
index 1f99bb4..1f7dff5 100644
--- a/lib/tesla/adapter/httpc.ex
+++ b/lib/tesla/adapter/httpc.ex
@@ -1,47 +1,63 @@
defmodule Tesla.Adapter.Httpc do
@moduledoc """
Adapter for `:httpc`
**NOTE** Tesla overrides default autoredirect value with false to ensure
consistency between adapters
"""
@override_defaults autoredirect: false
@http_opts ~w(timeout connect_timeout ssl essl autoredirect proxy_auth version relaxed url_encode)a
def call(env, opts) do
opts = Keyword.merge(@override_defaults, opts || [])
with {:ok, {status, headers, body}} <- request(env, opts) do
format_response(env, status, headers, body)
end
end
defp format_response(env, {_, status, _}, headers, body) do
%{env | status: status,
headers: headers,
body: body}
end
defp request(env, opts) do
content_type = to_char_list(env.headers["content-type"] || "")
handle request(
env.method || :get,
Tesla.build_url(env.url, env.query) |> to_char_list,
Enum.into(env.headers, [], fn {k,v} -> {to_char_list(k), to_char_list(v)} end),
content_type,
env.body,
Keyword.split(opts ++ env.opts, @http_opts)
)
end
defp request(method, url, headers, _content_type, nil, {http_opts, opts}) do
:httpc.request(method, {url, headers}, http_opts, opts)
end
+ defp request(method, url, headers, content_type, %Stream{} = body, opts) do
+ reductor = fn(item, _acc) -> {:suspend, item} end
+ {_, _, fun} = Enumerable.reduce(body, {:suspend, nil}, reductor)
+ request(method, url, headers, content_type, fun, opts)
+ end
+
+ defp request(method, url, headers, content_type, body, opts) when is_function(body) do
+ body = {:chunkify, &next_chunk/1, body}
+ request(method, url, headers, content_type, body, opts)
+ end
+
defp request(method, url, headers, content_type, body, {http_opts, opts}) do
:httpc.request(method, {url, headers, content_type, body}, http_opts, opts)
end
+ defp next_chunk(fun), do: parse_chunk fun.({:cont, nil})
+
+ defp parse_chunk({:suspended, item, fun}), do: {:ok, item, fun}
+ defp parse_chunk(_), do: :eof
+
defp handle({:error, {:failed_connect, _}}), do: {:error, :econnrefused}
defp handle(response), do: response
end
diff --git a/test/tesla/adapter/httpc_test.exs b/test/tesla/adapter/httpc_test.exs
index f1dd9e6..6f773ea 100644
--- a/test/tesla/adapter/httpc_test.exs
+++ b/test/tesla/adapter/httpc_test.exs
@@ -1,5 +1,6 @@
defmodule HttpcTest do
use ExUnit.Case
use Tesla.Adapter.TestCase.Basic, adapter: :httpc
+ use Tesla.Adapter.TestCase.StreamRequestBody, adapter: :httpc
use Tesla.Adapter.TestCase.SSL, adapter: :httpc
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 5:13 PM (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55341
Default Alt Text
(2 KB)

Event Timeline