Fixes #114: added support for async client requests. Also includes some cleanup, support for parallel requests, and client reconnect

This commit is contained in:
eidheim 2017-06-14 15:16:24 +02:00
commit e1aebba344
4 changed files with 471 additions and 213 deletions

View file

@ -33,6 +33,12 @@ int main() {
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
};
server.resource["^/header$"]["GET"]=[](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
auto content=request->header.find("test1")->second+request->header.find("test2")->second;
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
};
thread server_thread([&server](){
//Start server
server.start();
@ -89,6 +95,50 @@ int main() {
output << r->content.rdbuf();
assert(output.str()=="A string");
}
{
stringstream output;
auto r=client.request("GET", "/header", "", {{"test1", "test"}, {"test2", "ing"}});
output << r->content.rdbuf();
assert(output.str()=="testing");
}
}
{
HttpClient client("localhost:8080");
bool call=false;
client.request("GET", "/match/123", [&call](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
assert(!ec);
stringstream output;
output << response->content.rdbuf();
assert(output.str()=="123");
call=true;
});
client.io_service->run();
assert(call);
{
vector<bool> calls(100);
vector<thread> threads;
for(size_t c=0;c<100;++c) {
calls[c]=false;
threads.emplace_back([c, &client, &calls] {
client.request("GET", "/match/123", [c, &calls](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
assert(!ec);
stringstream output;
output << response->content.rdbuf();
assert(output.str()=="123");
calls[c]=true;
});
});
}
for(auto &thread: threads)
thread.join();
client.io_service->reset();
client.io_service->run();
for(auto call: calls)
assert(call);
}
}
server.stop();