http-parser.js 13 KB

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