Skip to content

Commit 8f77350

Browse files
committed
Fix webdav server
1 parent 3ded57a commit 8f77350

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

tests/mcp/yaml/get-log-file-contents.full-mode.test.mcp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ tests:
225225
arguments:
226226
filename: "jobs/ImportCatalog/Job-ImportCatalog-0987654321.log"
227227
tailOnly: true
228-
maxBytes: 1200
228+
maxBytes: 10
229229
expect:
230230
response:
231231
jsonrpc: "2.0"
@@ -234,7 +234,7 @@ tests:
234234
content:
235235
match:arrayElements:
236236
type: "text"
237-
text: "match:contains:(tail read)"
237+
text: "\"# Log File Contents: jobs/ImportCatalog/Job-ImportCatalog-0987654321.log (tail read)\\n\\nTotal lines: 1\\nContent size: 10 bytes\\n\\n---\\n\\ntus [OK].\\n\""
238238
isError: false
239239
stderr: "toBeEmpty"
240240
performance:

tests/servers/webdav/server.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class SFCCMockWebDAVServer {
7070
// Add CORS headers
7171
res.setHeader('Access-Control-Allow-Origin', '*');
7272
res.setHeader('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS, PROPFIND');
73-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Depth');
73+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Depth, Range');
74+
res.setHeader('Access-Control-Expose-Headers', 'Content-Range, Accept-Ranges');
7475

7576
if (this.isDevMode) {
7677
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
@@ -128,9 +129,17 @@ class SFCCMockWebDAVServer {
128129
return;
129130
}
130131

132+
// Handle range requests for partial content
133+
const rangeHeader = req.headers.range;
134+
if (rangeHeader) {
135+
return this.handleRangeRequest(req, res, fsPath, stats);
136+
}
137+
138+
// Normal full file request
131139
res.statusCode = 200;
132140
res.setHeader('Content-Type', 'text/plain');
133141
res.setHeader('Content-Length', stats.size);
142+
res.setHeader('Accept-Ranges', 'bytes');
134143

135144
const stream = fs.createReadStream(fsPath);
136145
stream.pipe(res);
@@ -141,6 +150,57 @@ class SFCCMockWebDAVServer {
141150
}
142151
}
143152

153+
handleRangeRequest(req, res, fsPath, stats) {
154+
try {
155+
const rangeHeader = req.headers.range;
156+
const fileSize = stats.size;
157+
158+
if (this.isDevMode) {
159+
console.log(`[DEBUG] Range request: ${rangeHeader} for file ${fsPath} (size: ${fileSize})`);
160+
}
161+
162+
// Parse range header (e.g., "bytes=0-499" or "bytes=500-999")
163+
const rangeMatch = rangeHeader.match(/bytes=(\d+)-(\d*)/);
164+
if (!rangeMatch) {
165+
res.statusCode = 416; // Range Not Satisfiable
166+
res.setHeader('Content-Range', `bytes */${fileSize}`);
167+
res.end('Range Not Satisfiable');
168+
return;
169+
}
170+
171+
const start = parseInt(rangeMatch[1], 10);
172+
const end = rangeMatch[2] ? parseInt(rangeMatch[2], 10) : fileSize - 1;
173+
174+
// Validate range
175+
if (start >= fileSize || end >= fileSize || start > end) {
176+
res.statusCode = 416; // Range Not Satisfiable
177+
res.setHeader('Content-Range', `bytes */${fileSize}`);
178+
res.end('Range Not Satisfiable');
179+
return;
180+
}
181+
182+
const contentLength = end - start + 1;
183+
184+
if (this.isDevMode) {
185+
console.log(`[DEBUG] Range: ${start}-${end}, Content-Length: ${contentLength}`);
186+
}
187+
188+
// Send partial content response
189+
res.statusCode = 206; // Partial Content
190+
res.setHeader('Content-Type', 'text/plain');
191+
res.setHeader('Content-Length', contentLength);
192+
res.setHeader('Content-Range', `bytes ${start}-${end}/${fileSize}`);
193+
res.setHeader('Accept-Ranges', 'bytes');
194+
195+
const stream = fs.createReadStream(fsPath, { start, end });
196+
stream.pipe(res);
197+
} catch (error) {
198+
console.error('Error handling range request:', error);
199+
res.statusCode = 500;
200+
res.end('Internal Server Error');
201+
}
202+
}
203+
144204
handlePropfind(req, res, fsPath, urlPath) {
145205
try {
146206
if (this.isDevMode) {

0 commit comments

Comments
 (0)