Permalink
Browse files

send headers through to service, requires my cowboy_http_req fork

  • Loading branch information...
1 parent 31061fc commit 16d33fd1bb515870a6e1c9ce23adc19949c3cbf9 @tsloughter committed Jul 10, 2012
@@ -23,12 +23,12 @@ handle(Req, State) ->
{ok, Host, Port} ->
{Method, Req3} = cowboy_http_req:method(Req2),
{Path, Req4} = cowboy_http_req:path(Req3),
- {_Headers, Req5} = cowboy_http_req:headers(Req4),
+ {Headers, Req5} = cowboy_http_req:headers(Req4),
{ok, Body, Req6} = cowboy_http_req:body(Req5),
%% make request
{RespStatus, RespHeaders, RespBody} =
- dwight_core_req_server:send(atom_to_list(Method), Host, Port, Path, Body),
+ dwight_core_req_server:send(atom_to_list(Method), Host, Port, Headers, Path, Body),
{ok, Req7} = cowboy_http_req:set_resp_body(RespBody, Req6),
{ok, Req8} = cowboy_http_req:reply(RespStatus, RespHeaders, Req7),
@@ -22,11 +22,12 @@ init({tcp, http}, Req, _Opts) ->
{ok, Host, Port} ->
{Method, Req3} = cowboy_http_req:method(Req2),
{Path, Req4} = cowboy_http_req:path(Req3),
- {_Headers, Req5} = cowboy_http_req:headers(Req4),
+ {Headers, Req5} = cowboy_http_req:headers(Req4),
{ok, Body, Req6} = cowboy_http_req:body(Req5),
%% make async request
- dwight_core_req_server:send_async(self(), atom_to_list(Method), Host, Port, Path, Body),
+ dwight_core_req_server:send_async(self(), atom_to_list(Method), Host,
+ Port, Headers, Path, Body),
{loop, Req6, #state{}, ?TIMEOUT, hibernate};
undefined ->
@@ -13,9 +13,9 @@
%% API
-export([start_link/2,
close/1,
- send/5,
send/6,
- send_async/6]).
+ send/7,
+ send_async/7]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -32,14 +32,14 @@
start_link(Host, Port) ->
gen_server:start_link(?MODULE, [Host, Port], []).
-send(Pid, Method, Host, Port, Path, Body) ->
- gen_server:call(Pid, {Method, Host, Port, Path, Body}).
+send(Pid, Method, Host, Port, Headers, Path, Body) ->
+ gen_server:call(Pid, {Method, Host, Port, Headers, Path, Body}).
-send(Method, Host, Port, Path, Body) ->
- gen_server:call(get_client(Host, Port), {Method, Host, Port, Path, Body}).
+send(Method, Host, Port, Headers, Path, Body) ->
+ gen_server:call(get_client(Host, Port), {Method, Host, Port, Headers, Path, Body}).
-send_async(From, Method, Host, Port, Path, Body) ->
- gen_server:cast(get_client(Host, Port), {From, Method, Host, Port, Path, Body}).
+send_async(From, Method, Host, Port, Headers, Path, Body) ->
+ gen_server:cast(get_client(Host, Port), {From, Method, Host, Port, Headers, Path, Body}).
close(Pid) ->
gen_server:call(Pid, close).
@@ -59,19 +59,19 @@ init([Host, Port]) ->
handle_call(close, _From, State=#state{client=Client}) ->
{ok, Client2} = cowboy_client:close(Client),
{reply, ok, State#state{client=Client2}};
-handle_call({Method, Host, Port, Path, Body}, _From, State=#state{client=Client}) ->
- {ok, Status, Headers, RespBody, Client2} =
- send_request(Client, Method, Host, Port, Path, Body),
+handle_call({Method, Host, Port, Headers, Path, Body}, _From, State=#state{client=Client}) ->
+ {ok, Status, RespHeaders, RespBody, Client2} =
+ send_request(Client, Method, Host, Port, Headers, Path, Body),
- {reply, {Status, Headers, RespBody}, State#state{client=Client2}}.
+ {reply, {Status, RespHeaders, RespBody}, State#state{client=Client2}}.
%%--------------------------------------------------------------------
-handle_cast({From, Method, Host, Port, Path, Body}, State=#state{client=Client}) ->
- {ok, Status, Headers, RespBody, Client2} =
- send_request(Client, Method, Host, Port, Path, Body),
+handle_cast({From, Method, Host, Port, Headers, Path, Body}, State=#state{client=Client}) ->
+ {ok, Status, RespHeaders, RespBody, Client2} =
+ send_request(Client, Method, Host, Port, Headers, Path, Body),
- From ! {reply, Status, Headers, RespBody},
+ From ! {reply, Status, RespHeaders, RespBody},
{noreply, State#state{client=Client2}}.
@@ -104,9 +104,11 @@ get_client(Host, Port) ->
Pid
end.
-send_request(Client, Method, Host, Port, Path, _Body) ->
+send_request(Client, Method, Host, Port, Headers, Path, _Body) ->
Url = list_to_binary(lists:flatten(io_lib:format("http://~s:~p/~s", [Host, Port, Path]))),
- {ok, Client2} = cowboy_client:request(Method, Url, Client),
+ BinHeaders = [{cowboy_http_req:header_to_binary(H), V} || {H, V} <- Headers],
+
+ {ok, Client2} = cowboy_client:request(Method, Url, BinHeaders, Client),
{ok, Status, Response, Client3} = cowboy_client:response(Client2),
{ok, RespBody, Client4} = cowboy_client:response_body(Client3),

0 comments on commit 16d33fd

Please sign in to comment.