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_ci// Verify that the HTTP server implementation handles multiple instances
241cb0ef41Sopenharmony_ci// of the same header as per RFC2616: joining the handful of fields by ', '
251cb0ef41Sopenharmony_ci// that support it, and dropping duplicates for other fields.
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cirequire('../common');
281cb0ef41Sopenharmony_ciconst assert = require('assert');
291cb0ef41Sopenharmony_ciconst http = require('http');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst multipleAllowed = [
321cb0ef41Sopenharmony_ci  'Accept',
331cb0ef41Sopenharmony_ci  'Accept-Charset',
341cb0ef41Sopenharmony_ci  'Accept-Encoding',
351cb0ef41Sopenharmony_ci  'Accept-Language',
361cb0ef41Sopenharmony_ci  'Connection',
371cb0ef41Sopenharmony_ci  'Cookie',
381cb0ef41Sopenharmony_ci  'DAV', // GH-2750
391cb0ef41Sopenharmony_ci  'Pragma', // GH-715
401cb0ef41Sopenharmony_ci  'Link', // GH-1187
411cb0ef41Sopenharmony_ci  'WWW-Authenticate', // GH-1083
421cb0ef41Sopenharmony_ci  'Proxy-Authenticate', // GH-4052
431cb0ef41Sopenharmony_ci  'Sec-Websocket-Extensions', // GH-2764
441cb0ef41Sopenharmony_ci  'Sec-Websocket-Protocol', // GH-2764
451cb0ef41Sopenharmony_ci  'Via', // GH-6660
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  // not a special case, just making sure it's parsed correctly
481cb0ef41Sopenharmony_ci  'X-Forwarded-For',
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  // Make sure that unspecified headers is treated as multiple
511cb0ef41Sopenharmony_ci  'Some-Random-Header',
521cb0ef41Sopenharmony_ci  'X-Some-Random-Header',
531cb0ef41Sopenharmony_ci];
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciconst multipleForbidden = [
561cb0ef41Sopenharmony_ci  'Content-Type',
571cb0ef41Sopenharmony_ci  'User-Agent',
581cb0ef41Sopenharmony_ci  'Referer',
591cb0ef41Sopenharmony_ci  'Host',
601cb0ef41Sopenharmony_ci  'Authorization',
611cb0ef41Sopenharmony_ci  'Proxy-Authorization',
621cb0ef41Sopenharmony_ci  'If-Modified-Since',
631cb0ef41Sopenharmony_ci  'If-Unmodified-Since',
641cb0ef41Sopenharmony_ci  'From',
651cb0ef41Sopenharmony_ci  'Location',
661cb0ef41Sopenharmony_ci  'Max-Forwards',
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  // Special case, tested differently
691cb0ef41Sopenharmony_ci  // 'Content-Length',
701cb0ef41Sopenharmony_ci];
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) {
731cb0ef41Sopenharmony_ci  multipleForbidden.forEach(function(header) {
741cb0ef41Sopenharmony_ci    assert.strictEqual(req.headers[header.toLowerCase()], 'foo',
751cb0ef41Sopenharmony_ci                       `header parsed incorrectly: ${header}`);
761cb0ef41Sopenharmony_ci  });
771cb0ef41Sopenharmony_ci  multipleAllowed.forEach(function(header) {
781cb0ef41Sopenharmony_ci    const sep = (header.toLowerCase() === 'cookie' ? '; ' : ', ');
791cb0ef41Sopenharmony_ci    assert.strictEqual(req.headers[header.toLowerCase()], `foo${sep}bar`,
801cb0ef41Sopenharmony_ci                       `header parsed incorrectly: ${header}`);
811cb0ef41Sopenharmony_ci  });
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  res.writeHead(200, { 'Content-Type': 'text/plain' });
841cb0ef41Sopenharmony_ci  res.end('EOF');
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  server.close();
871cb0ef41Sopenharmony_ci});
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_cifunction makeHeader(value) {
901cb0ef41Sopenharmony_ci  return function(header) {
911cb0ef41Sopenharmony_ci    return [header, value];
921cb0ef41Sopenharmony_ci  };
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciconst headers = []
961cb0ef41Sopenharmony_ci  .concat(multipleAllowed.map(makeHeader('foo')))
971cb0ef41Sopenharmony_ci  .concat(multipleForbidden.map(makeHeader('foo')))
981cb0ef41Sopenharmony_ci  .concat(multipleAllowed.map(makeHeader('bar')))
991cb0ef41Sopenharmony_ci  .concat(multipleForbidden.map(makeHeader('bar')));
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciserver.listen(0, function() {
1021cb0ef41Sopenharmony_ci  http.get({
1031cb0ef41Sopenharmony_ci    host: 'localhost',
1041cb0ef41Sopenharmony_ci    port: this.address().port,
1051cb0ef41Sopenharmony_ci    path: '/',
1061cb0ef41Sopenharmony_ci    headers: headers,
1071cb0ef41Sopenharmony_ci  });
1081cb0ef41Sopenharmony_ci});
109