Skip to content

Commit

Permalink
fix: normalize header keys to lowercase in HTTP modules
Browse files Browse the repository at this point in the history
  • Loading branch information
🎲 committed Jan 14, 2025
1 parent 4747a1e commit 828c284
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 65 deletions.
6 changes: 3 additions & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.32.15"
}
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.32.15"
}
6 changes: 1 addition & 5 deletions packages/tslua-base64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-base64",
"version": "0.32.15",
"description": "Base64 library with typings",
"keywords": [
"lua",
"dcs",
"base64"
],
"keywords": ["lua", "dcs", "base64"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-common",
"version": "0.32.15",
"description": "Common Utils Library",
"keywords": [
"lua",
"dcs",
"logger"
],
"keywords": ["lua", "dcs", "logger"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-dcs-gui-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-dcs-gui-types",
"version": "0.32.15",
"description": "Typescript type definitions for DCS GUI environment",
"keywords": [
"lua",
"dcs",
"gui"
],
"keywords": ["lua", "dcs", "gui"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-dcs-mission-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-dcs-mission-types",
"version": "0.32.15",
"description": "Typescript type definitions for DCS",
"keywords": [
"lua",
"dcs",
"mission"
],
"keywords": ["lua", "dcs", "mission"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-http-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-http-api",
"version": "0.32.15",
"description": "LUA HTTP API Server written in Typescript Transpiled to lua using TypeScriptToLua",
"keywords": [
"lua",
"http",
"api"
],
"keywords": ["lua", "http", "api"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
23 changes: 16 additions & 7 deletions packages/tslua-http-api/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ import { getPathParameters, isMatch } from "./path";
export class AppHttpRequest<PARAMS = any, QUERY = any, BODY = any> {
protected logger = new Logger("AppHttpRequest");

/**
* @deprecated Use getQueryParameterValue and getQueryParameterValueOrThrow
*/
get query(): QUERY {
return this.req.parameters as QUERY;
}

/**
* @deprecated Use getBody and getBodyOrThrow
*/
get body(): BODY | undefined {
if (!this.req.body) return undefined;

// Body Parse JSON
if (
this.getHeaderValue("content-type") === "application/json" ||
this.getHeaderValue("Content-Type") === "application/json"
) {
if (this.getHeaderValue("content-type") === "application/json") {
try {
return json.decode(this.req.body) as BODY;
} catch (e) {
Expand All @@ -36,12 +39,18 @@ export class AppHttpRequest<PARAMS = any, QUERY = any, BODY = any> {
return this.req.body as BODY;
}

/**
* @deprecated Use getPathParameterValue and getPathParameterValueOrThrow
*/
get params(): PARAMS {
return this.__params as PARAMS;
}

__params: Record<string, string>;

/**
* @deprecated Use getHeaderValue and getHeaderValueOrThrow
*/
get headers() {
return this.req.headers;
}
Expand Down Expand Up @@ -71,17 +80,17 @@ export class AppHttpRequest<PARAMS = any, QUERY = any, BODY = any> {
}

getHeaderValue<T>(key: string) {
return this.req.headers[key] as T;
return this.req.headers[key.toLowerCase()] as T;
}

getHeaderValueOrThrow<T>(
key: string,
error = new Error("Header is not defined"),
) {
if (this.req.headers[key] === undefined) {
if (this.req.headers[key.toLowerCase()] === undefined) {
throw error;
}
return this.req.headers[key] as T;
return this.req.headers[key.toLowerCase()] as T;
}

getBody<T = string>() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tslua-http-api/src/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ app.get("/complex/:id", (req, res) => {
});

const authMiddleware: AppMiddleware = (req, res, next) => {
if (req.headers.Authorization !== "Bearer 123") {
if (req.getHeaderValue("Authorization") !== "Bearer 123") {
return res.status(HttpStatus.UNAUTHORIZED).send("Unauthorized");
}

Expand Down
5 changes: 1 addition & 4 deletions packages/tslua-http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"name": "@flying-dice/tslua-http",
"version": "0.32.15",
"description": "LUA HTTP Server written in Typescript Transpiled to lua using TypeScriptToLua",
"keywords": [
"lua",
"http"
],
"keywords": ["lua", "http"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
6 changes: 3 additions & 3 deletions packages/tslua-http/src/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("readRequestHead", () => {
method: "GET",
originalUrl: "/home",
protocol: "HTTP/1.1",
headers: { Host: "example.com" },
headers: { host: "example.com" },
path: "/home",
parameters: {},
};
Expand All @@ -23,7 +23,7 @@ describe("readRequestHead", () => {
method: "POST",
originalUrl: "/submit",
protocol: "HTTP/1.1",
headers: { Host: "example.com", "Content-Type": "application/json" },
headers: { host: "example.com", "content-type": "application/json" },
path: "/submit",
parameters: {},
};
Expand All @@ -38,7 +38,7 @@ describe("readRequestHead", () => {
method: "GET",
originalUrl: "/search?q=test",
protocol: "HTTP/1.1",
headers: { Host: "example.com" },
headers: { host: "example.com" },
path: "/search",
parameters: { q: "test" },
};
Expand Down
2 changes: 1 addition & 1 deletion packages/tslua-http/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const readRequestHead = (requestPayload: string): HttpRequest => {
for (const headerLine of headerLines) {
if (headerLine === "") break;
const [key, value] = headerLine.split(":");
httpRequest.headers[key.trim()] = value.trim();
httpRequest.headers[key.trim().toLowerCase()] = value.trim();
}

return httpRequest;
Expand Down
6 changes: 2 additions & 4 deletions packages/tslua-http/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ export class HttpServer {
this.logger.debug("Received request head");
const request = readRequestHead(requestHeadLines.join(CRLF));

const contentLength = request.headers["Content-Length"];
const contentLength = request.headers["content-length"];
const contentLengthNum = tonumber(contentLength);
if (contentLengthNum && contentLengthNum > 0) {
this.logger.debug(
`Fetching request body ${request.headers["Content-Length"]}`,
);
this.logger.debug(`Fetching request body ${contentLength}`);

client.settimeout(2);
request.body = client.receive(contentLengthNum) as string;
Expand Down
6 changes: 1 addition & 5 deletions packages/tslua-luatest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
"name": "@flying-dice/tslua-luatest",
"version": "0.32.15",
"description": "tslua testing library",
"keywords": [
"lua",
"dcs",
"test"
],
"keywords": ["lua", "dcs", "test"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
7 changes: 1 addition & 6 deletions packages/tslua-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
"name": "@flying-dice/tslua-openapi",
"version": "0.32.15",
"description": "Re-export of https://github.com/metadevpro/openapi3-ts/ library for tslua",
"keywords": [
"lua",
"http",
"api",
"openapi"
],
"keywords": ["lua", "http", "api", "openapi"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down
7 changes: 1 addition & 6 deletions packages/tslua-rxi-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
"description": "rxi-json library with typings",
"types": "./index.d.ts",
"main": "./index",
"keywords": [
"lua",
"dcs",
"json",
"rxi/json"
],
"keywords": ["lua", "dcs", "json", "rxi/json"],
"repository": {
"type": "git",
"url": "https://github.com/flying-dice/tslua-dcs.git"
Expand Down

0 comments on commit 828c284

Please sign in to comment.