Skip to content

Commit 3a05a4f

Browse files
authored
fix fetch path logic (#4890)
1 parent 23e3cd3 commit 3a05a4f

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

lib/web/fetch/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ async function httpNetworkFetch (
21342134

21352135
return new Promise((resolve, reject) => agent.dispatch(
21362136
{
2137-
path: url.href.slice(url.origin.length, url.hash.length ? -url.hash.length : undefined),
2137+
path: url.href.slice(url.href.indexOf(url.host) + url.host.length, url.hash.length ? -url.hash.length : undefined),
21382138
origin: url.origin,
21392139
method: request.method,
21402140
body: agent.isMockActive ? request.body && (request.body.source || request.body.stream) : body,

test/websocket/issue-4889.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { test } = require('node:test')
2+
const http = require('node:http')
3+
const crypto = require('node:crypto')
4+
const { WebSocket } = require('../..')
5+
const { createDeferredPromise } = require('../../lib/util/promise')
6+
7+
test('WebSocket basic auth', (t) => {
8+
const server = http.createServer()
9+
10+
server.on('upgrade', (req, socket) => {
11+
const auth = req.headers.authorization
12+
if (!auth || auth !== `Basic ${Buffer.from('user:pass').toString('base64')}`) {
13+
socket.write(
14+
'HTTP/1.1 401 Unauthorized\r\n' +
15+
'WWW-Authenticate: Basic realm="test"\r\n' +
16+
'Content-Length: 0\r\n' +
17+
'\r\n'
18+
)
19+
socket.destroy()
20+
return
21+
}
22+
23+
const key = req.headers['sec-websocket-key']
24+
const accept = crypto
25+
.createHash('sha1')
26+
.update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
27+
.digest('base64')
28+
29+
socket.write(
30+
'HTTP/1.1 101 Switching Protocols\r\n' +
31+
'Upgrade: websocket\r\n' +
32+
'Connection: Upgrade\r\n' +
33+
'Sec-WebSocket-Accept: ' + accept + '\r\n' +
34+
'\r\n'
35+
)
36+
37+
socket.on('data', () => socket.destroy())
38+
}).listen(0)
39+
40+
const { port } = server.address()
41+
const url = `ws://user:pass@127.0.0.1:${port}/path`
42+
43+
const ws = new WebSocket(url)
44+
45+
t.after(() => {
46+
ws.close()
47+
server.close()
48+
})
49+
50+
const promise = createDeferredPromise()
51+
52+
ws.addEventListener('open', () => {
53+
promise.resolve()
54+
ws.send('h')
55+
})
56+
57+
ws.addEventListener('error', (e) => {
58+
promise.reject(e)
59+
})
60+
61+
return promise.promise
62+
})

0 commit comments

Comments
 (0)