Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 80 additions & 78 deletions src/core/uritemplate/uritemplate_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,19 @@ auto URITemplateRouter::add(const std::string_view uri_template,
throw URITemplateRouterDuplicateOperationIdError{operation_id};
}

// Walk base path segments to establish the trie prefix
if (!uri_template.empty() && uri_template.front() != '/' &&
!(uri_template.size() >= 2 && uri_template[0] == '{' &&
uri_template[1] == '/')) {
throw URITemplateRouterInvalidSegmentError{"Template must start with '/'",
uri_template};
}

Node *current = nullptr;
if (!this->base_path_.empty()) {
const char *base_position = this->base_path_.data();
const char *const base_end = base_position + this->base_path_.size();
while (base_position < base_end) {
while (base_position < base_end && *base_position == '/') {
++base_position;
}
if (base_position >= base_end) {
break;
}
const char *base_position = this->base_path_.data() + 1;
const char *const base_end =
this->base_path_.data() + this->base_path_.size();
while (true) {
const char *segment_start = base_position;
while (base_position < base_end && *base_position != '/') {
++base_position;
Expand All @@ -262,6 +263,10 @@ auto URITemplateRouter::add(const std::string_view uri_template,
static_cast<std::size_t>(base_position - segment_start)};
auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, segment);
if (base_position >= base_end) {
break;
}
++base_position;
}
}

Expand Down Expand Up @@ -300,18 +305,23 @@ auto URITemplateRouter::add(const std::string_view uri_template,
return;
}

Node *base_path_end = current;
bool absorbed = false;
const char *position = uri_template.data();
const char *const end = position + uri_template.size();

while (position < end && !absorbed) {
while (position < end && *position == '/') {
++position;
}
if (position < end && *position == '/') {
++position;
}

if (position >= end) {
break;
while (true) {
if (position >= end || *position == '/') {
auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, "");
if (position >= end) {
break;
}
++position;
continue;
}

const char *segment_start = position;
Expand Down Expand Up @@ -425,14 +435,16 @@ auto URITemplateRouter::add(const std::string_view uri_template,
const std::string_view varname{
varname_start, static_cast<std::size_t>(varname_end - varname_start)};

++position; // skip '}'
++position;

if (position < end && *position != '/') {
if (*position != '{' || position + 1 >= end || *(position + 1) != '/') {
throw URITemplateRouterInvalidSegmentError{
"Path segment cannot mix literals and variables",
extract_segment(expression_start, end)};
}
const bool followed_by_path_operator =
position < end && *position == '{' && position + 1 < end &&
*(position + 1) == '/';

if (position < end && *position != '/' && !followed_by_path_operator) {
throw URITemplateRouterInvalidSegmentError{
"Path segment cannot mix literals and variables",
extract_segment(expression_start, end)};
}

if (is_expansion_type(type) && position < end) {
Expand All @@ -448,47 +460,54 @@ auto URITemplateRouter::add(const std::string_view uri_template,
} else {
current = result;
}
} else {
while (position < end && *position != '/' && *position != '{') {
if (*position == '}') {
throw URITemplateRouterInvalidSegmentError{
"Unmatched closing brace", extract_segment(segment_start, end)};
}
++position;

if (absorbed || position >= end) {
break;
}
if (followed_by_path_operator) {
continue;
}
++position;
continue;
}

if (position < end && *position == '{') {
if (position + 1 < end && *(position + 1) == '/') {
const std::string_view segment{
segment_start,
static_cast<std::size_t>(position - segment_start)};
auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, segment);
continue;
}
const char *expr_end = find_expression_end(position, end);
const char *seg_end = expr_end;
while (seg_end < end && *seg_end != '/') {
++seg_end;
}
while (position < end && *position != '/' && *position != '{') {
if (*position == '}') {
throw URITemplateRouterInvalidSegmentError{
"Path segment cannot mix literals and variables",
std::string_view{segment_start, static_cast<std::size_t>(
seg_end - segment_start)}};
"Unmatched closing brace", extract_segment(segment_start, end)};
}
++position;
}

const std::string_view segment{
segment_start, static_cast<std::size_t>(position - segment_start)};

auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, segment);
if (position < end && *position == '{') {
if (position + 1 < end && *(position + 1) == '/') {
const std::string_view segment{
segment_start, static_cast<std::size_t>(position - segment_start)};
auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, segment);
continue;
}
const char *expr_end = find_expression_end(position, end);
const char *seg_end = expr_end;
while (seg_end < end && *seg_end != '/') {
++seg_end;
}
throw URITemplateRouterInvalidSegmentError{
"Path segment cannot mix literals and variables",
std::string_view{segment_start,
static_cast<std::size_t>(seg_end - segment_start)}};
}
}

if (current == base_path_end && uri_template.size() == 1 &&
uri_template[0] == '/') {
const std::string_view segment{
segment_start, static_cast<std::size_t>(position - segment_start)};

auto &literals = current ? current->literals : this->root_.literals;
current = &find_or_create_literal_child(literals, "");
current = &find_or_create_literal_child(literals, segment);

if (position >= end) {
break;
}
++position;
}

if (!absorbed && current != nullptr) {
Expand Down Expand Up @@ -555,29 +574,20 @@ auto URITemplateRouter::match(const std::string_view path,
this->root_.context);
}

if (path.size() == 1 && path[0] == '/') {
if (auto *child = find_literal_child(this->root_.literals, "")) {
return finalize_match(this->otherwise_, child->identifier,
child->context);
}
if (path.front() != '/') {
return finalize_match(this->otherwise_, 0, 0);
}

const Node *current = nullptr;
const char *position = path.data();
const char *const path_end = position + path.size();
const char *position = path.data() + 1;
const char *const path_end = path.data() + path.size();

const std::vector<std::unique_ptr<Node>> *literal_children =
&this->root_.literals;
const std::unique_ptr<Node> *variable_child = &this->root_.variable;

std::size_t variable_index = 0;

// Skip leading slash
if (position < path_end && *position == '/') {
++position;
}

while (true) {
const char *segment_start = position;
while (position < path_end && *position != '/') {
Expand All @@ -586,14 +596,9 @@ auto URITemplateRouter::match(const std::string_view path,
const std::string_view segment{
segment_start, static_cast<std::size_t>(position - segment_start)};

// Empty segment (from double slash or trailing slash) doesn't match
if (segment.empty()) {
return finalize_match(this->otherwise_, 0, 0);
}

if (auto *literal_match = find_literal_child(*literal_children, segment)) {
current = literal_match;
} else if (*variable_child) {
} else if (!segment.empty() && *variable_child) {
assert(variable_index <=
std::numeric_limits<URITemplateRouter::Index>::max());
if (is_expansion_type((*variable_child)->type)) {
Expand All @@ -615,12 +620,9 @@ auto URITemplateRouter::match(const std::string_view path,
literal_children = &current->literals;
variable_child = &current->variable;

// Check if there's more path
if (position >= path_end) {
break;
}

// Skip the slash and continue to next segment
++position;
}

Expand Down
75 changes: 15 additions & 60 deletions src/core/uritemplate/uritemplate_router_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -497,87 +497,46 @@ auto URITemplateRouterView::match(
const auto string_table_size =
header->arguments_offset - header->string_table_offset;

// Empty path matches empty template
if (path.empty()) {
return finalize_match(otherwise_context, nodes[0].identifier,
nodes[0].context);
}

// Root path "/" is stored as an empty literal segment
if (path.size() == 1 && path[0] == '/') {
const auto &root = nodes[0];
if (root.first_literal_child == NO_CHILD) {
return finalize_match(otherwise_context, 0, 0);
}

if (root.first_literal_child >= header->node_count ||
root.literal_child_count >
header->node_count - root.first_literal_child) {
return finalize_match(otherwise_context, 0, 0);
}

const auto match = binary_search_literal_children(
nodes, string_table, string_table_size, root.first_literal_child,
root.literal_child_count, "", 0);
if (match == NO_CHILD) {
return finalize_match(otherwise_context, 0, 0);
}
return finalize_match(otherwise_context, nodes[match].identifier,
nodes[match].context);
if (path.front() != '/') {
return finalize_match(otherwise_context, 0, 0);
}

// Walk the trie, matching each path segment
std::uint32_t current_node = 0;
const char *position = path.data();
const char *const path_end = position + path.size();
const char *position = path.data() + 1;
const char *const path_end = path.data() + path.size();
Comment on lines +505 to +511
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This introduces a behavioral regression by rejecting non-leading-slash paths that were previously matchable. Keep optional leading-slash handling unless the API contract was intentionally tightened.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/core/uritemplate/uritemplate_router_view.cc, line 505:

<comment>This introduces a behavioral regression by rejecting non-leading-slash paths that were previously matchable. Keep optional leading-slash handling unless the API contract was intentionally tightened.</comment>

<file context>
@@ -497,102 +497,46 @@ auto URITemplateRouterView::match(
-    }
-    return finalize_match(otherwise_context, nodes[match].identifier,
-                          nodes[match].context);
+  if (path.front() != '/') {
+    return finalize_match(otherwise_context, 0, 0);
   }
</file context>
Suggested change
if (path.front() != '/') {
return finalize_match(otherwise_context, 0, 0);
}
// Walk the trie, matching each path segment
std::uint32_t current_node = 0;
const char *position = path.data();
const char *const path_end = position + path.size();
const char *position = path.data() + 1;
const char *const path_end = path.data() + path.size();
const char *position = path.data();
const char *const path_end = path.data() + path.size();
if (position < path_end && *position == '/') {
++position;
}
std::uint32_t current_node = 0;


std::size_t variable_index = 0;

// Skip leading slash
if (position < path_end && *position == '/') {
++position;
}

while (true) {
// Extract segment
const char *segment_start = position;
while (position < path_end && *position != '/') {
++position;
}

const auto segment_length =
static_cast<std::uint32_t>(position - segment_start);

// Empty segment (from double slash or trailing slash) doesn't match
if (segment_length == 0) {
return finalize_match(otherwise_context, 0, 0);
}

const auto &node = nodes[current_node];
const auto node_count = header->node_count;

// Try literal children first
std::uint32_t literal_match = NO_CHILD;
if (node.first_literal_child != NO_CHILD) {
if (node.first_literal_child >= node_count ||
node.literal_child_count > node_count - node.first_literal_child) {
return finalize_match(otherwise_context, 0, 0);
}

const auto literal_match = binary_search_literal_children(
literal_match = binary_search_literal_children(
nodes, string_table, string_table_size, node.first_literal_child,
node.literal_child_count, segment_start, segment_length);
if (literal_match != NO_CHILD) {
current_node = literal_match;
if (position >= path_end) {
break;
}
++position;
continue;
}
}

// Fall back to variable child
if (node.variable_child != NO_CHILD) {
if (literal_match != NO_CHILD) {
current_node = literal_match;
} else if (segment_length > 0 && node.variable_child != NO_CHILD) {
if (node.variable_child >= node_count ||
variable_index >
std::numeric_limits<URITemplateRouter::Index>::max()) {
Expand All @@ -592,8 +551,6 @@ auto URITemplateRouterView::match(
return finalize_match(otherwise_context, 0, 0);
}

// Both Expansion and OptionalExpansion consume the rest of the path
// verbatim
if (is_expansion_type(variable_node.type)) {
const auto remaining_length =
static_cast<std::uint32_t>(path_end - segment_start);
Expand All @@ -605,22 +562,20 @@ auto URITemplateRouterView::match(
variable_node.context);
}

// Regular variable - match single segment
callback(static_cast<URITemplateRouter::Index>(variable_index),
{string_table + variable_node.string_offset,
variable_node.string_length},
{segment_start, segment_length});
++variable_index;
current_node = node.variable_child;
if (position >= path_end) {
break;
}
++position;
continue;
} else {
return finalize_match(otherwise_context, 0, 0);
}

// No match
return finalize_match(otherwise_context, 0, 0);
if (position >= path_end) {
break;
}
++position;
}

const auto &final_node = nodes[current_node];
Expand Down
Loading
Loading