Skip to content
Newer
Older
100644 410 lines (380 sloc) 19.8 KB
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
1 %%%
2 %%% Copyright © IDEALX S.A.S. 2004
3 %%%
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
4 %%% Author : Nicolas Niclausse <nicolas.niclausse@niclux.org>
41e965d @nniclausse update email
nniclausse authored Nov 27, 2005
5 %%% Created: 20 Apr 2004 by Nicolas Niclausse <nicolas.niclausse@niclux.org>
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
6 %%%
7 %%% This program is free software; you can redistribute it and/or modify
8 %%% it under the terms of the GNU General Public License as published by
9 %%% the Free Software Foundation; either version 2 of the License, or
10 %%% (at your option) any later version.
11 %%%
12 %%% This program is distributed in the hope that it will be useful,
13 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
14 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 %%% GNU General Public License for more details.
16 %%%
17 %%% You should have received a copy of the GNU General Public License
18 %%% along with this program; if not, write to the Free Software
19 %%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
20 %%%
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
21
5d10a86 @nniclausse add explicit exception in the GPL for linking EPL modules (obvious, b…
nniclausse authored Aug 10, 2004
22 %%% In addition, as a special exception, you have the permission to
23 %%% link the code of this program with any library released under
24 %%% the EPL license and distribute linked combinations including
25 %%% the two.
26
72734af @nniclausse fix comment
nniclausse authored Apr 20, 2004
27 %%% common functions used by http clients to parse config
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
28
29 -module(ts_config_http).
30 -vc('$Id$ ').
41e965d @nniclausse update email
nniclausse authored Nov 27, 2005
31 -author('nicolas.niclausse@niclux.org').
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
32
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
33 -export([parse_config/2, parse_URL/1, set_port/1, set_scheme/1,
5a453e7 @nniclausse set relative URL when url is substituted (fix Cookie issue TSUN-110)
nniclausse authored Feb 14, 2009
34 check_user_agent_sum/1, set_query/1]).
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
35
36 -include("ts_profile.hrl").
37 -include("ts_http.hrl").
38 -include("ts_config.hrl").
9dc328f @cstar [TECH-1154] OAuth support
cstar authored Nov 8, 2010
39
bc45901 @nniclausse improved configure: add --with-erlang option and xmerl PATH detection…
nniclausse authored Oct 28, 2004
40 -include("xmerl.hrl").
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
41
42 %%----------------------------------------------------------------------
43 %% Func: parse_config/2
44 %% Args: Element, Config
45 %% Returns: List
46 %% Purpose: parse a request defined in the XML config file
47 %%----------------------------------------------------------------------
48 %% Parsing other elements
49 parse_config(Element = #xmlElement{name=dyn_variable}, Conf = #config{}) ->
50 ts_config:parse(Element,Conf);
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
51 parse_config(Element = #xmlElement{name=http},
9dc328f @cstar [TECH-1154] OAuth support
cstar authored Nov 8, 2010
52 Config=#config{curid = Id, session_tab = Tab, servers = [Server|_] = Servers,
07b9061 @nniclausse code cleanup
nniclausse authored Oct 12, 2004
53 sessions = [CurS | _], dynvar=DynVar,
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
54 subst = SubstFlag, match=MatchRegExp}) ->
a5a6fd1 @nniclausse fix TSUN-23 : set default http version.
nniclausse authored Aug 9, 2006
55 Version = ts_config:getAttr(string,Element#xmlElement.attributes, version, "1.1"),
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
56 URL = ts_config:getAttr(Element#xmlElement.attributes, url),
499e3ac @nniclausse add option to read http contents from a file (TSUN-69)
nniclausse authored Apr 18, 2008
57 Contents = case ts_config:getAttr(string, Element#xmlElement.attributes, contents_from_file) of
58 [] ->
59 C=ts_config:getAttr(Element#xmlElement.attributes, contents),
60 list_to_binary(C);
61 FileName ->
62 {ok, FileContent} = file:read_file(FileName),
63 FileContent
64 end,
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
65 UseProxy = case ets:lookup(Tab,{http_use_server_as_proxy}) of
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
66 [] -> false;
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
67 _ -> true
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
68 end,
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
69 %% Apache Tomcat applications need content-type informations to read post forms
2537083 @nniclausse refactoring
nniclausse authored Jul 15, 2005
70 ContentType = ts_config:getAttr(string,Element#xmlElement.attributes,
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
71 content_type, "application/x-www-form-urlencoded"),
72 Date = ts_config:getAttr(string, Element#xmlElement.attributes,
73 'if_modified_since', undefined),
5b764cc @nniclausse default method for http is GET
nniclausse authored Feb 1, 2008
74 Method = list_to_atom(ts_utils:to_lower(ts_config:getAttr(string, Element#xmlElement.attributes, method, "get"))),
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
75 Request = #http_request{url = URL,
76 method = Method,
77 version = Version,
78 get_ims_date= Date,
79 content_type= ContentType,
499e3ac @nniclausse add option to read http contents from a file (TSUN-69)
nniclausse authored Apr 18, 2008
80 body = Contents},
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
81 %% SOAP Support: Add SOAPAction header to the message
82 Request2 = case lists:keysearch(soap,#xmlElement.name,
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
83 Element#xmlElement.content) of
84 {value, SoapEl=#xmlElement{} } ->
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
85 SOAPAction = ts_config:getAttr(SoapEl#xmlElement.attributes,
2537083 @nniclausse refactoring
nniclausse authored Jul 15, 2005
86 action),
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
87 Request#http_request{soap_action=SOAPAction};
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
88 _ ->
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
89 Request
90 end,
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
91 PreviousHTTPServer = get_previous_http_server(Tab, CurS#session.id),
34e4b3d @nniclausse option to manually add a cookie in http requests (TSUN-111)
nniclausse authored Feb 22, 2009
92 %% client side cookies
93 Cookies = parse_cookie(Element#xmlElement.content, []),
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
94 %% Custom HTTP headers
34e4b3d @nniclausse option to manually add a cookie in http requests (TSUN-111)
nniclausse authored Feb 22, 2009
95 Headers= parse_headers(Element#xmlElement.content,
96 Request2#http_request.headers),
97 Request3 = Request2#http_request{headers=Headers,cookie=Cookies},
448e977 @nniclausse tabs/spaces
nniclausse authored Nov 28, 2007
98 %% HTTP Authentication
9dc328f @cstar [TECH-1154] OAuth support
cstar authored Nov 8, 2010
99 Request4 = case lists:keysearch(www_authenticate, #xmlElement.name,
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
100 Element#xmlElement.content) of
101 {value, AuthEl=#xmlElement{} } ->
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
102 UserId= ts_config:getAttr(string,AuthEl#xmlElement.attributes,
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
103 userid, undefined),
eb59f73 @cstar rfc2617 HTTP DIGEST authentication
cstar authored Nov 12, 2010
104 Type = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
c6207d5 @nniclausse default authentication is basic
nniclausse authored Sep 7, 2012
105 type, "basic"),
eb59f73 @cstar rfc2617 HTTP DIGEST authentication
cstar authored Nov 12, 2010
106 Nonce = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
107 nonce, undefined),
108 Opaque = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
109 opaque, undefined),
110 Cnonce = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
111 cnonce, "%%ts_user_server:get_really_unique_id%%"),
112 Nc = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
113 nc, "00000001"),
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
114 Passwd= ts_config:getAttr(string,AuthEl#xmlElement.attributes,
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
115 passwd, undefined),
eb59f73 @cstar rfc2617 HTTP DIGEST authentication
cstar authored Nov 12, 2010
116 Realm = ts_config:getAttr(string,AuthEl#xmlElement.attributes,
117 realm, undefined),
118 ?LOGF("DIGEST ? : ~p ~p ~p", [Type, Nonce, Realm], ?WARN),
119 Request3#http_request{userid=UserId, passwd=Passwd,
120 auth_type=Type, digest_nonce=Nonce,
121 digest_cnonce=Cnonce, digest_nc=Nc,
122 digest_opaque=Opaque, realm=Realm};
9dc328f @cstar [TECH-1154] OAuth support
cstar authored Nov 8, 2010
123 _ ->
124 Request3
125 end,
126 %% OAuth
127 Msg = case lists:keysearch(oauth, #xmlElement.name,
128 Element#xmlElement.content) of
129 {value, AuthEl2=#xmlElement{} } ->
130 ConsumerKey = ts_config:getAttr(string,AuthEl2#xmlElement.attributes,
131 consumer_key, undefined),
132 ConsumerSecret = ts_config:getAttr(string,AuthEl2#xmlElement.attributes,
133 consumer_secret, undefined),
134 AccessToken = ts_config:getAttr(string,AuthEl2#xmlElement.attributes,
135 access_token, []),
136 AccessTokenSecret = ts_config:getAttr(string,AuthEl2#xmlElement.attributes,
137 access_token_secret, []),
138 Encoding = ts_config:getAttr(string,AuthEl2#xmlElement.attributes,
139 encoding, "HMAC-SHA1"),
140 AbsoluteURL = case URL of
141 "http" ++ _Rest ->
142 hd(string:tokens(URL, "?"));
143 _Relative ->
144 server_to_url(Server) ++ hd(string:tokens(URL, "?"))
145 end,
146 SigEncoding = case Encoding of
147 "PLAINTEXT" ->
148 plaintext;
149 "HMAC-SHA1" ->
150 hmac_sha1;
151 "RSA-SHA1" ->
152 rsa_sha1
153 end,
154 NewReq=Request4#http_request{oauth_access_token=AccessToken,
155 oauth_url=AbsoluteURL,
156 oauth_access_secret=AccessTokenSecret,
157 oauth_consumer={ConsumerKey,
158 ConsumerSecret,
159 SigEncoding}},
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
160 set_msg(NewReq, {SubstFlag, MatchRegExp, UseProxy, Servers, PreviousHTTPServer, Tab, CurS#session.id} );
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
161 _ ->
9dc328f @cstar [TECH-1154] OAuth support
cstar authored Nov 8, 2010
162 set_msg(Request4, {SubstFlag, MatchRegExp, UseProxy, Servers, PreviousHTTPServer, Tab, CurS#session.id} )
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
163 end,
164 ts_config:mark_prev_req(Id-1, Tab, CurS),
165 ets:insert(Tab,{{CurS#session.id, Id},Msg#ts_request{endpage=true,
166 dynvar_specs=DynVar}}),
167 lists:foldl( fun(A,B)->ts_config:parse(A,B) end,
fc3c8a5 @nniclausse refactoring: add ts_dynvars api (TSUN-81). Initial api from Pablo Pol…
nniclausse authored Oct 7, 2008
168 Config#config{dynvar=[]},
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
169 Element#xmlElement.content);
170 %% Parsing default values
7155210 @nniclausse major update to DTD : add sessions, load, options, information; renam…
nniclausse authored Dec 4, 2005
171 parse_config(Element = #xmlElement{name=option}, Conf = #config{session_tab = Tab}) ->
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
172 case ts_config:getAttr(Element#xmlElement.attributes, name) of
c640d11 @nniclausse http: user_agent is now customizable in the xml config file.
nniclausse authored Jul 11, 2005
173 "user_agent" ->
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
174 lists:foldl( fun(A,B)->parse_config(A,B) end, Conf, Element#xmlElement.content);
175 "http_use_server_as_proxy" ->
176 Val = ts_config:getAttr(Element#xmlElement.attributes, value),
177 ets:insert(Tab,{{http_use_server_as_proxy}, Val})
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
178 end,
179 lists:foldl( fun(A,B)->ts_config:parse(A,B) end, Conf, Element#xmlElement.content);
c640d11 @nniclausse http: user_agent is now customizable in the xml config file.
nniclausse authored Jul 11, 2005
180 %% Parsing user_agent
181 parse_config(Element = #xmlElement{name=user_agent}, Conf = #config{session_tab = Tab}) ->
7155210 @nniclausse major update to DTD : add sessions, load, options, information; renam…
nniclausse authored Dec 4, 2005
182 Proba = ts_config:getAttr(integer,Element#xmlElement.attributes, probability),
ad283a1 @nniclausse ts_config:getText was completely broken (split the string with % !). …
nniclausse authored Jun 14, 2006
183 ValRaw = ts_config:getText(Element#xmlElement.content),
c474d40 @nniclausse clean user_agent string
nniclausse authored Aug 20, 2005
184 Val = ts_utils:clean_str(ValRaw),
7155210 @nniclausse major update to DTD : add sessions, load, options, information; renam…
nniclausse authored Dec 4, 2005
185 ?LOGF("Get user agent: ~p ~p ~n",[Proba, Val],?WARN),
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
186 Previous = case ets:lookup(Tab, {http_user_agent, value}) of
c640d11 @nniclausse http: user_agent is now customizable in the xml config file.
nniclausse authored Jul 11, 2005
187 [] ->
188 [];
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
189 [{_Key,Old}] ->
c640d11 @nniclausse http: user_agent is now customizable in the xml config file.
nniclausse authored Jul 11, 2005
190 Old
191 end,
7155210 @nniclausse major update to DTD : add sessions, load, options, information; renam…
nniclausse authored Dec 4, 2005
192 ets:insert(Tab,{{http_user_agent, value}, [{Proba, Val}|Previous]}),
c640d11 @nniclausse http: user_agent is now customizable in the xml config file.
nniclausse authored Jul 11, 2005
193 lists:foldl( fun(A,B)->parse_config(A,B) end, Conf, Element#xmlElement.content);
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
194 %% Parsing other elements
195 parse_config(Element = #xmlElement{}, Conf = #config{}) ->
196 ts_config:parse(Element,Conf);
197 %% Parsing non #xmlElement elements
07b9061 @nniclausse code cleanup
nniclausse authored Oct 12, 2004
198 parse_config(_, Conf = #config{}) ->
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
199 Conf.
200
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
201 get_previous_http_server(Ets, Id) ->
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
202 case ets:lookup(Ets, {http_server, Id}) of
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
203 [] -> [];
204 [{_Key,PrevServ}] -> PrevServ
205 end.
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
206
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
207 parse_headers([], Headers) ->
208 Headers;
209 parse_headers([Element = #xmlElement{name=http_header} | Tail], Headers) ->
210 Name = ts_config:getAttr(string, Element#xmlElement.attributes, name),
211 Value = ts_config:getAttr(string, Element#xmlElement.attributes, value),
212 EncodedValue = case ts_config:getAttr(atom, Element#xmlElement.attributes, encoding, none) of
213 base64 ->
56050db @nniclausse use new funs in base64 and string modules for R12B compatibility (TSU…
nniclausse authored Jan 5, 2008
214 ts_utils:encode_base64(Value);
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
215 none ->
216 Value
217 end,
218 parse_headers(Tail, [{Name, EncodedValue} | Headers]);
219 parse_headers([_| Tail], Headers) ->
220 parse_headers(Tail, Headers).
221
34e4b3d @nniclausse option to manually add a cookie in http requests (TSUN-111)
nniclausse authored Feb 22, 2009
222 parse_cookie([], Cookies) ->
223 Cookies;
224 parse_cookie([Element = #xmlElement{name=add_cookie} | Tail], Cookies) ->
225 Key = ts_config:getAttr(string, Element#xmlElement.attributes, key),
226 Value = ts_config:getAttr(string, Element#xmlElement.attributes, value),
227 Path = ts_config:getAttr(string, Element#xmlElement.attributes, path,"/"),
228 Domain= ts_config:getAttr(string,Element#xmlElement.attributes,domain,"."),
229 parse_cookie(Tail,[#cookie{key=Key,value=Value,path=Path,domain=Domain}|Cookies]);
230 parse_cookie([_| Tail], Cookies) ->
231 parse_cookie(Tail, Cookies).
232
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
233 %%----------------------------------------------------------------------
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
234 %% Func: set_msg/2
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
235 %% Returns: #ts_request record
07b9061 @nniclausse code cleanup
nniclausse authored Oct 12, 2004
236 %% Purpose: build the #ts_request record from an #http_request,
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
237 %% and Substition def.
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
238 %%----------------------------------------------------------------------
239 %% if the URL is full (http://...), we parse it and get server host,
240 %% port and scheme from the URL and override the global setup of the
241 %% server. These informations are stored in the #ts_request record.
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
242 set_msg(HTTP=#http_request{url="http" ++ URL},
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
243 {SubstFlag, MatchRegExp, UseProxy, [Server|_], _PrevHTTPServer, Tab, Id}) ->
244 URLrec = parse_URL("http" ++ URL),
245 Path = set_query(URLrec),
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
246 HostHeader = set_host_header(URLrec),
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
247 Port = set_port(URLrec),
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
248 Scheme = set_scheme({URLrec#url.scheme,Server#server.type}),
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
249 ets:insert(Tab,{{http_server, Id}, {HostHeader, URLrec#url.scheme}}),
250
251 {RealServer, RealPath} = case UseProxy of
252 true -> {Server, "http"++ URL};
253 false -> {#server{host=URLrec#url.host, port=Port, type=Scheme},
254 Path}
255 end,
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
256
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
257 set_msg2(HTTP#http_request{url=RealPath, host_header = HostHeader},
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
258 #ts_request{ack = parse,
259 subst = SubstFlag,
260 match = MatchRegExp,
261 host = RealServer#server.host,
262 scheme = RealServer#server.type,
263 port = RealServer#server.port});
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
264
265 %% relative URL, no previous HTTP server, use proxy, error !
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
266 set_msg(_, {_, _, true, _Server, [],_Tab,_Id}) ->
928c56d @nniclausse fix url substitution (TSUN-155)
nniclausse authored Aug 3, 2010
267 ?LOG("Need absolute URL when using a proxy ! Abort",?ERR),
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
268 throw({error, badurl_proxy});
928c56d @nniclausse fix url substitution (TSUN-155)
nniclausse authored Aug 3, 2010
269 %% url head is a dynvar, don't preset host header
d03a220 @nniclausse Merge branch 're'
nniclausse authored Aug 18, 2010
270 set_msg(HTTP=#http_request{url="%%" ++ _TailURL}, {true, MatchRegExp, _Proxy, _Servers, _Headers,_Tab,_Id}) ->
928c56d @nniclausse fix url substitution (TSUN-155)
nniclausse authored Aug 3, 2010
271 set_msg2(HTTP,
272 #ts_request{ack = parse, subst = true, match = MatchRegExp });
f3816a5 @nniclausse add support for multiples servers. Use RoundRobin to choose the serve…
nniclausse authored Dec 9, 2005
273 %% relative URL, no proxy, a single server => we can preset host header at configuration time
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
274 set_msg(HTTPRequest, Args={_SubstFlag, _MatchRegExp, false, [Server], [],_Tab,_Id}) ->
590533a @nniclausse change log level of controller msgs
nniclausse authored Aug 21, 2012
275 ?LOG("Relative URL, single server ",?INFO),
f3816a5 @nniclausse add support for multiples servers. Use RoundRobin to choose the serve…
nniclausse authored Dec 9, 2005
276 URL = server_to_url(Server) ++ HTTPRequest#http_request.url,
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
277 set_msg(HTTPRequest#http_request{url=URL}, Args);
f3816a5 @nniclausse add support for multiples servers. Use RoundRobin to choose the serve…
nniclausse authored Dec 9, 2005
278 %% relative URL, no proxy, several servers: don't set host header
279 %% since the real server will be choose at run time
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
280 set_msg(HTTPRequest, {SubstFlag, MatchRegExp, false, _Servers, [],_Tab,_Id}) ->
281 set_msg2(HTTPRequest,
f3816a5 @nniclausse add support for multiples servers. Use RoundRobin to choose the serve…
nniclausse authored Dec 9, 2005
282 #ts_request{ack = parse, subst = SubstFlag, match = MatchRegExp });
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
283 %% relative URL, no proxy
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
284 set_msg(HTTPRequest, {SubstFlag, MatchRegExp, false, _Server, {HostHeader,_},_Tab,_Id}) ->
285 set_msg2(HTTPRequest#http_request{host_header= HostHeader},
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
286 #ts_request{ack = parse, subst = SubstFlag, match = MatchRegExp });
f3816a5 @nniclausse add support for multiples servers. Use RoundRobin to choose the serve…
nniclausse authored Dec 9, 2005
287 %% relative URL, use proxy
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
288 set_msg(HTTPRequest, {SubstFlag, MatchRegExp, true, Server, {HostHeader, PrevScheme},Tab,Id}) ->
928c56d @nniclausse fix url substitution (TSUN-155)
nniclausse authored Aug 3, 2010
289 %% set absolute URL using previous Server used.
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
290 URL = atom_to_list(PrevScheme) ++ "://" ++ HostHeader ++ HTTPRequest#http_request.url,
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
291 set_msg(HTTPRequest#http_request{url=URL}, {SubstFlag, MatchRegExp, true, Server, {HostHeader, PrevScheme},Tab,Id}).
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
292
293 %% Func: set_mgs2/3
194f479 @nniclausse remove dead code
nniclausse authored Jul 22, 2008
294 %% Purpose: set param in ts_request
295 set_msg2(HTTPRequest, Msg) -> Msg#ts_request{ param = HTTPRequest }.
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
296
db32eae @nniclausse merge master
nniclausse authored Sep 6, 2012
297 server_to_url(#server{port=443, host= Host, type= Type}) when Type==ts_ssl orelse Type==ts_ssl6->
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
298 "https://" ++ encode_ipv6_address(Host);
db32eae @nniclausse merge master
nniclausse authored Sep 6, 2012
299 server_to_url(#server{port=Port, host= Host, type= Type})when Type==ts_ssl orelse Type==ts_ssl6->
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
300 "https://" ++ encode_ipv6_address(Host) ++ ":" ++ integer_to_list(Port);
301 server_to_url(#server{port=80, host= Host})->
302 "http://" ++ encode_ipv6_address(Host);
303 server_to_url(#server{port=Port, host= Host})->
304 "http://" ++ encode_ipv6_address(Host) ++ ":" ++ integer_to_list(Port).
681df4d @nniclausse new: we can bench an HTTP proxy, with http_use_server_as_proxy option.
nniclausse authored Jul 19, 2005
305
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
306 %%--------------------------------------------------------------------
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
307 %% Func: set_host_header/1
308 %%--------------------------------------------------------------------
76b4bdd @nniclausse don't add ":443" in the Host header for https (fix TSUN-68)
nniclausse authored Apr 17, 2008
309 %% if port is undefined, don't need to set port, because it use the default (80 or 443)
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
310 set_host_header(#url{host=Host,port=undefined}) -> encode_ipv6_address(Host);
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
311 set_host_header(#url{host=Host,port=Port}) when is_integer(Port) ->
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
312 encode_ipv6_address(Host) ++ ":" ++ integer_to_list(Port).
313
314
315 encode_ipv6_address(Host) when hd(Host)==$[ ->
316 %% ipv6; already using [] (rfc2732), no need to add
317 Host;
318 encode_ipv6_address(Host)->
319 case string:chr(Host,$:) of
320 0 -> Host; % regular name or ipv4 address
321 _ -> "["++Host++"]"
322 end.
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
323
324 %%--------------------------------------------------------------------
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
325 %% Func: set_port/1
326 %% Purpose: Returns port according to scheme if not already defined
327 %% Returns: PortNumber (integer)
328 %%--------------------------------------------------------------------
329 set_port(#url{scheme=https,port=undefined}) -> 443;
330 set_port(#url{scheme=http,port=undefined}) -> 80;
331 set_port(#url{port=Port}) when is_integer(Port) -> Port;
332 set_port(#url{port=Port}) -> integer_to_list(Port).
333
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
334 %% @spec set_scheme({http|https,gen_tcp|gen_tcp6|ssl|ssl6})-> gen_tcp|gen_tcp6|ssl|ssl6
335 %% @doc set scheme for given protocol and server setup. If the main
336 %% server is configured with IPv6, we assume that the we should also
337 %% use IPv6 for the given absolut URL
338 %% @end
db32eae @nniclausse merge master
nniclausse authored Sep 6, 2012
339 set_scheme({http, gen_tcp6}) -> ts_tcp6;
340 set_scheme({http, ssl6}) -> ts_tcp6;
341 set_scheme({http, _}) -> ts_tcp;
342 set_scheme({https, ssl6}) -> ts_ssl6;
343 set_scheme({https, gen_tcp6})-> ts_ssl6;
344 set_scheme({https, _}) -> ts_ssl.
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
345
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
346
347 set_query(URLrec = #url{querypart=""}) ->
348 URLrec#url.path;
349 set_query(URLrec = #url{}) ->
350 URLrec#url.path ++ "?" ++ URLrec#url.querypart.
351
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
352
353 %%----------------------------------------------------------------------
354 %% Func: parse_URL/1
355 %% Returns: #url
356 %%----------------------------------------------------------------------
357 parse_URL("https://" ++ String) ->
358 parse_URL(host, String, [], #url{scheme=https});
359 parse_URL("http://" ++ String) ->
d7fd02a @nniclausse preliminary support for CONNECT in recorder
nniclausse authored Sep 19, 2005
360 parse_URL(host, String, [], #url{scheme=http});
361 parse_URL(String) when is_list(String) ->
3f088d1 @nniclausse allow recording of relative URLs (reverse proxy setup)
nniclausse authored Aug 27, 2010
362 case string:tokens(String,":") of
363 [Host, Port] ->
364 #url{scheme=connect, host=Host, port=list_to_integer(Port)};
365 RelativeURL when is_list(RelativeURL) ->
366 parse_URL(path, RelativeURL, [], #url{scheme=http})
367 end.
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
368
369 %%----------------------------------------------------------------------
370 %% Func: parse_URL/4 (inspired by yaws_api.erl)
371 %% Returns: #url record
372 %%----------------------------------------------------------------------
373 % parse host
19df298 @nniclausse IPv6 support, first try (TSUN-202)
nniclausse authored Dec 1, 2011
374 parse_URL(host, [$[|Tail] , [], URL) -> % host starts with '[': ipv6 address in url
375 {match,[Host,Rest]} = re:run(Tail,"^([^\\]]*)\\](.*)",[{capture,all_but_first,list}]),
376 parse_URL(host, Rest, lists:reverse(Host), URL);
bce3b7d @nniclausse code cleanup (put config stuff in dedicated modules in
nniclausse authored Apr 20, 2004
377 parse_URL(host, [], Acc, URL) -> % no path or port
378 URL#url{host=lists:reverse(Acc), path= "/"};
379 parse_URL(host, [$/|Tail], Acc, URL) -> % path starts here
380 parse_URL(path, Tail, "/", URL#url{host=lists:reverse(Acc)});
381 parse_URL(host, [$:|Tail], Acc, URL) -> % port starts here
382 parse_URL(port, Tail, [], URL#url{host=lists:reverse(Acc)});
383 parse_URL(host, [H|Tail], Acc, URL) ->
384 parse_URL(host, Tail, [H|Acc], URL);
385
386 % parse port
387 parse_URL(port,[], Acc, URL) ->
388 URL#url{port=list_to_integer(lists:reverse(Acc)), path= "/"};
389 parse_URL(port,[$/|T], Acc, URL) ->
390 parse_URL(path, T, "/", URL#url{port=list_to_integer(lists:reverse(Acc))});
391 parse_URL(port,[H|T], Acc, URL) ->
392 parse_URL(port, T, [H|Acc], URL);
393
394 % parse path
395 parse_URL(path,[], Acc, URL) ->
396 URL#url{path=lists:reverse(Acc)};
397 parse_URL(path,[$?|T], Acc, URL) ->
398 URL#url{path=lists:reverse(Acc), querypart=T};
399 parse_URL(path,[H|T], Acc, URL) ->
400 parse_URL(path, T, [H|Acc], URL).
b628eaa @nniclausse - set the Host header during configuration phase
nniclausse authored Jul 19, 2005
401
7155210 @nniclausse major update to DTD : add sessions, load, options, information; renam…
nniclausse authored Dec 4, 2005
402 % check if the sum of all user agent probabilities is equal to 100%
069be48 @nniclausse add check for user agent frequency sum
nniclausse authored Aug 9, 2005
403 check_user_agent_sum(Tab) ->
404 case ets:lookup(Tab, {http_user_agent, value}) of
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
405 [] ->
069be48 @nniclausse add check for user agent frequency sum
nniclausse authored Aug 9, 2005
406 ok; % no user agent, will use the default one.
657fd3f @nniclausse add custom headers for HTTP (fix TSUN-34, patch from TSUN-33 by Jerom…
nniclausse authored Jun 6, 2007
407 [{_Key, UserAgents}] ->
069be48 @nniclausse add check for user agent frequency sum
nniclausse authored Aug 9, 2005
408 ts_utils:check_sum(UserAgents, 1, ?USER_AGENT_ERROR_MSG)
409 end.
Something went wrong with that request. Please try again.