11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_cirequire('../common');
241cb0ef41Sopenharmony_ciconst http = require('http');
251cb0ef41Sopenharmony_ciconst net = require('net');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// Check that our HTTP server correctly handles HTTP/1.0 keep-alive requests.
281cb0ef41Sopenharmony_cicheck([{
291cb0ef41Sopenharmony_ci  name: 'keep-alive, no TE header',
301cb0ef41Sopenharmony_ci  requests: [{
311cb0ef41Sopenharmony_ci    expectClose: true,
321cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
331cb0ef41Sopenharmony_ci          'Connection: keep-alive\r\n' +
341cb0ef41Sopenharmony_ci          '\r\n'
351cb0ef41Sopenharmony_ci  }, {
361cb0ef41Sopenharmony_ci    expectClose: true,
371cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
381cb0ef41Sopenharmony_ci          'Connection: keep-alive\r\n' +
391cb0ef41Sopenharmony_ci          '\r\n'
401cb0ef41Sopenharmony_ci  }],
411cb0ef41Sopenharmony_ci  responses: [{
421cb0ef41Sopenharmony_ci    headers: { 'Connection': 'keep-alive' },
431cb0ef41Sopenharmony_ci    chunks: ['OK']
441cb0ef41Sopenharmony_ci  }, {
451cb0ef41Sopenharmony_ci    chunks: []
461cb0ef41Sopenharmony_ci  }]
471cb0ef41Sopenharmony_ci}, {
481cb0ef41Sopenharmony_ci  name: 'keep-alive, with TE: chunked',
491cb0ef41Sopenharmony_ci  requests: [{
501cb0ef41Sopenharmony_ci    expectClose: false,
511cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
521cb0ef41Sopenharmony_ci          'Connection: keep-alive\r\n' +
531cb0ef41Sopenharmony_ci          'TE: chunked\r\n' +
541cb0ef41Sopenharmony_ci          '\r\n'
551cb0ef41Sopenharmony_ci  }, {
561cb0ef41Sopenharmony_ci    expectClose: true,
571cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
581cb0ef41Sopenharmony_ci          '\r\n'
591cb0ef41Sopenharmony_ci  }],
601cb0ef41Sopenharmony_ci  responses: [{
611cb0ef41Sopenharmony_ci    headers: { 'Connection': 'keep-alive' },
621cb0ef41Sopenharmony_ci    chunks: ['OK']
631cb0ef41Sopenharmony_ci  }, {
641cb0ef41Sopenharmony_ci    chunks: []
651cb0ef41Sopenharmony_ci  }]
661cb0ef41Sopenharmony_ci}, {
671cb0ef41Sopenharmony_ci  name: 'keep-alive, with Transfer-Encoding: chunked',
681cb0ef41Sopenharmony_ci  requests: [{
691cb0ef41Sopenharmony_ci    expectClose: false,
701cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
711cb0ef41Sopenharmony_ci          'Connection: keep-alive\r\n' +
721cb0ef41Sopenharmony_ci          '\r\n'
731cb0ef41Sopenharmony_ci  }, {
741cb0ef41Sopenharmony_ci    expectClose: true,
751cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
761cb0ef41Sopenharmony_ci          '\r\n'
771cb0ef41Sopenharmony_ci  }],
781cb0ef41Sopenharmony_ci  responses: [{
791cb0ef41Sopenharmony_ci    headers: { 'Connection': 'keep-alive',
801cb0ef41Sopenharmony_ci               'Transfer-Encoding': 'chunked' },
811cb0ef41Sopenharmony_ci    chunks: ['OK']
821cb0ef41Sopenharmony_ci  }, {
831cb0ef41Sopenharmony_ci    chunks: []
841cb0ef41Sopenharmony_ci  }]
851cb0ef41Sopenharmony_ci}, {
861cb0ef41Sopenharmony_ci  name: 'keep-alive, with Content-Length',
871cb0ef41Sopenharmony_ci  requests: [{
881cb0ef41Sopenharmony_ci    expectClose: false,
891cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
901cb0ef41Sopenharmony_ci          'Connection: keep-alive\r\n' +
911cb0ef41Sopenharmony_ci          '\r\n'
921cb0ef41Sopenharmony_ci  }, {
931cb0ef41Sopenharmony_ci    expectClose: true,
941cb0ef41Sopenharmony_ci    data: 'POST / HTTP/1.0\r\n' +
951cb0ef41Sopenharmony_ci          '\r\n'
961cb0ef41Sopenharmony_ci  }],
971cb0ef41Sopenharmony_ci  responses: [{
981cb0ef41Sopenharmony_ci    headers: { 'Connection': 'keep-alive',
991cb0ef41Sopenharmony_ci               'Content-Length': '2' },
1001cb0ef41Sopenharmony_ci    chunks: ['OK']
1011cb0ef41Sopenharmony_ci  }, {
1021cb0ef41Sopenharmony_ci    chunks: []
1031cb0ef41Sopenharmony_ci  }]
1041cb0ef41Sopenharmony_ci}]);
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_cifunction check(tests) {
1071cb0ef41Sopenharmony_ci  const test = tests[0];
1081cb0ef41Sopenharmony_ci  let server;
1091cb0ef41Sopenharmony_ci  if (test) {
1101cb0ef41Sopenharmony_ci    server = http.createServer(serverHandler).listen(0, '127.0.0.1', client);
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci  let current = 0;
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  function next() {
1151cb0ef41Sopenharmony_ci    check(tests.slice(1));
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci  function serverHandler(req, res) {
1191cb0ef41Sopenharmony_ci    if (current + 1 === test.responses.length) this.close();
1201cb0ef41Sopenharmony_ci    const ctx = test.responses[current];
1211cb0ef41Sopenharmony_ci    console.error('<  SERVER SENDING RESPONSE', ctx);
1221cb0ef41Sopenharmony_ci    res.writeHead(200, ctx.headers);
1231cb0ef41Sopenharmony_ci    ctx.chunks.slice(0, -1).forEach(function(chunk) { res.write(chunk); });
1241cb0ef41Sopenharmony_ci    res.end(ctx.chunks[ctx.chunks.length - 1]);
1251cb0ef41Sopenharmony_ci  }
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  function client() {
1281cb0ef41Sopenharmony_ci    if (current === test.requests.length) return next();
1291cb0ef41Sopenharmony_ci    const port = server.address().port;
1301cb0ef41Sopenharmony_ci    const conn = net.createConnection(port, '127.0.0.1', connected);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci    function connected() {
1331cb0ef41Sopenharmony_ci      const ctx = test.requests[current];
1341cb0ef41Sopenharmony_ci      console.error(' > CLIENT SENDING REQUEST', ctx);
1351cb0ef41Sopenharmony_ci      conn.setEncoding('utf8');
1361cb0ef41Sopenharmony_ci      conn.write(ctx.data);
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci      function onclose() {
1391cb0ef41Sopenharmony_ci        console.error(' > CLIENT CLOSE');
1401cb0ef41Sopenharmony_ci        if (!ctx.expectClose) throw new Error('unexpected close');
1411cb0ef41Sopenharmony_ci        client();
1421cb0ef41Sopenharmony_ci      }
1431cb0ef41Sopenharmony_ci      conn.on('close', onclose);
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci      function ondata(s) {
1461cb0ef41Sopenharmony_ci        console.error(' > CLIENT ONDATA %j %j', s.length, s.toString());
1471cb0ef41Sopenharmony_ci        current++;
1481cb0ef41Sopenharmony_ci        if (ctx.expectClose) return;
1491cb0ef41Sopenharmony_ci        conn.removeListener('close', onclose);
1501cb0ef41Sopenharmony_ci        conn.removeListener('data', ondata);
1511cb0ef41Sopenharmony_ci        connected();
1521cb0ef41Sopenharmony_ci      }
1531cb0ef41Sopenharmony_ci      conn.on('data', ondata);
1541cb0ef41Sopenharmony_ci    }
1551cb0ef41Sopenharmony_ci  }
1561cb0ef41Sopenharmony_ci}
157