Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reverted format
  • Loading branch information
marcosmol204 committed Dec 2, 2025
commit fd0e9d41fa52071f9dcaf0ee56c3e3cb7a01766c
152 changes: 84 additions & 68 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@
* MIT Licensed
*/

"use strict";
'use strict';

/**
* Module dependencies.
* @private
*/

var accepts = require("accepts");
var isIP = require("node:net").isIP;
var typeis = require("type-is");
var http = require("node:http");
var fresh = require("fresh");
var parseRange = require("range-parser");
var parse = require("parseurl");
var proxyaddr = require("proxy-addr");
var accepts = require('accepts');
var isIP = require('node:net').isIP;
var typeis = require('type-is');
var http = require('node:http');
var fresh = require('fresh');
var parseRange = require('range-parser');
var parse = require('parseurl');
var proxyaddr = require('proxy-addr');

/**
* Request prototype.
* @public
*/

var req = Object.create(http.IncomingMessage.prototype);
var req = Object.create(http.IncomingMessage.prototype)

/**
* Module exports.
* @public
*/

module.exports = req;
module.exports = req

/**
* Return request header.
Expand All @@ -60,34 +60,39 @@ module.exports = req;
* @public
*/

req.get = req.header = function header(name) {
req.get =
req.header = function header(name) {
if (!name) {
throw new TypeError("name argument is required to req.get");
throw new TypeError('name argument is required to req.get');
}

if (typeof name !== "string") {
throw new TypeError("name must be a string to req.get");
if (typeof name !== 'string') {
throw new TypeError('name must be a string to req.get');
}

var lc = name.toLowerCase();

switch (lc) {
case "referer":
case "referrer":
return this.headers.referrer || this.headers.referer;
case 'referer':
case 'referrer':
return this.headers.referrer
|| this.headers.referer;
default:
return this.headers[lc];
}
};

/**
* To do: update docs.
*
* Check if the given `type(s)` is acceptable, returning
* the best match when true, otherwise `false`, in which
* the best match when true, otherwise `undefined`, in which
* case you should respond with 406 "Not Acceptable".
*
* The `type` value may be a single MIME type string
* such as "application/json", an extension name
* such as "json", an argument list such as `"json", "html", "text/plain"`,
* such as "json", a comma-delimited list such as "json, html, text/plain",
* an argument list such as `"json", "html", "text/plain"`,
* or an array `["json", "html", "text/plain"]`. When a list
* or array is given, the _best_ match, if any is returned.
*
Expand Down Expand Up @@ -115,14 +120,15 @@ req.get = req.header = function header(name) {
* // Accept: text/*;q=.5, application/json
* req.accepts(['html', 'json']);
* req.accepts('html', 'json');
* req.accepts('html, json');
* // => "json"
*
* @param {String|Array} type(s)
* @return {String|Array|Boolean}
* @public
*/

req.accepts = function () {
req.accepts = function(){
var accept = accepts(this);
return accept.types.apply(accept, arguments);
};
Expand All @@ -135,7 +141,7 @@ req.accepts = function () {
* @public
*/

req.acceptsEncodings = function () {
req.acceptsEncodings = function(){
var accept = accepts(this);
return accept.encodings.apply(accept, arguments);
};
Expand All @@ -149,7 +155,7 @@ req.acceptsEncodings = function () {
* @public
*/

req.acceptsCharsets = function () {
req.acceptsCharsets = function(){
var accept = accepts(this);
return accept.charsets.apply(accept, arguments);
};
Expand All @@ -163,7 +169,7 @@ req.acceptsCharsets = function () {
* @public
*/

req.acceptsLanguages = function (...languages) {
req.acceptsLanguages = function(...languages) {
return accepts(this).languages(...languages);
};

Expand Down Expand Up @@ -193,7 +199,7 @@ req.acceptsLanguages = function (...languages) {
*/

req.range = function range(size, options) {
var range = this.get("Range");
var range = this.get('Range');
if (!range) return;
return parseRange(size, range, options);
};
Expand All @@ -208,8 +214,8 @@ req.range = function range(size, options) {
* @api public
*/

defineGetter(req, "query", function query() {
var queryparse = this.app.get("query parser fn");
defineGetter(req, 'query', function query(){
var queryparse = this.app.get('query parser fn');

if (!queryparse) {
// parsing is disabled
Expand Down Expand Up @@ -275,20 +281,24 @@ req.is = function is(types) {
* @public
*/

defineGetter(req, "protocol", function protocol() {
var proto = this.socket.encrypted ? "https" : "http";
var trust = this.app.get("trust proxy fn");
defineGetter(req, 'protocol', function protocol(){
var proto = this.socket.encrypted
? 'https'
: 'http';
var trust = this.app.get('trust proxy fn');

if (!trust(this.socket.remoteAddress, 0)) {
return proto;
}

// Note: X-Forwarded-Proto is normally only ever a
// single value, but this is to be safe.
var header = this.get("X-Forwarded-Proto") || proto;
var index = header.indexOf(",");
var header = this.get('X-Forwarded-Proto') || proto
var index = header.indexOf(',')

return index !== -1 ? header.substring(0, index).trim() : header.trim();
return index !== -1
? header.substring(0, index).trim()
: header.trim()
});

/**
Expand All @@ -300,8 +310,8 @@ defineGetter(req, "protocol", function protocol() {
* @public
*/

defineGetter(req, "secure", function secure() {
return this.protocol === "https";
defineGetter(req, 'secure', function secure(){
return this.protocol === 'https';
});

/**
Expand All @@ -314,8 +324,8 @@ defineGetter(req, "secure", function secure() {
* @public
*/

defineGetter(req, "ip", function ip() {
var trust = this.app.get("trust proxy fn");
defineGetter(req, 'ip', function ip(){
var trust = this.app.get('trust proxy fn');
return proxyaddr(this, trust);
});

Expand All @@ -331,15 +341,15 @@ defineGetter(req, "ip", function ip() {
* @public
*/

defineGetter(req, "ips", function ips() {
var trust = this.app.get("trust proxy fn");
defineGetter(req, 'ips', function ips() {
var trust = this.app.get('trust proxy fn');
var addrs = proxyaddr.all(this, trust);

// reverse the order (to farthest -> closest)
// and remove socket address
addrs.reverse().pop();
addrs.reverse().pop()

return addrs;
return addrs
});

/**
Expand All @@ -357,13 +367,15 @@ defineGetter(req, "ips", function ips() {
* @public
*/

defineGetter(req, "subdomains", function subdomains() {
defineGetter(req, 'subdomains', function subdomains() {
var hostname = this.hostname;

if (!hostname) return [];

var offset = this.app.get("subdomain offset");
var subdomains = !isIP(hostname) ? hostname.split(".").reverse() : [hostname];
var offset = this.app.get('subdomain offset');
var subdomains = !isIP(hostname)
? hostname.split('.').reverse()
: [hostname];

return subdomains.slice(offset);
});
Expand All @@ -375,7 +387,7 @@ defineGetter(req, "subdomains", function subdomains() {
* @public
*/

defineGetter(req, "path", function path() {
defineGetter(req, 'path', function path() {
return parse(this).pathname;
});

Expand All @@ -390,16 +402,16 @@ defineGetter(req, "path", function path() {
* @public
*/

defineGetter(req, "host", function host() {
var trust = this.app.get("trust proxy fn");
var val = this.get("X-Forwarded-Host");
defineGetter(req, 'host', function host(){
var trust = this.app.get('trust proxy fn');
var val = this.get('X-Forwarded-Host');

if (!val || !trust(this.socket.remoteAddress, 0)) {
val = this.get("Host");
} else if (val.indexOf(",") !== -1) {
val = this.get('Host');
} else if (val.indexOf(',') !== -1) {
// Note: X-Forwarded-Host is normally only ever a
// single value, but this is to be safe.
val = val.substring(0, val.indexOf(",")).trimRight();
val = val.substring(0, val.indexOf(',')).trimRight()
}

return val || undefined;
Expand All @@ -416,16 +428,20 @@ defineGetter(req, "host", function host() {
* @api public
*/

defineGetter(req, "hostname", function hostname() {
defineGetter(req, 'hostname', function hostname(){
var host = this.host;

if (!host) return;

// IPv6 literal support
var offset = host[0] === "[" ? host.indexOf("]") + 1 : 0;
var index = host.indexOf(":", offset);

return index !== -1 ? host.substring(0, index) : host;
var offset = host[0] === '['
? host.indexOf(']') + 1
: 0;
var index = host.indexOf(':', offset);

return index !== -1
? host.substring(0, index)
: host;
});

/**
Expand All @@ -437,20 +453,20 @@ defineGetter(req, "hostname", function hostname() {
* @public
*/

defineGetter(req, "fresh", function () {
defineGetter(req, 'fresh', function(){
var method = this.method;
var res = this.res;
var status = res.statusCode;
var res = this.res
var status = res.statusCode

// GET or HEAD for weak freshness validation only
if ("GET" !== method && "HEAD" !== method) return false;
if ('GET' !== method && 'HEAD' !== method) return false;

// 2xx or 304 as per rfc2616 14.26
if ((status >= 200 && status < 300) || 304 === status) {
return fresh(this.headers, {
etag: res.get("ETag"),
"last-modified": res.get("Last-Modified"),
});
'etag': res.get('ETag'),
'last-modified': res.get('Last-Modified')
})
}

return false;
Expand All @@ -465,7 +481,7 @@ defineGetter(req, "fresh", function () {
* @public
*/

defineGetter(req, "stale", function stale() {
defineGetter(req, 'stale', function stale(){
return !this.fresh;
});

Expand All @@ -476,9 +492,9 @@ defineGetter(req, "stale", function stale() {
* @public
*/

defineGetter(req, "xhr", function xhr() {
var val = this.get("X-Requested-With") || "";
return val.toLowerCase() === "xmlhttprequest";
defineGetter(req, 'xhr', function xhr(){
var val = this.get('X-Requested-With') || '';
return val.toLowerCase() === 'xmlhttprequest';
});

/**
Expand All @@ -493,6 +509,6 @@ function defineGetter(obj, name, getter) {
Object.defineProperty(obj, name, {
configurable: true,
enumerable: true,
get: getter,
get: getter
});
}