Request.js
14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Events.js
*/
/**
* Namespace: OpenLayers.Request
* The OpenLayers.Request namespace contains convenience methods for working
* with XMLHttpRequests. These methods work with a cross-browser
* W3C compliant <OpenLayers.Request.XMLHttpRequest> class.
*/
OpenLayers.Request = {
/**
* Constant: DEFAULT_CONFIG
* {Object} Default configuration for all requests.
*/
DEFAULT_CONFIG: {
method: "GET",
url: window.location.href,
async: true,
user: undefined,
password: undefined,
params: null,
proxy: OpenLayers.ProxyHost,
headers: {},
data: null,
callback: function() {},
success: null,
failure: null,
scope: null
},
/**
* Constant: URL_SPLIT_REGEX
*/
URL_SPLIT_REGEX: /([^:]*:)\/\/([^:]*:?[^@]*@)?([^:\/\?]*):?([^\/\?]*)/,
/**
* APIProperty: events
* {<OpenLayers.Events>} An events object that handles all
* events on the {<OpenLayers.Request>} object.
*
* All event listeners will receive an event object with three properties:
* request - {<OpenLayers.Request.XMLHttpRequest>} The request object.
* config - {Object} The config object sent to the specific request method.
* requestUrl - {String} The request url.
*
* Supported event types:
* complete - Triggered when we have a response from the request, if a
* listener returns false, no further response processing will take
* place.
* success - Triggered when the HTTP response has a success code (200-299).
* failure - Triggered when the HTTP response does not have a success code.
*/
events: new OpenLayers.Events(this, null, ["complete", "success", "failure"]),
/**
* APIMethod: issue
* Create a new XMLHttpRequest object, open it, set any headers, bind
* a callback to done state, and send any data. It is recommended that
* you use one <GET>, <POST>, <PUT>, <DELETE>, <OPTIONS>, or <HEAD>.
* This method is only documented to provide detail on the configuration
* options available to all request methods.
*
* Parameters:
* config - {Object} Object containing properties for configuring the
* request. Allowed configuration properties are described below.
* This object is modified and should not be reused.
*
* Allowed config properties:
* method - {String} One of GET, POST, PUT, DELETE, HEAD, or
* OPTIONS. Default is GET.
* url - {String} URL for the request.
* async - {Boolean} Open an asynchronous request. Default is true.
* user - {String} User for relevant authentication scheme. Set
* to null to clear current user.
* password - {String} Password for relevant authentication scheme.
* Set to null to clear current password.
* proxy - {String} Optional proxy. Defaults to
* <OpenLayers.ProxyHost>.
* params - {Object} Any key:value pairs to be appended to the
* url as a query string. Assumes url doesn't already include a query
* string or hash. Typically, this is only appropriate for <GET>
* requests where the query string will be appended to the url.
* Parameter values that are arrays will be
* concatenated with a comma (note that this goes against form-encoding)
* as is done with <OpenLayers.Util.getParameterString>.
* headers - {Object} Object with header:value pairs to be set on
* the request.
* data - {String | Document} Optional data to send with the request.
* Typically, this is only used with <POST> and <PUT> requests.
* Make sure to provide the appropriate "Content-Type" header for your
* data. For <POST> and <PUT> requests, the content type defaults to
* "application-xml". If your data is a different content type, or
* if you are using a different HTTP method, set the "Content-Type"
* header to match your data type.
* callback - {Function} Function to call when request is done.
* To determine if the request failed, check request.status (200
* indicates success).
* success - {Function} Optional function to call if request status is in
* the 200s. This will be called in addition to callback above and
* would typically only be used as an alternative.
* failure - {Function} Optional function to call if request status is not
* in the 200s. This will be called in addition to callback above and
* would typically only be used as an alternative.
* scope - {Object} If callback is a public method on some object,
* set the scope to that object.
*
* Returns:
* {XMLHttpRequest} Request object. To abort the request before a response
* is received, call abort() on the request object.
*/
issue: function(config) {
// apply default config - proxy host may have changed
var defaultConfig = OpenLayers.Util.extend(
this.DEFAULT_CONFIG,
{proxy: OpenLayers.ProxyHost}
);
config = OpenLayers.Util.applyDefaults(config, defaultConfig);
// create request, open, and set headers
var request = new OpenLayers.Request.XMLHttpRequest();
var url = OpenLayers.Util.urlAppend(config.url,
OpenLayers.Util.getParameterString(config.params || {}));
var sameOrigin = !(url.indexOf("http") == 0);
var urlParts = !sameOrigin && url.match(this.URL_SPLIT_REGEX);
if (urlParts) {
var location = window.location;
sameOrigin =
urlParts[1] == location.protocol &&
urlParts[3] == location.hostname;
var uPort = urlParts[4], lPort = location.port;
if (uPort != 80 && uPort != "" || lPort != "80" && lPort != "") {
sameOrigin = sameOrigin && uPort == lPort;
}
}
if (!sameOrigin) {
if (config.proxy) {
if (typeof config.proxy == "function") {
url = config.proxy(url);
} else {
url = config.proxy + encodeURIComponent(url);
}
} else {
OpenLayers.Console.warn(
OpenLayers.i18n("proxyNeeded"), {url: url});
}
}
request.open(
config.method, url, config.async, config.user, config.password
);
for(var header in config.headers) {
request.setRequestHeader(header, config.headers[header]);
}
var events = this.events;
// we want to execute runCallbacks with "this" as the
// execution scope
var self = this;
request.onreadystatechange = function() {
if(request.readyState == OpenLayers.Request.XMLHttpRequest.DONE) {
var proceed = events.triggerEvent(
"complete",
{request: request, config: config, requestUrl: url}
);
if(proceed !== false) {
self.runCallbacks(
{request: request, config: config, requestUrl: url}
);
}
}
};
// send request (optionally with data) and return
// call in a timeout for asynchronous requests so the return is
// available before readyState == 4 for cached docs
if(config.async === false) {
request.send(config.data);
} else {
window.setTimeout(function(){
if (request.readyState !== 0) { // W3C: 0-UNSENT
request.send(config.data);
}
}, 0);
}
return request;
},
/**
* Method: runCallbacks
* Calls the complete, success and failure callbacks. Application
* can listen to the "complete" event, have the listener
* display a confirm window and always return false, and
* execute OpenLayers.Request.runCallbacks if the user
* hits "yes" in the confirm window.
*
* Parameters:
* options - {Object} Hash containing request, config and requestUrl keys
*/
runCallbacks: function(options) {
var request = options.request;
var config = options.config;
// bind callbacks to readyState 4 (done)
var complete = (config.scope) ?
OpenLayers.Function.bind(config.callback, config.scope) :
config.callback;
// optional success callback
var success;
if(config.success) {
success = (config.scope) ?
OpenLayers.Function.bind(config.success, config.scope) :
config.success;
}
// optional failure callback
var failure;
if(config.failure) {
failure = (config.scope) ?
OpenLayers.Function.bind(config.failure, config.scope) :
config.failure;
}
if (OpenLayers.Util.createUrlObject(config.url).protocol == "file:" &&
request.responseText) {
request.status = 200;
}
complete(request);
if (!request.status || (request.status >= 200 && request.status < 300)) {
this.events.triggerEvent("success", options);
if(success) {
success(request);
}
}
if(request.status && (request.status < 200 || request.status >= 300)) {
this.events.triggerEvent("failure", options);
if(failure) {
failure(request);
}
}
},
/**
* APIMethod: GET
* Send an HTTP GET request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to GET.
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties.
* This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
GET: function(config) {
config = OpenLayers.Util.extend(config, {method: "GET"});
return OpenLayers.Request.issue(config);
},
/**
* APIMethod: POST
* Send a POST request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to POST and "Content-Type" header set to "application/xml".
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties. The
* default "Content-Type" header will be set to "application-xml" if
* none is provided. This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
POST: function(config) {
config = OpenLayers.Util.extend(config, {method: "POST"});
// set content type to application/xml if it isn't already set
config.headers = config.headers ? config.headers : {};
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
config.headers["Content-Type"] = "application/xml";
}
return OpenLayers.Request.issue(config);
},
/**
* APIMethod: PUT
* Send an HTTP PUT request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to PUT and "Content-Type" header set to "application/xml".
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties. The
* default "Content-Type" header will be set to "application-xml" if
* none is provided. This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
PUT: function(config) {
config = OpenLayers.Util.extend(config, {method: "PUT"});
// set content type to application/xml if it isn't already set
config.headers = config.headers ? config.headers : {};
if(!("CONTENT-TYPE" in OpenLayers.Util.upperCaseObject(config.headers))) {
config.headers["Content-Type"] = "application/xml";
}
return OpenLayers.Request.issue(config);
},
/**
* APIMethod: DELETE
* Send an HTTP DELETE request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to DELETE.
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties.
* This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
DELETE: function(config) {
config = OpenLayers.Util.extend(config, {method: "DELETE"});
return OpenLayers.Request.issue(config);
},
/**
* APIMethod: HEAD
* Send an HTTP HEAD request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to HEAD.
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties.
* This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
HEAD: function(config) {
config = OpenLayers.Util.extend(config, {method: "HEAD"});
return OpenLayers.Request.issue(config);
},
/**
* APIMethod: OPTIONS
* Send an HTTP OPTIONS request. Additional configuration properties are
* documented in the <issue> method, with the method property set
* to OPTIONS.
*
* Parameters:
* config - {Object} Object with properties for configuring the request.
* See the <issue> method for documentation of allowed properties.
* This object is modified and should not be reused.
*
* Returns:
* {XMLHttpRequest} Request object.
*/
OPTIONS: function(config) {
config = OpenLayers.Util.extend(config, {method: "OPTIONS"});
return OpenLayers.Request.issue(config);
}
};