You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by jh...@apache.org on 2013/02/11 14:51:16 UTC
[17/19] git commit: Proxy requests to node-inspector
Proxy requests to node-inspector
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/7b59667e
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/7b59667e
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/7b59667e
Branch: refs/heads/nodejs_couchdb
Commit: 7b59667e6ba94c5e21a7c880fd58782a4f212f28
Parents: 801b731
Author: Jason Smith (work) <jh...@iriscouch.com>
Authored: Fri Feb 8 11:28:22 2013 +0000
Committer: Jason Smith (work) <jh...@iriscouch.com>
Committed: Fri Feb 8 11:28:22 2013 +0000
----------------------------------------------------------------------
share/www/debug.html | 4 +-
src/couchdb/couch_httpd_debug.erl | 108 +++++++++++++++++++++-----------
src/couchdb/couch_httpd_proxy.erl | 56 ----------------
3 files changed, 74 insertions(+), 94 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7b59667e/share/www/debug.html
----------------------------------------------------------------------
diff --git a/share/www/debug.html b/share/www/debug.html
index d477f0f..447ab70 100644
--- a/share/www/debug.html
+++ b/share/www/debug.html
@@ -93,9 +93,9 @@ function get_debuggers() {
res.body.pids.forEach(function(pid) {
var port = res.body.procs[pid] + 1
- var url = 'http://' + window.location.hostname + ':' + port + '/_debug/' + pid + '/'
+ var url = '/_debug/' + pid + '/'
var link = $('<li><a href="'+url+'">' + pid + '</a></li>')
- console.log(res.body)
+ //console.log(res.body)
servers.append(link)
link.find('a').click(function() {
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7b59667e/src/couchdb/couch_httpd_debug.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd_debug.erl b/src/couchdb/couch_httpd_debug.erl
index 9ca0eab..8fa7686 100644
--- a/src/couchdb/couch_httpd_debug.erl
+++ b/src/couchdb/couch_httpd_debug.erl
@@ -31,14 +31,17 @@ handle_debug_req(#httpd{method='GET', path_parts=[_Debug, Pid | _Rest]=Path}=Req
, Procs = couch_query_servers:debug_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
- %, proxy_to_inspector(Req, InspectorPort)
- %, R = couch_httpd:send_json(Req, 200, {[ {ok,true}, {todo,<<"To do">>} ]})
- %, io:format("send_json returns: ~p\n", [R])
- %, R
- , Url = io_lib:format("http://127.0.0.1:~w/_debug/", [InspectorPort])
- %, io:format("Proxy: ~p\n", [Url])
- , couch_httpd_proxy:handle_proxy_req(Req, ?l2b(Url))
+ , case Path
+ of [<<"_debug">>, Pid, <<"socket.io">>, <<"1">>, <<"websocket">>, _Key] -> ok
+ , relay(Req, InspectorPort)
+ ; _ -> ok
+ , Url = io_lib:format("http://127.0.0.1:~w/_debug/", [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} ]})
@@ -49,36 +52,69 @@ handle_debug_req(Req) -> ok
, send_method_not_allowed(Req, "GET")
.
-%relay(#httpd{mochi_req=MochiReq}=Req, InspectorSocket) -> ok
-% , ReqBytes = request_to_iolist(Req)
-% , Client = MochiReq:get(socket)
-% , gen_tcp:send(InspectorSocket, ReqBytes)
-% , relay(Client, InspectorSocket, 0, 0)
-% .
-
-%proxy_to_inspector(Req, Port) -> ok
-% , io:format("Connect to port ~w\n", [Port])
-% , case gen_tcp:connect("127.0.0.1", Port, [binary, {packet,0}, {delay_send,true}])
-% 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(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
+ ;
-%request_to_iolist(#httpd{method=Method, mochi_req=MochiReq}=Req) -> 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(#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"]
+ .
%% Login handler with Browser ID.
%handle_id_req(#httpd{method='POST'}=Req) -> ok
http://git-wip-us.apache.org/repos/asf/couchdb/blob/7b59667e/src/couchdb/couch_httpd_proxy.erl
----------------------------------------------------------------------
diff --git a/src/couchdb/couch_httpd_proxy.erl b/src/couchdb/couch_httpd_proxy.erl
index 4acbc9c..dec3f55 100644
--- a/src/couchdb/couch_httpd_proxy.erl
+++ b/src/couchdb/couch_httpd_proxy.erl
@@ -254,11 +254,6 @@ stream_response(Req, ProxyDest, ReqId) ->
% doesn't get confused.
ibrowse:stream_next(ReqId),
stream_response(Req, ProxyDest, ReqId);
- {ibrowse_async_headers, ReqId, "101", Headers} ->
- % 101 Switching Protocols
- {Source, Dest} = get_urls(Req, ProxyDest),
- FixedHeaders = fix_headers(Source, Dest, Headers, []),
- relay(ReqId, Req, FixedHeaders);
{ibrowse_async_headers, ReqId, Status, Headers} ->
{Source, Dest} = get_urls(Req, ProxyDest),
FixedHeaders = fix_headers(Source, Dest, Headers, []),
@@ -291,57 +286,6 @@ stream_response(Req, ProxyDest, ReqId) ->
end
end.
-relay(ReqId, #httpd{mochi_req=MochiReq}=Req, FixedHeaders) ->
- %io:format("Relay: ~p\nReq=~p\nH=~p\n", [ReqId, Req, FixedHeaders]),
- case ets:lookup(ibrowse_stream, {req_id_pid, ReqId}) of
- [] ->
- io:format("Unknown req id ~p\n", [ReqId]);
- [{_, Pid}] ->
- io:format("Sending message to ibrowse: ~w\n", [Pid]),
- catch Pid ! {take_stream, self()},
- receive
- {permission, Remote, Data} ->
- io:format("Got control of socket: ~p\n", [Remote]),
- Client = MochiReq:get(socket),
- HeadersIo = [ [Key, ": ", Val, "\r\n"] || {Key, Val} <- FixedHeaders],
- Response = iolist_to_binary([ "HTTP/1.1 101 Switching Protocols\r\n", HeadersIo, "\r\n", Data ]),
- io:format("Need to send response:\n~p\n", [iolist_to_binary(Response)]),
- gen_tcp:send(Client, Response),
- relay(Client, Remote, 0, size(Response), Pid)
- after 1000 ->
- io:format("Error, no permission\n")
- end
- end,
- ok.
-
-relay(Client, Remote, FromClient, FromRemote, Owner) -> ok
- , io:format("Relay in=~w out=~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, Owner)
- ; {_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), Owner)
- ; {tcp_closed, S} -> ok
- , io:format("Closed socket: ~p\n", [S])
- , io:format("Relay finished in=~w out=~w\n", [FromClient, FromRemote])
- , gen_tcp:controlling_process(Remote, Owner)
- , catch Owner ! {stream_close, bad_reqid}
- %, gen_tcp:close(Client)
- %, gen_tcp:close(Remote)
- %, {ok, FromClient, FromRemote}
- , {ok, true}
- ; Else -> ok
- , ?LOG_ERROR("Relay error: ~p", [Else])
- end
- %, couch_httpd:send_json(Req, 200, {[ {ok,true}, {todo,<<"To do">>} ]})
- .
-
stream_chunked_response(Req, ReqId, Resp) ->
receive