Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Cleanup of the default_resource examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eidheim committed Mar 16, 2016
1 parent 38e2d09 commit c7f35ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions http_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fstream>
#include <boost/filesystem.hpp>
#include <array>
#include <algorithm>

using namespace std;
//Added for the json-example:
Expand Down Expand Up @@ -85,12 +86,14 @@ int main() {
//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"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
string web_root_path=boost::filesystem::canonical("web").string();
const auto web_root_path=boost::filesystem::canonical("web");
boost::filesystem::path path=web_root_path;
path/=request->path;
if(boost::filesystem::exists(path)) {
auto path_str=boost::filesystem::canonical(path).string();
if(path_str.substr(0, web_root_path.size())==web_root_path) {
path=boost::filesystem::canonical(path);
//Check if path is within web_root_path
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())) {
if(boost::filesystem::is_directory(path))
path/="index.html";
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
Expand Down
9 changes: 6 additions & 3 deletions https_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fstream>
#include <boost/filesystem.hpp>
#include <array>
#include <algorithm>

using namespace std;
//Added for the json-example:
Expand Down Expand Up @@ -85,12 +86,14 @@ int main() {
//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"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
string web_root_path=boost::filesystem::canonical("web").string();
const auto web_root_path=boost::filesystem::canonical("web");
boost::filesystem::path path=web_root_path;
path/=request->path;
if(boost::filesystem::exists(path)) {
auto path_str=boost::filesystem::canonical(path).string();
if(path_str.substr(0, web_root_path.size())==web_root_path) {
path=boost::filesystem::canonical(path);
//Check if path is within web_root_path
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())) {
if(boost::filesystem::is_directory(path))
path/="index.html";
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {
Expand Down

0 comments on commit c7f35ad

Please sign in to comment.