File: test-http-parser-bad-ref.js

package info (click to toggle)
nodejs 0.10.29~dfsg-1~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 32,756 kB
  • sloc: cpp: 330,356; ansic: 47,440; python: 12,756; sh: 1,365; makefile: 665; lisp: 222; ruby: 76; xml: 16; awk: 10
file content (81 lines) | stat: -rw-r--r-- 1,758 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Run this program with valgrind or efence with --expose_gc to expose the
// problem.

// Flags: --expose_gc

var common = require('../common');
var assert = require('assert');
var HTTPParser = process.binding('http_parser').HTTPParser;

var headersComplete = 0;
var messagesComplete = 0;

function flushPool() {
  new Buffer(Buffer.poolSize - 1);
  gc();
}

function demoBug(part1, part2) {
  flushPool();

  var parser = new HTTPParser('REQUEST');

  parser.headers = [];
  parser.url = '';

  parser.onHeaders = function(headers, url) {
    parser.headers = parser.headers.concat(headers);
    parser.url += url;
  };

  parser.onHeadersComplete = function(info) {
    headersComplete++;
    console.log('url', info.url);
  };

  parser.onBody = function(b, start, len) { };

  parser.onMessageComplete = function() {
    messagesComplete++;
  };


  // We use a function to eliminate references to the Buffer b
  // We want b to be GCed. The parser will hold a bad reference to it.
  (function() {
    var b = Buffer(part1);
    flushPool();

    console.log('parse the first part of the message');
    parser.execute(b, 0, b.length);
  })();

  flushPool();

  (function() {
    var b = Buffer(part2);

    console.log('parse the second part of the message');
    parser.execute(b, 0, b.length);
    parser.finish();
  })();

  flushPool();
}


demoBug('POST /1', '/22 HTTP/1.1\r\n' +
        'Content-Type: text/plain\r\n' +
        'Content-Length: 4\r\n\r\n' +
        'pong');

demoBug('POST /1/22 HTTP/1.1\r\n' +
        'Content-Type: tex', 't/plain\r\n' +
        'Content-Length: 4\r\n\r\n' +
        'pong');

process.on('exit', function() {
  assert.equal(2, headersComplete);
  assert.equal(2, messagesComplete);
  console.log('done!');
});