Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/HttpParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ struct HttpRequest {
return std::string_view(headers->key.data(), headers->key.length());
}

/* Returns whether the request declares a body that can be delivered through onData. */
bool hasBody() {
return getHeader("content-length").length() || getHeader("transfer-encoding").length();
}

/* Returns the raw querystring as a whole, still encoded */
std::string_view getQuery() {
if (querySeparator < headers->value.length()) {
Expand Down
22 changes: 21 additions & 1 deletion tests/HttpParser.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <cassert>
#include <string>

#include "../src/HttpParser.h"

Expand All @@ -16,6 +17,8 @@ int main() {

std::cout << httpRequest->getMethod() << std::endl;

assert(!httpRequest->hasBody());

for (auto [key, value] : *httpRequest) {
std::cout << key << ": " << value << std::endl;
}
Expand All @@ -35,4 +38,21 @@ int main() {

std::cout << "HTTP DONE" << std::endl;

}
std::string bodyRequest = "POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n\r\ntest";
bodyRequest.append(8, '\0');
uWS::HttpParser bodyParser;
auto [bodyErr, bodyUser] = bodyParser.consumePostPadded(
bodyRequest.data(), (unsigned int) bodyRequest.length() - 8, user, reserved,
[](void *user, uWS::HttpRequest *httpRequest) -> void * {
assert(httpRequest->hasBody());
return user;
},
[](void *user, std::string_view data, bool) -> void * {
assert(data == "test");
return user;
}
);
assert(bodyErr == 0);
assert(bodyUser == user);

}