default_resource examples should now always output errors
This commit is contained in:
parent
1a2fe59fa9
commit
4efd3bf83c
2 changed files with 64 additions and 60 deletions
|
|
@ -105,39 +105,41 @@ int main() {
|
||||||
//Default file: index.html
|
//Default file: index.html
|
||||||
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
|
//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<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
server.default_resource["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
|
||||||
const auto web_root_path=boost::filesystem::canonical("web");
|
try {
|
||||||
boost::filesystem::path path=web_root_path;
|
auto web_root_path=boost::filesystem::canonical("web");
|
||||||
path/=request->path;
|
auto path=boost::filesystem::canonical(web_root_path/request->path);
|
||||||
if(boost::filesystem::exists(path)) {
|
|
||||||
path=boost::filesystem::canonical(path);
|
|
||||||
//Check if path is within web_root_path
|
//Check if path is within web_root_path
|
||||||
if(distance(web_root_path.begin(), web_root_path.end())<=distance(path.begin(), path.end()) &&
|
if(distance(web_root_path.begin(), web_root_path.end())>distance(path.begin(), path.end()) ||
|
||||||
equal(web_root_path.begin(), web_root_path.end(), path.begin())) {
|
!equal(web_root_path.begin(), web_root_path.end(), path.begin()))
|
||||||
if(boost::filesystem::is_directory(path))
|
throw invalid_argument("path must be within root path");
|
||||||
path/="index.html";
|
if(boost::filesystem::is_directory(path))
|
||||||
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
|
path/="index.html";
|
||||||
auto ifs=make_shared<ifstream>();
|
if(!(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)))
|
||||||
ifs->open(path.string(), ifstream::in | ios::binary);
|
throw invalid_argument("file does not exist");
|
||||||
|
|
||||||
if(*ifs) {
|
auto ifs=make_shared<ifstream>();
|
||||||
//read and send 128 KB at a time
|
ifs->open(path.string(), ifstream::in | ios::binary);
|
||||||
streamsize buffer_size=131072;
|
|
||||||
auto buffer=make_shared<vector<char> >(buffer_size);
|
if(*ifs) {
|
||||||
|
//read and send 128 KB at a time
|
||||||
ifs->seekg(0, ios::end);
|
streamsize buffer_size=131072;
|
||||||
auto length=ifs->tellg();
|
auto buffer=make_shared<vector<char> >(buffer_size);
|
||||||
|
|
||||||
ifs->seekg(0, ios::beg);
|
ifs->seekg(0, ios::end);
|
||||||
|
auto length=ifs->tellg();
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
|
|
||||||
default_resource_send(server, response, ifs, buffer);
|
ifs->seekg(0, ios::beg);
|
||||||
return;
|
|
||||||
}
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
|
||||||
}
|
default_resource_send(server, response, ifs, buffer);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
throw invalid_argument("coult not read file");
|
||||||
|
}
|
||||||
|
catch(const exception &e) {
|
||||||
|
string content="Could not open path "+request->path+": "+e.what();
|
||||||
|
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
||||||
}
|
}
|
||||||
string content="Could not open path "+request->path;
|
|
||||||
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
thread server_thread([&server](){
|
thread server_thread([&server](){
|
||||||
|
|
|
||||||
|
|
@ -105,39 +105,41 @@ int main() {
|
||||||
//Default file: index.html
|
//Default file: index.html
|
||||||
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
|
//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"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) {
|
||||||
const auto web_root_path=boost::filesystem::canonical("web");
|
try {
|
||||||
boost::filesystem::path path=web_root_path;
|
auto web_root_path=boost::filesystem::canonical("web");
|
||||||
path/=request->path;
|
auto path=boost::filesystem::canonical(web_root_path/request->path);
|
||||||
if(boost::filesystem::exists(path)) {
|
|
||||||
path=boost::filesystem::canonical(path);
|
|
||||||
//Check if path is within web_root_path
|
//Check if path is within web_root_path
|
||||||
if(distance(web_root_path.begin(), web_root_path.end())<=distance(path.begin(), path.end()) &&
|
if(distance(web_root_path.begin(), web_root_path.end())>distance(path.begin(), path.end()) ||
|
||||||
equal(web_root_path.begin(), web_root_path.end(), path.begin())) {
|
!equal(web_root_path.begin(), web_root_path.end(), path.begin()))
|
||||||
if(boost::filesystem::is_directory(path))
|
throw invalid_argument("path must be within root path");
|
||||||
path/="index.html";
|
if(boost::filesystem::is_directory(path))
|
||||||
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
|
path/="index.html";
|
||||||
auto ifs=make_shared<ifstream>();
|
if(!(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)))
|
||||||
ifs->open(path.string(), ifstream::in | ios::binary);
|
throw invalid_argument("file does not exist");
|
||||||
|
|
||||||
if(*ifs) {
|
auto ifs=make_shared<ifstream>();
|
||||||
//read and send 128 KB at a time
|
ifs->open(path.string(), ifstream::in | ios::binary);
|
||||||
streamsize buffer_size=131072;
|
|
||||||
auto buffer=make_shared<vector<char> >(buffer_size);
|
if(*ifs) {
|
||||||
|
//read and send 128 KB at a time
|
||||||
ifs->seekg(0, ios::end);
|
streamsize buffer_size=131072;
|
||||||
auto length=ifs->tellg();
|
auto buffer=make_shared<vector<char> >(buffer_size);
|
||||||
|
|
||||||
ifs->seekg(0, ios::beg);
|
ifs->seekg(0, ios::end);
|
||||||
|
auto length=ifs->tellg();
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
|
|
||||||
default_resource_send(server, response, ifs, buffer);
|
ifs->seekg(0, ios::beg);
|
||||||
return;
|
|
||||||
}
|
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << length << "\r\n\r\n";
|
||||||
}
|
default_resource_send(server, response, ifs, buffer);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
throw invalid_argument("coult not read file");
|
||||||
|
}
|
||||||
|
catch(const exception &e) {
|
||||||
|
string content="Could not open path "+request->path+": "+e.what();
|
||||||
|
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
||||||
}
|
}
|
||||||
string content="Could not open path "+request->path;
|
|
||||||
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
thread server_thread([&server](){
|
thread server_thread([&server](){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue