Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add parent proxy support for http: first step (TSUN-37)

https not implemented (yet ?)

SVN Revision: 738
  • Loading branch information...
commit 3493e6219b3d4ca8348635e132404d8dbc4e031f 1 parent f7a8dc6
@nniclausse nniclausse authored
View
4 src/tsung_recorder/ts_client_proxy.erl
@@ -74,7 +74,9 @@ start(Socket) ->
%% {stop, Reason}
%%--------------------------------------------------------------------
init([Socket]) ->
- {ok, #proxy{clientsock=Socket, plugin=?config(plugin)}}.
+ ?LOGF("Parent proxy: ~p~n",[?config(parent_proxy)],?DEB),
+ {ok, #proxy{clientsock=Socket, plugin=?config(plugin),
+ parent_proxy=?config(parent_proxy)}}.
%%--------------------------------------------------------------------
%% Function: handle_call/3
View
63 src/tsung_recorder/ts_proxy_http.erl
@@ -61,7 +61,7 @@ rewrite_ssl(Data)->
%% Purpose: parse HTTP request
%% Returns: {ok, NewState}
%%--------------------------------------------------------------------
-parse(State=#proxy{parse_status=Status},_,ServerSocket,String) when Status==new ->
+parse(State=#proxy{parse_status=Status, parent_proxy=Parent},_,ServerSocket,String) when Status==new ->
NewString = lists:append(State#proxy.buffer,String),
case ts_http_common:parse_req(NewString) of
{more, _Http, _Head} ->
@@ -74,10 +74,10 @@ parse(State=#proxy{parse_status=Status},_,ServerSocket,String) when Status==new
case httpd_util:key1search(Http#http_request.headers,"content-length") of
undefined -> % no body, everything received
ts_proxy_recorder:dorecord({Http }),
- {NewSocket,RelURL} = check_serversocket(ServerSocket,RequestURI,State#proxy.clientsock),
+ {NewSocket,RelURL} = check_serversocket(Parent,ServerSocket,RequestURI,State#proxy.clientsock),
?LOGF("Remove server info from url:~p ~p in ~p~n",
[RequestURI,RelURL,NewString], ?INFO),
- {ok, RealString} = relative_url(NewString,RequestURI,RelURL),
+ {ok, RealString} = relative_url(Parent,NewString,RequestURI,RelURL),
ts_client_proxy:send(NewSocket,RealString, ?MODULE),
case Http#http_request.method of
'CONNECT' ->
@@ -96,7 +96,7 @@ parse(State=#proxy{parse_status=Status},_,ServerSocket,String) when Status==new
if
BodySize == CLength -> % end of response
{NewSocket,RelURL} = check_serversocket(ServerSocket,RequestURI,State#proxy.clientsock),
- {ok,RealString,_Count} = regexp:gsub(NewString,RequestURI,RelURL),%FIXME: why not use relative_url ?
+ {ok,RealString} = relative_url(Parent,NewString,RequestURI,RelURL),
ts_client_proxy:send(NewSocket,RealString,?MODULE),
?LOG("End of response, recording~n", ?DEB),
ts_proxy_recorder:dorecord({Http#http_request{body=Body}}),
@@ -107,7 +107,7 @@ parse(State=#proxy{parse_status=Status},_,ServerSocket,String) when Status==new
{error, bad_content_length};
true ->
{NewSocket,RelURL} = check_serversocket(ServerSocket,RequestURI,State#proxy.clientsock),
- {ok,RealString,_Count} = regexp:gsub(NewString,RequestURI,RelURL), %FIXME: why not use relative_url ?
+ {ok,RealString} = relative_url(Parent,NewString,RequestURI,RelURL),
ts_client_proxy:send(NewSocket,RealString,?MODULE),
?LOG("More data to come continue before recording~n", ?DEB),
{ok, State#proxy{http_version=HTTPVersion,
@@ -146,16 +146,43 @@ parse(State=#proxy{parse_status=connect},_,ServerSocket,String) ->
{ok, State}.
-relative_url("CONNECT"++_Tail,_RequestURI,[])->
+%%--------------------------------------------------------------------
+%% Func: relative_url/4
+%%--------------------------------------------------------------------
+relative_url(_,"CONNECT"++_Tail,_RequestURI,[])->
{ok, []};
-relative_url(NewString,RequestURI,RelURL)->
+relative_url(true,NewString,_RequestURI,_RelURL)->
+ {ok, NewString};
+relative_url(false,NewString,RequestURI,RelURL)->
[FullURL_noargs|_] = string:tokens(RequestURI,"?"),
[RelURL_noargs|_] = string:tokens(RelURL,"?"),
{ok,RealString,_Count} = regexp:gsub(NewString,FullURL_noargs,RelURL_noargs),
{ok, RealString}.
%%--------------------------------------------------------------------
-%% Func: check_serversocket/2
+%% Func: check_serversocket/4
+%% Purpose: If the socket is not defined, or if the server is not the
+%% same, connect to the server as specified in URL
+%% Check if we use a parent proxy, otherwise use check_serversocket/3
+%% Returns: {Socket, URL (String)}
+%%--------------------------------------------------------------------
+check_serversocket(false, Socket, URL , ClientSock) ->
+ check_serversocket(Socket, URL , ClientSock);
+check_serversocket(true, Socket, "http://{"++URL, ClientSock) ->
+ check_serversocket(true, Socket, "https://"++URL, ClientSock);
+check_serversocket(true, Socket, "http://%7B"++URL, ClientSock) ->
+ check_serversocket(true, Socket, "https://"++URL, ClientSock);
+check_serversocket(true, undefined, URL, _ClientSock) ->
+ ?LOGF("Connecting to parent proxy ~p:~p ...~n",
+ [?config(pgsql_server),?config(pgsql_port)],?WARN),
+ {ok ,Socket} = connect(http,?config(pgsql_server),?config(pgsql_port)),
+ {Socket,URL};
+check_serversocket(true, Socket, URL, _ClientSock) ->
+ {Socket,URL}.
+
+
+%%--------------------------------------------------------------------
+%% Func: check_serversocket/3
%% Purpose: If the socket is not defined, or if the server is not the
%% same, connect to the server as specified in URL
%% Returns: {Socket, RelativeURL (String)}
@@ -181,10 +208,7 @@ check_serversocket(undefined, URL = #url{}, ClientSock) ->
ts_client_proxy:send(ClientSock, "HTTP/1.0 200 Connection established\r\nProxy-agent: tsung\r\n\r\n", ?MODULE),
{ Socket, [] };
_ ->
- case URL#url.querypart of
- [] -> {Socket, URL#url.path};
- Query -> {Socket, URL#url.path++"?"++Query}
- end
+ {Socket, url_with_query(URL)}
end;
check_serversocket(Socket, URL=#url{host=Host}, _ClientSock) ->
RealPort = ts_config_http:set_port(URL),
@@ -192,10 +216,7 @@ check_serversocket(Socket, URL=#url{host=Host}, _ClientSock) ->
case ts_client_proxy:peername(Socket) of
{ok, {RealIP, RealPort}} -> % same as previous URL
?LOGF("Reuse socket ~p on URL ~p~n", [Socket, URL],?DEB),
- case URL#url.querypart of
- [] -> {Socket, URL#url.path};
- Query -> {Socket, URL#url.path++"?"++Query}
- end;
+ {Socket, url_with_query(URL)};
Other ->
?LOGF("New server configuration (~p:~p, was ~p) on URL ~p~n",
[RealIP, RealPort, Other, URL],?DEB),
@@ -203,13 +224,13 @@ check_serversocket(Socket, URL=#url{host=Host}, _ClientSock) ->
{sslsocket, _, _} -> ssl:close(Socket);
_ -> gen_tcp:close(Socket)
end,
- {ok, NewSocket} = connect(URL#url.scheme, Host,RealPort),
- case URL#url.querypart of
- [] -> {NewSocket, URL#url.path};
- Query -> {NewSocket, URL#url.path++"?"++Query}
- end
+ {ok, NewSocket} = connect(URL#url.scheme, Host, RealPort),
+ {NewSocket, url_with_query(URL)}
end.
+url_with_query(#url{path=Path, querypart=[]}) -> Path;
+url_with_query(#url{path=Path, querypart=Query}) -> Path ++"?"++Query.
+
connect(Scheme, Host, Port)->
case Scheme of
https ->
View
1  src/tsung_recorder/tsung_recorder.app.src
@@ -17,6 +17,7 @@
{ts_cookie, "humhum"},
{log_file, "./tsung.log"},
{plugin, ts_proxy_http},
+ {parent_proxy, false},
{pgsql_server, "127.0.0.1"},
{pgsql_port, 5432},
{proxy_log_file, "./tsung_recorder"},
View
39 tsung.sh.in
@@ -15,6 +15,7 @@ LOG_OPT="log_file \"$LOG_DIR/tsung.log\""
VERSION=@PACKAGE_VERSION@
LISTEN_PORT=8090
+USE_PARENT_PROXY=false
PGSQL_SERVER_IP=127.0.0.1
PGSQL_SERVER_PORT=5432
NAME=tsung
@@ -57,9 +58,9 @@ recorder_status() {
checkversion() {
if [ $ERTS_RUN != $ERTS_BOOT ]
then
- echo "Erlang version has changed ! [$ERTS_BOOT] != [$ERTS_RUN]"
- echo "Must create new boot files (you may have to run this one time as root ! )"
- makebootfiles
+ echo "Erlang version has changed ! [$ERTS_BOOT] != [$ERTS_RUN]"
+ echo "Must create new boot files (you may have to run this one time as root ! )"
+ makebootfiles
fi
}
@@ -76,7 +77,7 @@ makebootfiles() {
}
start() {
- echo "Starting Tsung"
+ echo "Starting Tsung"
$ERL $ERL_OPTS $ERL_RSH -noshell -sname $CONTROLLER -setcookie $COOKIE $BOOT_OPT \
-pa $TSUNGPATH -pa $CONTROLLERPATH \
-tsung_controller debug_level $DEBUG_LEVEL \
@@ -89,6 +90,7 @@ recorder() {
-pa $TSUNGPATH -pa $RECORDERPATH -pa $CONTROLLERPATH \
-tsung_recorder debug_level $DEBUG_LEVEL \
-tsung_recorder $LOG_OPT \
+ -tsung_recorder parent_proxy $USE_PARENT_PROXY \
-tsung_recorder plugin ts_proxy_$RECORDER_PLUGIN \
-tsung_recorder proxy_log_file \"$MAIN_DIR/tsung_recorder.xml\" \
-tsung_recorder pgsql_server \"${PGSQL_SERVER_IP}\" \
@@ -105,23 +107,23 @@ debug() {
}
version() {
- echo "Tsung version $VERSION"
+ echo "Tsung version $VERSION"
}
checkconfig() {
- if [ ! -e $CONF_OPT_FILE ]
- then
- echo "Config file $CONF_OPT_FILE doesn't exist, aborting !"
- exit 1
- fi
+ if [ ! -e $CONF_OPT_FILE ]
+ then
+ echo "Config file $CONF_OPT_FILE doesn't exist, aborting !"
+ exit 1
+ fi
}
maindir() {
- if [ ! -d $MAIN_DIR ]
- then
- echo "Creating local Tsung directory $MAIN_DIR"
- mkdir $MAIN_DIR
- fi
+ if [ ! -d $MAIN_DIR ]
+ then
+ echo "Creating local Tsung directory $MAIN_DIR"
+ mkdir $MAIN_DIR
+ fi
}
logdir() {
@@ -157,11 +159,11 @@ checkrunning(){
usage() {
prog=`basename $0`
- echo "$prog start|stop|restart|debug|status|recorder|stop_recorder|version"
+ echo "$prog <options> start|stop|restart|debug|status|recorder|stop_recorder|version"
exit
}
-while getopts ":f:p:l:d:r:i:P:L:I:" Option
+while getopts ":f:p:l:d:r:i:P:L:I:u" Option
do
case $Option in
f) CONF_OPT_FILE=$OPTARG;;
@@ -170,11 +172,12 @@ do
r) ERL_RSH=" -rsh $OPTARG ";;
p) RECORDER_PLUGIN=$OPTARG;;
I) PGSQL_SERVER_IP=$OPTARG;;
+ u) USE_PARENT_PROXY=true;;
P) PGSQL_SERVER_PORT=$OPTARG;;
L) LISTEN_PORT=$OPTARG;;
*) usage ;;
esac
-done
+done
shift $(($OPTIND - 1))
case $1 in
Please sign in to comment.
Something went wrong with that request. Please try again.