Comments addition and cleanup
This commit is contained in:
parent
d8b8716a17
commit
c03e378e69
7 changed files with 107 additions and 102 deletions
|
|
@ -134,7 +134,7 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
error_code ec;
|
error_code ec;
|
||||||
std::unique_lock<std::mutex> lock(socket_close_mutex); // the following operations seems to be needed to run sequentially
|
std::unique_lock<std::mutex> lock(socket_close_mutex); // The following operations seems to be needed to run sequentially
|
||||||
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
socket->lowest_layer().close(ec);
|
socket->lowest_layer().close(ec);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ int main() {
|
||||||
for(auto &header : request->header)
|
for(auto &header : request->header)
|
||||||
stream << header.first << ": " << header.second << "<br>";
|
stream << header.first << ": " << header.second << "<br>";
|
||||||
|
|
||||||
//find length of content_stream (length received using content_stream.tellp())
|
// Find length of content_stream (length received using content_stream.tellp())
|
||||||
stream.seekp(0, ios::end);
|
stream.seekp(0, ios::end);
|
||||||
|
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n"
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n"
|
||||||
|
|
@ -181,10 +181,11 @@ int main() {
|
||||||
header.emplace("Content-Length", to_string(length));
|
header.emplace("Content-Length", to_string(length));
|
||||||
response->write(header);
|
response->write(header);
|
||||||
|
|
||||||
|
// Trick to define a recursive function within this scope (for your convenience)
|
||||||
class FileServer {
|
class FileServer {
|
||||||
public:
|
public:
|
||||||
static void read_and_send(const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs) {
|
static void read_and_send(const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs) {
|
||||||
//read and send 128 KB at a time
|
// Read and send 128 KB at a time
|
||||||
static vector<char> buffer(131072); // Safe when server is running on one thread
|
static vector<char> buffer(131072); // Safe when server is running on one thread
|
||||||
streamsize read_length;
|
streamsize read_length;
|
||||||
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
|
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
|
||||||
|
|
@ -211,7 +212,7 @@ int main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
server.on_error = [](shared_ptr<HttpServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) {
|
server.on_error = [](shared_ptr<HttpServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) {
|
||||||
// handle errors here
|
// Handle errors here
|
||||||
};
|
};
|
||||||
|
|
||||||
thread server_thread([&server]() {
|
thread server_thread([&server]() {
|
||||||
|
|
@ -225,7 +226,7 @@ int main() {
|
||||||
// Client examples
|
// Client examples
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
||||||
// synchronous request examples
|
// Synchronous request examples
|
||||||
auto r1 = client.request("GET", "/match/123");
|
auto r1 = client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
||||||
|
|
||||||
|
|
@ -233,7 +234,7 @@ int main() {
|
||||||
auto r2 = client.request("POST", "/string", json_string);
|
auto r2 = client.request("POST", "/string", json_string);
|
||||||
cout << r2->content.rdbuf() << endl;
|
cout << r2->content.rdbuf() << endl;
|
||||||
|
|
||||||
// asynchronous request example
|
// Asynchronous request example
|
||||||
client.request("POST", "/json", json_string, [](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
client.request("POST", "/json", json_string, [](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||||
if(!ec)
|
if(!ec)
|
||||||
cout << response->content.rdbuf() << endl;
|
cout << response->content.rdbuf() << endl;
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ int main() {
|
||||||
for(auto &header : request->header)
|
for(auto &header : request->header)
|
||||||
stream << header.first << ": " << header.second << "<br>";
|
stream << header.first << ": " << header.second << "<br>";
|
||||||
|
|
||||||
//find length of content_stream (length received using content_stream.tellp())
|
// Find length of content_stream (length received using content_stream.tellp())
|
||||||
stream.seekp(0, ios::end);
|
stream.seekp(0, ios::end);
|
||||||
|
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n"
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n"
|
||||||
|
|
@ -179,10 +179,11 @@ int main() {
|
||||||
header.emplace("Content-Length", to_string(length));
|
header.emplace("Content-Length", to_string(length));
|
||||||
response->write(header);
|
response->write(header);
|
||||||
|
|
||||||
|
// Trick to define a recursive function within this scope (for your convenience)
|
||||||
class FileServer {
|
class FileServer {
|
||||||
public:
|
public:
|
||||||
static void read_and_send(const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs) {
|
static void read_and_send(const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs) {
|
||||||
//read and send 128 KB at a time
|
// Read and send 128 KB at a time
|
||||||
static vector<char> buffer(131072); // Safe when server is running on one thread
|
static vector<char> buffer(131072); // Safe when server is running on one thread
|
||||||
streamsize read_length;
|
streamsize read_length;
|
||||||
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
|
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
|
||||||
|
|
@ -209,7 +210,7 @@ int main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
server.on_error = [](shared_ptr<HttpsServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) {
|
server.on_error = [](shared_ptr<HttpsServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) {
|
||||||
// handle errors here
|
// Handle errors here
|
||||||
};
|
};
|
||||||
|
|
||||||
thread server_thread([&server]() {
|
thread server_thread([&server]() {
|
||||||
|
|
@ -224,7 +225,7 @@ int main() {
|
||||||
// Second create() parameter set to false: no certificate verification
|
// Second create() parameter set to false: no certificate verification
|
||||||
HttpsClient client("localhost:8080", false);
|
HttpsClient client("localhost:8080", false);
|
||||||
|
|
||||||
// synchronous request examples
|
// Synchronous request examples
|
||||||
auto r1 = client.request("GET", "/match/123");
|
auto r1 = client.request("GET", "/match/123");
|
||||||
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
|
||||||
|
|
||||||
|
|
@ -232,7 +233,7 @@ int main() {
|
||||||
auto r2 = client.request("POST", "/string", json_string);
|
auto r2 = client.request("POST", "/string", json_string);
|
||||||
cout << r2->content.rdbuf() << endl;
|
cout << r2->content.rdbuf() << endl;
|
||||||
|
|
||||||
// asynchronous request example
|
// Asynchronous request example
|
||||||
client.request("POST", "/json", json_string, [](shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) {
|
client.request("POST", "/json", json_string, [](shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) {
|
||||||
if(!ec)
|
if(!ec)
|
||||||
cout << response->content.rdbuf() << endl;
|
cout << response->content.rdbuf() << endl;
|
||||||
|
|
|
||||||
|
|
@ -271,7 +271,7 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
void close() {
|
void close() {
|
||||||
error_code ec;
|
error_code ec;
|
||||||
std::unique_lock<std::mutex> lock(socket_close_mutex); // the following operations seems to be needed to run sequentially
|
std::unique_lock<std::mutex> lock(socket_close_mutex); // The following operations seems to be needed to run sequentially
|
||||||
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
socket->lowest_layer().close(ec);
|
socket->lowest_layer().close(ec);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
{
|
{
|
||||||
|
// Test SharedMutex
|
||||||
SimpleWeb::SharedMutex mutex;
|
SimpleWeb::SharedMutex mutex;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
{
|
{
|
||||||
|
|
@ -125,9 +126,9 @@ int main() {
|
||||||
|
|
||||||
this_thread::sleep_for(chrono::seconds(1));
|
this_thread::sleep_for(chrono::seconds(1));
|
||||||
|
|
||||||
|
// Test various request types
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
auto r = client.request("POST", "/string", "A string");
|
auto r = client.request("POST", "/string", "A string");
|
||||||
|
|
@ -235,6 +236,7 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test asynchronous requests
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
bool call = false;
|
bool call = false;
|
||||||
|
|
@ -273,7 +275,7 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test concurrent synchronous request calls
|
// Test concurrent synchronous request calls
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
{
|
{
|
||||||
|
|
@ -300,6 +302,7 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test multiple requests through a persistent connection
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
assert(client.connections.size() == 0);
|
assert(client.connections.size() == 0);
|
||||||
|
|
@ -317,6 +320,7 @@ int main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test multiple requests through new several client objects
|
||||||
for(size_t c = 0; c < 100; ++c) {
|
for(size_t c = 0; c < 100; ++c) {
|
||||||
{
|
{
|
||||||
HttpClient client("localhost:8080");
|
HttpClient client("localhost:8080");
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ namespace SimpleWeb {
|
||||||
static auto hex_chars = "0123456789ABCDEF";
|
static auto hex_chars = "0123456789ABCDEF";
|
||||||
|
|
||||||
std::string result;
|
std::string result;
|
||||||
result.reserve(value.size()); // minimum size of result
|
result.reserve(value.size()); // Minimum size of result
|
||||||
|
|
||||||
for(auto &chr : value) {
|
for(auto &chr : value) {
|
||||||
if(chr == ' ')
|
if(chr == ' ')
|
||||||
|
|
@ -62,7 +62,7 @@ namespace SimpleWeb {
|
||||||
/// Returns percent-decoded string
|
/// Returns percent-decoded string
|
||||||
static std::string decode(const std::string &value) {
|
static std::string decode(const std::string &value) {
|
||||||
std::string result;
|
std::string result;
|
||||||
result.reserve(value.size() / 3 + (value.size() % 3)); // minimum size of result
|
result.reserve(value.size() / 3 + (value.size() % 3)); // Minimum size of result
|
||||||
|
|
||||||
for(size_t i = 0; i < value.size(); ++i) {
|
for(size_t i = 0; i < value.size(); ++i) {
|
||||||
auto &chr = value[i];
|
auto &chr = value[i];
|
||||||
|
|
@ -137,7 +137,6 @@ namespace SimpleWeb {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: see if there is an MSYS2 definition in an MSYS2 environment
|
|
||||||
#ifdef PTHREAD_RWLOCK_INITIALIZER
|
#ifdef PTHREAD_RWLOCK_INITIALIZER
|
||||||
namespace SimpleWeb {
|
namespace SimpleWeb {
|
||||||
/// Read-preferring R/W lock.
|
/// Read-preferring R/W lock.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue