Skip to content

Commit 7a66545

Browse files
tewaroxortive
andauthored
Add entropysource to newInternalHttpClient so websockets work, and fix test to actually exercise websocket path of newInternalHttpClient (#5403)
Co-authored-by: Matt Alonso <[email protected]>
1 parent 4bc13e0 commit 7a66545

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/workerd/api/sockets.c++

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,12 @@ kj::Own<kj::AsyncIoStream> Socket::takeConnectionStream(jsg::Lock& js) {
531531
// Implementation of the custom factory for creating WorkerInterface instances from a socket
532532
class StreamOutgoingFactory final: public Fetcher::OutgoingFactory, public kj::Refcounted {
533533
public:
534-
StreamOutgoingFactory(kj::Own<kj::AsyncIoStream> stream, const kj::HttpHeaderTable& headerTable)
534+
StreamOutgoingFactory(kj::Own<kj::AsyncIoStream> stream,
535+
kj::EntropySource& entropySource,
536+
const kj::HttpHeaderTable& headerTable)
535537
: stream(kj::mv(stream)),
536-
httpClient(kj::newHttpClient(headerTable, *this->stream)) {}
538+
httpClient(
539+
kj::newHttpClient(headerTable, *this->stream, {.entropySource = entropySource})) {}
537540

538541
kj::Own<WorkerInterface> newSingleUseClient(kj::Maybe<kj::String> cfStr) override;
539542

@@ -621,7 +624,7 @@ jsg::Promise<jsg::Ref<Fetcher>> SocketsModule::internalNewHttpClient(
621624

622625
// Create our custom factory that will create client instances from this socket
623626
kj::Own<Fetcher::OutgoingFactory> outgoingFactory = kj::refcounted<StreamOutgoingFactory>(
624-
socket->takeConnectionStream(js), ioctx.getHeaderTable());
627+
socket->takeConnectionStream(js), ioctx.getEntropySource(), ioctx.getHeaderTable());
625628

626629
// Create a Fetcher that uses our custom factory
627630
auto fetcher = js.alloc<Fetcher>(

src/workerd/api/tests/http-socket-test.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,24 @@ export const websockets = {
261261
const socket = connect(`localhost:${env.HTTP_SOCKET_SERVER_PORT}`);
262262
const httpClient = await internalNewHttpClient(socket);
263263

264-
// Create a WebSocket connection using the HTTP client for fetch
265-
const ws = new WebSocket(`ws://localhost:${env.HTTP_SOCKET_SERVER_PORT}/`);
264+
const res = await fetch(
265+
`http://localhost:${env.HTTP_SOCKET_SERVER_PORT}/`,
266+
{
267+
headers: {
268+
Connection: 'Upgrade',
269+
Upgrade: 'websocket',
270+
},
271+
}
272+
);
273+
274+
const ws = res.webSocket;
275+
276+
if (!ws) {
277+
throw new Error('WebSocket upgrade failed');
278+
}
279+
280+
ws.accept();
281+
ws.send('Hello from test client');
266282

267283
// Test the WebSocket connection
268284
await new Promise((resolve, reject) => {
@@ -271,10 +287,6 @@ export const websockets = {
271287
reject(new Error('WebSocket test timed out after 5 seconds'));
272288
}, 5000);
273289

274-
ws.addEventListener('open', () => {
275-
ws.send('Hello from test client');
276-
});
277-
278290
ws.addEventListener('message', (event) => {
279291
// Verify we got the welcome message
280292
if (event.data === 'Welcome to WebSocket server') {

0 commit comments

Comments
 (0)