Skip to content

Commit

Permalink
use new funs in base64 and string modules for R12B compatibility (TSU…
Browse files Browse the repository at this point in the history
…N-51)

SVN Revision: 785
  • Loading branch information
nniclausse committed Jan 5, 2008
1 parent 8699a6e commit 56050db
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/test/ts_test_recorder.erl
Expand Up @@ -10,6 +10,7 @@
-compile(export_all). -compile(export_all).


-include("ts_http.hrl"). -include("ts_http.hrl").
-include("ts_profile.hrl").
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-import(ts_http_common,[parse_req/1, parse_req/2]). -import(ts_http_common,[parse_req/1, parse_req/2]).


Expand Down Expand Up @@ -50,3 +51,12 @@ parse_http_request6_test() ->
parse_http_request7_test() -> parse_http_request7_test() ->
Req= "GET http://www.niclux.org/ HTTP/1.1\r\nHost: www.niclux.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041209 Firefox/1.0 (Ubuntu) (Ubuntu package 1.0-2ubuntu4-warty99)\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nAccept-Language: fr-fr,en-us;q=0.7,en;q=0.3\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nProxy-Connection: keep-alive\r\n\r\n", Req= "GET http://www.niclux.org/ HTTP/1.1\r\nHost: www.niclux.org\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041209 Firefox/1.0 (Ubuntu) (Ubuntu package 1.0-2ubuntu4-warty99)\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\nAccept-Language: fr-fr,en-us;q=0.7,en;q=0.3\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nProxy-Connection: keep-alive\r\n\r\n",
?assertMatch({ok, #http_request{method='GET', version="1.1"}, []},parse_req(Req)). ?assertMatch({ok, #http_request{method='GET', version="1.1"}, []},parse_req(Req)).

decode_base64_test()->
Base="QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
?assertMatch({"Aladdin","open sesame"}, ts_proxy_http:decode_basic_auth(Base)).

%%TODO: should be in ts_test_http
encode_base64_test()->
Base="QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
?assertMatch(["Authorization: Basic ",Base,?CRLF], ts_http_common:authenticate("Aladdin","open sesame")).
2 changes: 1 addition & 1 deletion src/tsung/ts_digest.erl
Expand Up @@ -71,7 +71,7 @@ shahex(Clear) ->
%%%---------------------------------------------------------------------- %%%----------------------------------------------------------------------
tohex(A)-> tohex(A)->
Fun = fun(X)-> Fun = fun(X)->
httpd_util:to_lower(padhex(httpd_util:integer_to_hexlist(X))) ts_utils:to_lower(padhex(httpd_util:integer_to_hexlist(X)))
end, end,
lists:flatten( lists:map(Fun, A) ). lists:flatten( lists:map(Fun, A) ).


Expand Down
2 changes: 1 addition & 1 deletion src/tsung/ts_http_common.erl
Expand Up @@ -124,7 +124,7 @@ http_body(Method,#http_request{url=URL, version=Version,
authenticate(undefined,_)-> []; authenticate(undefined,_)-> [];
authenticate(_,undefined)-> []; authenticate(_,undefined)-> [];
authenticate(UserId,Passwd)-> authenticate(UserId,Passwd)->
AuthStr = httpd_util:encode_base64(lists:append([UserId,":",Passwd])), AuthStr = ts_utils:encode_base64(lists:append([UserId,":",Passwd])),
["Authorization: Basic ",AuthStr,?CRLF]. ["Authorization: Basic ",AuthStr,?CRLF].


user_agent(undefined) -> user_agent(undefined) ->
Expand Down
37 changes: 36 additions & 1 deletion src/tsung/ts_utils.erl
Expand Up @@ -39,7 +39,8 @@
foreach_parallel/2, spawn_par/3, inet_setopts/3, resolve/2, foreach_parallel/2, spawn_par/3, inet_setopts/3, resolve/2,
stop_all/2, stop_all/3, stop_all/4, join/2, split2/2, split2/3, stop_all/2, stop_all/3, stop_all/4, join/2, split2/2, split2/3,
make_dir_rec/1, is_ip/1, from_https/1, to_https/1, keymax/2, make_dir_rec/1, is_ip/1, from_https/1, to_https/1, keymax/2,
check_sum/3, check_sum/5, clean_str/1, file_to_list/1]). check_sum/3, check_sum/5, clean_str/1, file_to_list/1,
decode_base64/1, encode_base64/1, to_lower/1]).


level2int("debug") -> ?DEB; level2int("debug") -> ?DEB;
level2int("info") -> ?INFO; level2int("info") -> ?INFO;
Expand Down Expand Up @@ -159,6 +160,40 @@ node_to_hostname(Node) ->
[_Nodename, Hostname] = string:tokens( atom_to_list(Node), "@"), [_Nodename, Hostname] = string:tokens( atom_to_list(Node), "@"),
{ok, Hostname}. {ok, Hostname}.


to_lower(String)->
case check_httpd_old_version() of
false ->
string:to_lower(String);
true ->
httpd_util:to_lower(String)
end.

encode_base64(String)->
case check_httpd_old_version() of
false ->
base64:encode_to_string(String);
true ->
httpd_util:encode_base64(String)
end.

decode_base64(Base64)->
case check_httpd_old_version() of
false ->
base64:decode_to_string(Base64);
true ->
httpd_util:decode_base64(Base64)
end.

%% check erlang version to know if we need to use the old httpd_utils functions
check_httpd_old_version()->
case erlang:system_info(version) of
[$5,$.,Maj, $.,Min] when Maj > $5 orelse (Maj == $5 andalso Min > $3 )->
false;
_ ->
true
end.


%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%% Func: key1search/2 %% Func: key1search/2
%% Purpose: wrapper around httpd_utils module funs (maybe one day %% Purpose: wrapper around httpd_utils module funs (maybe one day
Expand Down
4 changes: 2 additions & 2 deletions src/tsung_controller/ts_config_http.erl
Expand Up @@ -64,7 +64,7 @@ parse_config(Element = #xmlElement{name=http},
content_type, "application/x-www-form-urlencoded"), content_type, "application/x-www-form-urlencoded"),
Date = ts_config:getAttr(string, Element#xmlElement.attributes, Date = ts_config:getAttr(string, Element#xmlElement.attributes,
'if_modified_since', undefined), 'if_modified_since', undefined),
Method = list_to_atom(httpd_util:to_lower(ts_config:getAttr(string, Element#xmlElement.attributes, method))), Method = list_to_atom(ts_utils:to_lower(ts_config:getAttr(string, Element#xmlElement.attributes, method))),
Request = #http_request{url = URL, Request = #http_request{url = URL,
method = Method, method = Method,
version = Version, version = Version,
Expand Down Expand Up @@ -149,7 +149,7 @@ parse_headers([Element = #xmlElement{name=http_header} | Tail], Headers) ->
Value = ts_config:getAttr(string, Element#xmlElement.attributes, value), Value = ts_config:getAttr(string, Element#xmlElement.attributes, value),
EncodedValue = case ts_config:getAttr(atom, Element#xmlElement.attributes, encoding, none) of EncodedValue = case ts_config:getAttr(atom, Element#xmlElement.attributes, encoding, none) of
base64 -> base64 ->
httpd_util:encode_base64(Value); ts_utils:encode_base64(Value);
none -> none ->
Value Value
end, end,
Expand Down
2 changes: 1 addition & 1 deletion src/tsung_recorder/ts_proxy_http.erl
Expand Up @@ -297,7 +297,7 @@ record_request(State=#state_rec{prev_host=Host, prev_port=Port, prev_scheme=Sche
%% Returns: {User, Passwd} %% Returns: {User, Passwd}
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
decode_basic_auth(Base64)-> decode_basic_auth(Base64)->
AuthStr= httpd_util:decode_base64(Base64), AuthStr= ts_utils:decode_base64(Base64),
Sep = string:chr(AuthStr,$:), Sep = string:chr(AuthStr,$:),
{string:substr(AuthStr,1,Sep-1),string:substr(AuthStr,Sep+1)}. {string:substr(AuthStr,1,Sep-1),string:substr(AuthStr,Sep+1)}.


Expand Down

0 comments on commit 56050db

Please sign in to comment.