couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject [10/13] git commit: Relay to the Node app
Date Wed, 13 Feb 2013 13:17:14 GMT
Relay to the Node app


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/a6c54a55
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/a6c54a55
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/a6c54a55

Branch: refs/heads/nodejs_couchdb
Commit: a6c54a55049895851139f7fcc58e6078d054ff6e
Parents: b7eb2af
Author: Jason Smith (work) <jhs@iriscouch.com>
Authored: Tue Feb 12 16:48:20 2013 +0000
Committer: Jason Smith (work) <jhs@iriscouch.com>
Committed: Tue Feb 12 16:48:20 2013 +0000

----------------------------------------------------------------------
 src/couchdb/couch_httpd_nodejs.erl |  158 +++++++++++++------------------
 src/couchdb/couch_os_daemons.erl   |   18 ++++-
 2 files changed, 84 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/a6c54a55/src/couchdb/couch_httpd_nodejs.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd_nodejs.erl b/src/couchdb/couch_httpd_nodejs.erl
index fc14983..5e7c7bf 100644
--- a/src/couchdb/couch_httpd_nodejs.erl
+++ b/src/couchdb/couch_httpd_nodejs.erl
@@ -14,103 +14,79 @@
 -include("couch_db.hrl").
 
 -export([handle_node_req/1]).
+-export([relay/2]).
 
 -import(couch_httpd, [header_value/2, send_method_not_allowed/2]).
 
 
 handle_node_req(#httpd{}=Req) -> ok
-    , couch_httpd:send_json(Req, 200, {[ {ok,true}, {todo,true} ]}).
+    , AppName = "nodejs_couchdb"
+    , case couch_os_daemons:get_app_port(AppName)
+        of nil -> ok
+            , couch_httpd:send_json(Req, 502, {[ {error,no_app}, {name,AppName} ]})
+        ; AppPort -> ok
+            , io:format("Relay to :~p\n", [AppPort])
+            , relay(Req, AppPort)
+        end
+    .
 
-%handle_git_req(#httpd{method='GET', path_parts=[_Debug, Pid | _Rest]=Path}=Req) -> ok
-%    , ok = couch_httpd:verify_is_server_admin(Req)
-%    %, io:format("Must look up: ~p for: ~p\n", [Pid, Path])
-%    , Procs = couch_query_servers:git_ports("javascript")
-%    , case lists:keyfind(Pid, 1, Procs)
-%        of {Pid, Port} -> ok
-%            % Proxy to socket.io. Use the normal httpd_proxy for everything except the
-%            % websockets request.
-%            , InspectorPort = Port + 1
-%            , case Path
-%                of [<<"_git">>, Pid, <<"socket.io">>, <<"1">>,
<<"websocket">>, _Key] -> ok
-%                    , relay(Req, InspectorPort)
-%                ; _ -> ok
-%                    , Url = io_lib:format("http://127.0.0.1:~w/_git/", [InspectorPort])
-%                    %, io:format("Proxy: ~p\nPath = ~p\n", [Url, Path])
-%                    , couch_httpd_proxy:handle_proxy_req(Req, ?l2b(Url))
-%                end
-%        ; false -> ok
-%            , io:format("No such pid: ~p\n", [Pid])
-%            , couch_httpd:send_json(Req, 404, {[ {error,pid_not_found}, {pid,Pid} ]})
-%        end
-%    ;
-%
-%handle_git_req(Req) -> ok
-%    , send_method_not_allowed(Req, "GET")
-%    .
-%
-%relay(Req, Port) when is_integer(Port) -> ok
-%    , io:format("socket.io connection to port ~w\n", [Port])
-%    , case gen_tcp:connect("127.0.0.1", Port, [binary, {packet,0}, {active,false}])
-%        of {ok, InspectorSocket} -> ok
-%            , io:format("Connected to inspector on :~w\n", [Port])
-%            , relay(Req, InspectorSocket)
-%        ; {error, Error} -> ok
-%            , Resp = {[ {error,inspector_proxy}, {result, Error} ]}
-%            , couch_httpd:send_json(Req, 502, Resp)
-%        end
-%    ;
-%
-%relay(#httpd{mochi_req=MochiReq}=Req, InspectorSocket) -> ok
-%    , Client = MochiReq:get(socket)
-%    , ReqBytes = request_to_iolist(Req)
-%    , Size = iolist_size(ReqBytes)
-%    , io:format("Send ~w byte request to inspector\n", [Size])
-%    , ok = gen_tcp:send(InspectorSocket, ReqBytes)
-%    , relay(Client, InspectorSocket, Size, 0)
-%    .
-%
-%relay(Client, Remote, FromClient, FromRemote) -> ok
-%    %, io:format("Relay client=~w remote=~w\n", [FromClient, FromRemote])
-%    , inet:setopts(Client, [{packet,0}, {active,once}, {nodelay,true} ])
-%    , inet:setopts(Remote, [{packet,0}, {active,once}, {nodelay,true} ])
-%    , receive
-%        {_Type, Client, Data} -> ok
-%            %, io:format("Client: ~p\n", [Data])
-%            , gen_tcp:send(Remote, Data)
-%            , relay(Client, Remote, FromClient + size(Data), FromRemote)
-%        ; {_Type, Remote, Data} -> ok
-%            %, io:format("Remote ~w bytes: ~p\n", [size(Data), Data])
-%            , gen_tcp:send(Client, Data)
-%            , relay(Client, Remote, FromClient, FromRemote + size(Data))
-%        ; {tcp_closed, Client} -> ok
-%            , io:format("Client disconnected\n")
-%            , gen_tcp:close(Remote)
-%            , {ok, ok}
-%        ; {tcp_closed, Remote} -> ok
-%            % XXX
-%            , io:format("Remote disconnected\n")
-%            %, gen_tcp:controlling_process(Remote)
-%            %, catch Owner ! {stream_close, bad_reqid}
-%            , gen_tcp:close(Client)
-%            %, {ok, FromClient, FromRemote}
-%            , {ok, ok}
-%        ; Else -> ok
-%            , ?LOG_ERROR("Relay error: ~p", [Else])
-%        end
-%    %, couch_httpd:send_json(Req, 200, {[ {ok,true}, {todo,<<"To do">>} ]})
-%    .
-%
-%request_to_iolist(#httpd{method=Method, mochi_req=MochiReq}) -> ok
-%    , Path = MochiReq:get(raw_path)
-%    , Version = case MochiReq:get(version)
-%        of {1,1} -> "1.1"
-%        ;  _     -> "1.0"
-%        end
-%    , Action = io_lib:format("~s ~s HTTP/~s", [Method, Path, Version])
-%    , MochiHeaders = mochiweb_headers:to_list(MochiReq:get(headers))
-%    , Headers = [ [couch_util:to_binary(Key), ": ", Val, "\r\n"] || {Key, Val} <- MochiHeaders]
-%    , [Action, "\r\n", Headers, "\r\n"]
-%    .
+relay(Req, Port) when is_integer(Port) -> ok
+    , case gen_tcp:connect("127.0.0.1", Port, [binary, {packet,0}, {active,false}])
+        of {ok, AppSocket} -> ok
+            , io:format("Connected to app on :~w\n", [Port])
+            , relay(Req, AppSocket)
+        ; {error, Error} -> ok
+            , Resp = {[ {error,nodejs_proxy}, {result, Error} ]}
+            , couch_httpd:send_json(Req, 502, Resp)
+        end
+    ;
+
+relay(#httpd{mochi_req=MochiReq}=Req, AppSocket) -> ok
+    , Client = MochiReq:get(socket)
+    , ReqBytes = request_to_iolist(Req)
+    , Size = iolist_size(ReqBytes)
+    , io:format("Send ~w byte request to app\n", [Size])
+    , ok = gen_tcp:send(AppSocket, ReqBytes)
+    , relay(Client, AppSocket, Size, 0)
+    .
+
+relay(Client, Remote, FromClient, FromRemote) -> ok
+    %, io:format("Relay client=~w remote=~w\n", [FromClient, FromRemote])
+    , inet:setopts(Client, [{packet,0}, {active,once}, {nodelay,true} ])
+    , inet:setopts(Remote, [{packet,0}, {active,once}, {nodelay,true} ])
+    , receive
+        {_Type, Client, Data} -> ok
+            %, io:format("Client: ~p\n", [Data])
+            , gen_tcp:send(Remote, Data)
+            , relay(Client, Remote, FromClient + size(Data), FromRemote)
+        ; {_Type, Remote, Data} -> ok
+            %, io:format("Remote ~w bytes: ~p\n", [size(Data), Data])
+            , gen_tcp:send(Client, Data)
+            , relay(Client, Remote, FromClient, FromRemote + size(Data))
+        ; {tcp_closed, Client} -> ok
+            , io:format("Client disconnected\n")
+            , gen_tcp:close(Remote)
+            , {ok, ok}
+        ; {tcp_closed, Remote} -> ok
+            , io:format("Remote disconnected\n")
+            , gen_tcp:close(Client)
+            , {ok, ok}
+        ; Else -> ok
+            , ?LOG_ERROR("Relay error: ~p", [Else])
+        end
+    .
+
+request_to_iolist(#httpd{method=Method, mochi_req=MochiReq}) -> ok
+    , Path = MochiReq:get(raw_path)
+    , Version = case MochiReq:get(version)
+        of {1,1} -> "1.1"
+        ;  _     -> "1.0"
+        end
+    , Action = io_lib:format("~s ~s HTTP/~s", [Method, Path, Version])
+    , MochiHeaders = mochiweb_headers:to_list(MochiReq:get(headers))
+    , Headers = [ [couch_util:to_binary(Key), ": ", Val, "\r\n"] || {Key, Val} <- MochiHeaders]
+    , [Action, "\r\n", Headers, "\r\n"]
+    .
 
 
 %

http://git-wip-us.apache.org/repos/asf/couchdb/blob/a6c54a55/src/couchdb/couch_os_daemons.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_os_daemons.erl b/src/couchdb/couch_os_daemons.erl
index e24a188..c311860 100644
--- a/src/couchdb/couch_os_daemons.erl
+++ b/src/couchdb/couch_os_daemons.erl
@@ -13,7 +13,7 @@
 -behaviour(gen_server).
 
 -export([start_link/0, info/0, info/1, config_change/2]).
--export([check_app_password/1]).
+-export([check_app_password/1, get_app_port/1]).
 
 -export([init/1, terminate/2, code_change/3]).
 -export([handle_call/3, handle_cast/2, handle_info/2]).
@@ -51,6 +51,9 @@ info(Options) ->
 check_app_password(Pw) ->
     gen_server:call(?MODULE, {check_app_password, Pw}).
 
+get_app_port(Name) ->
+    gen_server:call(?MODULE, {get_app_port, Name}).
+
 config_change(Section, Key) ->
     gen_server:cast(?MODULE, {config_change, Section, Key}).
 
@@ -75,6 +78,9 @@ handle_call({daemon_info, Options}, _From, Table) when is_list(Options)
->
 handle_call({check_app_password, Password}, _From, Table) ->
     Result = check_app_password(Password, Table),
     {reply, Result, Table};
+handle_call({get_app_port, Name}, _From, Table) ->
+    Result = get_app_port(Name, Table),
+    {reply, Result, Table};
 handle_call(Msg, From, Table) ->
     ?LOG_ERROR("Unknown call message to ~p from ~p: ~p", [?MODULE, From, Msg]),
     {stop, error, Table}.
@@ -206,6 +212,16 @@ code_change(_OldVsn, State, _Extra) ->
 
 % Internal API
 
+get_app_port(Name, Table) ->
+    AppsSpec = #daemon{name=Name, app_port='$1', _='_'},
+    Ports = [ Port || [Port] <- ets:match(Table, AppsSpec) ],
+    case Ports of
+    [] ->
+        nil;
+    [Port | _Rest] ->
+        Port
+    end.
+
 check_app_password(Password, Table) ->
     Daemons = ets:tab2list(Table),
     CheckPw = fun(_Daemon, ok) ->


Mime
View raw message