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
4 changes: 2 additions & 2 deletions src/HttpParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ struct HttpParser {

/* Puts method as key, target as value and returns non-null (or nullptr on error). */
static inline char *consumeRequestLine(char *data, char *end, HttpRequest::Header &header) {
/* Scan until single SP, assume next is / (origin request) */
/* Scan until single SP, assume next is / (origin request), or * for OPTIONS (asterisk form) */
char *start = data;
/* This catches the post padded CR and fails */
while (data[0] > 32) data++;
if (&data[1] == end) [[unlikely]] {
return nullptr;
}
if (data[0] == 32 && data[1] == '/') [[likely]] {
if (data[0] == 32 && (data[1] == '/' || (data[1] == '*' && data[2] == 32 && data - start == 7 && !memcmp(start, "OPTIONS", 7)))) [[likely]] {
header.key = {start, (size_t) (data - start)};
data++;
/* Scan for less than 33 (catches post padded CR and fails) or more than 126 (not ASCII, fails) */
Expand Down
23 changes: 23 additions & 0 deletions tests/HttpParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,27 @@ int main() {
return 1;
}
}

/* RFC 9112 3.2.4: the asterisk form is only for OPTIONS, the app gets "*" as URL */
for (std::string method : {"OPTIONS", "GET"}) {
std::string req = method + " * HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n";
unsigned int length = (unsigned int) req.size();
req.append(32, 'E');

std::string url;
void *starUser = (void *) 13;
uWS::HttpParser starParser;
auto [starErr, starReturned] = starParser.consumePostPadded(req.data(), length, starUser, nullptr, [&url](void *s, uWS::HttpRequest *httpRequest) -> void * {
url = httpRequest->getUrl();
return s;
}, [](void *s, std::string_view, bool) -> void * {
return s;
});

bool accepted = starReturned == starUser && url == "*";
if (accepted != (method == "OPTIONS")) {
std::cerr << method << " * expected " << (method == "OPTIONS" ? "accept" : "400") << ", got err=" << starErr << std::endl;
return 1;
}
}
}
Loading