2022-02-24 06:00:35 +00:00
|
|
|
import {Container, Injectable, ScopedContainer} from '../../di'
|
|
|
|
import {HTTPStatus, infer, Pipeline, Safe, UniversalPath} from '../../util'
|
2021-06-03 03:36:25 +00:00
|
|
|
import {IncomingMessage, ServerResponse} from 'http'
|
|
|
|
import {HTTPCookieJar} from '../kernel/HTTPCookieJar'
|
|
|
|
import {TLSSocket} from 'tls'
|
|
|
|
import * as url from 'url'
|
|
|
|
import {Response} from './Response'
|
|
|
|
import * as Negotiator from 'negotiator'
|
2022-02-24 06:00:35 +00:00
|
|
|
import {HTTPError} from '../HTTPError'
|
2022-03-30 20:35:09 +00:00
|
|
|
import {ActivatedRoute} from '../routing/ActivatedRoute'
|
2021-03-07 02:58:48 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Enumeration of different HTTP verbs.
|
|
|
|
* @todo add others?
|
|
|
|
*/
|
2021-03-07 02:58:48 +00:00
|
|
|
export type HTTPMethod = 'post' | 'get' | 'patch' | 'put' | 'delete' | 'unknown';
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the given item is a valid HTTP verb.
|
|
|
|
* @param what
|
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
export function isHTTPMethod(what: unknown): what is HTTPMethod {
|
|
|
|
return ['post', 'get', 'patch', 'put', 'delete'].includes(String(what))
|
2021-03-07 02:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Interface that describes the HTTP protocol version.
|
|
|
|
*/
|
2021-03-07 02:58:48 +00:00
|
|
|
export interface HTTPProtocol {
|
|
|
|
string: string,
|
|
|
|
major: number,
|
|
|
|
minor: number,
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Interface that describes the origin IP address of a request.
|
|
|
|
*/
|
2021-03-07 02:58:48 +00:00
|
|
|
export interface HTTPSourceAddress {
|
|
|
|
address: string;
|
|
|
|
family: 'IPv4' | 'IPv6';
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
2021-05-22 15:44:52 +00:00
|
|
|
/**
|
|
|
|
* Interface describing a container that holds user input data.
|
|
|
|
*/
|
|
|
|
export interface DataContainer {
|
|
|
|
input(key?: string): any
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* A class that represents an HTTP request from a client.
|
|
|
|
*/
|
2021-03-07 02:58:48 +00:00
|
|
|
@Injectable()
|
2021-05-22 15:44:52 +00:00
|
|
|
export class Request extends ScopedContainer implements DataContainer {
|
2021-03-07 02:58:48 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** The cookie manager for the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly cookies: HTTPCookieJar;
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** The URL suffix of the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly url: string;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The fully-qualified URL of the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly fullUrl: string;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The HTTP verb of the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly method: HTTPMethod;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** True if the request was made via TLS. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly secure: boolean;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The request HTTP protocol version. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly protocol: HTTPProtocol;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The URL path, stripped of query params. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly path: string;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The raw parsed query data from the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly rawQueryData: {[key: string]: string | string[] | undefined};
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The inferred query data. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly query: {[key: string]: any};
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** True if the request was made via XMLHttpRequest. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly isXHR: boolean;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The origin IP address of the request. */
|
2021-03-07 02:58:48 +00:00
|
|
|
public readonly address: HTTPSourceAddress;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The associated response. */
|
2021-03-07 15:58:21 +00:00
|
|
|
public readonly response: Response;
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The media types accepted by the client. */
|
2021-03-08 16:07:10 +00:00
|
|
|
public readonly mediaTypes: string[];
|
2021-03-07 02:58:48 +00:00
|
|
|
|
2021-03-31 02:15:39 +00:00
|
|
|
/** Input parsed from the request */
|
|
|
|
public readonly parsedInput: {[key: string]: any} = {}
|
|
|
|
|
|
|
|
/** Files parsed from the request. */
|
|
|
|
public readonly uploadedFiles: {[key: string]: UniversalPath} = {}
|
|
|
|
|
2021-04-12 16:43:06 +00:00
|
|
|
/** If true, the response lifecycle will not time out and send errors. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public bypassTimeout = false
|
2021-04-12 16:43:06 +00:00
|
|
|
|
2021-03-07 02:58:48 +00:00
|
|
|
constructor(
|
2021-03-25 13:50:13 +00:00
|
|
|
/** The native Node.js request. */
|
2021-03-07 15:58:21 +00:00
|
|
|
protected clientRequest: IncomingMessage,
|
2021-03-25 13:50:13 +00:00
|
|
|
|
|
|
|
/** The native Node.js response. */
|
2021-03-07 15:58:21 +00:00
|
|
|
protected serverResponse: ServerResponse,
|
2021-03-07 02:58:48 +00:00
|
|
|
) {
|
|
|
|
super(Container.getContainer())
|
2021-10-18 17:48:16 +00:00
|
|
|
this.registerSingletonInstance(Request, this)
|
2021-03-07 02:58:48 +00:00
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
this.secure = Boolean((clientRequest.connection as TLSSocket).encrypted)
|
2021-03-07 02:58:48 +00:00
|
|
|
|
|
|
|
this.cookies = new HTTPCookieJar(this)
|
|
|
|
this.url = String(clientRequest.url)
|
|
|
|
this.fullUrl = (this.secure ? 'https' : 'http') + `://${this.getHeader('host')}${this.url}`
|
|
|
|
|
|
|
|
const method = clientRequest.method?.toLowerCase()
|
|
|
|
this.method = isHTTPMethod(method) ? method : 'unknown'
|
|
|
|
|
|
|
|
this.protocol = {
|
|
|
|
string: clientRequest.httpVersion,
|
|
|
|
major: clientRequest.httpVersionMajor,
|
|
|
|
minor: clientRequest.httpVersionMinor,
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = url.parse(this.url, true)
|
|
|
|
|
|
|
|
this.path = parts.pathname ?? '/'
|
|
|
|
this.rawQueryData = parts.query
|
|
|
|
|
|
|
|
const query: {[key: string]: any} = {}
|
|
|
|
for ( const key in this.rawQueryData ) {
|
2021-06-03 03:36:25 +00:00
|
|
|
if ( !Object.prototype.hasOwnProperty.call(this.rawQueryData, key) ) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:58:48 +00:00
|
|
|
const value = this.rawQueryData[key]
|
|
|
|
|
|
|
|
if ( Array.isArray(value) ) {
|
|
|
|
query[key] = value.map(x => infer(x))
|
|
|
|
} else if ( value ) {
|
|
|
|
query[key] = infer(value)
|
|
|
|
} else {
|
|
|
|
query[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.query = query
|
|
|
|
this.isXHR = String(this.clientRequest.headers['x-requested-with']).toLowerCase() === 'xmlhttprequest'
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
const {address = '0.0.0.0', family = 'IPv4', port = 0} = this.clientRequest.connection.address() as any
|
2021-03-07 02:58:48 +00:00
|
|
|
this.address = {
|
|
|
|
address,
|
|
|
|
family,
|
2021-06-03 03:36:25 +00:00
|
|
|
port,
|
2021-03-07 02:58:48 +00:00
|
|
|
}
|
2021-03-07 15:58:21 +00:00
|
|
|
|
2021-03-08 16:07:10 +00:00
|
|
|
this.mediaTypes = (new Negotiator(clientRequest)).mediaTypes()
|
2021-03-07 15:58:21 +00:00
|
|
|
this.response = new Response(this, serverResponse)
|
2021-03-07 02:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get the value of a header, if it exists. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public getHeader(name: string): string | string[] | undefined {
|
2021-03-07 02:58:48 +00:00
|
|
|
return this.clientRequest.headers[name.toLowerCase()]
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get the native Node.js IncomingMessage object. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public toNative(): IncomingMessage {
|
2021-03-07 02:58:48 +00:00
|
|
|
return this.clientRequest
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Get the value of an input field on the request. Spans multiple input sources.
|
|
|
|
* @param key
|
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
public input(key?: string): unknown {
|
2022-03-30 20:35:09 +00:00
|
|
|
let sources = {
|
|
|
|
...this.parsedInput,
|
|
|
|
...this.query,
|
|
|
|
...this.uploadedFiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.hasKey(ActivatedRoute) ) {
|
|
|
|
sources = {
|
|
|
|
...sources,
|
|
|
|
...this.make<ActivatedRoute<unknown, unknown[]>>(ActivatedRoute).params,
|
2021-05-22 15:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:35:09 +00:00
|
|
|
if ( !key ) {
|
|
|
|
return sources
|
2021-03-31 02:15:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 20:35:09 +00:00
|
|
|
if ( key in sources ) {
|
|
|
|
return sources[key]
|
2021-03-07 02:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 06:00:35 +00:00
|
|
|
/**
|
|
|
|
* Look up a field from the request and wrap it in a safe-value accessor.
|
|
|
|
* @param key
|
|
|
|
*/
|
|
|
|
public safe(key?: string): Safe {
|
|
|
|
return Pipeline.id()
|
|
|
|
.tap(val => new Safe(val))
|
|
|
|
.tap(safe => safe.onError(message => {
|
|
|
|
throw new HTTPError(HTTPStatus.BAD_REQUEST, `Invalid field (${key}): ${message}`)
|
|
|
|
}))
|
|
|
|
.apply(this.input(key))
|
|
|
|
}
|
|
|
|
|
2021-03-31 02:15:39 +00:00
|
|
|
/**
|
|
|
|
* Get the UniversalPath instance for a file uploaded in the given field on the request.
|
|
|
|
*/
|
|
|
|
public file(key: string): UniversalPath | undefined {
|
|
|
|
return this.uploadedFiles[key]
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the request accepts the given media type.
|
|
|
|
* @param type - a mimetype, or the short forms json, xml, or html
|
|
|
|
*/
|
2021-06-03 03:36:25 +00:00
|
|
|
accepts(type: string): boolean {
|
|
|
|
if ( type === 'json' ) {
|
|
|
|
type = 'application/json'
|
|
|
|
} else if ( type === 'xml' ) {
|
|
|
|
type = 'application/xml'
|
|
|
|
} else if ( type === 'html' ) {
|
|
|
|
type = 'text/html'
|
|
|
|
}
|
2021-03-08 16:07:10 +00:00
|
|
|
|
|
|
|
type = type.toLowerCase()
|
|
|
|
|
|
|
|
const possible = [
|
|
|
|
type,
|
|
|
|
type.split('/')[0] + '/*',
|
2021-06-03 03:36:25 +00:00
|
|
|
'*/*',
|
2021-03-08 16:07:10 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
return this.mediaTypes.some(media => possible.includes(media.toLowerCase()))
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the short form of the content type the client has requested.
|
|
|
|
*/
|
2021-03-08 16:07:10 +00:00
|
|
|
wants(): 'html' | 'json' | 'xml' | 'unknown' {
|
|
|
|
const jsonIdx = this.mediaTypes.indexOf('application/json') ?? this.mediaTypes.indexOf('application/*') ?? this.mediaTypes.indexOf('*/*')
|
|
|
|
const xmlIdx = this.mediaTypes.indexOf('application/xml') ?? this.mediaTypes.indexOf('application/*') ?? this.mediaTypes.indexOf('*/*')
|
|
|
|
const htmlIdx = this.mediaTypes.indexOf('text/html') ?? this.mediaTypes.indexOf('text/*') ?? this.mediaTypes.indexOf('*/*')
|
|
|
|
|
2021-06-03 03:36:25 +00:00
|
|
|
if ( htmlIdx >= 0 && htmlIdx <= jsonIdx && htmlIdx <= xmlIdx ) {
|
|
|
|
return 'html'
|
|
|
|
}
|
|
|
|
if ( jsonIdx >= 0 && jsonIdx <= htmlIdx && jsonIdx <= xmlIdx ) {
|
|
|
|
return 'json'
|
|
|
|
}
|
|
|
|
if ( xmlIdx >= 0 && xmlIdx <= jsonIdx && xmlIdx <= htmlIdx ) {
|
|
|
|
return 'xml'
|
|
|
|
}
|
2021-03-08 16:07:10 +00:00
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2021-03-07 02:58:48 +00:00
|
|
|
// hostname
|
|
|
|
|
|
|
|
/*
|
|
|
|
param
|
|
|
|
json
|
|
|
|
fresh/stale - cache
|
|
|
|
remote ips (proxy)
|
|
|
|
signedCookies
|
2021-03-08 16:07:10 +00:00
|
|
|
accepts charsets, encodings, languages
|
2021-03-07 02:58:48 +00:00
|
|
|
range header parser
|
|
|
|
*/
|
|
|
|
}
|