Spaces:
Configuration error
Configuration error
Fix forwarded HTTPS source URLs
Browse files- server.mjs +16 -3
server.mjs
CHANGED
|
@@ -96,6 +96,18 @@ function normalizeOrigin(origin) {
|
|
| 96 |
return typeof origin === 'string' && origin.trim() ? origin.trim() : '*'
|
| 97 |
}
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
function writeJson(res, status, body, origin, extraHeaders = {}) {
|
| 100 |
const headers = {
|
| 101 |
'Content-Type': 'application/json; charset=utf-8',
|
|
@@ -283,6 +295,7 @@ function referencePage(reference) {
|
|
| 283 |
const server = http.createServer(async (req, res) => {
|
| 284 |
const origin = req.headers.origin ?? '*'
|
| 285 |
const requestUrl = new URL(req.url ?? '/', `http://${req.headers.host ?? `127.0.0.1:${PORT}`}`)
|
|
|
|
| 286 |
|
| 287 |
if (req.method === 'OPTIONS') {
|
| 288 |
res.writeHead(204, {
|
|
@@ -310,7 +323,7 @@ const server = http.createServer(async (req, res) => {
|
|
| 310 |
'<!doctype html><html lang="en"><head><meta charset="utf-8" /><title>Trask Technical References</title></head><body>',
|
| 311 |
'<h1>Trask Technical References</h1>',
|
| 312 |
'<ul>',
|
| 313 |
-
...REFERENCES.map((reference) => `<li><a href="/reference/${reference.slug}">${reference.title}</a></li>`),
|
| 314 |
'</ul>',
|
| 315 |
'</body></html>',
|
| 316 |
].join('')
|
|
@@ -354,7 +367,7 @@ const server = http.createServer(async (req, res) => {
|
|
| 354 |
...SOURCE_DESCRIPTOR,
|
| 355 |
id: `${SOURCE_DESCRIPTOR.id}:${reference.slug}`,
|
| 356 |
name: `${SOURCE_DESCRIPTOR.name}: ${reference.title}`,
|
| 357 |
-
homeUrl: sourceUrl(
|
| 358 |
}))
|
| 359 |
writeJson(res, 200, { sources }, origin)
|
| 360 |
return
|
|
@@ -409,7 +422,7 @@ const server = http.createServer(async (req, res) => {
|
|
| 409 |
const queryId = randomUUID()
|
| 410 |
const now = new Date().toISOString()
|
| 411 |
const match = chooseReference(query)
|
| 412 |
-
const { answer, sources, retrievedSources } = buildFallbackAnswer(query,
|
| 413 |
const record = {
|
| 414 |
queryId,
|
| 415 |
threadId,
|
|
|
|
| 96 |
return typeof origin === 'string' && origin.trim() ? origin.trim() : '*'
|
| 97 |
}
|
| 98 |
|
| 99 |
+
function externalOrigin(req) {
|
| 100 |
+
const forwardedProto = typeof req.headers['x-forwarded-proto'] === 'string'
|
| 101 |
+
? req.headers['x-forwarded-proto'].split(',')[0].trim()
|
| 102 |
+
: ''
|
| 103 |
+
const forwardedHost = typeof req.headers['x-forwarded-host'] === 'string'
|
| 104 |
+
? req.headers['x-forwarded-host'].split(',')[0].trim()
|
| 105 |
+
: ''
|
| 106 |
+
const host = forwardedHost || req.headers.host || `127.0.0.1:${PORT}`
|
| 107 |
+
const proto = forwardedProto || (host.includes('localhost') || host.startsWith('127.0.0.1') ? 'http' : 'https')
|
| 108 |
+
return `${proto}://${host}`
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
function writeJson(res, status, body, origin, extraHeaders = {}) {
|
| 112 |
const headers = {
|
| 113 |
'Content-Type': 'application/json; charset=utf-8',
|
|
|
|
| 295 |
const server = http.createServer(async (req, res) => {
|
| 296 |
const origin = req.headers.origin ?? '*'
|
| 297 |
const requestUrl = new URL(req.url ?? '/', `http://${req.headers.host ?? `127.0.0.1:${PORT}`}`)
|
| 298 |
+
const publicOrigin = externalOrigin(req)
|
| 299 |
|
| 300 |
if (req.method === 'OPTIONS') {
|
| 301 |
res.writeHead(204, {
|
|
|
|
| 323 |
'<!doctype html><html lang="en"><head><meta charset="utf-8" /><title>Trask Technical References</title></head><body>',
|
| 324 |
'<h1>Trask Technical References</h1>',
|
| 325 |
'<ul>',
|
| 326 |
+
...REFERENCES.map((reference) => `<li><a href="${new URL(`/reference/${reference.slug}`, publicOrigin).toString()}">${reference.title}</a></li>`),
|
| 327 |
'</ul>',
|
| 328 |
'</body></html>',
|
| 329 |
].join('')
|
|
|
|
| 367 |
...SOURCE_DESCRIPTOR,
|
| 368 |
id: `${SOURCE_DESCRIPTOR.id}:${reference.slug}`,
|
| 369 |
name: `${SOURCE_DESCRIPTOR.name}: ${reference.title}`,
|
| 370 |
+
homeUrl: sourceUrl(publicOrigin, reference.slug),
|
| 371 |
}))
|
| 372 |
writeJson(res, 200, { sources }, origin)
|
| 373 |
return
|
|
|
|
| 422 |
const queryId = randomUUID()
|
| 423 |
const now = new Date().toISOString()
|
| 424 |
const match = chooseReference(query)
|
| 425 |
+
const { answer, sources, retrievedSources } = buildFallbackAnswer(query, publicOrigin, match)
|
| 426 |
const record = {
|
| 427 |
queryId,
|
| 428 |
threadId,
|