Skip to content

Commit

Permalink
Pull some upstream changes from uWS (#16275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Jan 10, 2025
1 parent 5e584e9 commit cfa4998
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/bun-uws/src/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,9 @@ struct HttpResponse : public AsyncSocket<SSL> {

/* Try and end the response. Returns [true, true] on success.
* Starts a timeout in some cases. Returns [ok, hasResponded] */
std::pair<bool, bool> tryEnd(std::string_view data, uint64_t totalSize = 0, bool closeConnection = false) {
return {internalEnd(data, totalSize, true, true, closeConnection), hasResponded()};
std::pair<bool, bool> tryEnd(std::string_view data, uintmax_t totalSize = 0, bool closeConnection = false) {
bool ok = internalEnd(data, totalSize, true, true, closeConnection);
return {ok, hasResponded()};
}

/* Write the end of chunked encoded stream */
Expand Down
21 changes: 15 additions & 6 deletions packages/bun-uws/src/HttpRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ struct HttpRouter {
std::string segment = std::string(getUrlSegment(i).first);
Node *next = nullptr;
for (std::unique_ptr<Node> &child : n->children) {
if (child->name == segment && child->isHighPriority == (priority == HIGH_PRIORITY)) {
if (((segment.length() && child->name.length() && segment[0] == ':' && child->name[0] == ':') || child->name == segment) && child->isHighPriority == (priority == HIGH_PRIORITY)) {
next = child.get();
break;
}
Expand Down Expand Up @@ -304,12 +304,19 @@ struct HttpRouter {
for (auto &p : root.children) {
if (p->name == method) {
/* Then route the url */
return executeHandlers(p.get(), 0, userData);
if (executeHandlers(p.get(), 0, userData)) {
return true;
} else {
break;
}
}
}

/* We did not find any handler for this method and url */
return false;
/* Always test any route last (this check should not be necessary if we always have at least one handler) */
if (root.children.empty()) [[unlikely]] {
return false;
}
return executeHandlers(root.children.back().get(), 0, userData);
}

/* Adds the corresponding entires in matching tree and handler list */
Expand Down Expand Up @@ -379,11 +386,11 @@ struct HttpRouter {
/* Removes ALL routes with the same handler as can be found with the given parameters.
* Removing a wildcard is done by removing ONE OF the methods the wildcard would match with.
* Example: If wildcard includes POST, GET, PUT, you can remove ALL THREE by removing GET. */
void remove(std::string method, std::string pattern, uint32_t priority) {
bool remove(std::string method, std::string pattern, uint32_t priority) {
uint32_t handler = findHandler(method, pattern, priority);
if (handler == UINT32_MAX) {
/* Not found or already removed, do nothing */
return;
return false;
}

/* Cull the entire tree */
Expand All @@ -394,6 +401,8 @@ struct HttpRouter {

/* Now remove the actual handler */
handlers.erase(handlers.begin() + (handler & HANDLER_MASK));

return true;
}
};

Expand Down

0 comments on commit cfa4998

Please sign in to comment.