OSDN Git Service

Version 0.6.132, fix X.Node._flags & fix X.EventDispatcher._listeners & start to...
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 01_XNetXHR.js
index 1ea396c..6e64e81 100644 (file)
@@ -30,7 +30,10 @@ itozyun 2014-10-30 20:55:41
 basic 認証のかかったhtml を表示して、そのjsが xhr をすると Android1.6 では 401 error が返る。Android 2.3 では解決している。\r
 Android1.6- の XHR で 401 エラーが返った場合は、iframe に xml を表示させてその内容を取ればサーバ側の対応無しでいけるかも?\r
  */\r
-var X_Net_XHR_W3C      = ( !X_UA.IE7 || !X_URL_IS_LOCAL ) && window[ 'XMLHttpRequest' ] && new XMLHttpRequest(),\r
+var // Opera7.6+, Safari1.2+, khtml3.?+, Gecko0.9.7+\r
+       // ie7 ではローカルリソースには ActiveX の XHR を使う\r
+       X_Net_XHR_W3C      = ( !X_UA.IE7 || !X_URL_IS_LOCAL ) && window[ 'XMLHttpRequest' ] && new XMLHttpRequest(),\r
+       \r
        X_Net_XHR_X_DOMAIN = window[ 'XDomainRequest' ] && new XDomainRequest(),\r
        X_Net_XHR_VERSION  = 0,\r
        X_Net_XHR_ACTIVE_X = !X_UA.IE4 && X_UA.IE < 8 && X_UA.ActiveX && ( new Function( [\r
@@ -52,13 +55,6 @@ if( X_Net_XHR_ACTIVE_X ){
 };\r
 \r
 X.Net.XHR = {\r
-       // Opera7.6+, Safari1.2+, khtml3.?+, Gecko0.9.7+\r
-       W3C         : !!X_Net_XHR_W3C,\r
-\r
-       X_DOMAIN    : !!X_Net_XHR_X_DOMAIN,\r
-       \r
-       // ie7 ではローカルリソースには ActiveX の XHR を使う\r
-       ACTIVE_X    : !!X_Net_XHR_ACTIVE_X,\r
 \r
 /*\r
  * http://hakuhin.jp/as/import.html\r
@@ -72,11 +68,7 @@ X.Net.XHR = {
 // Progress Events     Chrome7, firefox3.5, ie10, opera12, Safari?, Chrome for Android 0.16\r
        PROGRESS    : false, //\r
 \r
-       UL_PROGRESS : false,\r
-\r
-       CANCELABLE  : false,\r
-       \r
-       TIMEOUT     : false\r
+       UL_PROGRESS : false\r
 \r
 };\r
 \r
@@ -85,15 +77,17 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
        X_NET_XHRWrapper = X_Class_override(\r
                new X.EventDispatcher(),\r
                {\r
-                                               \r
-                       _rawObject : X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X,\r
-                       _isXHR     : true,  // X.EventDispatcher で使用 for ie8-\r
+                       \r
+                       '_rawType'   : X_EventDispatcher_EVENT_TARGET_TYPE.XHR,\r
+                       '_rawObject' : X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X,\r
+                       \r
                        _isXDR     : false, // for ie8\r
                        \r
                        _method    : '',\r
                        _type      : '',\r
                        _busy      : false,\r
                        _canceled  : false,\r
+                       _error     : false,\r
                        _percent   : 0,\r
                        _timerID   : 0,\r
                        \r
@@ -102,9 +96,9 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        method   = obj[ 'method' ],\r
                                        url      = obj[ 'url' ],\r
                                        async    = obj[ 'async' ],\r
-                                       user     = obj[ 'user' ],\r
+                                       username = obj[ 'username' ],\r
                                        password = obj[ 'password' ],\r
-                                       headers  = obj[ 'headers' ],\r
+                                       headers  = obj[ 'headers' ] || {},\r
                                        postbody = obj[ 'postbody' ],\r
                                        timeout  = obj[ 'timeout' ] || 20000,\r
                                        temp;\r
@@ -136,7 +130,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        };\r
                                };\r
                                \r
-                               raw.open( method, url, true );\r
+                               raw.open( method, url, true, username, password );\r
                                \r
                                if( raw.responseType !== undefined ){\r
                                        switch( this._type ){\r
@@ -167,7 +161,13 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_1.html\r
                                // raw.overrideMimeType()\r
                                if( !X_Net_XHR_ACTIVE_X && X.Type.isFunction( raw.setRequestHeader ) ){\r
+                                       \r
+                                       // http://nakigao.sitemix.jp/blog/?p=2040\r
+                                       // SafariでHTTP/412のエラー\r
+                                       headers[ 'If-Modified-Since' ] = ( new Date ).toUTCString();\r
+                                       \r
                                        for( p in headers ){\r
+                                               if( X_EMPTY_OBJECT[ p ] ) continue;\r
                                                raw.setRequestHeader( p, headers[ p ] ); // Opera8.01+, MSXML3+\r
                                        };              \r
                                };\r
@@ -175,17 +175,17 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                if( raw.timeout !== undefined ){\r
                                        raw.timeout = timeout;\r
                                } else {\r
-                                       //this._timerID = X.Timer.once( timeout, this, this.onTimeout );\r
+                                       this._timerID = X.Timer.once( timeout, this, this.onTimeout );\r
                                };      \r
                                \r
                                // send 前にフラグを立てる,回線が早いと raw.send() 内で onload -> _busy = false ののち、 _busy = true するため。\r
                                this._busy = true;\r
                                \r
 \r
-                               //raw.send( postbody || '' );\r
-                               this._timerID = X.Timer.once( 16, this, this._lazySend, [ postbody || '', timeout ] );\r
+                               raw.send( postbody || '' );\r
+                               //this._timerID = X.Timer.once( 16, this, this._lazySend, [ postbody || '', timeout ] );\r
                        },\r
-                       \r
+                       /*\r
                        // send() 内で onload するケースがあり、そのときはイベントリスナが間に合わないので、タイマーをかませる。\r
                        _lazySend : function( postbody, timeout ){\r
                                if( this._rawObject.timeout === undefined ){\r
@@ -198,7 +198,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // sendをonreadystatechangeの前に記述すると、ieでは動作しなくなります、、、。\r
                                // konquerorでエラーが発生するのでここでは、とりあえず、send('') としました。\r
                                this._rawObject.send( postbody );                               \r
-                       },\r
+                       }, */\r
                        \r
                        cancel : function(){\r
                                /* X.Net.XHR.CANCELABLE && */ this._rawObject.abort && this._rawObject.abort();\r
@@ -215,8 +215,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                // domes.lingua.heliohost.org/dom-intro/load-save2.html\r
                                // 規定上は open() を呼び出すと XMLHttpRequest オブジェクトが未送信状態に戻りますが、\r
                                // Opera 10.10、Safari 4.1 では、同一オリジン制限に違反した XMLHttpRequest オブジェクトは再度 open() しても未送信状態に戻りません。\r
-                               if( X_UA.Opera || X_UA.Webkit ){\r
-                                       \r
+                               \r
+                               // Timeout した Gecko の xhr.response に触るとエラー??\r
+                               if( X_UA.Opera || X_UA.Webkit || X_UA.Gecko ){\r
+                                       if( this._error ){\r
+                                               X_EventDispatcher_toggleAllEvents( this, false );\r
+                                               this._rawObject = new XMLHttpRequest();\r
+                                               X_EventDispatcher_toggleAllEvents( this, true );\r
+                                       };\r
                                };\r
 \r
                                // XMLHttpRequest の使い方\r
@@ -225,7 +231,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                this._rawObject.abort && this._rawObject.abort();\r
 \r
                                this._method   = this._type = '';\r
-                               this._canceled = this._busy = false;\r
+                               this._canceled = this._busy = this._error = false;\r
                                this._timerID && X.Timer.remove( this._timerID );\r
                                this._percent  = this._timerID = 0;\r
                        },\r
@@ -276,7 +282,7 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        case 'load' :\r
                                                \r
                                                if( !live ) return this.reset();\r
-                                               if( this._percent === 100 ) return;\r
+                                               if( !this._busy ) return;\r
                                                \r
                                                \r
                                                this._percent = 100;\r
@@ -325,9 +331,9 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                                                        break;\r
                                                        };\r
 \r
-                                                       this.asyncDispatch( { type : X.Event.SUCCESS, status : status || 200, data : data } );                                                  \r
+                                                       this.asyncDispatch( 32, { type : X.Event.SUCCESS, status : status || 200, data : data } );\r
                                                } else {\r
-                                                       live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } );\r
+                                                       live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status || 0, percent : 100 } );\r
                                                };\r
                                                break;\r
                                        \r
@@ -340,12 +346,14 @@ if( X_Net_XHR_W3C || X_Net_XHR_ACTIVE_X ){
                                        \r
                                        case 'error' :\r
                                        //console.dir( e );\r
-                                               this._busy = false;\r
-                                               live && this.asyncDispatch( { type : X.Event.ERROR, status : raw.status } );\r
+                                               this._busy  = false;\r
+                                               this._error = X_UA.Opera || X_UA.Webkit;\r
+                                               live && this.asyncDispatch( 32, { type : X.Event.ERROR, status : raw.status } );\r
                                                break;\r
 \r
                                        case 'timeout' : // Gecko 12.0 https://developer.mozilla.org/ja/docs/XMLHttpRequest/Synchronous_and_Asynchronous_Requests\r
-                                               this._busy = false;\r
+                                               this._busy  = false;\r
+                                               this._error = !!X_UA.Gecko;\r
                                                this.asyncDispatch( X.Event.TIMEOUT );\r
                                                break;\r
                                };\r