Skip to content

Commit e6dc9aa

Browse files
authored
Implemented two ways to check content length (#437)
Signed-off-by: Keval Mahajan <[email protected]>
1 parent d180078 commit e6dc9aa

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

mcpgateway/static/admin.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,32 @@ function fetchWithTimeout(url, options = {}, timeout = 10000) {
178178
})
179179
.then((response) => {
180180
clearTimeout(timeoutId);
181-
182-
// Handle empty responses explicitly
183181
if (
184182
response.status === 0 ||
185-
(response.ok &&
186-
response.status === 200 &&
187-
!response.headers.get("content-length"))
183+
(response.ok && response.status === 200)
188184
) {
189-
console.warn(`Empty response received from ${url}`);
190-
throw new Error("Server returned an empty response");
185+
const contentLength = response.headers.get("content-length");
186+
187+
// Check Content-Length if present
188+
if (contentLength !== null) {
189+
if (parseInt(contentLength, 10) === 0) {
190+
throw new Error(
191+
"Server returned an empty response (via header)",
192+
);
193+
}
194+
} else {
195+
// Fallback: check actual body
196+
const cloned = response.clone();
197+
return cloned.text().then((text) => {
198+
if (!text.trim()) {
199+
throw new Error(
200+
"Server returned an empty response (via body)",
201+
);
202+
}
203+
return response;
204+
});
205+
}
191206
}
192-
193207
return response;
194208
})
195209
.catch((error) => {

0 commit comments

Comments
 (0)