Skip to content

Commit f6a36d5

Browse files
committed
WIP
1 parent 78fd584 commit f6a36d5

23 files changed

+1253
-55
lines changed

packages/bun-usockets/src/crypto/openssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ struct us_socket_t *us_internal_ssl_socket_context_connect(
16171617
2, &context->sc, host, port, options,
16181618
sizeof(struct us_internal_ssl_socket_t) - sizeof(struct us_socket_t) +
16191619
socket_ext_size, is_connecting);
1620-
if (*is_connecting) {
1620+
if (*is_connecting && s) {
16211621
us_internal_zero_ssl_data_for_connected_socket_before_onopen(s);
16221622
}
16231623

packages/bun-uws/src/App.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -620,12 +620,12 @@ struct TemplatedApp {
620620
}
621621

622622
TemplatedApp &&setUsingCustomExpectHandler(bool value) {
623-
httpContext->getSocketContextData()->usingCustomExpectHandler = value;
623+
httpContext->getSocketContextData()->flags.usingCustomExpectHandler = value;
624624
return std::move(*this);
625625
}
626626

627627
TemplatedApp &&setRequireHostHeader(bool value) {
628-
httpContext->getSocketContextData()->requireHostHeader = value;
628+
httpContext->getSocketContextData()->flags.requireHostHeader = value;
629629
return std::move(*this);
630630
}
631631

packages/bun-uws/src/HttpContext.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct HttpContext {
7474
if (!us_socket_is_closed(SSL, s) && !us_socket_is_shut_down(SSL, s)) {
7575
HttpContextData<SSL> *httpContextData = getSocketContextDataS(s);
7676

77-
if(httpContextData->rejectUnauthorized) {
77+
if(httpContextData->flags.rejectUnauthorized) {
7878
if(!success || verify_error.error != 0) {
7979
// we failed to handshake, close the socket
8080
us_socket_close(SSL, s, 0, nullptr);
@@ -163,7 +163,7 @@ struct HttpContext {
163163
((AsyncSocket<SSL> *) s)->cork();
164164

165165
/* Mark that we are inside the parser now */
166-
httpContextData->isParsingHttp = true;
166+
httpContextData->flags.isParsingHttp = true;
167167

168168
// clients need to know the cursor after http parse, not servers!
169169
// how far did we read then? we need to know to continue with websocket parsing data? or?
@@ -174,7 +174,7 @@ struct HttpContext {
174174
#endif
175175

176176
/* The return value is entirely up to us to interpret. The HttpParser cares only for whether the returned value is DIFFERENT from passed user */
177-
auto [err, returnedSocket] = httpResponseData->consumePostPadded(httpContextData->requireHostHeader,data, (unsigned int) length, s, proxyParser, [httpContextData](void *s, HttpRequest *httpRequest) -> void * {
177+
auto [err, parserError, returnedSocket] = httpResponseData->consumePostPadded(httpContextData->flags.requireHostHeader,data, (unsigned int) length, s, proxyParser, [httpContextData](void *s, HttpRequest *httpRequest) -> void * {
178178
/* For every request we reset the timeout and hang until user makes action */
179179
/* Warning: if we are in shutdown state, resetting the timer is a security issue! */
180180
us_socket_timeout(SSL, (us_socket_t *) s, 0);
@@ -290,10 +290,14 @@ struct HttpContext {
290290
});
291291

292292
/* Mark that we are no longer parsing Http */
293-
httpContextData->isParsingHttp = false;
293+
httpContextData->flags.isParsingHttp = false;
294294

295295
/* If we got fullptr that means the parser wants us to close the socket from error (same as calling the errorHandler) */
296296
if (returnedSocket == FULLPTR) {
297+
auto onClientError = httpContextData->onClientError;
298+
if(onClientError) {
299+
onClientError(httpResponseData->socketData, SSL, s, parserError, data, length);
300+
}
297301
/* For errors, we only deliver them "at most once". We don't care if they get halfways delivered or not. */
298302
us_socket_write(SSL, s, httpErrorResponses[err].data(), (int) httpErrorResponses[err].length(), false);
299303
us_socket_shutdown(SSL, s);
@@ -467,7 +471,7 @@ struct HttpContext {
467471
/* Init socket context data */
468472
auto* httpContextData = new ((HttpContextData<SSL> *) us_socket_context_ext(SSL, (us_socket_context_t *) httpContext)) HttpContextData<SSL>();
469473
if(options.request_cert && options.reject_unauthorized) {
470-
httpContextData->rejectUnauthorized = true;
474+
httpContextData->flags.rejectUnauthorized = true;
471475
}
472476
return httpContext->init();
473477
}
@@ -515,7 +519,7 @@ struct HttpContext {
515519
}
516520
}
517521

518-
const bool &customContinue = httpContextData->usingCustomExpectHandler;
522+
const bool &customContinue = httpContextData->flags.usingCustomExpectHandler;
519523

520524
httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler), parameterOffsets = std::move(parameterOffsets), &customContinue](auto *r) mutable {
521525
auto user = r->getUserData();

packages/bun-uws/src/HttpContextData.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ namespace uWS {
2727
template<bool> struct HttpResponse;
2828
struct HttpRequest;
2929

30+
struct HttpFlags {
31+
bool isParsingHttp: 1 = false;
32+
bool rejectUnauthorized: 1 = false;
33+
bool usingCustomExpectHandler: 1 = false;
34+
bool requireHostHeader: 1 = true;
35+
};
36+
3037
template <bool SSL>
3138
struct alignas(16) HttpContextData {
3239
template <bool> friend struct HttpContext;
@@ -35,6 +42,7 @@ struct alignas(16) HttpContextData {
3542
private:
3643
std::vector<MoveOnlyFunction<void(HttpResponse<SSL> *, int)>> filterHandlers;
3744
using OnSocketClosedCallback = void (*)(void* userData, int is_ssl, struct us_socket_t *rawSocket);
45+
using OnClientErrorCallback = void (*)(void* userData, int is_ssl, struct us_socket_t *rawSocket, uint8_t errorCode, char *rawPacket, int rawPacketLength);
3846

3947
MoveOnlyFunction<void(const char *hostname)> missingServerNameHandler;
4048

@@ -49,13 +57,11 @@ struct alignas(16) HttpContextData {
4957
/* This is the default router for default SNI or non-SSL */
5058
HttpRouter<RouterData> router;
5159
void *upgradedWebSocket = nullptr;
52-
bool isParsingHttp = false;
53-
bool rejectUnauthorized = false;
54-
bool usingCustomExpectHandler = false;
55-
bool requireHostHeader = true;
56-
5760
/* Used to simulate Node.js socket events. */
5861
OnSocketClosedCallback onSocketClosed = nullptr;
62+
OnClientErrorCallback onClientError = nullptr;
63+
64+
HttpFlags flags;
5965

6066
// TODO: SNI
6167
void clearRoutes() {

0 commit comments

Comments
 (0)