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';
23const common = require('../common');
24const Countdown = require('../common/countdown');
25
26const http = require('http');
27const net = require('net');
28
29const seeds = [ 3, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 ];
30const countdown = new Countdown(seeds.length, () => server.close());
31
32// Set up some timing issues where sockets can be destroyed
33// via either the req or res.
34const server = http.createServer(common.mustCall(function(req, res) {
35  switch (req.url) {
36    case '/1':
37      return setImmediate(function() {
38        req.socket.destroy();
39        server.emit('requestDone');
40      });
41
42    case '/2':
43      return process.nextTick(function() {
44        res.destroy();
45        server.emit('requestDone');
46      });
47
48    // In one case, actually send a response in 2 chunks
49    case '/3':
50      res.write('hello ');
51      return setImmediate(function() {
52        res.end('world!');
53        server.emit('requestDone');
54      });
55
56    default:
57      res.destroy();
58      server.emit('requestDone');
59  }
60}, seeds.length));
61
62
63// Make a bunch of requests pipelined on the same socket
64function generator(seeds) {
65  const port = server.address().port;
66  return seeds.map(function(r) {
67    return `GET /${r} HTTP/1.1\r\n` +
68           `Host: localhost:${port}\r\n` +
69           '\r\n' +
70           '\r\n';
71  }).join('');
72}
73
74
75server.listen(0, common.mustCall(function() {
76  const client = net.connect({ port: this.address().port });
77  server.on('requestDone', function() {
78    countdown.dec();
79  });
80
81  // Immediately write the pipelined requests.
82  // Some of these will not have a socket to destroy!
83  client.write(generator(seeds));
84}));
85