react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / http-browserify / lib / request.js
80724 viewsvar Stream = require('stream');1var Response = require('./response');2var Base64 = require('Base64');3var inherits = require('inherits');45var Request = module.exports = function (xhr, params) {6var self = this;7self.writable = true;8self.xhr = xhr;9self.body = [];1011self.uri = (params.protocol || 'http:') + '//'12+ params.host13+ (params.port ? ':' + params.port : '')14+ (params.path || '/')15;1617if (typeof params.withCredentials === 'undefined') {18params.withCredentials = true;19}2021try { xhr.withCredentials = params.withCredentials }22catch (e) {}2324if (params.responseType) try { xhr.responseType = params.responseType }25catch (e) {}2627xhr.open(28params.method || 'GET',29self.uri,30true31);3233xhr.onerror = function(event) {34self.emit('error', new Error('Network error'));35};3637self._headers = {};3839if (params.headers) {40var keys = objectKeys(params.headers);41for (var i = 0; i < keys.length; i++) {42var key = keys[i];43if (!self.isSafeRequestHeader(key)) continue;44var value = params.headers[key];45self.setHeader(key, value);46}47}4849if (params.auth) {50//basic auth51this.setHeader('Authorization', 'Basic ' + Base64.btoa(params.auth));52}5354var res = new Response;55res.on('close', function () {56self.emit('close');57});5859res.on('ready', function () {60self.emit('response', res);61});6263res.on('error', function (err) {64self.emit('error', err);65});6667xhr.onreadystatechange = function () {68// Fix for IE9 bug69// SCRIPT575: Could not complete the operation due to error c00c023f70// It happens when a request is aborted, calling the success callback anyway with readyState === 471if (xhr.__aborted) return;72res.handle(xhr);73};74};7576inherits(Request, Stream);7778Request.prototype.setHeader = function (key, value) {79this._headers[key.toLowerCase()] = value80};8182Request.prototype.getHeader = function (key) {83return this._headers[key.toLowerCase()]84};8586Request.prototype.removeHeader = function (key) {87delete this._headers[key.toLowerCase()]88};8990Request.prototype.write = function (s) {91this.body.push(s);92};9394Request.prototype.destroy = function (s) {95this.xhr.__aborted = true;96this.xhr.abort();97this.emit('close');98};99100Request.prototype.end = function (s) {101if (s !== undefined) this.body.push(s);102103var keys = objectKeys(this._headers);104for (var i = 0; i < keys.length; i++) {105var key = keys[i];106var value = this._headers[key];107if (isArray(value)) {108for (var j = 0; j < value.length; j++) {109this.xhr.setRequestHeader(key, value[j]);110}111}112else this.xhr.setRequestHeader(key, value)113}114115if (this.body.length === 0) {116this.xhr.send('');117}118else if (typeof this.body[0] === 'string') {119this.xhr.send(this.body.join(''));120}121else if (isArray(this.body[0])) {122var body = [];123for (var i = 0; i < this.body.length; i++) {124body.push.apply(body, this.body[i]);125}126this.xhr.send(body);127}128else if (/Array/.test(Object.prototype.toString.call(this.body[0]))) {129var len = 0;130for (var i = 0; i < this.body.length; i++) {131len += this.body[i].length;132}133var body = new(this.body[0].constructor)(len);134var k = 0;135136for (var i = 0; i < this.body.length; i++) {137var b = this.body[i];138for (var j = 0; j < b.length; j++) {139body[k++] = b[j];140}141}142this.xhr.send(body);143}144else if (isXHR2Compatible(this.body[0])) {145this.xhr.send(this.body[0]);146}147else {148var body = '';149for (var i = 0; i < this.body.length; i++) {150body += this.body[i].toString();151}152this.xhr.send(body);153}154};155156// Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html157Request.unsafeHeaders = [158"accept-charset",159"accept-encoding",160"access-control-request-headers",161"access-control-request-method",162"connection",163"content-length",164"cookie",165"cookie2",166"content-transfer-encoding",167"date",168"expect",169"host",170"keep-alive",171"origin",172"referer",173"te",174"trailer",175"transfer-encoding",176"upgrade",177"user-agent",178"via"179];180181Request.prototype.isSafeRequestHeader = function (headerName) {182if (!headerName) return false;183return indexOf(Request.unsafeHeaders, headerName.toLowerCase()) === -1;184};185186var objectKeys = Object.keys || function (obj) {187var keys = [];188for (var key in obj) keys.push(key);189return keys;190};191192var isArray = Array.isArray || function (xs) {193return Object.prototype.toString.call(xs) === '[object Array]';194};195196var indexOf = function (xs, x) {197if (xs.indexOf) return xs.indexOf(x);198for (var i = 0; i < xs.length; i++) {199if (xs[i] === x) return i;200}201return -1;202};203204var isXHR2Compatible = function (obj) {205if (typeof Blob !== 'undefined' && obj instanceof Blob) return true;206if (typeof ArrayBuffer !== 'undefined' && obj instanceof ArrayBuffer) return true;207if (typeof FormData !== 'undefined' && obj instanceof FormData) return true;208};209210211