Skip to content

Commit

Permalink
feat(logging): adds referrer logging
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Apr 11, 2019
1 parent f10b2b9 commit 77cbce8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/server/i-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IRequest {
method: Method;
url: URL;
userAgent: string;
referer: string;
accept?: Set<string>;
acceptEncoding?: Set<string>;
acceptLanguage?: Set<string>;
Expand Down
1 change: 1 addition & 0 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 11 additions & 9 deletions src/util/request-util/request-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -41,17 +42,18 @@ export function getRequestFromIncomingHeaders(headers: IncomingHttpHeaders, http
// Replace all literal "+" with its literal encoded variant
path = path.replace(/\+/g, "%2B");
}
return <Request>{

return {
http2,
method: <Request["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;
}

/**
Expand All @@ -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)})` : ``}`);
}

/**
Expand Down

0 comments on commit 77cbce8

Please sign in to comment.