http-parser.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*jshint node:true */
  2. var assert = require('assert');
  3. exports.HTTPParser = HTTPParser;
  4. function HTTPParser(type) {
  5. assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE);
  6. this.type = type;
  7. this.state = type + '_LINE';
  8. this.info = {
  9. headers: [],
  10. upgrade: false
  11. };
  12. this.trailers = [];
  13. this.line = '';
  14. this.isChunked = false;
  15. this.connection = '';
  16. this.headerSize = 0; // for preventing too big headers
  17. this.body_bytes = null;
  18. this.isUserCall = false;
  19. this.hadError = false;
  20. }
  21. HTTPParser.maxHeaderSize = 80 * 1024; // maxHeaderSize (in bytes) is configurable, but 80kb by default;
  22. HTTPParser.REQUEST = 'REQUEST';
  23. HTTPParser.RESPONSE = 'RESPONSE';
  24. var kOnHeaders = HTTPParser.kOnHeaders = 0;
  25. var kOnHeadersComplete = HTTPParser.kOnHeadersComplete = 1;
  26. var kOnBody = HTTPParser.kOnBody = 2;
  27. var kOnMessageComplete = HTTPParser.kOnMessageComplete = 3;
  28. // Some handler stubs, needed for compatibility
  29. HTTPParser.prototype[kOnHeaders] =
  30. HTTPParser.prototype[kOnHeadersComplete] =
  31. HTTPParser.prototype[kOnBody] =
  32. HTTPParser.prototype[kOnMessageComplete] = function () {};
  33. var compatMode0_12 = true;
  34. Object.defineProperty(HTTPParser, 'kOnExecute', {
  35. get: function () {
  36. // hack for backward compatibility
  37. compatMode0_12 = false;
  38. return 4;
  39. }
  40. });
  41. var methods = exports.methods = HTTPParser.methods = [
  42. 'DELETE',
  43. 'GET',
  44. 'HEAD',
  45. 'POST',
  46. 'PUT',
  47. 'CONNECT',
  48. 'OPTIONS',
  49. 'TRACE',
  50. 'COPY',
  51. 'LOCK',
  52. 'MKCOL',
  53. 'MOVE',
  54. 'PROPFIND',
  55. 'PROPPATCH',
  56. 'SEARCH',
  57. 'UNLOCK',
  58. 'BIND',
  59. 'REBIND',
  60. 'UNBIND',
  61. 'ACL',
  62. 'REPORT',
  63. 'MKACTIVITY',
  64. 'CHECKOUT',
  65. 'MERGE',
  66. 'M-SEARCH',
  67. 'NOTIFY',
  68. 'SUBSCRIBE',
  69. 'UNSUBSCRIBE',
  70. 'PATCH',
  71. 'PURGE',
  72. 'MKCALENDAR',
  73. 'LINK',
  74. 'UNLINK'
  75. ];
  76. var method_connect = methods.indexOf('CONNECT');
  77. HTTPParser.prototype.reinitialize = HTTPParser;
  78. HTTPParser.prototype.close =
  79. HTTPParser.prototype.pause =
  80. HTTPParser.prototype.resume =
  81. HTTPParser.prototype.free = function () {};
  82. HTTPParser.prototype._compatMode0_11 = false;
  83. HTTPParser.prototype.getAsyncId = function() { return 0; };
  84. var headerState = {
  85. REQUEST_LINE: true,
  86. RESPONSE_LINE: true,
  87. HEADER: true
  88. };
  89. HTTPParser.prototype.execute = function (chunk, start, length) {
  90. if (!(this instanceof HTTPParser)) {
  91. throw new TypeError('not a HTTPParser');
  92. }
  93. // backward compat to node < 0.11.4
  94. // Note: the start and length params were removed in newer version
  95. start = start || 0;
  96. length = typeof length === 'number' ? length : chunk.length;
  97. this.chunk = chunk;
  98. this.offset = start;
  99. var end = this.end = start + length;
  100. try {
  101. while (this.offset < end) {
  102. if (this[this.state]()) {
  103. break;
  104. }
  105. }
  106. } catch (err) {
  107. if (this.isUserCall) {
  108. throw err;
  109. }
  110. this.hadError = true;
  111. return err;
  112. }
  113. this.chunk = null;
  114. length = this.offset - start;
  115. if (headerState[this.state]) {
  116. this.headerSize += length;
  117. if (this.headerSize > HTTPParser.maxHeaderSize) {
  118. return new Error('max header size exceeded');
  119. }
  120. }
  121. return length;
  122. };
  123. var stateFinishAllowed = {
  124. REQUEST_LINE: true,
  125. RESPONSE_LINE: true,
  126. BODY_RAW: true
  127. };
  128. HTTPParser.prototype.finish = function () {
  129. if (this.hadError) {
  130. return;
  131. }
  132. if (!stateFinishAllowed[this.state]) {
  133. return new Error('invalid state for EOF');
  134. }
  135. if (this.state === 'BODY_RAW') {
  136. this.userCall()(this[kOnMessageComplete]());
  137. }
  138. };
  139. // These three methods are used for an internal speed optimization, and it also
  140. // works if theses are noops. Basically consume() asks us to read the bytes
  141. // ourselves, but if we don't do it we get them through execute().
  142. HTTPParser.prototype.consume =
  143. HTTPParser.prototype.unconsume =
  144. HTTPParser.prototype.getCurrentBuffer = function () {};
  145. //For correct error handling - see HTTPParser#execute
  146. //Usage: this.userCall()(userFunction('arg'));
  147. HTTPParser.prototype.userCall = function () {
  148. this.isUserCall = true;
  149. var self = this;
  150. return function (ret) {
  151. self.isUserCall = false;
  152. return ret;
  153. };
  154. };
  155. HTTPParser.prototype.nextRequest = function () {
  156. this.userCall()(this[kOnMessageComplete]());
  157. this.reinitialize(this.type);
  158. };
  159. HTTPParser.prototype.consumeLine = function () {
  160. var end = this.end,
  161. chunk = this.chunk;
  162. for (var i = this.offset; i < end; i++) {
  163. if (chunk[i] === 0x0a) { // \n
  164. var line = this.line + chunk.toString('ascii', this.offset, i);
  165. if (line.charAt(line.length - 1) === '\r') {
  166. line = line.substr(0, line.length - 1);
  167. }
  168. this.line = '';
  169. this.offset = i + 1;
  170. return line;
  171. }
  172. }
  173. //line split over multiple chunks
  174. this.line += chunk.toString('ascii', this.offset, this.end);
  175. this.offset = this.end;
  176. };
  177. var headerExp = /^([^: \t]+):[ \t]*((?:.*[^ \t])|)/;
  178. var headerContinueExp = /^[ \t]+(.*[^ \t])/;
  179. HTTPParser.prototype.parseHeader = function (line, headers) {
  180. if (line.indexOf('\r') !== -1) {
  181. throw parseErrorCode('HPE_LF_EXPECTED');
  182. }
  183. var match = headerExp.exec(line);
  184. var k = match && match[1];
  185. if (k) { // skip empty string (malformed header)
  186. headers.push(k);
  187. headers.push(match[2]);
  188. } else {
  189. var matchContinue = headerContinueExp.exec(line);
  190. if (matchContinue && headers.length) {
  191. if (headers[headers.length - 1]) {
  192. headers[headers.length - 1] += ' ';
  193. }
  194. headers[headers.length - 1] += matchContinue[1];
  195. }
  196. }
  197. };
  198. var requestExp = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
  199. HTTPParser.prototype.REQUEST_LINE = function () {
  200. var line = this.consumeLine();
  201. if (!line) {
  202. return;
  203. }
  204. var match = requestExp.exec(line);
  205. if (match === null) {
  206. throw parseErrorCode('HPE_INVALID_CONSTANT');
  207. }
  208. this.info.method = this._compatMode0_11 ? match[1] : methods.indexOf(match[1]);
  209. if (this.info.method === -1) {
  210. throw new Error('invalid request method');
  211. }
  212. this.info.url = match[2];
  213. this.info.versionMajor = +match[3];
  214. this.info.versionMinor = +match[4];
  215. this.body_bytes = 0;
  216. this.state = 'HEADER';
  217. };
  218. var responseExp = /^HTTP\/(\d)\.(\d) (\d{3}) ?(.*)$/;
  219. HTTPParser.prototype.RESPONSE_LINE = function () {
  220. var line = this.consumeLine();
  221. if (!line) {
  222. return;
  223. }
  224. var match = responseExp.exec(line);
  225. if (match === null) {
  226. throw parseErrorCode('HPE_INVALID_CONSTANT');
  227. }
  228. this.info.versionMajor = +match[1];
  229. this.info.versionMinor = +match[2];
  230. var statusCode = this.info.statusCode = +match[3];
  231. this.info.statusMessage = match[4];
  232. // Implied zero length.
  233. if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) {
  234. this.body_bytes = 0;
  235. }
  236. this.state = 'HEADER';
  237. };
  238. HTTPParser.prototype.shouldKeepAlive = function () {
  239. if (this.info.versionMajor > 0 && this.info.versionMinor > 0) {
  240. if (this.connection.indexOf('close') !== -1) {
  241. return false;
  242. }
  243. } else if (this.connection.indexOf('keep-alive') === -1) {
  244. return false;
  245. }
  246. if (this.body_bytes !== null || this.isChunked) { // || skipBody
  247. return true;
  248. }
  249. return false;
  250. };
  251. HTTPParser.prototype.HEADER = function () {
  252. var line = this.consumeLine();
  253. if (line === undefined) {
  254. return;
  255. }
  256. var info = this.info;
  257. if (line) {
  258. this.parseHeader(line, info.headers);
  259. } else {
  260. var headers = info.headers;
  261. var hasContentLength = false;
  262. var currentContentLengthValue;
  263. var hasUpgradeHeader = false;
  264. for (var i = 0; i < headers.length; i += 2) {
  265. switch (headers[i].toLowerCase()) {
  266. case 'transfer-encoding':
  267. this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
  268. break;
  269. case 'content-length':
  270. currentContentLengthValue = +headers[i + 1];
  271. if (hasContentLength) {
  272. // Fix duplicate Content-Length header with same values.
  273. // Throw error only if values are different.
  274. // Known issues:
  275. // https://github.com/request/request/issues/2091#issuecomment-328715113
  276. // https://github.com/nodejs/node/issues/6517#issuecomment-216263771
  277. if (currentContentLengthValue !== this.body_bytes) {
  278. throw parseErrorCode('HPE_UNEXPECTED_CONTENT_LENGTH');
  279. }
  280. } else {
  281. hasContentLength = true;
  282. this.body_bytes = currentContentLengthValue;
  283. }
  284. break;
  285. case 'connection':
  286. this.connection += headers[i + 1].toLowerCase();
  287. break;
  288. case 'upgrade':
  289. hasUpgradeHeader = true;
  290. break;
  291. }
  292. }
  293. // See https://github.com/creationix/http-parser-js/pull/53
  294. // if both isChunked and hasContentLength, content length wins
  295. // because it has been verified to match the body length already
  296. if (this.isChunked && hasContentLength) {
  297. this.isChunked = false;
  298. }
  299. // Logic from https://github.com/nodejs/http-parser/blob/921d5585515a153fa00e411cf144280c59b41f90/http_parser.c#L1727-L1737
  300. // "For responses, "Upgrade: foo" and "Connection: upgrade" are
  301. // mandatory only when it is a 101 Switching Protocols response,
  302. // otherwise it is purely informational, to announce support.
  303. if (hasUpgradeHeader && this.connection.indexOf('upgrade') != -1) {
  304. info.upgrade = this.type === HTTPParser.REQUEST || info.statusCode === 101;
  305. } else {
  306. info.upgrade = info.method === method_connect;
  307. }
  308. info.shouldKeepAlive = this.shouldKeepAlive();
  309. //problem which also exists in original node: we should know skipBody before calling onHeadersComplete
  310. var skipBody;
  311. if (compatMode0_12) {
  312. skipBody = this.userCall()(this[kOnHeadersComplete](info));
  313. } else {
  314. skipBody = this.userCall()(this[kOnHeadersComplete](info.versionMajor,
  315. info.versionMinor, info.headers, info.method, info.url, info.statusCode,
  316. info.statusMessage, info.upgrade, info.shouldKeepAlive));
  317. }
  318. if (skipBody === 2) {
  319. this.nextRequest();
  320. return true;
  321. } else if (this.isChunked && !skipBody) {
  322. this.state = 'BODY_CHUNKHEAD';
  323. } else if (skipBody || this.body_bytes === 0) {
  324. this.nextRequest();
  325. // For older versions of node (v6.x and older?), that return skipBody=1 or skipBody=true,
  326. // need this "return true;" if it's an upgrade request.
  327. return info.upgrade;
  328. } else if (this.body_bytes === null) {
  329. this.state = 'BODY_RAW';
  330. } else {
  331. this.state = 'BODY_SIZED';
  332. }
  333. }
  334. };
  335. HTTPParser.prototype.BODY_CHUNKHEAD = function () {
  336. var line = this.consumeLine();
  337. if (line === undefined) {
  338. return;
  339. }
  340. this.body_bytes = parseInt(line, 16);
  341. if (!this.body_bytes) {
  342. this.state = 'BODY_CHUNKTRAILERS';
  343. } else {
  344. this.state = 'BODY_CHUNK';
  345. }
  346. };
  347. HTTPParser.prototype.BODY_CHUNK = function () {
  348. var length = Math.min(this.end - this.offset, this.body_bytes);
  349. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  350. this.offset += length;
  351. this.body_bytes -= length;
  352. if (!this.body_bytes) {
  353. this.state = 'BODY_CHUNKEMPTYLINE';
  354. }
  355. };
  356. HTTPParser.prototype.BODY_CHUNKEMPTYLINE = function () {
  357. var line = this.consumeLine();
  358. if (line === undefined) {
  359. return;
  360. }
  361. assert.equal(line, '');
  362. this.state = 'BODY_CHUNKHEAD';
  363. };
  364. HTTPParser.prototype.BODY_CHUNKTRAILERS = function () {
  365. var line = this.consumeLine();
  366. if (line === undefined) {
  367. return;
  368. }
  369. if (line) {
  370. this.parseHeader(line, this.trailers);
  371. } else {
  372. if (this.trailers.length) {
  373. this.userCall()(this[kOnHeaders](this.trailers, ''));
  374. }
  375. this.nextRequest();
  376. }
  377. };
  378. HTTPParser.prototype.BODY_RAW = function () {
  379. var length = this.end - this.offset;
  380. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  381. this.offset = this.end;
  382. };
  383. HTTPParser.prototype.BODY_SIZED = function () {
  384. var length = Math.min(this.end - this.offset, this.body_bytes);
  385. this.userCall()(this[kOnBody](this.chunk, this.offset, length));
  386. this.offset += length;
  387. this.body_bytes -= length;
  388. if (!this.body_bytes) {
  389. this.nextRequest();
  390. }
  391. };
  392. // backward compat to node < 0.11.6
  393. ['Headers', 'HeadersComplete', 'Body', 'MessageComplete'].forEach(function (name) {
  394. var k = HTTPParser['kOn' + name];
  395. Object.defineProperty(HTTPParser.prototype, 'on' + name, {
  396. get: function () {
  397. return this[k];
  398. },
  399. set: function (to) {
  400. // hack for backward compatibility
  401. this._compatMode0_11 = true;
  402. method_connect = 'CONNECT';
  403. return (this[k] = to);
  404. }
  405. });
  406. });
  407. function parseErrorCode(code) {
  408. var err = new Error('Parse Error');
  409. err.code = code;
  410. return err;
  411. }