You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

581 lines
15 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: bcoin/http/base.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: bcoin/http/base.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*!
* http.js - http server for bcoin
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
* https://github.com/indutny/bcoin
*/
var EventEmitter = require('events').EventEmitter;
var utils = require('../utils');
/**
* HTTPBase
* @exports HTTPBase
* @constructor
* @param {Object?} options
* @emits HTTPBase#websocket
*/
function HTTPBase(options) {
if (!(this instanceof HTTPBase))
return new HTTPBase(options);
if (!options)
options = {};
this.options = options;
this.io = null;
this.routes = {
get: [],
post: [],
put: [],
del: []
};
this.stack = [];
this.server = options.key
? require('https').createServer(options)
: require('http').createServer();
this._init();
}
utils.inherits(HTTPBase, EventEmitter);
HTTPBase.prototype._init = function _init() {
var self = this;
this._initRouter();
this._initIO();
this.server.on('connection', function(socket) {
socket.on('error', function(err) {
self.emit('error', err);
try {
socket.destroy();
} catch (e) {
;
}
});
});
this.server.on('error', function(err) {
self.emit('error', err);
});
};
HTTPBase.prototype._initIO = function _initIO() {
var self = this;
var IOServer;
if (!this.options.sockets)
return;
try {
IOServer = require('socket.io');
} catch (e) {
;
}
if (!IOServer)
return;
this.io = new IOServer();
this.io.attach(this.server);
this.io.on('connection', function(socket) {
self.emit('websocket', socket);
});
};
HTTPBase.prototype._initRouter = function _initRouter() {
var self = this;
this.server.on('request', function(req, res) {
function _send(code, msg, type) {
send(res, code, msg, type);
}
function done(err) {
if (err) {
send(res, 400, { error: err.message + '' });
try {
req.destroy();
req.socket.destroy();
} catch (e) {
self.emit('error', e);
}
self.emit('error', err);
}
}
try {
parsePath(req);
} catch (e) {
return done(e);
}
self.emit('request', req, res);
parseBody(req, function(err) {
var method, routes, i;
if (err)
return done(err);
method = (req.method || 'GET').toLowerCase();
routes = self.routes[method];
i = 0;
if (!routes)
return done(new Error('No routes found.'));
(function next(err) {
var route, path, callback, compiled, matched;
if (err)
return done(err);
if (i === routes.length)
return done(new Error('Route not found.'));
route = routes[i++];
path = route.path;
callback = route.callback;
if (!route.regex) {
compiled = compilePath(path);
route.regex = compiled.regex;
route.map = compiled.map;
}
matched = route.regex.exec(req.pathname);
if (!matched)
return next();
req.params = {};
matched.slice(1).forEach(function(item, i) {
if (route.map[i])
req.params[route.map[i]] = item;
req.params[i] = item;
});
self._handle(req, res, _send, function(err) {
if (err)
return done(err);
// Avoid stack overflows
utils.nextTick(function() {
try {
callback(req, res, next, _send);
} catch (e) {
done(e);
}
});
});
})();
});
});
};
HTTPBase.prototype._handle = function _handle(req, res, _send, callback) {
var self = this;
var i = 0;
var handler;
(function next(err) {
if (err)
return callback(err);
if (i === self.stack.length)
return callback();
handler = self.stack[i++];
utils.nextTick(function() {
if (handler.path &amp;&amp; req.pathname.indexOf(handler.path) === -1)
return next();
try {
handler.callback(req, res, next, _send);
} catch (e) {
next(e);
}
});
})();
};
/**
* Middleware and route callback.
* @callback RouteCallback
* @param {HTTPRequest} req
* @param {HTTPResponse} res
* @param {Function} next
* @param {Function} send
*/
/**
* Add a middleware to the stack.
* @param {String?} path
* @param {RouteCallback} callback
*/
HTTPBase.prototype.use = function use(path, callback) {
if (!callback) {
callback = path;
path = null;
}
if (Array.isArray(path)) {
path.forEach(function(path) {
this.use(path, callback);
}, this);
return;
}
this.stack.push({ path: path, callback: callback });
};
/**
* Add a GET route.
* @param {String?} path
* @param {RouteCallback} callback
*/
HTTPBase.prototype.get = function get(path, callback) {
if (Array.isArray(path)) {
path.forEach(function(path) {
this.get(path, callback);
}, this);
return;
}
this.routes.get.push({ path: path, callback: callback });
};
/**
* Add a POST route.
* @param {String?} path
* @param {RouteCallback} callback
*/
HTTPBase.prototype.post = function post(path, callback) {
if (Array.isArray(path)) {
path.forEach(function(path) {
this.post(path, callback);
}, this);
return;
}
this.routes.post.push({ path: path, callback: callback });
};
/**
* Add a PUT route.
* @param {String?} path
* @param {RouteCallback} callback
*/
HTTPBase.prototype.put = function put(path, callback) {
if (Array.isArray(path)) {
path.forEach(function(path) {
this.put(path, callback);
}, this);
return;
}
this.routes.put.push({ path: path, callback: callback });
};
/**
* Add a DELETE route.
* @param {String?} path
* @param {RouteCallback} callback
*/
HTTPBase.prototype.del = function del(path, callback) {
if (Array.isArray(path)) {
path.forEach(function(path) {
this.del(path, callback);
}, this);
return;
}
this.routes.del.push({ path: path, callback: callback });
};
/**
* Get server address.
* @returns {Object}
*/
HTTPBase.prototype.address = function address() {
return this.server.address();
};
/**
* Listen on port and host.
* @param {Number} port
* @param {String?} host
* @param {Function} callback
*/
HTTPBase.prototype.listen = function listen(port, host, callback) {
var self = this;
this.server.listen(port, host, function(err) {
if (!callback) {
if (err)
throw err;
return;
}
if (err)
return callback(err);
return callback(null, self.address());
});
};
/**
* Close the the server.
* @param {Function} callback
*/
HTTPBase.prototype.close = function close(callback) {
this.server.close(callback);
};
/*
* Helpers
*/
function send(res, code, msg, type) {
var len;
if (!msg)
msg = { error: 'No message.' };
try {
res.statusCode = code;
if (msg &amp;&amp; typeof msg === 'object' &amp;&amp; !Buffer.isBuffer(msg)) {
msg = JSON.stringify(msg, null, 2) + '\n';
type = 'json';
}
if (type === 'html')
res.setHeader('Content-Type', 'text/html; charset=utf-8');
else if (type === 'text')
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
else if (type === 'json')
res.setHeader('Content-Type', 'application/json; charset=utf-8');
else if (type === 'js')
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
else if (type === 'binary')
res.setHeader('Content-Type', 'application/octet-stream; charset=utf-8');
len = typeof msg === 'string'
? Buffer.byteLength(msg, 'utf8')
: msg.length;
res.setHeader('Content-Length', len + '');
res.write(msg);
res.end();
} catch (e) {
;
}
}
function compilePath(path) {
var map = [];
if (path instanceof RegExp)
return { regex: path, map: map };
var regex = path
.replace(/(\/[^\/]+)\?/g, '(?:$1)?')
.replace(/\.(?!\+)/g, '\\.')
.replace(/\*/g, '.*?')
.replace(/%/g, '\\')
.replace(/:(\w+)/g, function(__, name) {
map.push(name);
return '([^/]+)';
}
);
regex = new RegExp('^' + regex + '$');
return {
map: map,
regex: regex
};
}
function parseBody(req, callback) {
var StringDecoder = require('string_decoder').StringDecoder;
var decode = new StringDecoder('utf8');
var total = 0;
var body = '';
req.body = {};
if (req.method === 'GET')
return callback();
req.on('data', function(data) {
total += data.length;
if (total > 20 * 1024 * 1024)
return callback(new Error('Overflow.'));
body += decode.write(data);
});
req.on('error', function(err) {
try {
req.destroy();
req.socket.destroy();
} catch (e) {
;
}
callback(err);
});
req.on('end', function() {
try {
if (body)
req.body = JSON.parse(body);
} catch (e) {
return callback(e);
}
callback();
});
}
function parsePairs(str, del, eq) {
var out, s, i, parts;
if (!str)
return {};
if (!del)
del = '&amp;';
if (!eq)
eq = '=';
out = {};
s = str.split(del);
for (i = 0; i &lt; s.length; i++) {
parts = s[i].split(eq);
if (parts[0]) {
parts[0] = unescape(parts[0]);
parts[1] = parts[1] ? unescape(parts[1]) : '';
out[parts[0]] = parts[1];
}
}
return out;
}
function parsePath(req) {
var url = require('url');
var uri = url.parse(req.url);
var pathname = uri.pathname || '/';
if (pathname[pathname.length - 1] === '/')
pathname = pathname.slice(0, -1);
pathname = unescape(pathname);
req.path = pathname;
if (req.path[0] === '/')
req.path = req.path.substring(1);
req.path = req.path.split('/');
if (!req.path[0])
req.path = [];
req.pathname = pathname || '/';
if (req.url.indexOf('//') !== -1) {
req.url = req.url.replace(/^([^:\/]+)?\/\/[^\/]+/, '');
if (!req.url)
req.url = '/';
}
if (!req.query) {
req.query = uri.query
? parsePairs(uri.query, '&amp;')
: {};
}
}
function unescape(str) {
try {
str = decodeURIComponent(str).replace(/\+/g, ' ');
} finally {
return str.replace(/\0/g, '');
}
}
/*
* Expose
*/
module.exports = HTTPBase;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bcoin.html">bcoin</a></li><li><a href="module-constants.html">constants</a></li><li><a href="module-ec.html">ec</a></li><li><a href="module-ldb.html">ldb</a></li><li><a href="module-network.html">network</a></li><li><a href="module-profiler.html">profiler</a></li><li><a href="module-request.html">request</a></li><li><a href="module-uri.html">uri</a></li><li><a href="module-utils.html">utils</a></li></ul><h3>Classes</h3><ul><li><a href="AbstractBlock.html">AbstractBlock</a></li><li><a href="Account.html">Account</a></li><li><a href="Address.html">Address</a></li><li><a href="AES.html">AES</a></li><li><a href="Block.html">Block</a></li><li><a href="Bloom.html">Bloom</a></li><li><a href="BroadcastItem.html">BroadcastItem</a></li><li><a href="BST.html">BST</a></li><li><a href="BufferReader.html">BufferReader</a></li><li><a href="BufferWriter.html">BufferWriter</a></li><li><a href="Chain.html">Chain</a></li><li><a href="ChainDB.html">ChainDB</a></li><li><a href="ChainEntry.html">ChainEntry</a></li><li><a href="Coin.html">Coin</a></li><li><a href="Coins.html">Coins</a></li><li><a href="CoinView.html">CoinView</a></li><li><a href="CompactBlock.html">CompactBlock</a></li><li><a href="Environment.html">Environment</a></li><li><a href="Framer.html">Framer</a></li><li><a href="Fullnode.html">Fullnode</a></li><li><a href="HD.html">HD</a></li><li><a href="HDPrivateKey.html">HDPrivateKey</a></li><li><a href="HDPublicKey.html">HDPublicKey</a></li><li><a href="Headers.html">Headers</a></li><li><a href="HTTPBase.html">HTTPBase</a></li><li><a href="HTTPClient.html">HTTPClient</a></li><li><a href="HTTPServer.html">HTTPServer</a></li><li><a href="HTTPWallet.html">HTTPWallet</a></li><li><a href="Input.html">Input</a></li><li><a href="KeyPair.html">KeyPair</a></li><li><a href="KeyRing.html">KeyRing</a></li><li><a href="LoadRequest.html">LoadRequest</a></li><li><a href="Locker.html">Locker</a></li><li><a href="LowlevelUp.html">LowlevelUp</a></li><li><a href="LRU.html">LRU</a></li><li><a href="Master.html">Master</a></li><li><a href="Mempool.html">Mempool</a></li><li><a href="MempoolEntry.html">MempoolEntry</a></li><li><a href="MerkleBlock.html">MerkleBlock</a></li><li><a href="Miner.html">Miner</a></li><li><a href="MinerBlock.html">MinerBlock</a></li><li><a href="Mnemonic.html">Mnemonic</a></li><li><a href="MTX.html">MTX</a></li><li><a href="Network.html">Network</a></li><li><a href="NetworkAddress.html">NetworkAddress</a></li><li><a href="Node.html">Node</a></li><li><a href="NullCache.html">NullCache</a></li><li><a href="Output.html">Output</a></li><li><a href="Parser.html">Parser</a></li><li><a href="Peer.html">Peer</a></li><li><a href="Pool.html">Pool</a></li><li><a href="Profile.html">Profile</a></li><li><a href="RollingFilter.html">RollingFilter</a></li><li><a href="Script.html">Script</a></li><li><a href="ScriptError.html">ScriptError</a></li><li><a href="Snapshot.html">Snapshot</a></li><li><a href="SPVNode.html">SPVNode</a></li><li><a href="Stack.html">Stack</a></li><li><a href="TimeData.html">TimeData</a></li><li><a href="TX.html">TX</a></li><li><a href="TXDB.html">TXDB</a></li><li><a href="VerifyError.html">VerifyError</a></li><li><a href="Wallet.html">Wallet</a></li><li><a href="WalletDB.html">WalletDB</a></li><li><a href="Witness.html">Witness</a></li><li><a href="Worker.html">Worker</a></li><li><a href="Workers.html">Workers</a></li></ul><h3>Global</h3><ul><li><a href="global.html#hostname">hostname</a></li><li><a href="global.html#isMapped">isMapped</a></li><li><a href="global.html#mkdirp">mkdirp</a></li><li><a href="global.html#normalize">normalize</a></li><li><a href="global.html#parseHost">parseHost</a></li><li><a href="global.html#scrypt">scrypt</a></li><li><a href="global.html#toBuffer">toBuffer</a></li><li><a href="global.html#toString">toString</a></li><li><a href="global.html#version">version</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Jun 05 2016 20:46:56 GMT-0700 (PDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>