Skip to content

Commit 1a1b8a3

Browse files
committed
performance - remove switch and check fro callback
1 parent 44cdf05 commit 1a1b8a3

File tree

5 files changed

+17
-33
lines changed

5 files changed

+17
-33
lines changed

fuzzing/Http.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
135135
/* Return ok */
136136
return user;
137137

138+
}, [](uWS::HttpRequest */*req*/, unsigned int /*errorCode*/) {
138139
});
139140

140141
if (!returnedUser) {

src/HttpContext.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,7 @@ struct HttpContext {
256256
/* Call the high-level error handler if one is registered */
257257
if (httpContextData->httpParsingErrorHandler) {
258258
/* Map internal error codes to HTTP status codes and response bodies */
259-
int statusCode;
260-
switch (errorCode) {
261-
case HTTP_ERROR_505_HTTP_VERSION_NOT_SUPPORTED:
262-
statusCode = 505;
263-
break;
264-
case HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE:
265-
statusCode = 431;
266-
break;
267-
case HTTP_ERROR_400_BAD_REQUEST:
268-
default:
269-
statusCode = 400;
270-
break;
271-
}
272-
httpContextData->httpParsingErrorHandler(httpRequest, statusCode, httpErrorResponses[errorCode]);
259+
httpContextData->httpParsingErrorHandler(httpRequest, httpErrorStatusCodes[errorCode], httpErrorResponses[errorCode]);
273260
}
274261
});
275262

src/HttpErrors.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ enum HttpError {
3030

3131
#ifndef UWS_HTTPRESPONSE_NO_WRITEMARK
3232

33+
static const int httpErrorStatusCodes[] = {
34+
400, /* Zeroth place (unused) */
35+
505, /* HTTP_ERROR_505_HTTP_VERSION_NOT_SUPPORTED */
36+
431, /* HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE */
37+
400 /* HTTP_ERROR_400_BAD_REQUEST */
38+
};
39+
3340
/* Returned parser errors match this LUT. */
3441
static const std::string_view httpErrorResponses[] = {
3542
"", /* Zeroth place is no error so don't use it */

src/HttpParser.h

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ struct HttpParser {
614614
}
615615

616616
public:
617-
std::pair<unsigned int, void *> consumePostPadded(char *data, unsigned int length, void *user, void *reserved, MoveOnlyFunction<void *(void *, HttpRequest *)> &&requestHandler, MoveOnlyFunction<void *(void *, std::string_view, bool)> &&dataHandler, MoveOnlyFunction<void(HttpRequest *, unsigned int)> &&errorHandler = nullptr) {
617+
std::pair<unsigned int, void *> consumePostPadded(char *data, unsigned int length, void *user, void *reserved, MoveOnlyFunction<void *(void *, HttpRequest *)> &&requestHandler, MoveOnlyFunction<void *(void *, std::string_view, bool)> &&dataHandler, MoveOnlyFunction<void(HttpRequest *, unsigned int)> &&errorHandler) {
618618

619619
/* This resets BloomFilter by construction, but later we also reset it again.
620620
* Optimize this to skip resetting twice (req could be made global) */
@@ -629,9 +629,7 @@ struct HttpParser {
629629
dataHandler(user, chunk, chunk.length() == 0);
630630
}
631631
if (isParsingInvalidChunkedEncoding(remainingStreamingBytes)) {
632-
if (errorHandler) {
633-
errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST);
634-
}
632+
errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST);
635633
return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR};
636634
}
637635
data = (char *) dataToConsume.data();
@@ -669,9 +667,7 @@ struct HttpParser {
669667
// break here on break
670668
std::pair<unsigned int, void *> consumed = fenceAndConsumePostPadded<true>(fallback.data(), (unsigned int) fallback.length(), user, reserved, &req, requestHandler, dataHandler);
671669
if (consumed.second != user) {
672-
if (errorHandler) {
673-
errorHandler(&req, consumed.first);
674-
}
670+
errorHandler(&req, consumed.first);
675671
return consumed;
676672
}
677673

@@ -692,9 +688,7 @@ struct HttpParser {
692688
dataHandler(user, chunk, chunk.length() == 0);
693689
}
694690
if (isParsingInvalidChunkedEncoding(remainingStreamingBytes)) {
695-
if (errorHandler) {
696-
errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST);
697-
}
691+
errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST);
698692
return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR};
699693
}
700694
data = (char *) dataToConsume.data();
@@ -722,9 +716,7 @@ struct HttpParser {
722716

723717
} else {
724718
if (fallback.length() == MAX_FALLBACK_SIZE) {
725-
if (errorHandler) {
726-
errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE);
727-
}
719+
errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE);
728720
return {HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE, FULLPTR};
729721
}
730722
return {0, user};
@@ -733,9 +725,7 @@ struct HttpParser {
733725

734726
std::pair<unsigned int, void *> consumed = fenceAndConsumePostPadded<false>(data, length, user, reserved, &req, requestHandler, dataHandler);
735727
if (consumed.second != user) {
736-
if (errorHandler) {
737-
errorHandler(&req, consumed.first);
738-
}
728+
errorHandler(&req, consumed.first);
739729
return consumed;
740730
}
741731

@@ -746,9 +736,7 @@ struct HttpParser {
746736
if (length < MAX_FALLBACK_SIZE) {
747737
fallback.append(data, length);
748738
} else {
749-
if (errorHandler) {
750-
errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE);
751-
}
739+
errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE);
752740
return {HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE, FULLPTR};
753741
}
754742
}

tests/HttpParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int main() {
3131
/* Return ok */
3232
return user;
3333

34+
}, [](uWS::HttpRequest */*req*/, unsigned int /*errorCode*/) {
3435
});
3536

3637
std::cout << "HTTP DONE" << std::endl;

0 commit comments

Comments
 (0)