Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140110
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/adapter/httpc.ex b/lib/tesla/adapter/httpc.ex
index 1f7dff5..4dea458 100644
--- a/lib/tesla/adapter/httpc.ex
+++ b/lib/tesla/adapter/httpc.ex
@@ -1,63 +1,59 @@
defmodule Tesla.Adapter.Httpc do
@moduledoc """
Adapter for `:httpc`
**NOTE** Tesla overrides default autoredirect value with false to ensure
consistency between adapters
"""
+ import Tesla.Adapter.Shared, only: [stream_to_fun: 1, next_chunk: 1]
+
@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)
+ fun = stream_to_fun(body)
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/lib/tesla/adapter/ibrowse.ex b/lib/tesla/adapter/ibrowse.ex
index a65727d..a5afa84 100644
--- a/lib/tesla/adapter/ibrowse.ex
+++ b/lib/tesla/adapter/ibrowse.ex
@@ -1,46 +1,42 @@
if Code.ensure_loaded?(:ibrowse) do
defmodule Tesla.Adapter.Ibrowse do
+ import Tesla.Adapter.Shared, only: [stream_to_fun: 1, next_chunk: 1]
+
def call(env, opts) do
with {:ok, status, headers, body} <- request(env, opts || []) do
%{env | status: status,
headers: headers,
body: body}
end
end
defp request(env, opts) do
body = env.body || []
handle request(
Tesla.build_url(env.url, env.query) |> to_char_list,
Enum.into(env.headers, []),
env.method,
body,
opts
)
end
defp request(url, headers, method, %Stream{} = body, opts) do
- reductor = fn(item, _acc) -> {:suspend, item} end
- {_, _, fun} = Enumerable.reduce(body, {:suspend, nil}, reductor)
+ fun = stream_to_fun(body)
request(url, headers, method, fun, opts)
end
defp request(url, headers, method, body, opts) when is_function(body) do
body = {&next_chunk/1, body}
opts = Keyword.put(opts, :transfer_encoding, :chunked)
request(url, headers, method, body, opts)
end
defp request(url, headers, method, body, opts) do
:ibrowse.send_req(url, headers, method, body, 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, {:conn_failed, error}}), do: error
defp handle(response), do: response
end
end
diff --git a/lib/tesla/adapter/shared.ex b/lib/tesla/adapter/shared.ex
new file mode 100644
index 0000000..ebe7dc8
--- /dev/null
+++ b/lib/tesla/adapter/shared.ex
@@ -0,0 +1,13 @@
+defmodule Tesla.Adapter.Shared do
+ def stream_to_fun(stream) do
+ reductor = fn(item, _acc) -> {:suspend, item} end
+ {_, _, fun} = Enumerable.reduce(stream, {:suspend, nil}, reductor)
+
+ fun
+ end
+
+ def next_chunk(fun), do: parse_chunk fun.({:cont, nil})
+
+ defp parse_chunk({:suspended, item, fun}), do: {:ok, item, fun}
+ defp parse_chunk(_), do: :eof
+end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Jan 19, 12:49 PM (8 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55179
Default Alt Text
(4 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment