[ Index ]
 

Code source de Mantis 1.1.0rc3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/javascript/ -> xmlhttprequest.js (source)

   1  /*
   2  # Mantis - a php based bugtracking system
   3  
   4  # Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
   5  # Copyright (C) 2002 - 2007  Mantis Team   - mantisbt-dev@lists.sourceforge.net
   6  
   7  # Mantis 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  # Mantis 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 Mantis.  If not, see <http://www.gnu.org/licenses/>.
  19   *
  20   * --------------------------------------------------------
  21   * $Id: xmlhttprequest.js,v 1.2.22.1 2007-10-13 22:35:57 giallu Exp $
  22   * --------------------------------------------------------
  23   */
  24  /*
  25  
  26  Cross-Browser XMLHttpRequest v1.1
  27  =================================
  28  
  29  Emulate Gecko 'XMLHttpRequest()' functionality in IE and Opera. Opera requires
  30  the Sun Java Runtime Environment <http://www.java.com/>.
  31  
  32  by Andrew Gregory
  33  http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/
  34  
  35  This work is licensed under the Creative Commons Attribution License. To view a
  36  copy of this license, visit http://creativecommons.org/licenses/by/1.0/ or send
  37  a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305,
  38  USA.
  39  
  40  Not Supported in Opera
  41  ----------------------
  42  * user/password authentication
  43  * responseXML data member
  44  
  45  Not Fully Supported in Opera
  46  ----------------------------
  47  * async requests
  48  * abort()
  49  * getAllResponseHeaders(), getAllResponseHeader(header)
  50  */
  51  
  52  /*
  53   * commented out (30/07/2004) because it was causing subsequent request to freeze
  54  // IE support
  55  if (window.ActiveXObject && !window.XMLHttpRequest) {
  56    window.XMLHttpRequest = function() {
  57      return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP');
  58    };
  59  }
  60  */
  61  
  62  // Gecko support
  63  /* ;-) */
  64  // Opera support
  65  if (window.opera && !window.XMLHttpRequest) {
  66    window.XMLHttpRequest = function() {
  67      this.readyState = 0; // 0=uninitialized,1=loading,2=loaded,3=interactive,4=complete
  68      this.status = 0; // HTTP status codes
  69      this.statusText = '';
  70      this._headers = [];
  71      this._aborted = false;
  72      this._async = true;
  73      this.abort = function() {
  74        this._aborted = true;
  75      };
  76      this.getAllResponseHeaders = function() {
  77        return this.getAllResponseHeader('*');
  78      };
  79      this.getAllResponseHeader = function(header) {
  80        var ret = '';
  81        for (var i = 0; i < this._headers.length; i++) {
  82          if (header == '*' || this._headers[i].h == header) {
  83            ret += this._headers[i].h + ': ' + this._headers[i].v + '\n';
  84          }
  85        }
  86        return ret;
  87      };
  88      this.setRequestHeader = function(header, value) {
  89        this._headers[this._headers.length] = {h:header, v:value};
  90      };
  91      this.open = function(method, url, async, user, password) {
  92        this.method = method;
  93        this.url = url;
  94        this._async = true;
  95        this._aborted = false;
  96        if (arguments.length >= 3) {
  97          this._async = async;
  98        }
  99        if (arguments.length > 3) {
 100          // user/password support requires a custom Authenticator class
 101          opera.postError('XMLHttpRequest.open() - user/password not supported');
 102        }
 103        this._headers = [];
 104        this.readyState = 1;
 105        if (this.onreadystatechange) {
 106          this.onreadystatechange();
 107        }
 108      };
 109      this.send = function(data) {
 110        if (!navigator.javaEnabled()) {
 111          alert("XMLHttpRequest.send() - Java must be installed and enabled.");
 112          return;
 113        }
 114        if (this._async) {
 115          setTimeout(this._sendasync, 0, this, data);
 116          // this is not really asynchronous and won't execute until the current
 117          // execution context ends
 118        } else {
 119          this._sendsync(data);
 120        }
 121      }
 122      this._sendasync = function(req, data) {
 123        if (!req._aborted) {
 124          req._sendsync(data);
 125        }
 126      };
 127      this._sendsync = function(data) {
 128        this.readyState = 2;
 129        if (this.onreadystatechange) {
 130          this.onreadystatechange();
 131        }
 132        // open connection
 133        var url = new java.net.URL(new java.net.URL(window.location.href), this.url);
 134        var conn = url.openConnection();
 135        for (var i = 0; i < this._headers.length; i++) {
 136          conn.setRequestProperty(this._headers[i].h, this._headers[i].v);
 137        }
 138        this._headers = [];
 139        if (this.method == 'POST') {
 140          // POST data
 141          conn.setDoOutput(true);
 142          var wr = new java.io.OutputStreamWriter(conn.getOutputStream());
 143          wr.write(data);
 144          wr.flush();
 145          wr.close();
 146        }
 147        // read response headers
 148        // NOTE: the getHeaderField() methods always return nulls for me :(
 149        var gotContentEncoding = false;
 150        var gotContentLength = false;
 151        var gotContentType = false;
 152        var gotDate = false;
 153        var gotExpiration = false;
 154        var gotLastModified = false;
 155        for (var i = 0; ; i++) {
 156          var hdrName = conn.getHeaderFieldKey(i);
 157          var hdrValue = conn.getHeaderField(i);
 158          if (hdrName == null && hdrValue == null) {
 159            break;
 160          }
 161          if (hdrName != null) {
 162            this._headers[this._headers.length] = {h:hdrName, v:hdrValue};
 163            switch (hdrName.toLowerCase()) {
 164              case 'content-encoding': gotContentEncoding = true; break;
 165              case 'content-length'  : gotContentLength   = true; break;
 166              case 'content-type'    : gotContentType     = true; break;
 167              case 'date'            : gotDate            = true; break;
 168              case 'expires'         : gotExpiration      = true; break;
 169              case 'last-modified'   : gotLastModified    = true; break;
 170            }
 171          }
 172        }
 173        // try to fill in any missing header information
 174        var val;
 175        val = conn.getContentEncoding();
 176        if (val != null && !gotContentEncoding) this._headers[this._headers.length] = {h:'Content-encoding', v:val};
 177        val = conn.getContentLength();
 178        if (val != -1 && !gotContentLength) this._headers[this._headers.length] = {h:'Content-length', v:val};
 179        val = conn.getContentType();
 180        if (val != null && !gotContentType) this._headers[this._headers.length] = {h:'Content-type', v:val};
 181        val = conn.getDate();
 182        if (val != 0 && !gotDate) this._headers[this._headers.length] = {h:'Date', v:(new Date(val)).toUTCString()};
 183        val = conn.getExpiration();
 184        if (val != 0 && !gotExpiration) this._headers[this._headers.length] = {h:'Expires', v:(new Date(val)).toUTCString()};
 185        val = conn.getLastModified();
 186        if (val != 0 && !gotLastModified) this._headers[this._headers.length] = {h:'Last-modified', v:(new Date(val)).toUTCString()};
 187        // read response data
 188        var reqdata = '';
 189        var stream = conn.getInputStream();
 190        if (stream) {
 191          var reader = new java.io.BufferedReader(new java.io.InputStreamReader(stream));
 192          var line;
 193          while ((line = reader.readLine()) != null) {
 194            if (this.readyState == 2) {
 195              this.readyState = 3;
 196              if (this.onreadystatechange) {
 197                this.onreadystatechange();
 198              }
 199            }
 200            reqdata += line + '\n';
 201          }
 202          reader.close();
 203          this.status = 200;
 204          this.statusText = 'OK';
 205          this.responseText = reqdata;
 206          this.readyState = 4;
 207          if (this.onreadystatechange) {
 208            this.onreadystatechange();
 209          }
 210          if (this.onload) {
 211            this.onload();
 212          }
 213        } else {
 214          // error
 215          this.status = 404;
 216          this.statusText = 'Not Found';
 217          this.responseText = '';
 218          this.readyState = 4;
 219          if (this.onreadystatechange) {
 220            this.onreadystatechange();
 221          }
 222          if (this.onerror) {
 223            this.onerror();
 224          }
 225        }
 226      };
 227    };
 228  }
 229  // ActiveXObject emulation
 230  if (!window.ActiveXObject && window.XMLHttpRequest) {
 231    window.ActiveXObject = function(type) {
 232      switch (type.toLowerCase()) {
 233        case 'microsoft.xmlhttp':
 234        case 'msxml2.xmlhttp':
 235          return new XMLHttpRequest();
 236      }
 237      return null;
 238    };
 239  }


Généré le : Thu Nov 29 09:42:17 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics