THRIFT-305. erlang: Fix http_options for thrift_http_transport
authorDavid Reiss <dreiss@apache.org>
Mon, 30 Mar 2009 20:46:56 +0000 (20:46 +0000)
committerDavid Reiss <dreiss@apache.org>
Mon, 30 Mar 2009 20:46:56 +0000 (20:46 +0000)
The old implementation of thrift_http_transport:set_http_options assumed
that the return value of thrift_transport:new was a pid, but it isn't.
It is a private structure.  Therefore, set_http_options could never work.
Now, thrift_http_transport:new takes an optional third argument,
an assoc-list of options, including http_options.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@760164 13f79535-47bb-0310-9956-ffa450edef68

lib/erl/src/thrift_http_transport.erl

index ee6a864..05a0311 100644 (file)
@@ -11,7 +11,7 @@
 -behaviour(thrift_transport).
 
 %% API
--export([new/2, set_http_options/2]).
+-export([new/2, new/3]).
 
 %% gen_server callbacks
 -export([init/1,
                          path, % string()
                          read_buffer, % iolist()
                          write_buffer, % iolist()
-                         http_options
+                         http_options % see http(3)
                         }).
 
 %%====================================================================
 %% API
 %%====================================================================
 %%--------------------------------------------------------------------
-%% Function: new() -> {ok,Pid} | ignore | {error,Error}
+%% Function: new() -> {ok, Transport} | ignore | {error,Error}
 %% Description: Starts the server
 %%--------------------------------------------------------------------
 new(Host, Path) ->
-    case gen_server:start_link(?MODULE, {Host, Path}, []) of
+    new(Host, Path, _Options = []).
+
+%%--------------------------------------------------------------------
+%% Options include:
+%%   {http_options, HttpOptions}  = See http(3)
+%%   {extra_headers, ExtraHeaders}  = List of extra HTTP headers
+%%--------------------------------------------------------------------
+new(Host, Path, Options) ->
+    case gen_server:start_link(?MODULE, {Host, Path, Options}, []) of
         {ok, Pid} ->
             thrift_transport:new(?MODULE, Pid);
         Else ->
             Else
     end.
 
-%%--------------------------------------------------------------------
-%% Function: set_http_options() -> ok | {error,Error}
-%% Description: Set HTTP options
-%%--------------------------------------------------------------------
-set_http_options(Transport, HTTPOptions) ->
-    gen_server:call(Transport, {set_http_options, HTTPOptions}).
-
 %%--------------------------------------------------------------------
 %% Function: write(Transport, Data) -> ok
 %%
@@ -93,12 +94,27 @@ read(Transport, Len) when is_integer(Len) ->
 %% gen_server callbacks
 %%====================================================================
 
-init({Host, Path}) ->
-    {ok, #http_transport{host = Host,
-                         path = Path,
-                         read_buffer = [],
-                         write_buffer = [],
-                         http_options = []}}.
+init({Host, Path, Options}) ->
+    State1 = #http_transport{host = Host,
+                             path = Path,
+                             read_buffer = [],
+                             write_buffer = [],
+                             http_options = []},
+    ApplyOption =
+        fun
+            ({http_options, HttpOpts}, State = #http_transport{}) ->
+                State#http_transport{http_options = HttpOpts};
+            (Other, #http_transport{}) ->
+                {invalid_option, Other};
+            (_, Error) ->
+                Error
+        end,
+    case lists:foldl(ApplyOption, State1, Options) of
+        State2 = #http_transport{} ->
+            {ok, State2};
+        Else ->
+            {stop, Else}
+    end.
 
 handle_call({write, Data}, _From, State = #http_transport{write_buffer = WBuf}) ->
     {reply, ok, State#http_transport{write_buffer = [WBuf, Data]}};
@@ -115,9 +131,6 @@ handle_call({read, Len}, _From, State = #http_transport{read_buffer = RBuf}) ->
             {reply, {error, 'EOF'}, State}
     end;
 
-handle_call({set_http_options, HTTPOptions}, _From, State) ->
-    {reply, ok, State#http_transport{http_options = HTTPOptions}};
-
 handle_call(flush, _From, State) ->
     {Response, State1} = do_flush(State),
     {reply, Response, State1}.