Repository navigation
fix(server): end range responses when the file stream errors #42716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Sebastien Tardif (SebTardif)
wants to merge
2
commits into
microsoft:main
from
SebTardif:fix-httpserver-range-error
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Devin Rousso (@dcrousso)
No path we can show through
show-report/show-tracewithout a chmod race afterstatSync. I am closing this.