1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0//EN">
6 <style type=
"text/css">
9 font-family: verdana
, sans-serif
;
29 text-decoration:underline
;
34 text-decoration: none
;
47 border-bottom-width:1px;
48 border-bottom-style:dotted
;
64 <table width=
"90%" border=
"0" cellspacing=
"1" cellpadding=
"5">
67 <big><tt>Global.helma.Http
()
</tt></big><br>
73 Creates a new instance of helma.Http
<br><br>
77 <li><b>Returns
</b><br>
78 A newly created helma.Http instance
89 <table width=
"90%" border=
"0" cellspacing=
"1" cellpadding=
"5">
91 <td>Sourcecode in /root/helma-
1.6.1/modules/helma/Http.js:
92 <pre><font color=
"#aaaaaa">44:
</font> helma.Http = function() {
93 <font color=
"#aaaaaa">45:
</font> var self = this;
94 <font color=
"#aaaaaa">46:
</font> var proxy = null;
95 <font color=
"#aaaaaa">47:
</font> var content =
<font color=
"#9999aa">""</font>;
96 <font color=
"#aaaaaa">48:
</font> var userAgent =
<font color=
"#9999aa">"Helma Http Client
"</font>;
97 <font color=
"#aaaaaa">49:
</font> var method =
<font color=
"#9999aa">"GET
"</font>;
98 <font color=
"#aaaaaa">50:
</font> var cookies = null;
99 <font color=
"#aaaaaa">51:
</font> var credentials = null;
100 <font color=
"#aaaaaa">52:
</font> var followRedirects = true;
101 <font color=
"#aaaaaa">53:
</font> var binaryMode = false;
102 <font color=
"#aaaaaa">54:
</font> var headers = {};
103 <font color=
"#aaaaaa">55:
</font> var timeout = {
104 <font color=
"#aaaaaa">56:
</font> <font color=
"#9999aa">"connect
"</font>:
0,
105 <font color=
"#aaaaaa">57:
</font> <font color=
"#9999aa">"socket
"</font>:
0
106 <font color=
"#aaaaaa">58:
</font> };
107 <font color=
"#aaaaaa">59:
</font> var maxResponseSize = null;
108 <font color=
"#aaaaaa">60:
</font>
109 <font color=
"#aaaaaa">61:
</font> var responseHandler = function(connection, result) {
110 <font color=
"#aaaaaa">62:
</font> var input;
111 <font color=
"#aaaaaa">63:
</font> try {
112 <font color=
"#aaaaaa">64:
</font> input = new java.io.BufferedInputStream(connection.getInputStream());
113 <font color=
"#aaaaaa">65:
</font> } catch (error) {
114 <font color=
"#aaaaaa">66:
</font> input = new java.io.BufferedInputStream(connection.getErrorStream());
115 <font color=
"#aaaaaa">67:
</font> }
116 <font color=
"#aaaaaa">68:
</font> if (input) {
117 <font color=
"#aaaaaa">69:
</font> var body = new java.io.ByteArrayOutputStream();
118 <font color=
"#aaaaaa">70:
</font> var buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE,
1024);
119 <font color=
"#aaaaaa">71:
</font> var len;
120 <font color=
"#aaaaaa">72:
</font> var currentSize =
0;
121 <font color=
"#aaaaaa">73:
</font> while ((len = input.read(buf))
> -
1) {
122 <font color=
"#aaaaaa">74:
</font> body.write(buf,
0, len);
123 <font color=
"#aaaaaa">75:
</font> currentSize += len;
124 <font color=
"#aaaaaa">76:
</font> if (maxResponseSize
&& currentSize
> maxResponseSize) {
125 <font color=
"#aaaaaa">77:
</font> throw new Error(
<font color=
"#9999aa">"Maximum allowed response size is exceeded
"</font>);
126 <font color=
"#aaaaaa">78:
</font> }
127 <font color=
"#aaaaaa">79:
</font> }
128 <font color=
"#aaaaaa">80:
</font> try {
129 <font color=
"#aaaaaa">81:
</font> input.close();
130 <font color=
"#aaaaaa">82:
</font> } catch (error) {
131 <font color=
"#aaaaaa">83:
</font> <font color=
"#33aa00"> // safe to ignore
132 <font color=
"#aaaaaa">84:
</font> </font> }
133 <font color=
"#aaaaaa">85:
</font> if (binaryMode
&& (result.code
>=
200 && result.code
< 300)) {
134 <font color=
"#aaaaaa">86:
</font> <font color=
"#33aa00"> // only honor binaryMode if the request succeeded
135 <font color=
"#aaaaaa">87:
</font> </font> result.content = body.toByteArray();
136 <font color=
"#aaaaaa">88:
</font> } else {
137 <font color=
"#aaaaaa">89:
</font> result.content = result.charset ?
138 <font color=
"#aaaaaa">90:
</font> body.toString(result.charset) :
139 <font color=
"#aaaaaa">91:
</font> body.toString();
140 <font color=
"#aaaaaa">92:
</font> }
141 <font color=
"#aaaaaa">93:
</font> <font color=
"#33aa00"> // adjust content length
142 <font color=
"#aaaaaa">94:
</font> </font> if (result.content) {
143 <font color=
"#aaaaaa">95:
</font> result.length = result.content.length;
144 <font color=
"#aaaaaa">96:
</font> }
145 <font color=
"#aaaaaa">97:
</font> }
146 <font color=
"#aaaaaa">98:
</font> };
147 <font color=
"#aaaaaa">99:
</font>
148 <font color=
"#aaaaaa">100:
</font> /** @private */
149 <font color=
"#aaaaaa">101:
</font> var setTimeout = function(type, value) {
150 <font color=
"#aaaaaa">102:
</font> var v = java.lang.System.getProperty(
<font color=
"#9999aa">"java.specification.version
"</font>);
151 <font color=
"#aaaaaa">103:
</font> if (parseFloat(v,
10)
>=
1.5) {
152 <font color=
"#aaaaaa">104:
</font> timeout[type] = value;
153 <font color=
"#aaaaaa">105:
</font> } else {
154 <font color=
"#aaaaaa">106:
</font> app.logger.warn(
<font color=
"#9999aa">"helma.Http: Timeouts can only be set with Java Runtime version
>=
1.5"</font>);
155 <font color=
"#aaaaaa">107:
</font> }
156 <font color=
"#aaaaaa">108:
</font> return true;
157 <font color=
"#aaaaaa">109:
</font> }
158 <font color=
"#aaaaaa">110:
</font>
159 <font color=
"#aaaaaa">111:
</font> /**
160 <font color=
"#aaaaaa">112:
</font> * Sets the proxy host and port for later use. The argument must
161 <font color=
"#aaaaaa">113:
</font> * be in
<code
>host:port
</code
> format (eg.
<font color=
"#9999aa">"proxy.example.com:
3128"</font>).
162 <font color=
"#aaaaaa">114:
</font> * @param {String} proxyString The proxy to use for this request
163 <font color=
"#aaaaaa">115:
</font> * @see #getProxy
164 <font color=
"#aaaaaa">116:
</font> */
165 <font color=
"#aaaaaa">117:
</font> this.setProxy = function(proxyString) {
166 <font color=
"#aaaaaa">118:
</font> var idx = proxyString.indexOf(
<font color=
"#9999aa">":
"</font>);
167 <font color=
"#aaaaaa">119:
</font> var host = proxyString.substring(
0, idx);
168 <font color=
"#aaaaaa">120:
</font> var port = proxyString.substring(idx+
1);
169 <font color=
"#aaaaaa">121:
</font> if (java.lang.Class.forName(
<font color=
"#9999aa">"java.net.Proxy
"</font>) != null) {
170 <font color=
"#aaaaaa">122:
</font> <font color=
"#33aa00"> // construct a proxy instance
171 <font color=
"#aaaaaa">123:
</font> </font> var socket = new java.net.InetSocketAddress(host, port);
172 <font color=
"#aaaaaa">124:
</font> proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, socket);
173 <font color=
"#aaaaaa">125:
</font> } else {
174 <font color=
"#aaaaaa">126:
</font> <font color=
"#33aa00"> // the pre jdk1.5 way: set the system properties
175 <font color=
"#aaaaaa">127:
</font> </font> var sys = java.lang.System.getProperties();
176 <font color=
"#aaaaaa">128:
</font> if (host) {
177 <font color=
"#aaaaaa">129:
</font> app.logger.warn(
<font color=
"#9999aa">"[Helma Http Client] WARNING: setting system http proxy to
"</font> + host +
<font color=
"#9999aa">":
"</font> + port);
178 <font color=
"#aaaaaa">130:
</font> sys.put(
<font color=
"#9999aa">"http.proxySet
"</font>,
<font color=
"#9999aa">"true
"</font>);
179 <font color=
"#aaaaaa">131:
</font> sys.put(
<font color=
"#9999aa">"http.proxyHost
"</font>, host);
180 <font color=
"#aaaaaa">132:
</font> sys.put(
<font color=
"#9999aa">"http.proxyPort
"</font>, port);
181 <font color=
"#aaaaaa">133:
</font> }
182 <font color=
"#aaaaaa">134:
</font> }
183 <font color=
"#aaaaaa">135:
</font> return;
184 <font color=
"#aaaaaa">136:
</font> };
185 <font color=
"#aaaaaa">137:
</font>
186 <font color=
"#aaaaaa">138:
</font> /**
187 <font color=
"#aaaaaa">139:
</font> * Returns the proxy in
<code
>host:port
</code
> format
188 <font color=
"#aaaaaa">140:
</font> * @return The proxy defined for this request
189 <font color=
"#aaaaaa">141:
</font> * @type String
190 <font color=
"#aaaaaa">142:
</font> * @see #setProxy
191 <font color=
"#aaaaaa">143:
</font> */
192 <font color=
"#aaaaaa">144:
</font> this.getProxy = function() {
193 <font color=
"#aaaaaa">145:
</font> if (proxy != null) {
194 <font color=
"#aaaaaa">146:
</font> return proxy.address().getHostName() +
<font color=
"#9999aa">":
"</font> + proxy.address().getPort();
195 <font color=
"#aaaaaa">147:
</font> } else if (sys.get(
<font color=
"#9999aa">"http.proxySet
"</font>) ==
<font color=
"#9999aa">"true
"</font>) {
196 <font color=
"#aaaaaa">148:
</font> return sys.get(
<font color=
"#9999aa">"http.proxyHost
"</font>) +
<font color=
"#9999aa">":
"</font> + sys.get(
<font color=
"#9999aa">"http.proxyPort
"</font>);
197 <font color=
"#aaaaaa">149:
</font> } else {
198 <font color=
"#aaaaaa">150:
</font> return null;
199 <font color=
"#aaaaaa">151:
</font> }
200 <font color=
"#aaaaaa">152:
</font> };
201 <font color=
"#aaaaaa">153:
</font>
202 <font color=
"#aaaaaa">154:
</font> /**
203 <font color=
"#aaaaaa">155:
</font> * Sets the credentials for basic http authentication
204 <font color=
"#aaaaaa">156:
</font> * @param {String} username The username
205 <font color=
"#aaaaaa">157:
</font> * @param {String} password The password
206 <font color=
"#aaaaaa">158:
</font> */
207 <font color=
"#aaaaaa">159:
</font> this.setCredentials = function(username, password) {
208 <font color=
"#aaaaaa">160:
</font> var str = new java.lang.String(username +
<font color=
"#9999aa">":
"</font> + password);
209 <font color=
"#aaaaaa">161:
</font> credentials = (new Packages.sun.misc.BASE64Encoder()).encode(str.getBytes());
210 <font color=
"#aaaaaa">162:
</font> return;
211 <font color=
"#aaaaaa">163:
</font> }
212 <font color=
"#aaaaaa">164:
</font>
213 <font color=
"#aaaaaa">165:
</font> /**
214 <font color=
"#aaaaaa">166:
</font> * Sets the content to send to the remote server within this request.
215 <font color=
"#aaaaaa">167:
</font> * @param {String|Object} stringOrObject The content of the request, which
216 <font color=
"#aaaaaa">168:
</font> * can be either a string or an object. In the latter case all properties
217 <font color=
"#aaaaaa">169:
</font> * and their values are concatenated into a single string.
218 <font color=
"#aaaaaa">170:
</font> * If a property is an array, then for each value the propertyname and value pair is added.
219 <font color=
"#aaaaaa">171:
</font> * If the name of an array property ends with
<font color=
"#9999aa">"_array
"</font> then the _array part is removed.
220 <font color=
"#aaaaaa">172:
</font> */
221 <font color=
"#aaaaaa">173:
</font> this.setContent = function(stringOrObject) {
222 <font color=
"#aaaaaa">174:
</font> if (stringOrObject != null) {
223 <font color=
"#aaaaaa">175:
</font> if (stringOrObject.constructor == Object) {
224 <font color=
"#aaaaaa">176:
</font> res.push();
225 <font color=
"#aaaaaa">177:
</font> var value;
226 <font color=
"#aaaaaa">178:
</font> for (var key in stringOrObject) {
227 <font color=
"#aaaaaa">179:
</font> value = stringOrObject[key];
228 <font color=
"#aaaaaa">180:
</font> if (value instanceof Array) {
229 <font color=
"#aaaaaa">181:
</font> if (key.substring(key.length -
6) ==
<font color=
"#9999aa">"_array
"</font>)
230 <font color=
"#aaaaaa">182:
</font> key = key.substring(
0,key.length -
6);
231 <font color=
"#aaaaaa">183:
</font> for (var i =
0; i
< value.length; i++) {
232 <font color=
"#aaaaaa">184:
</font> res.write(encodeURIComponent(key));
233 <font color=
"#aaaaaa">185:
</font> res.write(
<font color=
"#9999aa">"=
"</font>);
234 <font color=
"#aaaaaa">186:
</font> res.write(encodeURIComponent(value[i]));
235 <font color=
"#aaaaaa">187:
</font> res.write(
<font color=
"#9999aa">"&"</font>);
236 <font color=
"#aaaaaa">188:
</font> }
237 <font color=
"#aaaaaa">189:
</font> } else {
238 <font color=
"#aaaaaa">190:
</font> res.write(encodeURIComponent(key));
239 <font color=
"#aaaaaa">191:
</font> res.write(
<font color=
"#9999aa">"=
"</font>);
240 <font color=
"#aaaaaa">192:
</font> res.write(encodeURIComponent(value));
241 <font color=
"#aaaaaa">193:
</font> res.write(
<font color=
"#9999aa">"&"</font>);
242 <font color=
"#aaaaaa">194:
</font> }
243 <font color=
"#aaaaaa">195:
</font> }
244 <font color=
"#aaaaaa">196:
</font> content = res.pop();
245 <font color=
"#aaaaaa">197:
</font> content = content.substring(
0, content.length-
1);
246 <font color=
"#aaaaaa">198:
</font> } else {
247 <font color=
"#aaaaaa">199:
</font> content = stringOrObject.toString();
248 <font color=
"#aaaaaa">200:
</font> }
249 <font color=
"#aaaaaa">201:
</font> } else {
250 <font color=
"#aaaaaa">202:
</font> content = null;
251 <font color=
"#aaaaaa">203:
</font> }
252 <font color=
"#aaaaaa">204:
</font> return;
253 <font color=
"#aaaaaa">205:
</font> };
254 <font color=
"#aaaaaa">206:
</font>
255 <font color=
"#aaaaaa">207:
</font> /**
256 <font color=
"#aaaaaa">208:
</font> * Sets the request method to use.
257 <font color=
"#aaaaaa">209:
</font> * @param {String} m The method to use (
<code
>GET
</code
>,
<code
>POST
</code
> ...)
258 <font color=
"#aaaaaa">210:
</font> * @see #getMethod
259 <font color=
"#aaaaaa">211:
</font> */
260 <font color=
"#aaaaaa">212:
</font> this.setMethod = function(m) {
261 <font color=
"#aaaaaa">213:
</font> method = m;
262 <font color=
"#aaaaaa">214:
</font> return;
263 <font color=
"#aaaaaa">215:
</font> };
264 <font color=
"#aaaaaa">216:
</font>
265 <font color=
"#aaaaaa">217:
</font> /**
266 <font color=
"#aaaaaa">218:
</font> * Returns the currently defined request method.
267 <font color=
"#aaaaaa">219:
</font> * @returns The method used
268 <font color=
"#aaaaaa">220:
</font> * @type String
269 <font color=
"#aaaaaa">221:
</font> * @see #setMethod
270 <font color=
"#aaaaaa">222:
</font> */
271 <font color=
"#aaaaaa">223:
</font> this.getMethod = function() {
272 <font color=
"#aaaaaa">224:
</font> return method;
273 <font color=
"#aaaaaa">225:
</font> };
274 <font color=
"#aaaaaa">226:
</font>
275 <font color=
"#aaaaaa">227:
</font> /**
276 <font color=
"#aaaaaa">228:
</font> * Sets a single HTTP request header field
277 <font color=
"#aaaaaa">229:
</font> * @param {String} name The name of the header field
278 <font color=
"#aaaaaa">230:
</font> * @param {String} value The value of the header field
279 <font color=
"#aaaaaa">231:
</font> * @see #getHeader
280 <font color=
"#aaaaaa">232:
</font> */
281 <font color=
"#aaaaaa">233:
</font> this.setHeader = function(name, value) {
282 <font color=
"#aaaaaa">234:
</font> headers[name] = value;
283 <font color=
"#aaaaaa">235:
</font> return;
284 <font color=
"#aaaaaa">236:
</font> };
285 <font color=
"#aaaaaa">237:
</font>
286 <font color=
"#aaaaaa">238:
</font> /**
287 <font color=
"#aaaaaa">239:
</font> * Returns the value of the request header field with the given name
288 <font color=
"#aaaaaa">240:
</font> * @param {String} name The name of the request header field
289 <font color=
"#aaaaaa">241:
</font> * @returns The value of the request header field
290 <font color=
"#aaaaaa">242:
</font> * @type String
291 <font color=
"#aaaaaa">243:
</font> * @see #setHeader
292 <font color=
"#aaaaaa">244:
</font> */
293 <font color=
"#aaaaaa">245:
</font> this.getHeader = function(name) {
294 <font color=
"#aaaaaa">246:
</font> return headers[name];
295 <font color=
"#aaaaaa">247:
</font> };
296 <font color=
"#aaaaaa">248:
</font>
297 <font color=
"#aaaaaa">249:
</font> /**
298 <font color=
"#aaaaaa">250:
</font> * Adds a cookie with the name and value passed as arguments
299 <font color=
"#aaaaaa">251:
</font> * to the list of cookies to send to the remote server.
300 <font color=
"#aaaaaa">252:
</font> * @param {String} name The name of the cookie
301 <font color=
"#aaaaaa">253:
</font> * @param {String} value The value of the cookie
302 <font color=
"#aaaaaa">254:
</font> * @see #getCookie
303 <font color=
"#aaaaaa">255:
</font> * @see #getCookies
304 <font color=
"#aaaaaa">256:
</font> */
305 <font color=
"#aaaaaa">257:
</font> this.setCookie = function(name, value) {
306 <font color=
"#aaaaaa">258:
</font> if (name != null
&& value != null) {
307 <font color=
"#aaaaaa">259:
</font> <font color=
"#33aa00"> // store the cookie in the cookies map
308 <font color=
"#aaaaaa">260:
</font> </font> if (!cookies) {
309 <font color=
"#aaaaaa">261:
</font> cookies = {};
310 <font color=
"#aaaaaa">262:
</font> }
311 <font color=
"#aaaaaa">263:
</font> cookies[name] = new helma.Http.Cookie(name, value);
312 <font color=
"#aaaaaa">264:
</font> }
313 <font color=
"#aaaaaa">265:
</font> return;
314 <font color=
"#aaaaaa">266:
</font> };
315 <font color=
"#aaaaaa">267:
</font>
316 <font color=
"#aaaaaa">268:
</font> /**
317 <font color=
"#aaaaaa">269:
</font> * Returns the value of the cookie with the given name
318 <font color=
"#aaaaaa">270:
</font> * @param {String} name The name of the cookie
319 <font color=
"#aaaaaa">271:
</font> * @returns The value of the cookie
320 <font color=
"#aaaaaa">272:
</font> * @type String
321 <font color=
"#aaaaaa">273:
</font> * @see #setCookie
322 <font color=
"#aaaaaa">274:
</font> */
323 <font color=
"#aaaaaa">275:
</font> this.getCookie = function(name) {
324 <font color=
"#aaaaaa">276:
</font> return (cookies != null) ? cookies[name] : null;
325 <font color=
"#aaaaaa">277:
</font> };
326 <font color=
"#aaaaaa">278:
</font>
327 <font color=
"#aaaaaa">279:
</font> /**
328 <font color=
"#aaaaaa">280:
</font> * Adds the cookies passed as argument to the list of cookies to send
329 <font color=
"#aaaaaa">281:
</font> * to the remote server.
330 <font color=
"#aaaaaa">282:
</font> * @param {Array} cookies An array containing objects with the properties
331 <font color=
"#aaaaaa">283:
</font> *
<font color=
"#9999aa">"name
"</font> (the name of the cookie) and
<font color=
"#9999aa">"value
"</font> (the value of the cookie) set.
332 <font color=
"#aaaaaa">284:
</font> */
333 <font color=
"#aaaaaa">285:
</font> this.setCookies = function(cookies) {
334 <font color=
"#aaaaaa">286:
</font> if (cookies != null) {
335 <font color=
"#aaaaaa">287:
</font> for (var i=
0; i
<cookies.length; i++) {
336 <font color=
"#aaaaaa">288:
</font> this.setCookie(cookies[i].name, cookies[i].value);
337 <font color=
"#aaaaaa">289:
</font> }
338 <font color=
"#aaaaaa">290:
</font> }
339 <font color=
"#aaaaaa">291:
</font> return;
340 <font color=
"#aaaaaa">292:
</font> };
341 <font color=
"#aaaaaa">293:
</font>
342 <font color=
"#aaaaaa">294:
</font> /**
343 <font color=
"#aaaaaa">295:
</font> * Returns all cookies set for this client
344 <font color=
"#aaaaaa">296:
</font> * @return An object containing all cookies, where the property
345 <font color=
"#aaaaaa">297:
</font> * name is the name of the cookie, and the value is the cookie value
346 <font color=
"#aaaaaa">298:
</font> * @see #setCookie
347 <font color=
"#aaaaaa">299:
</font> */
348 <font color=
"#aaaaaa">300:
</font> this.getCookies = function() {
349 <font color=
"#aaaaaa">301:
</font> return cookies;
350 <font color=
"#aaaaaa">302:
</font> };
351 <font color=
"#aaaaaa">303:
</font>
352 <font color=
"#aaaaaa">304:
</font> /**
353 <font color=
"#aaaaaa">305:
</font> * Sets the connection timeout to the amount of milliseconds
354 <font color=
"#aaaaaa">306:
</font> * passed as argument
355 <font color=
"#aaaaaa">307:
</font> * @param {Number} timeout The connection timeout in milliseconds
356 <font color=
"#aaaaaa">308:
</font> * @see #getTimeout
357 <font color=
"#aaaaaa">309:
</font> */
358 <font color=
"#aaaaaa">310:
</font> this.setTimeout = function(timeout) {
359 <font color=
"#aaaaaa">311:
</font> setTimeout(
<font color=
"#9999aa">"connect
"</font>, timeout);
360 <font color=
"#aaaaaa">312:
</font> return;
361 <font color=
"#aaaaaa">313:
</font> };
362 <font color=
"#aaaaaa">314:
</font>
363 <font color=
"#aaaaaa">315:
</font> /**
364 <font color=
"#aaaaaa">316:
</font> * Sets the read timeout (the maximum time a request may take after
365 <font color=
"#aaaaaa">317:
</font> * the connection has been successfully established) to the amount of
366 <font color=
"#aaaaaa">318:
</font> * milliseconds passed as argument.
367 <font color=
"#aaaaaa">319:
</font> * @param {Number} timeout The read timeout in milliseconds
368 <font color=
"#aaaaaa">320:
</font> * @see #getReadTimeout
369 <font color=
"#aaaaaa">321:
</font> */
370 <font color=
"#aaaaaa">322:
</font> this.setReadTimeout = function(timeout) {
371 <font color=
"#aaaaaa">323:
</font> setTimeout(
<font color=
"#9999aa">"socket
"</font>, timeout);
372 <font color=
"#aaaaaa">324:
</font> return true;
373 <font color=
"#aaaaaa">325:
</font> };
374 <font color=
"#aaaaaa">326:
</font>
375 <font color=
"#aaaaaa">327:
</font> /**
376 <font color=
"#aaaaaa">328:
</font> * Returns the connection timeout
377 <font color=
"#aaaaaa">329:
</font> * @returns The connection timeout in milliseconds
378 <font color=
"#aaaaaa">330:
</font> * @type Number
379 <font color=
"#aaaaaa">331:
</font> * @see #setTimeout
380 <font color=
"#aaaaaa">332:
</font> */
381 <font color=
"#aaaaaa">333:
</font> this.getTimeout = function() {
382 <font color=
"#aaaaaa">334:
</font> return timeout.connect;
383 <font color=
"#aaaaaa">335:
</font> };
384 <font color=
"#aaaaaa">336:
</font>
385 <font color=
"#aaaaaa">337:
</font> /**
386 <font color=
"#aaaaaa">338:
</font> * Returns the read timeout (the maximum time a request may take after
387 <font color=
"#aaaaaa">339:
</font> * the connection has been successfully established).
388 <font color=
"#aaaaaa">340:
</font> * @returns The read timeout in milliseconds
389 <font color=
"#aaaaaa">341:
</font> * @type Number
390 <font color=
"#aaaaaa">342:
</font> * @see #setReadTimeout
391 <font color=
"#aaaaaa">343:
</font> */
392 <font color=
"#aaaaaa">344:
</font> this.getReadTimeout = function() {
393 <font color=
"#aaaaaa">345:
</font> return timeout.socket;
394 <font color=
"#aaaaaa">346:
</font> };
395 <font color=
"#aaaaaa">347:
</font>
396 <font color=
"#aaaaaa">348:
</font> /**
397 <font color=
"#aaaaaa">349:
</font> * Enables or disables following redirects
398 <font color=
"#aaaaaa">350:
</font> * @param {Boolean} value If false this client won't follow redirects (the default is
399 <font color=
"#aaaaaa">351:
</font> * to follow them)
400 <font color=
"#aaaaaa">352:
</font> * @see #getFollowRedirects
401 <font color=
"#aaaaaa">353:
</font> */
402 <font color=
"#aaaaaa">354:
</font> this.setFollowRedirects = function(value) {
403 <font color=
"#aaaaaa">355:
</font> followRedirects = value;
404 <font color=
"#aaaaaa">356:
</font> return;
405 <font color=
"#aaaaaa">357:
</font> };
406 <font color=
"#aaaaaa">358:
</font>
407 <font color=
"#aaaaaa">359:
</font> /**
408 <font color=
"#aaaaaa">360:
</font> * Returns true if the client follows redirects
409 <font color=
"#aaaaaa">361:
</font> * @returns True if the client follows redirects, false otherwise.
410 <font color=
"#aaaaaa">362:
</font> * @see #setFollowRedirects
411 <font color=
"#aaaaaa">363:
</font> */
412 <font color=
"#aaaaaa">364:
</font> this.getFollowRedirects = function() {
413 <font color=
"#aaaaaa">365:
</font> return followRedirects;
414 <font color=
"#aaaaaa">366:
</font> };
415 <font color=
"#aaaaaa">367:
</font>
416 <font color=
"#aaaaaa">368:
</font> /**
417 <font color=
"#aaaaaa">369:
</font> * Sets the HTTP
<font color=
"#9999aa">"User-Agent
"</font> header field to the string passed as argument
418 <font color=
"#aaaaaa">370:
</font> * @param {String} agent The string to use as value of the
419 <font color=
"#aaaaaa">371:
</font> *
<font color=
"#9999aa">"User-Agent
"</font> header field (defaults to
<font color=
"#9999aa">"Helma Http Client
"</font>)
420 <font color=
"#aaaaaa">372:
</font> * @see #getUserAgent
421 <font color=
"#aaaaaa">373:
</font> */
422 <font color=
"#aaaaaa">374:
</font> this.setUserAgent = function(agent) {
423 <font color=
"#aaaaaa">375:
</font> userAgent = agent;
424 <font color=
"#aaaaaa">376:
</font> return;
425 <font color=
"#aaaaaa">377:
</font> };
426 <font color=
"#aaaaaa">378:
</font>
427 <font color=
"#aaaaaa">379:
</font> /**
428 <font color=
"#aaaaaa">380:
</font> * Returns the value of the HTTP
<font color=
"#9999aa">"User-Agent
"</font> header field
429 <font color=
"#aaaaaa">381:
</font> * @returns The value of the field
430 <font color=
"#aaaaaa">382:
</font> * @type String
431 <font color=
"#aaaaaa">383:
</font> * @see #setUserAgent
432 <font color=
"#aaaaaa">384:
</font> */
433 <font color=
"#aaaaaa">385:
</font> this.getUserAgent = function() {
434 <font color=
"#aaaaaa">386:
</font> return userAgent;
435 <font color=
"#aaaaaa">387:
</font> };
436 <font color=
"#aaaaaa">388:
</font>
437 <font color=
"#aaaaaa">389:
</font> /**
438 <font color=
"#aaaaaa">390:
</font> * Switches content text encoding on or off. Depending on this
439 <font color=
"#aaaaaa">391:
</font> * the content received from the remote server will be either a
440 <font color=
"#aaaaaa">392:
</font> * string or a byte array.
441 <font color=
"#aaaaaa">393:
</font> * @param {Boolean} mode If true binary mode is activated
442 <font color=
"#aaaaaa">394:
</font> * @see #getBinaryMode
443 <font color=
"#aaaaaa">395:
</font> */
444 <font color=
"#aaaaaa">396:
</font> this.setBinaryMode = function(mode) {
445 <font color=
"#aaaaaa">397:
</font> binaryMode = mode;
446 <font color=
"#aaaaaa">398:
</font> return;
447 <font color=
"#aaaaaa">399:
</font> };
448 <font color=
"#aaaaaa">400:
</font>
449 <font color=
"#aaaaaa">401:
</font> /**
450 <font color=
"#aaaaaa">402:
</font> * Returns the currently defined binary mode of this client
451 <font color=
"#aaaaaa">403:
</font> * @returns The binary mode of this client
452 <font color=
"#aaaaaa">404:
</font> * @type Boolean
453 <font color=
"#aaaaaa">405:
</font> * @see #setBinaryMode
454 <font color=
"#aaaaaa">406:
</font> */
455 <font color=
"#aaaaaa">407:
</font> this.getBinaryMode = function() {
456 <font color=
"#aaaaaa">408:
</font> return binaryMode;
457 <font color=
"#aaaaaa">409:
</font> };
458 <font color=
"#aaaaaa">410:
</font>
459 <font color=
"#aaaaaa">411:
</font> /**
460 <font color=
"#aaaaaa">412:
</font> * Sets the max allowed size for the response stream
461 <font color=
"#aaaaaa">413:
</font> * @param {Integer} Size in Byte
462 <font color=
"#aaaaaa">414:
</font> */
463 <font color=
"#aaaaaa">415:
</font> this.setMaxResponseSize = function(size) {
464 <font color=
"#aaaaaa">416:
</font> maxResponseSize = size;
465 <font color=
"#aaaaaa">417:
</font> return;
466 <font color=
"#aaaaaa">418:
</font> };
467 <font color=
"#aaaaaa">419:
</font>
468 <font color=
"#aaaaaa">420:
</font> /**
469 <font color=
"#aaaaaa">421:
</font> * Returns the currently set max response size
470 <font color=
"#aaaaaa">422:
</font> * @returns The max responsesize
471 <font color=
"#aaaaaa">423:
</font> * @type Integer
472 <font color=
"#aaaaaa">424:
</font> * @see #setMaxResponseSize
473 <font color=
"#aaaaaa">425:
</font> */
474 <font color=
"#aaaaaa">426:
</font> this.getMaxResponseSize = function() {
475 <font color=
"#aaaaaa">427:
</font> return maxResponseSize;
476 <font color=
"#aaaaaa">428:
</font> };
477 <font color=
"#aaaaaa">429:
</font>
478 <font color=
"#aaaaaa">430:
</font> /**
479 <font color=
"#aaaaaa">431:
</font> * Overloads the default response handler.
480 <font color=
"#aaaaaa">432:
</font> * Use this do implement your own response handling, like storing the response directly to the harddisk
481 <font color=
"#aaaaaa">433:
</font> * The handler function gets two parameter, first is the java.net.URLConnection and second is the result object.
482 <font color=
"#aaaaaa">434:
</font> * Note that custom response handler functions should check the HTTP status code before reading
483 <font color=
"#aaaaaa">435:
</font> * the response. The status code for successful requests is
200. Response bodies for requests with
484 <font color=
"#aaaaaa">436:
</font> * status codes less than
400 can be read from the connection's input stream, while response bodies
485 <font color=
"#aaaaaa">437:
</font> * with
4xx or
5xx status codes must be read using the error stream.
486 <font color=
"#aaaaaa">438:
</font> * @param {function} Response handler function
487 <font color=
"#aaaaaa">439:
</font> */
488 <font color=
"#aaaaaa">440:
</font> this.setResponseHandler = function(callback) {
489 <font color=
"#aaaaaa">441:
</font> responseHandler = callback;
490 <font color=
"#aaaaaa">442:
</font> return;
491 <font color=
"#aaaaaa">443:
</font> };
492 <font color=
"#aaaaaa">444:
</font>
493 <font color=
"#aaaaaa">445:
</font> /**
494 <font color=
"#aaaaaa">446:
</font> * Get the response handler. This is the function used to read the HTTP response body.
495 <font color=
"#aaaaaa">447:
</font> * @returns The response handler function
496 <font color=
"#aaaaaa">448:
</font> */
497 <font color=
"#aaaaaa">449:
</font> this.getResponseHandler = function() {
498 <font color=
"#aaaaaa">450:
</font> return responseHandler;
499 <font color=
"#aaaaaa">451:
</font> }
500 <font color=
"#aaaaaa">452:
</font>
501 <font color=
"#aaaaaa">453:
</font> /**
502 <font color=
"#aaaaaa">454:
</font> * Executes a http request
503 <font color=
"#aaaaaa">455:
</font> * @param {String} url The url to request
504 <font color=
"#aaaaaa">456:
</font> * @param {Date|String} opt If this argument is a string, it is used
505 <font color=
"#aaaaaa">457:
</font> * as value for the
<font color=
"#9999aa">"If-None-Match
"</font> request header field. If it is a
506 <font color=
"#aaaaaa">458:
</font> * Date instance it is used as
<font color=
"#9999aa">"IfModifiedSince
"</font> condition for this request.
507 <font color=
"#aaaaaa">459:
</font> * @return A result object containing the following properties:
508 <font color=
"#aaaaaa">460:
</font> *
<ul
>
509 <font color=
"#aaaaaa">461:
</font> *
<li
><code
>url
</code
>: (String) The Url of the request
</li
>
510 <font color=
"#aaaaaa">462:
</font> *
<li
><code
>location
</code
>: (String) The value of the location header field
</li
>
511 <font color=
"#aaaaaa">463:
</font> *
<li
><code
>code
</code
>: (Number) The HTTP response code
</li
>
512 <font color=
"#aaaaaa">464:
</font> *
<li
><code
>message
</code
>: (String) An optional HTTP response message
</li
>
513 <font color=
"#aaaaaa">465:
</font> *
<li
><code
>length
</code
>: (Number) The content length of the response
</li
>
514 <font color=
"#aaaaaa">466:
</font> *
<li
><code
>type
</code
>: (String) The mimetype of the response
</li
>
515 <font color=
"#aaaaaa">467:
</font> *
<li
><code
>charset
</code
>: (String) The character set of the response
</li
>
516 <font color=
"#aaaaaa">468:
</font> *
<li
><code
>encoding
</code
>: (String) An optional encoding to use with the response
</li
>
517 <font color=
"#aaaaaa">469:
</font> *
<li
><code
>lastModified
</code
>: (String) The value of the lastModified response header field
</li
>
518 <font color=
"#aaaaaa">470:
</font> *
<li
><code
>eTag
</code
>: (String) The eTag as received from the remote server
</li
>
519 <font color=
"#aaaaaa">471:
</font> *
<li
><code
>cookie
</code
>: (helma.Http.Cookie) An object containing the cookie parameters, if the remote
520 <font color=
"#aaaaaa">472:
</font> server has set the
<font color=
"#9999aa">"Set-Cookie
"</font> header field
</li
>
521 <font color=
"#aaaaaa">473:
</font> *
<li
><code
>headers
</code
>: (java.util.Map) A map object containing the headers, access them using get(
<font color=
"#9999aa">"headername
"</font>)
522 <font color=
"#aaaaaa">474:
</font> *
<li
><code
>content
</code
>: (String|ByteArray) The response received from the server. Can be either
523 <font color=
"#aaaaaa">475:
</font> a string or a byte array (see #setBinaryMode)
</li
>
524 <font color=
"#aaaaaa">476:
</font> *
</ul
>
525 <font color=
"#aaaaaa">477:
</font> */
526 <font color=
"#aaaaaa">478:
</font> this.getUrl = function(url, opt) {
527 <font color=
"#aaaaaa">479:
</font> if (typeof url ==
<font color=
"#9999aa">"string
"</font>) {
528 <font color=
"#aaaaaa">480:
</font> if (!(url = helma.Http.evalUrl(url)))
529 <font color=
"#aaaaaa">481:
</font> throw new Error(
<font color=
"#9999aa">"'
"</font> + url +
<font color=
"#9999aa">"' is not a valid URL.
"</font>);
530 <font color=
"#aaaaaa">482:
</font> } else if (!(url instanceof java.net.URL)) {
531 <font color=
"#aaaaaa">483:
</font> throw new Error(
<font color=
"#9999aa">"'
"</font> + url +
<font color=
"#9999aa">"' is not a valid URL.
"</font>);
532 <font color=
"#aaaaaa">484:
</font> }
533 <font color=
"#aaaaaa">485:
</font>
534 <font color=
"#aaaaaa">486:
</font> var conn = proxy ? url.openConnection(proxy) : url.openConnection();
535 <font color=
"#aaaaaa">487:
</font> <font color=
"#33aa00"> // Note: we must call setInstanceFollowRedirects() instead of
536 <font color=
"#aaaaaa">488:
</font> </font><font color=
"#33aa00"> // static method setFollowRedirects(), as the latter will
537 <font color=
"#aaaaaa">489:
</font> </font><font color=
"#33aa00"> // set the default value for all url connections, and will not work for
538 <font color=
"#aaaaaa">490:
</font> </font><font color=
"#33aa00"> // url connections that have already been created.
539 <font color=
"#aaaaaa">491:
</font> </font> conn.setInstanceFollowRedirects(followRedirects);
540 <font color=
"#aaaaaa">492:
</font> conn.setAllowUserInteraction(false);
541 <font color=
"#aaaaaa">493:
</font> conn.setRequestMethod(method);
542 <font color=
"#aaaaaa">494:
</font> conn.setRequestProperty(
<font color=
"#9999aa">"User-Agent
"</font>, userAgent);
543 <font color=
"#aaaaaa">495:
</font>
544 <font color=
"#aaaaaa">496:
</font> if (opt) {
545 <font color=
"#aaaaaa">497:
</font> if (opt instanceof Date)
546 <font color=
"#aaaaaa">498:
</font> conn.setIfModifiedSince(opt.getTime());
547 <font color=
"#aaaaaa">499:
</font> else if ((typeof opt ==
<font color=
"#9999aa">"string
"</font>)
&& (opt.length
> 0))
548 <font color=
"#aaaaaa">500:
</font> conn.setRequestProperty(
<font color=
"#9999aa">"If-None-Match
"</font>, opt);
549 <font color=
"#aaaaaa">501:
</font> }
550 <font color=
"#aaaaaa">502:
</font>
551 <font color=
"#aaaaaa">503:
</font> var userinfo;
552 <font color=
"#aaaaaa">504:
</font> if (userinfo = url.getUserInfo()) {
553 <font color=
"#aaaaaa">505:
</font> userinfo = userinfo.split(/:/,
2);
554 <font color=
"#aaaaaa">506:
</font> this.setCredentials(userinfo[
0], userinfo[
1]);
555 <font color=
"#aaaaaa">507:
</font> }
556 <font color=
"#aaaaaa">508:
</font> if (credentials != null) {
557 <font color=
"#aaaaaa">509:
</font> conn.setRequestProperty(
<font color=
"#9999aa">"Authorization
"</font>,
<font color=
"#9999aa">"Basic
"</font> + credentials);
558 <font color=
"#aaaaaa">510:
</font> }
559 <font color=
"#aaaaaa">511:
</font> <font color=
"#33aa00"> // set timeouts
560 <font color=
"#aaaaaa">512:
</font> </font> if (parseFloat(java.lang.System.getProperty(
<font color=
"#9999aa">"java.specification.version
"</font>),
10)
>=
1.5) {
561 <font color=
"#aaaaaa">513:
</font> conn.setConnectTimeout(timeout.connect);
562 <font color=
"#aaaaaa">514:
</font> conn.setReadTimeout(timeout.socket);
563 <font color=
"#aaaaaa">515:
</font> }
564 <font color=
"#aaaaaa">516:
</font> <font color=
"#33aa00"> // set header fields
565 <font color=
"#aaaaaa">517:
</font> </font> for (var i in headers) {
566 <font color=
"#aaaaaa">518:
</font> conn.setRequestProperty(i, headers[i]);
567 <font color=
"#aaaaaa">519:
</font> }
568 <font color=
"#aaaaaa">520:
</font> <font color=
"#33aa00"> // set cookies
569 <font color=
"#aaaaaa">521:
</font> </font> if (cookies != null) {
570 <font color=
"#aaaaaa">522:
</font> var arr = [];
571 <font color=
"#aaaaaa">523:
</font> for (var i in cookies) {
572 <font color=
"#aaaaaa">524:
</font> arr[arr.length] = cookies[i].getFieldValue();
573 <font color=
"#aaaaaa">525:
</font> }
574 <font color=
"#aaaaaa">526:
</font> conn.setRequestProperty(
<font color=
"#9999aa">"Cookie
"</font>, arr.join(
<font color=
"#9999aa">";
"</font>));
575 <font color=
"#aaaaaa">527:
</font> }
576 <font color=
"#aaaaaa">528:
</font> <font color=
"#33aa00"> // set content
577 <font color=
"#aaaaaa">529:
</font> </font> if (content) {
578 <font color=
"#aaaaaa">530:
</font> conn.setRequestProperty(
<font color=
"#9999aa">"Content-Length
"</font>, content.length);
579 <font color=
"#aaaaaa">531:
</font> conn.setDoOutput(true);
580 <font color=
"#aaaaaa">532:
</font> var out = new java.io.OutputStreamWriter(conn.getOutputStream());
581 <font color=
"#aaaaaa">533:
</font> out.write(content);
582 <font color=
"#aaaaaa">534:
</font> out.flush();
583 <font color=
"#aaaaaa">535:
</font> out.close();
584 <font color=
"#aaaaaa">536:
</font> }
585 <font color=
"#aaaaaa">537:
</font>
586 <font color=
"#aaaaaa">538:
</font> var result = {
587 <font color=
"#aaaaaa">539:
</font> url: conn.getURL(),
588 <font color=
"#aaaaaa">540:
</font> location: conn.getHeaderField(
<font color=
"#9999aa">"location
"</font>),
589 <font color=
"#aaaaaa">541:
</font> code: conn.getResponseCode(),
590 <font color=
"#aaaaaa">542:
</font> message: conn.getResponseMessage(),
591 <font color=
"#aaaaaa">543:
</font> length: conn.getContentLength(),
592 <font color=
"#aaaaaa">544:
</font> type: conn.getContentType(),
593 <font color=
"#aaaaaa">545:
</font> encoding: conn.getContentEncoding(),
594 <font color=
"#aaaaaa">546:
</font> lastModified: null,
595 <font color=
"#aaaaaa">547:
</font> eTag: conn.getHeaderField(
<font color=
"#9999aa">"ETag
"</font>),
596 <font color=
"#aaaaaa">548:
</font> cookies: null,
597 <font color=
"#aaaaaa">549:
</font> headers: conn.getHeaderFields(),
598 <font color=
"#aaaaaa">550:
</font> content: null,
599 <font color=
"#aaaaaa">551:
</font> }
600 <font color=
"#aaaaaa">552:
</font>
601 <font color=
"#aaaaaa">553:
</font> <font color=
"#33aa00"> // parse all
<font color=
"#9999aa">"Set-Cookie
"</font> header fields into an array of
602 <font color=
"#aaaaaa">554:
</font> </font><font color=
"#33aa00"> // helma.Http.Cookie instances
603 <font color=
"#aaaaaa">555:
</font> </font> var setCookies = conn.getHeaderFields().get(
<font color=
"#9999aa">"Set-Cookie
"</font>);
604 <font color=
"#aaaaaa">556:
</font> if (setCookies != null) {
605 <font color=
"#aaaaaa">557:
</font> var arr = [];
606 <font color=
"#aaaaaa">558:
</font> var cookie;
607 <font color=
"#aaaaaa">559:
</font> for (var i=
0; i
<setCookies.size(); i++) {
608 <font color=
"#aaaaaa">560:
</font> if ((cookie = helma.Http.Cookie.parse(setCookies.get(i))) != null) {
609 <font color=
"#aaaaaa">561:
</font> arr.push(cookie);
610 <font color=
"#aaaaaa">562:
</font> }
611 <font color=
"#aaaaaa">563:
</font> }
612 <font color=
"#aaaaaa">564:
</font> if (arr.length
> 0) {
613 <font color=
"#aaaaaa">565:
</font> result.cookies = arr;
614 <font color=
"#aaaaaa">566:
</font> }
615 <font color=
"#aaaaaa">567:
</font> }
616 <font color=
"#aaaaaa">568:
</font>
617 <font color=
"#aaaaaa">569:
</font> var lastmod = conn.getLastModified();
618 <font color=
"#aaaaaa">570:
</font> if (lastmod) {
619 <font color=
"#aaaaaa">571:
</font> result.lastModified = new Date(lastmod);
620 <font color=
"#aaaaaa">572:
</font> }
621 <font color=
"#aaaaaa">573:
</font>
622 <font color=
"#aaaaaa">574:
</font> if (maxResponseSize
&& result.length
> maxResponseSize) {
623 <font color=
"#aaaaaa">575:
</font> throw new Error(
<font color=
"#9999aa">"Maximum allowed response size is exceeded
"</font>);
624 <font color=
"#aaaaaa">576:
</font> }
625 <font color=
"#aaaaaa">577:
</font>
626 <font color=
"#aaaaaa">578:
</font> if (result.type
&& result.type.indexOf(
<font color=
"#9999aa">"charset=
"</font>) != -
1) {
627 <font color=
"#aaaaaa">579:
</font> var charset = result.type.substring(result.type.indexOf(
<font color=
"#9999aa">"charset=
"</font>) +
8);
628 <font color=
"#aaaaaa">580:
</font> charset = charset.replace('
"', ' ').trim();
629 <font color=
"#aaaaaa">581:
</font> result.charset = charset;
630 <font color=
"#aaaaaa">582:
</font> }
631 <font color=
"#aaaaaa">583:
</font>
632 <font color=
"#aaaaaa">584:
</font> <font color=
"#33aa00"> // invoke response handler
633 <font color=
"#aaaaaa">585:
</font> </font> responseHandler(conn, result);
634 <font color=
"#aaaaaa">586:
</font>
635 <font color=
"#aaaaaa">587:
</font> conn.disconnect();
636 <font color=
"#aaaaaa">588:
</font> return result;
637 <font color=
"#aaaaaa">589:
</font> }
638 <font color=
"#aaaaaa">590:
</font>
639 <font color=
"#aaaaaa">591:
</font> /** @ignore */
640 <font color=
"#aaaaaa">592:
</font> this.toString = function() {
641 <font color=
"#aaaaaa">593:
</font> return
<font color=
"#9999aa">"[Helma Http Client]
"</font>;
642 <font color=
"#aaaaaa">594:
</font> };
643 <font color=
"#aaaaaa">595:
</font>
644 <font color=
"#aaaaaa">596:
</font> for (var i in this)
645 <font color=
"#aaaaaa">597:
</font> this.dontEnum(i);
646 <font color=
"#aaaaaa">598:
</font>
647 <font color=
"#aaaaaa">599:
</font> return this;
648 <font color=
"#aaaaaa">600:
</font> }