• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 assert = require('assert');
25const net = require('net');
26const http = require('http');
27
28// `wget` sends an HTTP/1.0 request with Connection: Keep-Alive
29//
30// Sending back a chunked response to an HTTP/1.0 client would be wrong,
31// so what has to happen in this case is that the connection is closed
32// by the server after the entity body if the Content-Length was not
33// sent.
34//
35// If the Content-Length was sent, we can probably safely honor the
36// keep-alive request, even though HTTP 1.0 doesn't say that the
37// connection can be kept open.  Presumably any client sending this
38// header knows that it is extending HTTP/1.0 and can handle the
39// response.  We don't test that here however, just that if the
40// content-length is not provided, that the connection is in fact
41// closed.
42
43const server = http.createServer((req, res) => {
44  res.writeHead(200, { 'Content-Type': 'text/plain' });
45  res.write('hello ');
46  res.write('world\n');
47  res.end();
48});
49server.listen(0);
50
51server.on('listening', common.mustCall(() => {
52  const c = net.createConnection(server.address().port);
53  let server_response = '';
54
55  c.setEncoding('utf8');
56
57  c.on('connect', () => {
58    c.write('GET / HTTP/1.0\r\n' +
59            'Connection: Keep-Alive\r\n\r\n');
60  });
61
62  c.on('data', (chunk) => {
63    console.log(chunk);
64    server_response += chunk;
65  });
66
67  c.on('end', common.mustCall(() => {
68    const m = server_response.split('\r\n\r\n');
69    assert.strictEqual(m[1], 'hello world\n');
70    console.log('got end');
71    c.end();
72  }));
73
74  c.on('close', common.mustCall(() => {
75    console.log('got close');
76    server.close();
77  }));
78}));
79