• 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';
23
24const {
25  ArrayPrototypeSlice,
26  ArrayPrototypeSort,
27  ObjectDefineProperty,
28} = primordials;
29
30const { validateInteger } = require('internal/validators');
31const httpAgent = require('_http_agent');
32const { ClientRequest } = require('_http_client');
33const { methods, parsers } = require('_http_common');
34const { IncomingMessage } = require('_http_incoming');
35const {
36  validateHeaderName,
37  validateHeaderValue,
38  OutgoingMessage,
39} = require('_http_outgoing');
40const {
41  _connectionListener,
42  STATUS_CODES,
43  Server,
44  ServerResponse,
45} = require('_http_server');
46let maxHeaderSize;
47
48/**
49 * Returns a new instance of `http.Server`.
50 * @param {{
51 *   IncomingMessage?: IncomingMessage;
52 *   ServerResponse?: ServerResponse;
53 *   insecureHTTPParser?: boolean;
54 *   maxHeaderSize?: number;
55 *   joinDuplicateHeaders?: boolean;
56 *   highWaterMark?: number;
57 *   rejectNonStandardBodyWrites?: boolean;
58 *   }} [opts]
59 * @param {Function} [requestListener]
60 * @returns {Server}
61 */
62function createServer(opts, requestListener) {
63  return new Server(opts, requestListener);
64}
65
66/**
67 * @typedef {object} HTTPRequestOptions
68 * @property {httpAgent.Agent | boolean} [agent] Controls Agent behavior.
69 * @property {string} [auth] Basic authentication ('user:password') to compute an Authorization header.
70 * @property {Function} [createConnection] Produces a socket/stream to use when the agent option is not used.
71 * @property {number} [defaultPort] Default port for the protocol.
72 * @property {number} [family] IP address family to use when resolving host or hostname.
73 * @property {object} [headers] An object containing request headers.
74 * @property {number} [hints] Optional dns.lookup() hints.
75 * @property {string} [host] A domain name or IP address of the server to issue the request to.
76 * @property {string} [hostname] Alias for host.
77 * @property {boolean} [insecureHTTPParser] Use an insecure HTTP parser that accepts invalid HTTP headers when true.
78 * @property {string} [localAddress] Local interface to bind for network connections.
79 * @property {number} [localPort] Local port to connect from.
80 * @property {Function} [lookup] Custom lookup function. Default: dns.lookup().
81 * @property {number} [maxHeaderSize] Overrides the --max-http-header-size value for responses received from the server.
82 * @property {string} [method] A string specifying the HTTP request method.
83 * @property {string} [path] Request path.
84 * @property {number} [port] Port of remote server.
85 * @property {string} [protocol] Protocol to use.
86 * @property {boolean} [setHost] Specifies whether or not to automatically add the Host header.
87 * @property {AbortSignal} [signal] An AbortSignal that may be used to abort an ongoing request.
88 * @property {string} [socketPath] Unix domain socket.
89 * @property {number} [timeout] A number specifying the socket timeout in milliseconds.
90 */
91
92/**
93 * Makes an HTTP request.
94 * @param {string | URL} url
95 * @param {HTTPRequestOptions} [options]
96 * @param {Function} [cb]
97 * @returns {ClientRequest}
98 */
99function request(url, options, cb) {
100  return new ClientRequest(url, options, cb);
101}
102
103/**
104 * Makes a `GET` HTTP request.
105 * @param {string | URL} url
106 * @param {HTTPRequestOptions} [options]
107 * @param {Function} [cb]
108 * @returns {ClientRequest}
109 */
110function get(url, options, cb) {
111  const req = request(url, options, cb);
112  req.end();
113  return req;
114}
115
116module.exports = {
117  _connectionListener,
118  METHODS: ArrayPrototypeSort(ArrayPrototypeSlice(methods)),
119  STATUS_CODES,
120  Agent: httpAgent.Agent,
121  ClientRequest,
122  IncomingMessage,
123  OutgoingMessage,
124  Server,
125  ServerResponse,
126  createServer,
127  validateHeaderName,
128  validateHeaderValue,
129  get,
130  request,
131  setMaxIdleHTTPParsers(max) {
132    validateInteger(max, 'max', 1);
133    parsers.max = max;
134  },
135};
136
137ObjectDefineProperty(module.exports, 'maxHeaderSize', {
138  __proto__: null,
139  configurable: true,
140  enumerable: true,
141  get() {
142    if (maxHeaderSize === undefined) {
143      const { getOptionValue } = require('internal/options');
144      maxHeaderSize = getOptionValue('--max-http-header-size');
145    }
146
147    return maxHeaderSize;
148  },
149});
150
151ObjectDefineProperty(module.exports, 'globalAgent', {
152  __proto__: null,
153  configurable: true,
154  enumerable: true,
155  get() {
156    return httpAgent.globalAgent;
157  },
158  set(value) {
159    httpAgent.globalAgent = value;
160  },
161});
162