MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / handleRequest

Function handleRequest

src/ui-server/index.ts:237–353  ·  view source on GitHub ↗

* One request, start to finish. Order matters: the cheap refusals (method, * `Host`, `Origin`) run before anything touches the filesystem.

(
  req: http.IncomingMessage,
  res: http.ServerResponse,
  deps: HandlerDeps
)

Source from the content-addressed store, hash-verified

235 * `Host`, `Origin`) run before anything touches the filesystem.
236 */
237async function handleRequest(
238 req: http.IncomingMessage,
239 res: http.ServerResponse,
240 deps: HandlerDeps
241): Promise<void> {
242 const method = req.method ?? 'GET';
243 for (const [name, value] of Object.entries(SECURITY_HEADERS)) {
244 res.setHeader(name, value);
245 }
246
247 if (!ALLOWED_METHODS.includes(method)) {
248 res.setHeader('Allow', ALLOWED_METHODS.join(', '));
249 sendText(res, 405, `codegraph ui does not answer ${method}.`, method);
250 return;
251 }
252
253 const port = deps.port();
254 if (!isAllowedHost(req.headers.host, port)) {
255 // The DNS-rebinding refusal. Say why, since a human hitting this through a
256 // proxy or a container hostname needs to know what to change.
257 sendText(
258 res,
259 403,
260 'Refused: codegraph ui only answers requests addressed to this machine ' +
261 `(localhost, 127.0.0.1 or [::1] on port ${port}).\n` +
262 `This request said Host: ${forEcho(req.headers.host)}`,
263 method
264 );
265 return;
266 }
267
268 if (!isAllowedOrigin(readHeader(req, 'origin'), port)) {
269 sendText(res, 403, 'Refused: cross-origin requests are not served.', method);
270 return;
271 }
272
273 // Checked on the RAW url, before WHATWG parsing folds `..` segments away.
274 const rawPath = (req.url ?? '/').split(/[?#]/)[0] ?? '/';
275 // The `/api/` namespace answers JSON for EVERY outcome, refusals included:
276 // the viewer parses these responses, and a text/plain body here would surface
277 // as a parse error instead of the refusal it actually is.
278 const jsonNamespace = rawPath === '/api' || rawPath.startsWith('/api/');
279
280 // The one place this server stops being a pure reader. A write has to be
281 // under /api/ and carry the marker header — see `isWriteRequest` for what
282 // that closes that Host and Origin do not.
283 if (isWriteMethod(method)) {
284 const verdict = isWriteRequest(rawPath, {
285 marker: readHeader(req, WRITE_HEADER),
286 contentType: readHeader(req, 'content-type'),
287 });
288 if (!verdict.ok) {
289 if (!jsonNamespace) res.setHeader('Allow', READ_METHODS.join(', '));
290 const body = `Refused: ${verdict.reason}`;
291 if (jsonNamespace) sendJson(res, 403, { error: body, code: 'refused' }, method);
292 else sendText(res, 405, body, method);
293 return;
294 }

Callers 1

startUiServerFunction · 0.85

Calls 15

sendTextFunction · 0.90
isAllowedHostFunction · 0.90
isAllowedOriginFunction · 0.90
isWriteMethodFunction · 0.90
isWriteRequestFunction · 0.90
sendJsonFunction · 0.90
isSafeRequestPathFunction · 0.90
resolveStaticAssetFunction · 0.90
sendFileFunction · 0.90
shouldFallBackToIndexFunction · 0.90
forEchoFunction · 0.85
readHeaderFunction · 0.85

Tested by

no test coverage detected