Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions packages/utils/httpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,25 @@ export class HttpServer {
return response.end();
}

// Sending Partial Content: https://datatracker.ietf.org/doc/html/rfc7233#section-4.1
response.writeHead(206, {
'Content-Range': `bytes ${start}-${end}/${size}`,
'Accept-Ranges': 'bytes',
'Content-Length': end - start + 1,
'Content-Type': mime.getType(path.extname(absoluteFilePath))!,
});

const readable = fs.createReadStream(absoluteFilePath, { start, end });
readable.pipe(response);
readable.on('error', error => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this something that can actually be hit by real user code? as far as i can tell we haven't had any reports of this being an issue so im not sure it's actually necessary to fix (especially if it's not possible to hit due to other existing guards elsewhere)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Rousso (@dcrousso)

is this something that can actually be hit by real user code?

No path we can show through show-report / show-trace without a chmod race after statSync. I am closing this.

if (response.headersSent) {
response.destroy(error);
return;
}
response.writeHead(500);
response.end();
});
readable.on('open', () => {
// Sending Partial Content: https://datatracker.ietf.org/doc/html/rfc7233#section-4.1
response.writeHead(206, {
'Content-Range': `bytes ${start}-${end}/${size}`,
'Accept-Ranges': 'bytes',
'Content-Length': end - start + 1,
'Content-Type': mime.getType(path.extname(absoluteFilePath))!,
});
readable.pipe(response);
});
}

private _onRequest(request: http.IncomingMessage, response: http.ServerResponse) {
Expand Down
76 changes: 76 additions & 0 deletions tests/library/http-server.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import fs from 'fs';
import http from 'http';
import os from 'os';
import path from 'path';
import { test, expect } from '@playwright/test';
import { HttpServer } from '../../packages/utils/httpServer';

async function startServer(dir: string) {
const server = new HttpServer(dir);
server.routePrefix('/', (request, response) => {
const url = new URL(request.url!, 'http://localhost');
const filePath = path.join(dir, path.basename(url.pathname));
return server.serveFile(request, response, filePath);
});
await server.start({ port: 0, host: '127.0.0.1' });
return server;
}

test('range request returns partial content', async ({}, testInfo) => {
const dir = testInfo.outputPath();
const file = path.join(dir, 'data.bin');
fs.writeFileSync(file, Buffer.from('abcdefghij'));
const server = await startServer(dir);
try {
const url = server.urlPrefix('precise') + '/data.bin';
const res = await fetch(url, { headers: { Range: 'bytes=2-5' } });
expect(res.status).toBe(206);
expect(await res.text()).toBe('cdef');
} finally {
await server.stop();
}
});

test('unreadable file range request does not crash the server', async ({}, testInfo) => {
test.skip(process.platform === 'win32', 'chmod is not a reliable ACL on Windows');
test.skip(os.userInfo().uid === 0, 'root can read chmod 0 files');
const dir = testInfo.outputPath();
const file = path.join(dir, 'data.bin');
fs.writeFileSync(file, Buffer.alloc(1024, 1));
fs.chmodSync(file, 0);
const server = await startServer(dir);
try {
const url = server.urlPrefix('precise') + '/data.bin';
const status = await new Promise<number>((resolve, reject) => {
const req = http.get(url, { headers: { Range: 'bytes=0-10' } }, res => {
res.resume();
res.on('end', () => resolve(res.statusCode || 0));
});
req.on('error', reject);
req.setTimeout(5000, () => {
req.destroy();
reject(new Error('range request hung'));
});
});
expect(status).toBe(500);
} finally {
fs.chmodSync(file, 0o644);
await server.stop();
}
});
Loading