summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/io/remote/Exchange.js
blob: d9f07386609cf4d5300afe3f9221b28c7fea1b6e (plain)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
     2006 by Derrell Lipman
     2006 by STZ-IDA, Germany, http://www.stz-ida.de

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)
     * Derrell Lipman (derrell)
     * Andreas Junghans (lucidcake)

************************************************************************ */

/* ************************************************************************

#module(io_remote)

************************************************************************ */

/**
 * @event sending {qx.event.type.Event}
 * @event receiving {qx.event.type.Event}
 * @event completed {qx.event.type.Event}
 * @event aborted {qx.event.type.Event}
 * @event timeout {qx.event.type.Event}
 * @event failed {qx.event.type.Event}
 */
qx.OO.defineClass("qx.io.remote.Exchange", qx.core.Target,
function(vRequest)
{
  qx.core.Target.call(this);

  this.setRequest(vRequest);
  vRequest.setTransport(this);
});


/*
---------------------------------------------------------------------------
  DEFAULT SETTINGS
---------------------------------------------------------------------------
*/

qx.Settings.setDefault("enableDebug", false);






/* ************************************************************************
   Class data, properties and methods
************************************************************************ */

/*
---------------------------------------------------------------------------
  TRANSPORT TYPE HANDLING
---------------------------------------------------------------------------
*/

qx.io.remote.Exchange.typesOrder = [ "qx.io.remote.XmlHttpTransport", "qx.io.remote.IframeTransport", "qx.io.remote.ScriptTransport" ];

qx.io.remote.Exchange.typesReady = false;

qx.io.remote.Exchange.typesAvailable = {};
qx.io.remote.Exchange.typesSupported = {};

qx.io.remote.Exchange.registerType = function(vClass, vId) {
  qx.io.remote.Exchange.typesAvailable[vId] = vClass;
}

qx.io.remote.Exchange.initTypes = function()
{
  if (qx.io.remote.Exchange.typesReady) {
    return;
  }

  for (var vId in qx.io.remote.Exchange.typesAvailable)
  {
    vTransporterImpl = qx.io.remote.Exchange.typesAvailable[vId];

    if (vTransporterImpl.isSupported()) {
      qx.io.remote.Exchange.typesSupported[vId] = vTransporterImpl;
    }
  }

  qx.io.remote.Exchange.typesReady = true;

  if (qx.lang.Object.isEmpty(qx.io.remote.Exchange.typesSupported)) {
    throw new Error("No supported transport types were found!");
  }
}

qx.io.remote.Exchange.canHandle = function(vImpl, vNeeds, vResponseType)
{
  if (!qx.lang.Array.contains(vImpl.handles.responseTypes, vResponseType)) {
    return false;
  }

  for (var vKey in vNeeds)
  {
    if (!vImpl.handles[vKey]) {
      return false;
    }
  }

  return true;
}





/*
---------------------------------------------------------------------------
  MAPPING
---------------------------------------------------------------------------
*/

/*
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/0e6a34e4-f90c-489d-acff-cb44242fafc6.asp

0: UNINITIALIZED
The object has been created, but not initialized (the open method has not been called).

1: LOADING
The object has been created, but the send method has not been called.

2: LOADED
The send method has been called, but the status and headers are not yet available.

3: INTERACTIVE
Some data has been received. Calling the responseBody and responseText properties at this state to obtain partial results will return an error, because status and response headers are not fully available.

4: COMPLETED
All the data has been received, and the complete data is available in the
*/

qx.io.remote.Exchange._nativeMap =
{
  0 : "created",
  1 : "configured",
  2 : "sending",
  3 : "receiving",
  4 : "completed"
}






/*
---------------------------------------------------------------------------
  UTILS
---------------------------------------------------------------------------
*/

qx.io.remote.Exchange.wasSuccessful = function(vStatusCode, vReadyState, vIsLocal)
{
  if (vIsLocal)
  {
    switch(vStatusCode)
    {
      case null:
      case 0:
        return true;

      case -1:
        // Not Available (OK for readystates: MSXML<4=1-3, MSXML>3=1-2, Gecko=1)
        return vReadyState < 4;

      default:
        // at least older versions of Safari don't set the status code for local file access
        return typeof vStatusCode === "undefined";
    }
  }
  else
  {
    switch(vStatusCode)
    {
      case -1:  // Not Available (OK for readystates: MSXML<4=1-3, MSXML>3=1-2, Gecko=1)
        if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug") && vReadyState > 3) {
          qx.dev.log.Logger.getClassLogger(qx.io.remote.Exchange).debug("Failed with statuscode: -1 at readyState " + vReadyState);
        }

        return vReadyState < 4;


      case 200: // OK
      case 304: // Not Modified
        return true;


      case 201: // Created
      case 202: // Accepted
      case 203: // Non-Authoritative Information
      case 204: // No Content
      case 205: // Reset Content
        return true;


      case 206: // Partial Content
        if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug") && vReadyState === 4) {
          qx.dev.log.Logger.getClassLogger(qx.io.remote.Exchange).debug("Failed with statuscode: 206 (Partial content while being complete!)");
        }

        return vReadyState !== 4;


      case 300: // Multiple Choices
      case 301: // Moved Permanently
      case 302: // Moved Temporarily
      case 303: // See Other
      case 305: // Use Proxy
      case 400: // Bad Request
      case 401: // Unauthorized
      case 402: // Payment Required
      case 403: // Forbidden
      case 404: // Not Found
      case 405: // Method Not Allowed
      case 406: // Not Acceptable
      case 407: // Proxy Authentication Required
      case 408: // Request Time-Out
      case 409: // Conflict
      case 410: // Gone
      case 411: // Length Required
      case 412: // Precondition Failed
      case 413: // Request Entity Too Large
      case 414: // Request-URL Too Large
      case 415: // Unsupported Media Type
      case 500: // Server Error
      case 501: // Not Implemented
      case 502: // Bad Gateway
      case 503: // Out of Resources
      case 504: // Gateway Time-Out
      case 505: // HTTP Version not supported
        if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
          qx.dev.log.Logger.getClassLogger(qx.io.remote.Exchange).debug("Failed with typical HTTP statuscode: " + vStatusCode);
        }

        return false;


      // The following case labels are wininet.dll error codes that may be encountered.
      // Server timeout
      case 12002:
      // 12029 to 12031 correspond to dropped connections.
      case 12029:
      case 12030:
      case 12031:
      // Connection closed by server.
      case 12152:
      // See above comments for variable status.
      case 13030:
        if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
          qx.dev.log.Logger.getClassLogger(qx.io.remote.Exchange).debug("Failed with MSHTML specific HTTP statuscode: " + vStatusCode);
        }

        return false;


      default:
        // Handle all 20x status codes as OK as defined in the corresponding RFC
        // http://www.w3.org/Protocols/rfc2616/rfc2616.html
        if (vStatusCode > 206 && vStatusCode < 300) {
          return true;
        }

        qx.dev.log.Logger.getClassLogger(qx.io.remote.Exchange).debug("Unknown status code: " + vStatusCode + " (" + vReadyState + ")");
        throw new Error("Unknown status code: " + vStatusCode);
    }
  }
}


qx.io.remote.Exchange.statusCodeToString = function(vStatusCode)
{
  switch(vStatusCode)
  {
    case -1:    return "Not available";
    case 200:   return "Ok";
    case 304:   return "Not modified";
    case 206:   return "Partial content";
    case 204:   return "No content";
    case 300:   return "Multiple choices";
    case 301:   return "Moved permanently";
    case 302:   return "Moved temporarily";
    case 303:   return "See other";
    case 305:   return "Use proxy";
    case 400:   return "Bad request";
    case 401:   return "Unauthorized";
    case 402:   return "Payment required";
    case 403:   return "Forbidden";
    case 404:   return "Not found";
    case 405:   return "Method not allowed";
    case 406:   return "Not acceptable";
    case 407:   return "Proxy authentication required";
    case 408:   return "Request time-out";
    case 409:   return "Conflict";
    case 410:   return "Gone";
    case 411:   return "Length required";
    case 412:   return "Precondition failed";
    case 413:   return "Request entity too large";
    case 414:   return "Request-URL too large";
    case 415:   return "Unsupported media type";
    case 500:   return "Server error";
    case 501:   return "Not implemented";
    case 502:   return "Bad gateway";
    case 503:   return "Out of resources";
    case 504:   return "Gateway time-out";
    case 505:   return "HTTP version not supported";
    case 12002: return "Server timeout";
    case 12029: return "Connection dropped";
    case 12030: return "Connection dropped";
    case 12031: return "Connection dropped";
    case 12152: return "Connection closed by server";
    case 13030: return "MSHTML-specific HTTP status code";
    default:    return "Unknown status code";
  }
}







/* ************************************************************************
   Instance data, properties and methods
************************************************************************ */

/*
---------------------------------------------------------------------------
  PROPERTIES
---------------------------------------------------------------------------
*/

/*!
  Set the request to send with this transport.
*/
qx.OO.addProperty({ name : "request", type : "object", instance : "qx.io.remote.Request" });
/*!
  Set the implementation to use to send the request with.

  The implementation should be a subclass of qx.io.remote.AbstractRemoteTransport and
  must implement all methods in the transport API.
*/
qx.OO.addProperty({ name : "implementation", type : "object" });
qx.OO.addProperty(
{
  name           : "state",
  type           : "string",
  possibleValues : [
                   "configured", "sending",
                   "receiving", "completed",
                   "aborted", "timeout",
                   "failed"
                   ],
  defaultValue   : "configured"
});








/*
---------------------------------------------------------------------------
  CORE METHODS
---------------------------------------------------------------------------
*/

qx.Proto.send = function()
{
  var vRequest = this.getRequest();

  if (!vRequest) {
    return this.error("Please attach a request object first");
  }

  qx.io.remote.Exchange.initTypes();

  var vUsage = qx.io.remote.Exchange.typesOrder;
  var vSupported = qx.io.remote.Exchange.typesSupported;

  // Mapping settings to contenttype and needs to check later
  // if the selected transport implementation can handle
  // fulfill these requirements.
  var vResponseType = vRequest.getResponseType();
  var vNeeds = {};

  if (vRequest.getAsynchronous()) {
    vNeeds.asynchronous = true;
  } else {
    vNeeds.synchronous = true;
  }

  if (vRequest.getCrossDomain()) {
    vNeeds.crossDomain = true;
  }

  if (vRequest.getFileUpload()) {
    vNeeds.fileUpload = true;
  }

  var vTransportImpl, vTransport;
  for (var i=0, l=vUsage.length; i<l; i++)
  {
    vTransportImpl = vSupported[vUsage[i]];

    if (vTransportImpl)
    {
      if (!qx.io.remote.Exchange.canHandle(vTransportImpl, vNeeds, vResponseType)) {
        continue;
      }

      try
      {
        if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
          this.debug("Using implementation: " + vTransportImpl.classname);
        }

        vTransport = new vTransportImpl;
        this.setImplementation(vTransport);

        vTransport.setUseBasicHttpAuth(vRequest.getUseBasicHttpAuth());

        vTransport.send();
        return true;
      }
      catch(ex)
      {
        return this.error("Request handler throws error", ex);
      }
    }
  }

  this.error("There is no transport implementation available to handle this request: " + vRequest);
}
/*!
  Force the transport into the aborted ("aborted")
  state.
*/
qx.Proto.abort = function()
{
  var vImplementation = this.getImplementation();

  if (vImplementation)
  {
    if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
      this.debug("Abort: implementation " + vImplementation.toHashCode());
    }
    vImplementation.abort();
  }
  else
  {
    if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
      this.debug("Abort: forcing state to be aborted");
    }
    this.setState("aborted");
  }
}
/*!
  Force the transport into the timeout state.
*/
qx.Proto.timeout = function()
{
  var vImplementation = this.getImplementation();

  if (vImplementation)
  {
    this.warn("Timeout: implementation " + vImplementation.toHashCode());
    vImplementation.timeout();
  }
  else
  {
    this.warn("Timeout: forcing state to timeout");
    this.setState("timeout");
  }

  // Disable future timeouts in case user handler blocks
  if (this.getRequest()) {
    this.getRequest().setTimeout(0);
  }
}









/*
---------------------------------------------------------------------------
  EVENT HANDLER
---------------------------------------------------------------------------
*/

qx.Proto._onsending = function(e) {
  this.setState("sending");
}

qx.Proto._onreceiving = function(e) {
  this.setState("receiving");
}

qx.Proto._oncompleted = function(e) {
  this.setState("completed");
}

qx.Proto._onabort = function(e) {
  this.setState("aborted");
}

qx.Proto._onfailed = function(e) {
  this.setState("failed");
}

qx.Proto._ontimeout = function(e) {
  this.setState("timeout");
}






/*
---------------------------------------------------------------------------
  MODIFIER
---------------------------------------------------------------------------
*/

qx.Proto._modifyImplementation = function(propValue, propOldValue, propData)
{
  if (propOldValue)
  {
    propOldValue.removeEventListener("sending", this._onsending, this);
    propOldValue.removeEventListener("receiving", this._onreceiving, this);
    propOldValue.removeEventListener("completed", this._oncompleted, this);
    propOldValue.removeEventListener("aborted", this._onabort, this);
    propOldValue.removeEventListener("timeout", this._ontimeout, this);
    propOldValue.removeEventListener("failed", this._onfailed, this);
  }

  if (propValue)
  {
    var vRequest = this.getRequest();

    propValue.setUrl(vRequest.getUrl());
    propValue.setMethod(vRequest.getMethod());
    propValue.setAsynchronous(vRequest.getAsynchronous());

    propValue.setUsername(vRequest.getUsername());
    propValue.setPassword(vRequest.getPassword());

    propValue.setParameters(vRequest.getParameters());
    propValue.setRequestHeaders(vRequest.getRequestHeaders());
    propValue.setData(vRequest.getData());

    propValue.setResponseType(vRequest.getResponseType());

    propValue.addEventListener("sending", this._onsending, this);
    propValue.addEventListener("receiving", this._onreceiving, this);
    propValue.addEventListener("completed", this._oncompleted, this);
    propValue.addEventListener("aborted", this._onabort, this);
    propValue.addEventListener("timeout", this._ontimeout, this);
    propValue.addEventListener("failed", this._onfailed, this);
  }

  return true;
}

qx.Proto._modifyState = function(propValue, propOldValue, propData)
{
  var vRequest = this.getRequest();

  if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
    this.debug("State: " + propOldValue + " => " + propValue);
  }

  switch(propValue)
  {
    case "sending":
      this.createDispatchEvent("sending");
      break;

    case "receiving":
      this.createDispatchEvent("receiving");
      break;

    case "completed":
    case "aborted":
    case "timeout":
    case "failed":
      var vImpl = this.getImplementation();

      if (! vImpl) {
        // implementation has already been disposed
        break;
      }

      var vResponse = new qx.io.remote.Response;

      if (propValue == "completed") {
        var vContent = vImpl.getResponseContent();
        vResponse.setContent(vContent);

        /*
         * Was there acceptable content?  This might occur, for example, if
         * the web server was shut down unexpectedly and thus the connection
         * closed with no data having been sent.
         */
        if (vContent === null) {
          // Nope.  Change COMPLETED to FAILED.
          if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
            this.debug("Altered State: " + propValue + " => failed");
          }
          propValue = "failed";
        }
      }

      vResponse.setStatusCode(vImpl.getStatusCode());
      vResponse.setResponseHeaders(vImpl.getResponseHeaders());

      // this.debug("Result Text: " + vResponse.getTextContent());

      var vEventType;

      switch(propValue)
      {
        case "completed":
          vEventType = "completed";
          break;

        case "aborted":
          vEventType = "aborted";
          break;

        case "timeout":
          vEventType = "timeout";
          break;

        case "failed":
          vEventType = "failed";
          break;
      }

      // Disconnect and dispose implementation
      this.setImplementation(null);
      vImpl.dispose();

      // Fire event to listeners
      this.createDispatchDataEvent(vEventType, vResponse);
      break;
  }

  return true;
}








/*
---------------------------------------------------------------------------
  DISPOSER
---------------------------------------------------------------------------
*/

qx.Proto.dispose = function()
{
  if (this.getDisposed()) {
    return;
  }

  var vImpl = this.getImplementation();
  if (vImpl)
  {
    this.setImplementation(null);
    vImpl.dispose();
  }

  this.setRequest(null);

  return qx.core.Target.prototype.dispose.call(this);
}