-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add callback handler to report internal HTTP parsing errors (431, 400, 505) #1891
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
Open
pdeltour-syna
wants to merge
3
commits into
uNetworking:master
Choose a base branch
from
pdeltour-syna:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -489,6 +489,9 @@ struct HttpParser { | |
| data += consumed; | ||
| length -= consumed; | ||
| consumedTotal += consumed; | ||
| /* Parse query */ | ||
| const char *querySeparatorPtr = (const char *) memchr(req->headers->value.data(), '?', req->headers->value.length()); | ||
| req->querySeparator = (unsigned int) ((querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data()); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved this code, because HttpRequest::getUrl() requires querySeparator to be set, the below return on error will call the new callback handler and this allows to get the URL of the failed request for these cases |
||
|
|
||
| /* Even if we could parse it, check for length here as well */ | ||
| if (consumed > MAX_FALLBACK_SIZE) { | ||
|
|
@@ -529,10 +532,6 @@ struct HttpParser { | |
| return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR}; | ||
| } | ||
|
|
||
| /* Parse query */ | ||
| const char *querySeparatorPtr = (const char *) memchr(req->headers->value.data(), '?', req->headers->value.length()); | ||
| req->querySeparator = (unsigned int) ((querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data()); | ||
|
|
||
| /* If returned socket is not what we put in we need | ||
| * to break here as we either have upgraded to | ||
| * WebSockets or otherwise closed the socket. */ | ||
|
|
@@ -615,7 +614,7 @@ struct HttpParser { | |
| } | ||
|
|
||
| public: | ||
| 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) { | ||
| 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) { | ||
|
|
||
| /* This resets BloomFilter by construction, but later we also reset it again. | ||
| * Optimize this to skip resetting twice (req could be made global) */ | ||
|
|
@@ -630,6 +629,9 @@ struct HttpParser { | |
| dataHandler(user, chunk, chunk.length() == 0); | ||
| } | ||
| if (isParsingInvalidChunkedEncoding(remainingStreamingBytes)) { | ||
| if (errorHandler) { | ||
| errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST); | ||
| } | ||
| return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR}; | ||
| } | ||
| data = (char *) dataToConsume.data(); | ||
|
|
@@ -667,6 +669,9 @@ struct HttpParser { | |
| // break here on break | ||
| std::pair<unsigned int, void *> consumed = fenceAndConsumePostPadded<true>(fallback.data(), (unsigned int) fallback.length(), user, reserved, &req, requestHandler, dataHandler); | ||
| if (consumed.second != user) { | ||
| if (errorHandler) { | ||
| errorHandler(&req, consumed.first); | ||
| } | ||
| return consumed; | ||
| } | ||
|
|
||
|
|
@@ -687,6 +692,9 @@ struct HttpParser { | |
| dataHandler(user, chunk, chunk.length() == 0); | ||
| } | ||
| if (isParsingInvalidChunkedEncoding(remainingStreamingBytes)) { | ||
| if (errorHandler) { | ||
| errorHandler(&req, HTTP_ERROR_400_BAD_REQUEST); | ||
| } | ||
| return {HTTP_ERROR_400_BAD_REQUEST, FULLPTR}; | ||
| } | ||
| data = (char *) dataToConsume.data(); | ||
|
|
@@ -714,6 +722,9 @@ struct HttpParser { | |
|
|
||
| } else { | ||
| if (fallback.length() == MAX_FALLBACK_SIZE) { | ||
| if (errorHandler) { | ||
| errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE); | ||
| } | ||
| return {HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE, FULLPTR}; | ||
| } | ||
| return {0, user}; | ||
|
|
@@ -722,6 +733,9 @@ struct HttpParser { | |
|
|
||
| std::pair<unsigned int, void *> consumed = fenceAndConsumePostPadded<false>(data, length, user, reserved, &req, requestHandler, dataHandler); | ||
| if (consumed.second != user) { | ||
| if (errorHandler) { | ||
| errorHandler(&req, consumed.first); | ||
| } | ||
| return consumed; | ||
| } | ||
|
|
||
|
|
@@ -732,6 +746,9 @@ struct HttpParser { | |
| if (length < MAX_FALLBACK_SIZE) { | ||
| fallback.append(data, length); | ||
| } else { | ||
| if (errorHandler) { | ||
| errorHandler(&req, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE); | ||
| } | ||
| return {HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE, FULLPTR}; | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly this one needs a better name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error is a bad name. People will register it and think they need to handle it in some way. That's common for other libs. It sucks.
Log or logging could be used like App::log that simply gives you information of happenings that you don't need to handle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
App::log could get things like
Neither of these events need handling, they just need logging, so App::log makes more sense than anything containing "error".
One of the main benefits of the uWS interface is that error handling and regular close is the same handler, meaning the same business flow. This is different from other libs that have open/error, open/close as two different flows of logic. That sucks, and it makes the app more complex than it has to be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially looked at using App:filter, but it seemed more like a socket level notification. Also the info I would like to collect on unhandled http requests is :
I agree that there could be a better naming for the callback handler. e.g. App::log (or App::logEvent ?)
I don't mind combining HTTP error / connection open / connection close into a single callback, where there could be even more events in the future (but maybe that would open the door for overpopulating this with events). On the other hand the information returned for connection open/close and HTTP error is completely different.
How could the function signature look like then ?
App::log(int eventType, HttpRequest *req, int statusCode, string_view responseBody)
where eventType:
Alternatively when not combining the connection open/close with the unhandled request, the handler could be called something like : App::unhandledReq