Skip to content
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

fix: only handle get requests that are not websocket requests #128

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions packages/sirv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ export default function (dir, opts={}) {
let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);

return function (req, res, next) {
// only handle GET requests that are not WebSocket requests
if (req.method !== 'GET' || req.headers.upgrade) {
return next ? next() : isNotFound(req, res);
}

let extns = [''];
let pathname = parse(req).pathname;
let val = req.headers['accept-encoding'] || '';
Expand Down
30 changes: 30 additions & 0 deletions tests/sirv.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ types.run();

const basic = suite('basics');

basic('should only handle GET requests', async () => {
let server = utils.http();

try {
let res = await server.send('POST', '/contact').catch(err => {
assert.is(err.statusCode, 404);
})
assert.is(res, undefined)
}
finally {
server.close();
}
})

basic('should not handle WebSocket requests', async () => {
let server = utils.http();

try {
// mock a websocket upgrade request
const headers = {'Upgrade': 'websocket' }
let res = await server.send('GET', '/contact', { headers }).catch(err => {
assert.is(err.statusCode, 404);
})
assert.is(res, undefined)
}
finally {
server.close();
}
})

basic('should return the file if found', async () => {
let server = utils.http();

Expand Down