• 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  ObjectDefineProperty,
26} = primordials;
27
28const httpAgent = require('_http_agent');
29const { ClientRequest } = require('_http_client');
30const { methods } = require('_http_common');
31const { IncomingMessage } = require('_http_incoming');
32const {
33  validateHeaderName,
34  validateHeaderValue,
35  OutgoingMessage
36} = require('_http_outgoing');
37const {
38  _connectionListener,
39  STATUS_CODES,
40  Server,
41  ServerResponse
42} = require('_http_server');
43let maxHeaderSize;
44
45/**
46 * Returns a new instance of `http.Server`.
47 * @param {{
48 *   IncomingMessage?: IncomingMessage;
49 *   ServerResponse?: ServerResponse;
50 *   insecureHTTPParser?: boolean;
51 *   maxHeaderSize?: number;
52 *   }} [opts]
53 * @param {Function} [requestListener]
54 * @returns {Server}
55 */
56function createServer(opts, requestListener) {
57  return new Server(opts, requestListener);
58}
59
60/**
61 * @typedef {Object} HTTPRequestOptions
62 * @property {httpAgent.Agent | boolean} [agent]
63 * @property {string} [auth]
64 * @property {Function} [createConnection]
65 * @property {number} [defaultPort]
66 * @property {number} [family]
67 * @property {Object} [headers]
68 * @property {number} [hints]
69 * @property {string} [host]
70 * @property {string} [hostname]
71 * @property {boolean} [insecureHTTPParser]
72 * @property {string} [localAddress]
73 * @property {number} [localPort]
74 * @property {Function} [lookup]
75 * @property {number} [maxHeaderSize]
76 * @property {string} [method]
77 * @property {string} [path]
78 * @property {number} [port]
79 * @property {string} [protocol]
80 * @property {boolean} [setHost]
81 * @property {string} [socketPath]
82 * @property {number} [timeout]
83 * @property {AbortSignal} [signal]
84 */
85
86/**
87 * Makes an HTTP request.
88 * @param {string | URL} url
89 * @param {HTTPRequestOptions} [options]
90 * @param {Function} [cb]
91 * @returns {ClientRequest}
92 */
93function request(url, options, cb) {
94  return new ClientRequest(url, options, cb);
95}
96
97/**
98 * Makes a `GET` HTTP request.
99 * @param {string | URL} url
100 * @param {HTTPRequestOptions} [options]
101 * @param {Function} [cb]
102 * @returns {ClientRequest}
103 */
104function get(url, options, cb) {
105  const req = request(url, options, cb);
106  req.end();
107  return req;
108}
109
110module.exports = {
111  _connectionListener,
112  METHODS: methods.slice().sort(),
113  STATUS_CODES,
114  Agent: httpAgent.Agent,
115  ClientRequest,
116  IncomingMessage,
117  OutgoingMessage,
118  Server,
119  ServerResponse,
120  createServer,
121  validateHeaderName,
122  validateHeaderValue,
123  get,
124  request
125};
126
127ObjectDefineProperty(module.exports, 'maxHeaderSize', {
128  configurable: true,
129  enumerable: true,
130  get() {
131    if (maxHeaderSize === undefined) {
132      const { getOptionValue } = require('internal/options');
133      maxHeaderSize = getOptionValue('--max-http-header-size');
134    }
135
136    return maxHeaderSize;
137  }
138});
139
140ObjectDefineProperty(module.exports, 'globalAgent', {
141  configurable: true,
142  enumerable: true,
143  get() {
144    return httpAgent.globalAgent;
145  },
146  set(value) {
147    httpAgent.globalAgent = value;
148  }
149});
150