Read body in parseRequest for HttpServer (#244)

Co-authored-by: Jay <jasoncarr@Jasons-MacBook-Pro.local>
This commit is contained in:
carr-7 2020-09-12 21:53:56 +01:00 committed by GitHub
parent a4e5d1b47a
commit 72d6651ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 1 deletions

View File

@ -129,8 +129,28 @@ namespace ix
{ {
return std::make_tuple(false, "Error parsing HTTP headers", httpRequest); return std::make_tuple(false, "Error parsing HTTP headers", httpRequest);
} }
std::string body = "";
if (headers.find("Content-Length") != headers.end()){
int contentLength = 0;
try {
contentLength = std::stoi(headers["Content-Length"]);
}
catch (std::exception){
return std::make_tuple(false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
}
char c;
body.reserve(contentLength);
for (int i = 0; i < contentLength; i++){
if (socket->readByte(&c, isCancellationRequested))
body += c;
}
}
httpRequest = std::make_shared<HttpRequest>(uri, method, httpVersion, headers); httpRequest = std::make_shared<HttpRequest>(uri, method, httpVersion, body, headers);
return std::make_tuple(true, "", httpRequest); return std::make_tuple(true, "", httpRequest);
} }

View File

@ -95,15 +95,18 @@ namespace ix
std::string uri; std::string uri;
std::string method; std::string method;
std::string version; std::string version;
std::string body;
WebSocketHttpHeaders headers; WebSocketHttpHeaders headers;
HttpRequest(const std::string& u, HttpRequest(const std::string& u,
const std::string& m, const std::string& m,
const std::string& v, const std::string& v,
const std::string& b,
const WebSocketHttpHeaders& h = WebSocketHttpHeaders()) const WebSocketHttpHeaders& h = WebSocketHttpHeaders())
: uri(u) : uri(u)
, method(m) , method(m)
, version(v) , version(v)
, body(b)
, headers(h) , headers(h)
{ {
} }

View File

@ -63,6 +63,51 @@ TEST_CASE("http server", "[httpd]")
server.stop(); server.stop();
} }
SECTION("Posting plain text data to a local HTTP server")
{
int port = getFreePort();
ix::HttpServer server(port, "127.0.0.1");
server.setOnConnectionCallback([](HttpRequestPtr request, std::shared_ptr<ConnectionState>) -> HttpResponsePtr {
if (request->method == "POST"){
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), request->body);
}
return std::make_shared<HttpResponse>(400, "BAD REQUEST");
});
auto res = server.listen();
REQUIRE(res.first);
server.start();
HttpClient httpClient;
WebSocketHttpHeaders headers;
headers["Content-Type"] = "text/plain";
std::string url("http://127.0.0.1:");
url += std::to_string(port);
auto args = httpClient.createRequest(url);
args->extraHeaders = headers;
args->connectTimeout = 60;
args->transferTimeout = 60;
args->verbose = true;
args->logger = [](const std::string& msg) { std::cout << msg; };
args->body = "Hello World!";
auto response = httpClient.post(url, args->body, args);
std::cerr << "Status: " << response->statusCode << std::endl;
std::cerr << "Error message: " << response->errorMsg << std::endl;
std::cerr << "Payload: " << response->payload << std::endl;
REQUIRE(response->errorCode == HttpErrorCode::Ok);
REQUIRE(response->statusCode == 200);
REQUIRE(response->payload == args->body);
server.stop();
}
} }
TEST_CASE("http server redirection", "[httpd_redirect]") TEST_CASE("http server redirection", "[httpd_redirect]")