diff --git a/src/server/i-request.ts b/src/server/i-request.ts index ef476c6..5b755fb 100644 --- a/src/server/i-request.ts +++ b/src/server/i-request.ts @@ -17,6 +17,7 @@ export interface IRequest { method: Method; url: URL; userAgent: string; + referer: string; accept?: Set; acceptEncoding?: Set; acceptLanguage?: Set; diff --git a/src/server/server.ts b/src/server/server.ts index fbef0fe..f8cdd0c 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -137,6 +137,7 @@ export class Server implements IServer { const normalizedRequest = getRequestFromIncomingHeaders( { ...request.headers, + ":referer": ":referer" in request.headers ? request.headers[":referer"] : request.headers.referer, ":method": ":method" in request.headers ? request.headers[":method"] : request.method, ":path": ":path" in request.headers ? request.headers[":path"] : request.url, ":scheme": ":scheme" in request.headers ? request.headers[":scheme"] : tls ? "https" : "http", diff --git a/src/util/request-util/request-util.ts b/src/util/request-util/request-util.ts index b3e6aec..bc70cf4 100644 --- a/src/util/request-util/request-util.ts +++ b/src/util/request-util/request-util.ts @@ -3,10 +3,11 @@ import {request as requestHttps} from "https"; import {IRawRequest, Request} from "../../server/i-request"; import {IOKResponse, Response} from "../../server/i-response"; import chalk from "chalk"; -import {connect, ClientHttp2Stream, ClientHttp2Session} from "http2"; +import {ClientHttp2Session, ClientHttp2Stream, connect} from "http2"; import {ContentEncodingKind} from "../../encoding/content-encoding-kind"; import {URL} from "url"; import {constant} from "../../constant/constant"; +import {Method} from "../../server/method"; // tslint:disable:no-any @@ -41,17 +42,18 @@ export function getRequestFromIncomingHeaders(headers: IncomingHttpHeaders, http // Replace all literal "+" with its literal encoded variant path = path.replace(/\+/g, "%2B"); } - return { + + return { http2, - method: headers[":method"]!, + method: headers[":method"] == null ? "OPTIONS" : (headers[":method"] as Method), accept: headers.accept == null ? undefined : splitStringifiedListHeader(headers.accept), acceptEncoding: headers["accept-encoding"] == null ? undefined : splitStringifiedListHeader(headers["accept-encoding"]!), acceptLanguage: headers["accept-language"] == null ? undefined : splitStringifiedListHeader(headers["accept-language"]!), - userAgent: headers["user-agent"]!, - // @ts-ignore - url: new URL(path, `${headers[":scheme"]}://${headers[":authority"]}`), + userAgent: headers["user-agent"] != null ? headers["user-agent"] : "", + referer: headers[":referer"] != null ? (headers[":referer"] as string) : headers.referer != null ? headers.referer : "", + url: new URL(path as string, `${headers[":scheme"]}://${headers[":authority"]}`), cachedChecksum: headers["if-none-match"] - }; + } as Request; } /** @@ -78,8 +80,8 @@ function paintMethod(method: Request["method"]): string { * Prints the given request * @param {IRequest} request */ -export function printRequest({method, url, userAgent}: Request): void { - console.log(`${paintMethod(method)} ${url.toString()} (${chalk.gray(userAgent)})`); +export function printRequest({method, url, userAgent, referer}: Request): void { + console.log(`${paintMethod(method)} ${url.toString()} (${chalk.gray(userAgent)}) ${referer != null && referer.length > 0 ? `(${chalk.yellow(referer)})` : ``}`); } /**