The function Server::send was moved to Server::Response::send

This commit is contained in:
eidheim 2017-07-04 09:38:01 +02:00
commit cc1a8fc614
3 changed files with 27 additions and 28 deletions

View file

@ -21,8 +21,7 @@ typedef SimpleWeb::Server<SimpleWeb::HTTPS> HttpsServer;
typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient;
//Added for the default_resource example
void default_resource_send(const shared_ptr<HttpsServer> &server,
const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs);
void default_resource_send(const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs);
int main() {
//HTTPS-server at port 8080 using 1 thread
@ -137,7 +136,7 @@ int main() {
//Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server->default_resource["GET"] = [&server](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
server->default_resource["GET"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
try {
auto web_root_path = boost::filesystem::canonical("web");
auto path = boost::filesystem::canonical(web_root_path / request->path);
@ -182,7 +181,7 @@ int main() {
header.emplace("Content-Length", to_string(length));
response->write(header);
default_resource_send(server, response, ifs);
default_resource_send(response, ifs);
}
else
throw invalid_argument("could not read file");
@ -227,17 +226,16 @@ int main() {
server_thread.join();
}
void default_resource_send(const shared_ptr<HttpsServer> &server,
const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs) {
void default_resource_send(const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs) {
//read and send 128 KB at a time
static vector<char> buffer(131072); // Safe when server is running on one thread
streamsize read_length;
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
response->write(&buffer[0], read_length);
if(read_length == static_cast<streamsize>(buffer.size())) {
server->send(response, [server, response, ifs](const SimpleWeb::error_code &ec) {
response->send([response, ifs](const SimpleWeb::error_code &ec) {
if(!ec)
default_resource_send(server, response, ifs);
default_resource_send(response, ifs);
else
cerr << "Connection interrupted" << endl;
});