123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- var forge = require('./forge');
- require('./socket');
- require('./http');
- var xhrApi = module.exports = forge.xhr = forge.xhr || {};
- (function($) {
- var cat = 'forge.xhr';
- var UNSENT = 0;
- var OPENED = 1;
- var HEADERS_RECEIVED = 2;
- var LOADING = 3;
- var DONE = 4;
- var INVALID_STATE_ERR = 11;
- var SYNTAX_ERR = 12;
- var SECURITY_ERR = 18;
- var NETWORK_ERR = 19;
- var ABORT_ERR = 20;
- var _sp = null;
- var _policyPort = 0;
- var _policyUrl = null;
- var _client = null;
- var _clients = {};
- var _maxConnections = 10;
- var net = forge.net;
- var http = forge.http;
- xhrApi.init = function(options) {
- forge.log.debug(cat, 'initializing', options);
-
- _policyPort = options.policyPort || _policyPort;
- _policyUrl = options.policyUrl || _policyUrl;
- _maxConnections = options.connections || _maxConnections;
-
- _sp = net.createSocketPool({
- flashId: options.flashId,
- policyPort: _policyPort,
- policyUrl: _policyUrl,
- msie: options.msie || false
- });
-
- _client = http.createClient({
- url: options.url || (
- window.location.protocol + '//' + window.location.host),
- socketPool: _sp,
- policyPort: _policyPort,
- policyUrl: _policyUrl,
- connections: options.connections || _maxConnections,
- caCerts: options.caCerts,
- cipherSuites: options.cipherSuites,
- persistCookies: options.persistCookies || true,
- primeTlsSockets: options.primeTlsSockets || false,
- verify: options.verify,
- getCertificate: options.getCertificate,
- getPrivateKey: options.getPrivateKey,
- getSignature: options.getSignature
- });
- _clients[_client.url.full] = _client;
- forge.log.debug(cat, 'ready');
- };
- xhrApi.cleanup = function() {
-
- for(var key in _clients) {
- _clients[key].destroy();
- }
- _clients = {};
- _client = null;
-
- _sp.destroy();
- _sp = null;
- };
- xhrApi.setCookie = function(cookie) {
-
- cookie.maxAge = cookie.maxAge || -1;
-
- if(cookie.domain) {
-
- for(var key in _clients) {
- var client = _clients[key];
- if(http.withinCookieDomain(client.url, cookie) &&
- client.secure === cookie.secure) {
- client.setCookie(cookie);
- }
- }
- } else {
-
-
-
- _client.setCookie(cookie);
- }
- };
- xhrApi.getCookie = function(name, path, domain) {
- var rval = null;
- if(domain) {
-
- for(var key in _clients) {
- var client = _clients[key];
- if(http.withinCookieDomain(client.url, domain)) {
- var cookie = client.getCookie(name, path);
- if(cookie !== null) {
- if(rval === null) {
- rval = cookie;
- } else if(!forge.util.isArray(rval)) {
- rval = [rval, cookie];
- } else {
- rval.push(cookie);
- }
- }
- }
- }
- } else {
-
- rval = _client.getCookie(name, path);
- }
- return rval;
- };
- xhrApi.removeCookie = function(name, path, domain) {
- var rval = false;
- if(domain) {
-
- for(var key in _clients) {
- var client = _clients[key];
- if(http.withinCookieDomain(client.url, domain)) {
- if(client.removeCookie(name, path)) {
- rval = true;
- }
- }
- }
- } else {
-
- rval = _client.removeCookie(name, path);
- }
- return rval;
- };
- xhrApi.create = function(options) {
-
- options = $.extend({
- logWarningOnError: true,
- verbose: false,
- logError: function() {},
- logWarning: function() {},
- logDebug: function() {},
- logVerbose: function() {},
- url: null
- }, options || {});
-
- var _state = {
-
- client: null,
-
- request: null,
-
- response: null,
-
- asynchronous: true,
-
- sendFlag: false,
-
- errorFlag: false
- };
-
- var _log = {
- error: options.logError || forge.log.error,
- warning: options.logWarning || forge.log.warning,
- debug: options.logDebug || forge.log.debug,
- verbose: options.logVerbose || forge.log.verbose
- };
-
- var xhr = {
-
- onreadystatechange: null,
-
- readyState: UNSENT,
-
- responseText: '',
-
- responseXML: null,
-
- status: 0,
-
- statusText: ''
- };
-
- if(options.url === null) {
-
- _state.client = _client;
- } else {
- var url = http.parseUrl(options.url);
- if(!url) {
- var error = new Error('Invalid url.');
- error.details = {
- url: options.url
- };
- }
-
- if(url.full in _clients) {
-
- _state.client = _clients[url.full];
- } else {
-
- _state.client = http.createClient({
- url: options.url,
- socketPool: _sp,
- policyPort: options.policyPort || _policyPort,
- policyUrl: options.policyUrl || _policyUrl,
- connections: options.connections || _maxConnections,
- caCerts: options.caCerts,
- cipherSuites: options.cipherSuites,
- persistCookies: options.persistCookies || true,
- primeTlsSockets: options.primeTlsSockets || false,
- verify: options.verify,
- getCertificate: options.getCertificate,
- getPrivateKey: options.getPrivateKey,
- getSignature: options.getSignature
- });
- _clients[url.full] = _state.client;
- }
- }
-
- xhr.open = function(method, url, async, user, password) {
-
-
-
-
-
-
-
- switch(method) {
- case 'DELETE':
- case 'GET':
- case 'HEAD':
- case 'OPTIONS':
- case 'PATCH':
- case 'POST':
- case 'PUT':
-
- break;
- case 'CONNECT':
- case 'TRACE':
- case 'TRACK':
- throw new Error('CONNECT, TRACE and TRACK methods are disallowed');
- default:
- throw new Error('Invalid method: ' + method);
- }
-
-
-
-
-
-
-
-
- _state.sendFlag = false;
- xhr.responseText = '';
- xhr.responseXML = null;
-
- xhr.status = 0;
- xhr.statusText = '';
-
- _state.request = http.createRequest({
- method: method,
- path: url
- });
-
- xhr.readyState = OPENED;
-
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
- };
-
- xhr.setRequestHeader = function(header, value) {
-
- if(xhr.readyState != OPENED || _state.sendFlag) {
- throw new Error('XHR not open or sending');
- }
-
-
- _state.request.setField(header, value);
- };
-
- xhr.send = function(data) {
-
-
- if(xhr.readyState != OPENED || _state.sendFlag) {
- throw new Error('XHR not open or sending');
- }
-
- if(data &&
- _state.request.method !== 'GET' &&
- _state.request.method !== 'HEAD') {
-
- if(typeof(XMLSerializer) !== 'undefined') {
- if(data instanceof Document) {
- var xs = new XMLSerializer();
- _state.request.body = xs.serializeToString(data);
- } else {
- _state.request.body = data;
- }
- } else {
-
- if(typeof(data.xml) !== 'undefined') {
- _state.request.body = data.xml;
- } else {
- _state.request.body = data;
- }
- }
- }
-
-
- _state.errorFlag = false;
-
-
- _state.sendFlag = true;
-
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
-
- var options = {};
- options.request = _state.request;
- options.headerReady = function(e) {
-
- xhr.cookies = _state.client.cookies;
-
-
-
- xhr.readyState = HEADERS_RECEIVED;
- xhr.status = e.response.code;
- xhr.statusText = e.response.message;
- _state.response = e.response;
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
- if(!_state.response.aborted) {
-
- xhr.readyState = LOADING;
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
- }
- };
- options.bodyReady = function(e) {
- xhr.readyState = DONE;
- var ct = e.response.getField('Content-Type');
-
-
- if(ct) {
- if(ct.indexOf('text/xml') === 0 ||
- ct.indexOf('application/xml') === 0 ||
- ct.indexOf('+xml') !== -1) {
- try {
- var doc = new ActiveXObject('MicrosoftXMLDOM');
- doc.async = false;
- doc.loadXML(e.response.body);
- xhr.responseXML = doc;
- } catch(ex) {
- var parser = new DOMParser();
- xhr.responseXML = parser.parseFromString(ex.body, 'text/xml');
- }
- }
- }
- var length = 0;
- if(e.response.body !== null) {
- xhr.responseText = e.response.body;
- length = e.response.body.length;
- }
-
- var req = _state.request;
- var output =
- req.method + ' ' + req.path + ' ' +
- xhr.status + ' ' + xhr.statusText + ' ' +
- length + 'B ' +
- (e.request.connectTime + e.request.time + e.response.time) +
- 'ms';
- var lFunc;
- if(options.verbose) {
- lFunc = (xhr.status >= 400 && options.logWarningOnError) ?
- _log.warning : _log.verbose;
- lFunc(cat, output,
- e, e.response.body ? '\n' + e.response.body : '\nNo content');
- } else {
- lFunc = (xhr.status >= 400 && options.logWarningOnError) ?
- _log.warning : _log.debug;
- lFunc(cat, output);
- }
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
- };
- options.error = function(e) {
- var req = _state.request;
- _log.error(cat, req.method + ' ' + req.path, e);
-
- xhr.responseText = '';
- xhr.responseXML = null;
-
- _state.errorFlag = true;
- xhr.status = 0;
- xhr.statusText = '';
-
- xhr.readyState = DONE;
-
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
- };
-
- _state.client.send(options);
- };
-
- xhr.abort = function() {
-
-
- _state.request.abort();
-
- xhr.responseText = '';
- xhr.responseXML = null;
-
- _state.errorFlag = true;
- xhr.status = 0;
- xhr.statusText = '';
-
- _state.request = null;
- _state.response = null;
-
- if(xhr.readyState === DONE || xhr.readyState === UNSENT ||
- (xhr.readyState === OPENED && !_state.sendFlag)) {
-
- xhr.readyState = UNSENT;
- } else {
-
- xhr.readyState = DONE;
-
- _state.sendFlag = false;
-
- if(xhr.onreadystatechange) {
- xhr.onreadystatechange();
- }
-
- xhr.readyState = UNSENT;
- }
- };
-
- xhr.getAllResponseHeaders = function() {
- var rval = '';
- if(_state.response !== null) {
- var fields = _state.response.fields;
- $.each(fields, function(name, array) {
- $.each(array, function(i, value) {
- rval += name + ': ' + value + '\r\n';
- });
- });
- }
- return rval;
- };
-
- xhr.getResponseHeader = function(header) {
- var rval = null;
- if(_state.response !== null) {
- if(header in _state.response.fields) {
- rval = _state.response.fields[header];
- if(forge.util.isArray(rval)) {
- rval = rval.join();
- }
- }
- }
- return rval;
- };
- return xhr;
- };
- })(jQuery);
|