From c66c5a88dad2d3064af0a13ab47deeb0d0c90b90 Mon Sep 17 00:00:00 2001 From: hex2077 Date: Mon, 17 Nov 2025 11:35:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86):=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=81=A5=E5=BA=B7=E6=A3=80=E6=9F=A5=E7=AB=AF?= =?UTF-8?q?=E7=82=B9=E5=B9=B6=E4=BC=98=E5=8C=96=E6=8E=88=E6=9D=83=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将健康检查端点和count_tokens处理逻辑提前以提高可读性 - 移除对空Bearer令牌的特殊处理,简化授权逻辑 --- src/request-handler.js | 51 ++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/src/request-handler.js b/src/request-handler.js index d972fd4..f9200aa 100644 --- a/src/request-handler.js +++ b/src/request-handler.js @@ -50,6 +50,29 @@ export function createRequestHandler(config, providerPoolManager) { console.log(`\n${new Date().toLocaleString()}`); console.log(`[Server] Received request: ${req.method} http://${req.headers.host}${req.url}`); + + // Health check endpoint + if (method === 'GET' && path === '/health') { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + status: 'healthy', + timestamp: new Date().toISOString(), + provider: currentConfig.MODEL_PROVIDER + })); + return true; + } + + // Ignore count_tokens requests + if (path.includes('/count_tokens')) { + console.log(`[Server] Ignoring count_tokens request: ${path}`); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + tokens: 0, + message: 'Token counting is not supported' + })); + return true; + } + // Handle API requests // Allow overriding MODEL_PROVIDER via request header const modelProviderHeader = req.headers['model-provider']; @@ -88,35 +111,9 @@ export function createRequestHandler(config, providerPoolManager) { } return; } - - // Health check endpoint - if (method === 'GET' && path === '/health') { - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ - status: 'healthy', - timestamp: new Date().toISOString(), - provider: currentConfig.MODEL_PROVIDER - })); - return true; - } - - // Ignore count_tokens requests - if (path.includes('/count_tokens')) { - console.log(`[Server] Ignoring count_tokens request: ${path}`); - res.writeHead(200, { 'Content-Type': 'application/json' }); - res.end(JSON.stringify({ - tokens: 0, - message: 'Token counting is not supported' - })); - return true; - } // Check authentication for API requests - // Allow empty Bearer token (from Ollama clients like VS Code Copilot) - const authHeader = req.headers['authorization']; - const hasEmptyBearer = authHeader === 'Bearer' || authHeader === 'Bearer '; - - if (!isAuthorized(req, requestUrl, currentConfig.REQUIRED_API_KEY) && !hasEmptyBearer) { + if (!isAuthorized(req, requestUrl, currentConfig.REQUIRED_API_KEY)) { res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: { message: 'Unauthorized: API key is invalid or missing.' } })); return;