Client: added support for server-sent events, and improved the chunked transfer code. Also some cleanup of especially the client code, and added a few additional tests.
This commit is contained in:
parent
6097d86cb5
commit
b1d9476ff1
3 changed files with 228 additions and 86 deletions
145
client_http.hpp
145
client_http.hpp
|
|
@ -88,10 +88,17 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
std::unique_ptr<socket_type> socket; // Socket must be unique_ptr since asio::ssl::stream<asio::ip::tcp::socket> is not movable
|
std::unique_ptr<socket_type> socket; // Socket must be unique_ptr since asio::ssl::stream<asio::ip::tcp::socket> is not movable
|
||||||
bool in_use = false;
|
bool in_use = false;
|
||||||
|
bool event_stream = false;
|
||||||
bool attempt_reconnect = true;
|
bool attempt_reconnect = true;
|
||||||
|
|
||||||
std::unique_ptr<asio::steady_timer> timer;
|
std::unique_ptr<asio::steady_timer> timer;
|
||||||
|
|
||||||
|
void close() noexcept {
|
||||||
|
error_code ec;
|
||||||
|
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
|
socket->lowest_layer().cancel(ec);
|
||||||
|
}
|
||||||
|
|
||||||
void set_timeout(long seconds = 0) noexcept {
|
void set_timeout(long seconds = 0) noexcept {
|
||||||
if(seconds == 0)
|
if(seconds == 0)
|
||||||
seconds = timeout;
|
seconds = timeout;
|
||||||
|
|
@ -103,10 +110,8 @@ namespace SimpleWeb {
|
||||||
std::weak_ptr<Connection> self_weak(this->shared_from_this()); // To avoid keeping Connection instance alive longer than needed
|
std::weak_ptr<Connection> self_weak(this->shared_from_this()); // To avoid keeping Connection instance alive longer than needed
|
||||||
timer->async_wait([self_weak](const error_code &ec) {
|
timer->async_wait([self_weak](const error_code &ec) {
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
if(auto self = self_weak.lock()) {
|
if(auto self = self_weak.lock())
|
||||||
error_code ec;
|
self->close();
|
||||||
self->socket->lowest_layer().cancel(ec);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -144,8 +149,8 @@ namespace SimpleWeb {
|
||||||
/// Convenience function to perform synchronous request. The io_service is run within this function.
|
/// Convenience function to perform synchronous request. The io_service is run within this function.
|
||||||
/// If reusing the io_service for other tasks, use the asynchronous request functions instead.
|
/// If reusing the io_service for other tasks, use the asynchronous request functions instead.
|
||||||
/// Do not use concurrently with the asynchronous request functions.
|
/// Do not use concurrently with the asynchronous request functions.
|
||||||
std::shared_ptr<Response> request(const std::string &method, const std::string &path = std::string("/"),
|
/// When requesting Server-Sent Events: will throw on asio::error::eof, please use asynchronous request functions instead.
|
||||||
string_view content = "", const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
std::shared_ptr<Response> request(const std::string &method, const std::string &path = {"/"}, string_view content = {}, const CaseInsensitiveMultimap &header = {}) {
|
||||||
std::shared_ptr<Response> response;
|
std::shared_ptr<Response> response;
|
||||||
error_code ec;
|
error_code ec;
|
||||||
request(method, path, content, header, [&response, &ec](std::shared_ptr<Response> response_, const error_code &ec_) {
|
request(method, path, content, header, [&response, &ec](std::shared_ptr<Response> response_, const error_code &ec_) {
|
||||||
|
|
@ -174,8 +179,8 @@ namespace SimpleWeb {
|
||||||
/// Convenience function to perform synchronous request. The io_service is run within this function.
|
/// Convenience function to perform synchronous request. The io_service is run within this function.
|
||||||
/// If reusing the io_service for other tasks, use the asynchronous request functions instead.
|
/// If reusing the io_service for other tasks, use the asynchronous request functions instead.
|
||||||
/// Do not use concurrently with the asynchronous request functions.
|
/// Do not use concurrently with the asynchronous request functions.
|
||||||
std::shared_ptr<Response> request(const std::string &method, const std::string &path, std::istream &content,
|
/// When requesting Server-Sent Events: will throw on asio::error::eof, please use asynchronous request functions instead.
|
||||||
const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
std::shared_ptr<Response> request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header = {}) {
|
||||||
std::shared_ptr<Response> response;
|
std::shared_ptr<Response> response;
|
||||||
error_code ec;
|
error_code ec;
|
||||||
request(method, path, content, header, [&response, &ec](std::shared_ptr<Response> response_, const error_code &ec_) {
|
request(method, path, content, header, [&response, &ec](std::shared_ptr<Response> response_, const error_code &ec_) {
|
||||||
|
|
@ -203,6 +208,7 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
/// Do not use concurrently with the synchronous request functions.
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(config.max_response_streambuf_size, get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(config.max_response_streambuf_size, get_connection(), create_request_header(method, path, header));
|
||||||
|
|
@ -212,6 +218,7 @@ namespace SimpleWeb {
|
||||||
if(auto session = session_weak.lock()) {
|
if(auto session = session_weak.lock()) {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
||||||
|
if(!session->connection->event_stream)
|
||||||
session->connection->in_use = false;
|
session->connection->in_use = false;
|
||||||
|
|
||||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||||
|
|
@ -245,31 +252,38 @@ namespace SimpleWeb {
|
||||||
write_stream << "Content-Length: " << content.size() << "\r\n";
|
write_stream << "Content-Length: " << content.size() << "\r\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
write_stream << "\r\n"
|
write_stream << "\r\n";
|
||||||
<< content;
|
write_stream.write(content.data(), static_cast<ssize_t>(content.size()));
|
||||||
|
|
||||||
connect(session);
|
connect(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
/// Do not use concurrently with the synchronous request functions.
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, const std::string &path, string_view content,
|
void request(const std::string &method, const std::string &path, string_view content,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback_));
|
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback_));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, const std::string &path,
|
void request(const std::string &method, const std::string &path,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
request(method, path, std::string(), CaseInsensitiveMultimap(), std::move(request_callback_));
|
request(method, path, std::string(), CaseInsensitiveMultimap(), std::move(request_callback_));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
void request(const std::string &method, std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
request(method, std::string("/"), std::string(), CaseInsensitiveMultimap(), std::move(request_callback_));
|
request(method, std::string("/"), std::string(), CaseInsensitiveMultimap(), std::move(request_callback_));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(config.max_response_streambuf_size, get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(config.max_response_streambuf_size, get_connection(), create_request_header(method, path, header));
|
||||||
|
|
@ -279,6 +293,7 @@ namespace SimpleWeb {
|
||||||
if(auto session = session_weak.lock()) {
|
if(auto session = session_weak.lock()) {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
||||||
|
if(!session->connection->event_stream)
|
||||||
session->connection->in_use = false;
|
session->connection->in_use = false;
|
||||||
|
|
||||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||||
|
|
@ -323,6 +338,8 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
/// Do not use concurrently with the synchronous request functions.
|
||||||
|
/// When requesting Server-Sent Events: request_callback might be called more than twice, first call with empty contents on open, and with ec = asio::error::eof on last call
|
||||||
void request(const std::string &method, const std::string &path, std::istream &content,
|
void request(const std::string &method, const std::string &path, std::istream &content,
|
||||||
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) {
|
||||||
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback_));
|
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback_));
|
||||||
|
|
@ -332,8 +349,7 @@ namespace SimpleWeb {
|
||||||
void stop() noexcept {
|
void stop() noexcept {
|
||||||
std::lock_guard<std::mutex> lock(connections_mutex);
|
std::lock_guard<std::mutex> lock(connections_mutex);
|
||||||
for(auto it = connections.begin(); it != connections.end();) {
|
for(auto it = connections.begin(); it != connections.end();) {
|
||||||
error_code ec;
|
(*it)->close();
|
||||||
(*it)->socket->lowest_layer().cancel(ec);
|
|
||||||
it = connections.erase(it);
|
it = connections.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -457,10 +473,11 @@ namespace SimpleWeb {
|
||||||
auto lock = session->connection->handler_runner->continue_lock();
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
if(!lock)
|
if(!lock)
|
||||||
return;
|
return;
|
||||||
if((!ec || ec == asio::error::not_found) && session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
session->connection->attempt_reconnect = true;
|
session->connection->attempt_reconnect = true;
|
||||||
std::size_t num_additional_bytes = session->response->streambuf.size() - bytes_transferred;
|
std::size_t num_additional_bytes = session->response->streambuf.size() - bytes_transferred;
|
||||||
|
|
@ -480,13 +497,13 @@ namespace SimpleWeb {
|
||||||
auto lock = session->connection->handler_runner->continue_lock();
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
if(!lock)
|
if(!lock)
|
||||||
return;
|
return;
|
||||||
if(!ec) {
|
|
||||||
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!ec)
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
});
|
});
|
||||||
|
|
@ -496,6 +513,14 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
else if((header_it = session->response->header.find("Transfer-Encoding")) != session->response->header.end() && header_it->second == "chunked") {
|
else if((header_it = session->response->header.find("Transfer-Encoding")) != session->response->header.end() && header_it->second == "chunked") {
|
||||||
auto chunks_streambuf = std::make_shared<asio::streambuf>(this->config.max_response_streambuf_size);
|
auto chunks_streambuf = std::make_shared<asio::streambuf>(this->config.max_response_streambuf_size);
|
||||||
|
|
||||||
|
// Copy leftover bytes
|
||||||
|
std::ostream ostream(chunks_streambuf.get());
|
||||||
|
auto size = session->response->streambuf.size();
|
||||||
|
std::unique_ptr<char[]> buffer(new char[size]);
|
||||||
|
session->response->content.read(buffer.get(), static_cast<std::streamsize>(size));
|
||||||
|
ostream.write(buffer.get(), static_cast<std::streamsize>(size));
|
||||||
|
|
||||||
this->read_chunked_transfer_encoded(session, chunks_streambuf);
|
this->read_chunked_transfer_encoded(session, chunks_streambuf);
|
||||||
}
|
}
|
||||||
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
|
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
|
||||||
|
|
@ -505,22 +530,38 @@ namespace SimpleWeb {
|
||||||
auto lock = session->connection->handler_runner->continue_lock();
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
if(!lock)
|
if(!lock)
|
||||||
return;
|
return;
|
||||||
|
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
||||||
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ec) {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
std::lock_guard<std::mutex> lock(this->connections_mutex);
|
||||||
this->connections.erase(session->connection);
|
this->connections.erase(session->connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!ec) {
|
|
||||||
if(session->response->streambuf.size() == session->response->streambuf.max_size())
|
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
|
||||||
else
|
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
session->callback(ec == asio::error::eof ? error_code() : ec);
|
session->callback(ec == asio::error::eof ? error_code() : ec);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else if(((header_it = session->response->header.find("Content-Type")) != session->response->header.end() && header_it->second == "text/event-stream")) {
|
||||||
|
session->connection->event_stream = true;
|
||||||
|
|
||||||
|
auto events_streambuf = std::make_shared<asio::streambuf>(this->config.max_response_streambuf_size);
|
||||||
|
|
||||||
|
// Copy leftover bytes
|
||||||
|
std::ostream ostream(events_streambuf.get());
|
||||||
|
auto size = session->response->streambuf.size();
|
||||||
|
std::unique_ptr<char[]> buffer(new char[size]);
|
||||||
|
session->response->content.read(buffer.get(), static_cast<std::streamsize>(size));
|
||||||
|
ostream.write(buffer.get(), static_cast<std::streamsize>(size));
|
||||||
|
|
||||||
|
session->callback(ec); // Connection to a Server-Sent Events resource is opened
|
||||||
|
|
||||||
|
this->read_server_sent_event(session, events_streambuf);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
|
|
@ -533,7 +574,7 @@ namespace SimpleWeb {
|
||||||
session->connection = create_connection();
|
session->connection = create_connection();
|
||||||
session->connection->attempt_reconnect = false;
|
session->connection->attempt_reconnect = false;
|
||||||
session->connection->in_use = true;
|
session->connection->in_use = true;
|
||||||
session->response = std::shared_ptr<Response>(new Response(session->response->streambuf.max_size()));
|
session->response = std::shared_ptr<Response>(new Response(this->config.max_response_streambuf_size));
|
||||||
connections.emplace(session->connection);
|
connections.emplace(session->connection);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
this->connect(session);
|
this->connect(session);
|
||||||
|
|
@ -551,18 +592,20 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
void read_chunked_transfer_encoded(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &chunks_streambuf) {
|
void read_chunked_transfer_encoded(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &chunks_streambuf) {
|
||||||
session->connection->set_timeout();
|
session->connection->set_timeout();
|
||||||
asio::async_read_until(*session->connection->socket, session->response->streambuf, "\r\n", [this, session, chunks_streambuf](const error_code &ec, size_t bytes_transferred) {
|
asio::async_read_until(*session->connection->socket, *chunks_streambuf, "\r\n", [this, session, chunks_streambuf](const error_code &ec, size_t bytes_transferred) {
|
||||||
session->connection->cancel_timeout();
|
session->connection->cancel_timeout();
|
||||||
auto lock = session->connection->handler_runner->continue_lock();
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
if(!lock)
|
if(!lock)
|
||||||
return;
|
return;
|
||||||
if((!ec || ec == asio::error::not_found) && session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
if(chunks_streambuf->size() == chunks_streambuf->max_size()) {
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
|
std::istream istream(chunks_streambuf.get());
|
||||||
std::string line;
|
std::string line;
|
||||||
getline(session->response->content, line);
|
getline(istream, line);
|
||||||
bytes_transferred -= line.size() + 1;
|
bytes_transferred -= line.size() + 1;
|
||||||
line.pop_back();
|
line.pop_back();
|
||||||
unsigned long length = 0;
|
unsigned long length = 0;
|
||||||
|
|
@ -574,22 +617,22 @@ namespace SimpleWeb {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto num_additional_bytes = session->response->streambuf.size() - bytes_transferred;
|
auto num_additional_bytes = chunks_streambuf->size() - bytes_transferred;
|
||||||
|
|
||||||
if((2 + length) > num_additional_bytes) {
|
if((2 + length) > num_additional_bytes) {
|
||||||
session->connection->set_timeout();
|
session->connection->set_timeout();
|
||||||
asio::async_read(*session->connection->socket, session->response->streambuf, asio::transfer_exactly(2 + length - num_additional_bytes), [this, session, chunks_streambuf, length](const error_code &ec, size_t /*bytes_transferred*/) {
|
asio::async_read(*session->connection->socket, *chunks_streambuf, asio::transfer_exactly(2 + length - num_additional_bytes), [this, session, chunks_streambuf, length](const error_code &ec, size_t /*bytes_transferred*/) {
|
||||||
session->connection->cancel_timeout();
|
session->connection->cancel_timeout();
|
||||||
auto lock = session->connection->handler_runner->continue_lock();
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
if(!lock)
|
if(!lock)
|
||||||
return;
|
return;
|
||||||
if(!ec) {
|
if(chunks_streambuf->size() == chunks_streambuf->max_size()) {
|
||||||
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!ec)
|
||||||
this->read_chunked_transfer_encoded_chunk(session, chunks_streambuf, length);
|
this->read_chunked_transfer_encoded_chunk(session, chunks_streambuf, length);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
});
|
});
|
||||||
|
|
@ -603,31 +646,55 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_chunked_transfer_encoded_chunk(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &chunks_streambuf, unsigned long length) {
|
void read_chunked_transfer_encoded_chunk(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &chunks_streambuf, unsigned long length) {
|
||||||
std::ostream tmp_stream(chunks_streambuf.get());
|
std::istream istream(chunks_streambuf.get());
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
|
std::ostream ostream(&session->response->streambuf);
|
||||||
std::unique_ptr<char[]> buffer(new char[length]);
|
std::unique_ptr<char[]> buffer(new char[length]);
|
||||||
session->response->content.read(buffer.get(), static_cast<std::streamsize>(length));
|
istream.read(buffer.get(), static_cast<std::streamsize>(length));
|
||||||
tmp_stream.write(buffer.get(), static_cast<std::streamsize>(length));
|
ostream.write(buffer.get(), static_cast<std::streamsize>(length));
|
||||||
if(chunks_streambuf->size() == chunks_streambuf->max_size()) {
|
if(session->response->streambuf.size() == session->response->streambuf.max_size()) {
|
||||||
session->callback(make_error_code::make_error_code(errc::message_size));
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove "\r\n"
|
// Remove "\r\n"
|
||||||
session->response->content.get();
|
istream.get();
|
||||||
session->response->content.get();
|
istream.get();
|
||||||
|
|
||||||
if(length > 0)
|
if(length > 0)
|
||||||
read_chunked_transfer_encoded(session, chunks_streambuf);
|
read_chunked_transfer_encoded(session, chunks_streambuf);
|
||||||
else {
|
else
|
||||||
if(chunks_streambuf->size() > 0) {
|
session->callback(error_code());
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_server_sent_event(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &events_streambuf) {
|
||||||
|
session->connection->set_timeout();
|
||||||
|
asio::async_read_until(*session->connection->socket, *events_streambuf, "\n\n", [this, session, events_streambuf](const error_code &ec, std::size_t /*bytes_transferred*/) {
|
||||||
|
session->connection->cancel_timeout();
|
||||||
|
auto lock = session->connection->handler_runner->continue_lock();
|
||||||
|
if(!lock)
|
||||||
|
return;
|
||||||
|
if(events_streambuf->size() == events_streambuf->max_size()) {
|
||||||
|
session->callback(make_error_code::make_error_code(errc::message_size));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!ec) {
|
||||||
|
std::istream istream(events_streambuf.get());
|
||||||
std::ostream ostream(&session->response->streambuf);
|
std::ostream ostream(&session->response->streambuf);
|
||||||
ostream << chunks_streambuf.get();
|
std::string line;
|
||||||
|
while(std::getline(istream, line) && !line.empty()) {
|
||||||
|
ostream.write(line.data(), static_cast<ssize_t>(line.size()));
|
||||||
|
ostream.put('\n');
|
||||||
}
|
}
|
||||||
error_code ec;
|
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
|
read_server_sent_event(session, events_streambuf);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
session->callback(ec);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,13 +145,13 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function for writing status line, potential header fields, and empty content
|
/// Convenience function for writing status line, potential header fields, and empty content
|
||||||
void write(StatusCode status_code = StatusCode::success_ok, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
void write(StatusCode status_code = StatusCode::success_ok, const CaseInsensitiveMultimap &header = {}) {
|
||||||
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
||||||
write_header(header, 0);
|
write_header(header, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function for writing status line, header fields, and content
|
/// Convenience function for writing status line, header fields, and content
|
||||||
void write(StatusCode status_code, string_view content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
void write(StatusCode status_code, string_view content, const CaseInsensitiveMultimap &header = {}) {
|
||||||
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
||||||
write_header(header, content.size());
|
write_header(header, content.size());
|
||||||
if(!content.empty())
|
if(!content.empty())
|
||||||
|
|
@ -159,7 +159,7 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function for writing status line, header fields, and content
|
/// Convenience function for writing status line, header fields, and content
|
||||||
void write(StatusCode status_code, std::istream &content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
void write(StatusCode status_code, std::istream &content, const CaseInsensitiveMultimap &header = {}) {
|
||||||
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
*this << "HTTP/1.1 " << SimpleWeb::status_code(status_code) << "\r\n";
|
||||||
content.seekg(0, std::ios::end);
|
content.seekg(0, std::ios::end);
|
||||||
auto size = content.tellg();
|
auto size = content.tellg();
|
||||||
|
|
@ -170,12 +170,12 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function for writing success status line, header fields, and content
|
/// Convenience function for writing success status line, header fields, and content
|
||||||
void write(string_view content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
void write(string_view content, const CaseInsensitiveMultimap &header = {}) {
|
||||||
write(StatusCode::success_ok, content, header);
|
write(StatusCode::success_ok, content, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convenience function for writing success status line, header fields, and content
|
/// Convenience function for writing success status line, header fields, and content
|
||||||
void write(std::istream &content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
|
void write(std::istream &content, const CaseInsensitiveMultimap &header = {}) {
|
||||||
write(StatusCode::success_ok, content, header);
|
write(StatusCode::success_ok, content, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,6 +141,40 @@ int main() {
|
||||||
response->write("6\r\nSimple\r\n3\r\nWeb\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n", {{"Transfer-Encoding", "chunked"}});
|
response->write("6\r\nSimple\r\n3\r\nWeb\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n", {{"Transfer-Encoding", "chunked"}});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
server.resource["^/event-stream$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||||
|
thread work_thread([response] {
|
||||||
|
response->close_connection_after_response = true; // Unspecified content length
|
||||||
|
|
||||||
|
// Send header
|
||||||
|
promise<bool> header_error;
|
||||||
|
response->write({{"Content-Type", "text/event-stream"}});
|
||||||
|
response->send([&header_error](const SimpleWeb::error_code &ec) {
|
||||||
|
header_error.set_value(static_cast<bool>(ec));
|
||||||
|
});
|
||||||
|
ASSERT(!header_error.get_future().get());
|
||||||
|
|
||||||
|
*response << "data: 1\n\n";
|
||||||
|
promise<bool> error;
|
||||||
|
response->send([&error](const SimpleWeb::error_code &ec) {
|
||||||
|
error.set_value(static_cast<bool>(ec));
|
||||||
|
});
|
||||||
|
ASSERT(!error.get_future().get());
|
||||||
|
|
||||||
|
// Write result
|
||||||
|
*response << "data: 2\n\n";
|
||||||
|
});
|
||||||
|
work_thread.detach();
|
||||||
|
};
|
||||||
|
|
||||||
|
server.resource["^/session-close$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||||
|
response->close_connection_after_response = true; // Unspecified content length
|
||||||
|
response->write("test", {{"Session", "close"}});
|
||||||
|
};
|
||||||
|
server.resource["^/session-close-without-correct-header$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
|
||||||
|
response->close_connection_after_response = true; // Unspecified content length
|
||||||
|
response->write("test");
|
||||||
|
};
|
||||||
|
|
||||||
thread server_thread([&server]() {
|
thread server_thread([&server]() {
|
||||||
// Start server
|
// Start server
|
||||||
server.start();
|
server.start();
|
||||||
|
|
@ -237,6 +271,16 @@ int main() {
|
||||||
auto r = client.request("POST", "/chunked", "6\r\nSimple\r\n3\r\nWeb\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n", {{"Transfer-Encoding", "chunked"}});
|
auto r = client.request("POST", "/chunked", "6\r\nSimple\r\n3\r\nWeb\r\nE\r\n in\r\n\r\nchunks.\r\n0\r\n\r\n", {{"Transfer-Encoding", "chunked"}});
|
||||||
ASSERT(r->content.string() == "SimpleWeb in\r\n\r\nchunks.");
|
ASSERT(r->content.string() == "SimpleWeb in\r\n\r\nchunks.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test reconnecting
|
||||||
|
for(int c = 0; c < 20; ++c) {
|
||||||
|
auto r = client.request("GET", "/session-close");
|
||||||
|
ASSERT(r->content.string() == "test");
|
||||||
|
}
|
||||||
|
for(int c = 0; c < 20; ++c) {
|
||||||
|
auto r = client.request("GET", "/session-close-without-correct-header");
|
||||||
|
ASSERT(r->content.string() == "test");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
@ -294,6 +338,37 @@ int main() {
|
||||||
client.io_service->run();
|
client.io_service->run();
|
||||||
ASSERT(call);
|
ASSERT(call);
|
||||||
|
|
||||||
|
// Test event-stream
|
||||||
|
{
|
||||||
|
vector<int> calls(4, 0);
|
||||||
|
std::size_t call_num = 0;
|
||||||
|
client.request("GET", "/event-stream", [&calls, &call_num](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||||
|
calls.at(call_num) = 1;
|
||||||
|
if(call_num == 0) {
|
||||||
|
ASSERT(response->content.string().empty());
|
||||||
|
ASSERT(!ec);
|
||||||
|
}
|
||||||
|
else if(call_num == 1) {
|
||||||
|
ASSERT(response->content.string() == "data: 1\n");
|
||||||
|
ASSERT(!ec);
|
||||||
|
}
|
||||||
|
else if(call_num == 2) {
|
||||||
|
ASSERT(response->content.string() == "data: 2\n");
|
||||||
|
ASSERT(!ec);
|
||||||
|
}
|
||||||
|
else if(call_num == 3) {
|
||||||
|
ASSERT(response->content.string().empty());
|
||||||
|
ASSERT(ec == SimpleWeb::asio::error::eof);
|
||||||
|
}
|
||||||
|
++call_num;
|
||||||
|
});
|
||||||
|
SimpleWeb::restart(*client.io_service);
|
||||||
|
client.io_service->run();
|
||||||
|
for(auto call : calls)
|
||||||
|
ASSERT(call);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test concurrent requests from same client
|
||||||
{
|
{
|
||||||
vector<int> calls(100, 0);
|
vector<int> calls(100, 0);
|
||||||
vector<thread> threads;
|
vector<thread> threads;
|
||||||
|
|
@ -318,6 +393,33 @@ int main() {
|
||||||
ASSERT(call);
|
ASSERT(call);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test concurrent synchronous request calls from same client
|
||||||
|
{
|
||||||
|
HttpClient client("localhost:8080");
|
||||||
|
{
|
||||||
|
vector<int> calls(5, 0);
|
||||||
|
vector<thread> threads;
|
||||||
|
for(size_t c = 0; c < 5; ++c) {
|
||||||
|
threads.emplace_back([c, &client, &calls] {
|
||||||
|
try {
|
||||||
|
auto r = client.request("GET", "/match/123");
|
||||||
|
ASSERT(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
||||||
|
ASSERT(r->content.string() == "123");
|
||||||
|
calls[c] = 1;
|
||||||
|
}
|
||||||
|
catch(...) {
|
||||||
|
ASSERT(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for(auto &thread : threads)
|
||||||
|
thread.join();
|
||||||
|
ASSERT(client.connections.size() == 1);
|
||||||
|
for(auto call : calls)
|
||||||
|
ASSERT(call);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test concurrent requests from different clients
|
// Test concurrent requests from different clients
|
||||||
{
|
{
|
||||||
vector<int> calls(10, 0);
|
vector<int> calls(10, 0);
|
||||||
|
|
@ -340,33 +442,6 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test concurrent synchronous request calls
|
|
||||||
{
|
|
||||||
HttpClient client("localhost:8080");
|
|
||||||
{
|
|
||||||
vector<int> calls(2, 0);
|
|
||||||
vector<thread> threads;
|
|
||||||
for(size_t c = 0; c < 2; ++c) {
|
|
||||||
threads.emplace_back([c, &client, &calls] {
|
|
||||||
try {
|
|
||||||
auto r = client.request("GET", "/match/123");
|
|
||||||
ASSERT(SimpleWeb::status_code(r->status_code) == SimpleWeb::StatusCode::success_ok);
|
|
||||||
ASSERT(r->content.string() == "123");
|
|
||||||
calls[c] = 1;
|
|
||||||
}
|
|
||||||
catch(...) {
|
|
||||||
ASSERT(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for(auto &thread : threads)
|
|
||||||
thread.join();
|
|
||||||
ASSERT(client.connections.size() == 1);
|
|
||||||
for(auto call : calls)
|
|
||||||
ASSERT(call);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test multiple requests through a persistent connection
|
// Test multiple requests through a persistent connection
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue