diff --git a/packages/bun-uws/src/HttpResponse.h b/packages/bun-uws/src/HttpResponse.h index a58a86bd6ade5c..e2201f01fbcecd 100644 --- a/packages/bun-uws/src/HttpResponse.h +++ b/packages/bun-uws/src/HttpResponse.h @@ -432,8 +432,9 @@ struct HttpResponse : public AsyncSocket { /* Try and end the response. Returns [true, true] on success. * Starts a timeout in some cases. Returns [ok, hasResponded] */ - std::pair tryEnd(std::string_view data, uint64_t totalSize = 0, bool closeConnection = false) { - return {internalEnd(data, totalSize, true, true, closeConnection), hasResponded()}; + std::pair 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 */ diff --git a/packages/bun-uws/src/HttpRouter.h b/packages/bun-uws/src/HttpRouter.h index cb059cfb26a09f..0215006f846085 100644 --- a/packages/bun-uws/src/HttpRouter.h +++ b/packages/bun-uws/src/HttpRouter.h @@ -256,7 +256,7 @@ struct HttpRouter { std::string segment = std::string(getUrlSegment(i).first); Node *next = nullptr; for (std::unique_ptr &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; } @@ -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 */ @@ -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 */ @@ -394,6 +401,8 @@ struct HttpRouter { /* Now remove the actual handler */ handlers.erase(handlers.begin() + (handler & HANDLER_MASK)); + + return true; } };