1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26const url = require('url'); 27 28// 29// Slight variation on test-http-client-race to test for another race 30// condition involving the parsers FreeList used internally by http.Client. 31// 32 33const body1_s = '1111111111111111'; 34const body2_s = '22222'; 35const body3_s = '3333333333333333333'; 36 37const server = http.createServer(function(req, res) { 38 const pathname = url.parse(req.url).pathname; 39 40 let body; 41 switch (pathname) { 42 case '/1': body = body1_s; break; 43 case '/2': body = body2_s; break; 44 default: body = body3_s; 45 } 46 47 res.writeHead(200, { 48 'Content-Type': 'text/plain', 49 'Content-Length': body.length 50 }); 51 res.end(body); 52}); 53server.listen(0); 54 55let body1 = ''; 56let body2 = ''; 57let body3 = ''; 58 59server.on('listening', function() { 60 // 61 // Client #1 is assigned Parser #1 62 // 63 const req1 = http.get({ port: this.address().port, path: '/1' }); 64 req1.on('response', function(res1) { 65 res1.setEncoding('utf8'); 66 67 res1.on('data', function(chunk) { 68 body1 += chunk; 69 }); 70 71 res1.on('end', function() { 72 // 73 // Delay execution a little to allow the 'close' event to be processed 74 // (required to trigger this bug!) 75 // 76 setTimeout(function() { 77 // 78 // The bug would introduce itself here: Client #2 would be allocated the 79 // parser that previously belonged to Client #1. But we're not finished 80 // with Client #1 yet! 81 // 82 // At this point, the bug would manifest itself and crash because the 83 // internal state of the parser was no longer valid for use by Client #1 84 // 85 const req2 = http.get({ port: server.address().port, path: '/2' }); 86 req2.on('response', function(res2) { 87 res2.setEncoding('utf8'); 88 res2.on('data', function(chunk) { body2 += chunk; }); 89 res2.on('end', function() { 90 91 // 92 // Just to be really sure we've covered all our bases, execute a 93 // request using client2. 94 // 95 const req3 = http.get({ port: server.address().port, path: '/3' }); 96 req3.on('response', function(res3) { 97 res3.setEncoding('utf8'); 98 res3.on('data', function(chunk) { body3 += chunk; }); 99 res3.on('end', function() { server.close(); }); 100 }); 101 }); 102 }); 103 }, 500); 104 }); 105 }); 106}); 107 108process.on('exit', function() { 109 assert.strictEqual(body1_s, body1); 110 assert.strictEqual(body2_s, body2); 111 assert.strictEqual(body3_s, body3); 112}); 113