Skip to content

Commit

Permalink
test-app: allow paths with slashes for xkey
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Jun 11, 2024
1 parent 75dd3cb commit a7bfd3f
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions test/app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import fastify from "fastify";
import fastifyFormbody from "@fastify/formbody";

/**
* Cleanup header value.
* Returns default value if the header value is not a string or is empty or too long (more than 256 characters long).
*
* @param headerValue Value of the header
* @param defaultValue Default value of the header
* @returns Cleaned up header value
*/
const cleanupHeaderValue = (
headerValue: string,
defaultValue: string
): string => {
if (typeof headerValue !== "string") {
return defaultValue;
}

// Split, remove all lines except the first one (can be CRLF, LF or CR), trim, and return
const newValue = headerValue.split(/\r\n|\r|\n/)[0].trim();
if (newValue.length === 0) {
return defaultValue;
}
if (newValue.length > 256) {
return defaultValue;
}
return newValue;
};

// Fetch values from environment variables
const port = process.env.SERVER_PORT || 8080;
const host = process.env.SERVER_HOST || "::";
Expand Down Expand Up @@ -32,15 +59,15 @@ server.all<{
});

// Return a specific xkey header
server.all<{
Params: {
headerValue: string;
};
}>("/x-header/:headerValue", async (request, reply) => {
return reply.header("xkey", request.params.headerValue).send({
server.all("/x-header/*", async (request, reply) => {
const path =
request.raw.url?.split("?")[0].split("/").slice(2).join("/") || "";
const xkeyValue = cleanupHeaderValue(path, "default");

return reply.header("xkey", xkeyValue).send({
hello: "xkey header",
time: Date.now(),
value: request.params.headerValue,
value: xkeyValue,
});
});

Expand Down

0 comments on commit a7bfd3f

Please sign in to comment.