Permalink
Browse files

use new funs in base64 and string modules for R12B compatibility (TSU…

…N-51)

SVN Revision: 785
  • Loading branch information...
1 parent 8699a6e commit 56050dbc0df9f43c6a697515fa6a5e909a77717e @nniclausse nniclausse committed Jan 5, 2008
@@ -10,6 +10,7 @@
-compile(export_all).
-include("ts_http.hrl").
+-include("ts_profile.hrl").
-include_lib("eunit/include/eunit.hrl").
-import(ts_http_common,[parse_req/1, parse_req/2]).
@@ -50,3 +51,12 @@ parse_http_request6_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",
?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")).
@@ -71,7 +71,7 @@ shahex(Clear) ->
%%%----------------------------------------------------------------------
tohex(A)->
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,
lists:flatten( lists:map(Fun, A) ).
@@ -124,7 +124,7 @@ http_body(Method,#http_request{url=URL, version=Version,
authenticate(undefined,_)-> [];
authenticate(_,undefined)-> [];
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].
user_agent(undefined) ->
@@ -39,7 +39,8 @@
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,
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("info") -> ?INFO;
@@ -159,6 +160,40 @@ node_to_hostname(Node) ->
[_Nodename, Hostname] = string:tokens( atom_to_list(Node), "@"),
{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
%% Purpose: wrapper around httpd_utils module funs (maybe one day
@@ -64,7 +64,7 @@ parse_config(Element = #xmlElement{name=http},
content_type, "application/x-www-form-urlencoded"),
Date = ts_config:getAttr(string, Element#xmlElement.attributes,
'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,
method = Method,
version = Version,
@@ -149,7 +149,7 @@ parse_headers([Element = #xmlElement{name=http_header} | Tail], Headers) ->
Value = ts_config:getAttr(string, Element#xmlElement.attributes, value),
EncodedValue = case ts_config:getAttr(atom, Element#xmlElement.attributes, encoding, none) of
base64 ->
- httpd_util:encode_base64(Value);
+ ts_utils:encode_base64(Value);
none ->
Value
end,
@@ -297,7 +297,7 @@ record_request(State=#state_rec{prev_host=Host, prev_port=Port, prev_scheme=Sche
%% Returns: {User, Passwd}
%%--------------------------------------------------------------------
decode_basic_auth(Base64)->
- AuthStr= httpd_util:decode_base64(Base64),
+ AuthStr= ts_utils:decode_base64(Base64),
Sep = string:chr(AuthStr,$:),
{string:substr(AuthStr,1,Sep-1),string:substr(AuthStr,Sep+1)}.

0 comments on commit 56050db

Please sign in to comment.