Skip to content

Commit

Permalink
API - added debugging hook for client headers + IP (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
robgruen authored Dec 16, 2024
1 parent b3adea3 commit e7580ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ts/packages/api/src/webServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export class TypeAgentAPIWebServer {
? "index.html"
: request.url;

// special case - dev helper
if (requestedFile == "/__/__headers") {
return this.printHeaders(request, response);
}

// make sure requested file falls under web root
try {
requestedFile = realpathSync(
Expand Down Expand Up @@ -101,4 +106,36 @@ export class TypeAgentAPIWebServer {
stop() {
this.server.close();
}

printHeaders(request: any, response: any): boolean {
const headers = request.headers;

// Convert headers object to a JSON string
const headersJson = JSON.stringify(headers, null, 2);

response.writeHead(200, {
"Content-Type": "text/html",
});

// Send the HTML page with headers
response.end(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTTP Headers</title>
</head>
<body>
<h3>Client IP: ${request.socket.remoteAddress}</h3>
<h2>HTTP Headers</h2>
<pre>${headersJson}</pre>
</body>
</html>
`);

console.warn(`Served HTTP headers for ${request.url}`);

return true;
}
}
Binary file added ts/packages/shell/src/renderer/favicon.ico
Binary file not shown.

0 comments on commit e7580ed

Please sign in to comment.