formatting
This commit is contained in:
parent
72d6651ded
commit
1e8c421d66
@ -129,24 +129,27 @@ 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 = "";
|
std::string body = "";
|
||||||
if (headers.find("Content-Length") != headers.end()){
|
if (headers.find("Content-Length") != headers.end())
|
||||||
|
{
|
||||||
int contentLength = 0;
|
int contentLength = 0;
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
contentLength = std::stoi(headers["Content-Length"]);
|
contentLength = std::stoi(headers["Content-Length"]);
|
||||||
}
|
}
|
||||||
catch (std::exception){
|
catch (std::exception)
|
||||||
return std::make_tuple(false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
|
{
|
||||||
|
return std::make_tuple(
|
||||||
|
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
body.reserve(contentLength);
|
body.reserve(contentLength);
|
||||||
|
|
||||||
for (int i = 0; i < contentLength; i++){
|
for (int i = 0; i < contentLength; i++)
|
||||||
if (socket->readByte(&c, isCancellationRequested))
|
{
|
||||||
body += c;
|
if (socket->readByte(&c, isCancellationRequested)) body += c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,14 +69,17 @@ TEST_CASE("http server", "[httpd]")
|
|||||||
int port = getFreePort();
|
int port = getFreePort();
|
||||||
ix::HttpServer server(port, "127.0.0.1");
|
ix::HttpServer server(port, "127.0.0.1");
|
||||||
|
|
||||||
server.setOnConnectionCallback([](HttpRequestPtr request, std::shared_ptr<ConnectionState>) -> HttpResponsePtr {
|
server.setOnConnectionCallback(
|
||||||
if (request->method == "POST"){
|
[](HttpRequestPtr request, std::shared_ptr<ConnectionState>) -> HttpResponsePtr {
|
||||||
return std::make_shared<HttpResponse>(200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), request->body);
|
if (request->method == "POST")
|
||||||
}
|
{
|
||||||
|
return std::make_shared<HttpResponse>(
|
||||||
return std::make_shared<HttpResponse>(400, "BAD REQUEST");
|
200, "OK", HttpErrorCode::Ok, WebSocketHttpHeaders(), request->body);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
return std::make_shared<HttpResponse>(400, "BAD REQUEST");
|
||||||
|
});
|
||||||
|
|
||||||
auto res = server.listen();
|
auto res = server.listen();
|
||||||
REQUIRE(res.first);
|
REQUIRE(res.first);
|
||||||
server.start();
|
server.start();
|
||||||
@ -97,7 +100,7 @@ TEST_CASE("http server", "[httpd]")
|
|||||||
args->body = "Hello World!";
|
args->body = "Hello World!";
|
||||||
|
|
||||||
auto response = httpClient.post(url, args->body, args);
|
auto response = httpClient.post(url, args->body, args);
|
||||||
|
|
||||||
std::cerr << "Status: " << response->statusCode << std::endl;
|
std::cerr << "Status: " << response->statusCode << std::endl;
|
||||||
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
std::cerr << "Error message: " << response->errorMsg << std::endl;
|
||||||
std::cerr << "Payload: " << response->payload << std::endl;
|
std::cerr << "Payload: " << response->payload << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user