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

Commit c7f35ad

Browse files
committed
Cleanup of the default_resource examples
1 parent 38e2d09 commit c7f35ad

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

http_examples.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fstream>
1111
#include <boost/filesystem.hpp>
1212
#include <array>
13+
#include <algorithm>
1314

1415
using namespace std;
1516
//Added for the json-example:
@@ -85,12 +86,14 @@ int main() {
8586
//Default file: index.html
8687
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
8788
server.default_resource["GET"]=[](HttpServer::Response& response, shared_ptr<HttpServer::Request> request) {
88-
string web_root_path=boost::filesystem::canonical("web").string();
89+
const auto web_root_path=boost::filesystem::canonical("web");
8990
boost::filesystem::path path=web_root_path;
9091
path/=request->path;
9192
if(boost::filesystem::exists(path)) {
92-
auto path_str=boost::filesystem::canonical(path).string();
93-
if(path_str.substr(0, web_root_path.size())==web_root_path) {
93+
path=boost::filesystem::canonical(path);
94+
//Check if path is within web_root_path
95+
if(distance(web_root_path.begin(), web_root_path.end())<=distance(path.begin(), path.end()) &&
96+
equal(web_root_path.begin(), web_root_path.end(), path.begin())) {
9497
if(boost::filesystem::is_directory(path))
9598
path/="index.html";
9699
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {

https_examples.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fstream>
1111
#include <boost/filesystem.hpp>
1212
#include <array>
13+
#include <algorithm>
1314

1415
using namespace std;
1516
//Added for the json-example:
@@ -85,12 +86,14 @@ int main() {
8586
//Default file: index.html
8687
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
8788
server.default_resource["GET"]=[](HttpsServer::Response& response, shared_ptr<HttpsServer::Request> request) {
88-
string web_root_path=boost::filesystem::canonical("web").string();
89+
const auto web_root_path=boost::filesystem::canonical("web");
8990
boost::filesystem::path path=web_root_path;
9091
path/=request->path;
9192
if(boost::filesystem::exists(path)) {
92-
auto path_str=boost::filesystem::canonical(path).string();
93-
if(path_str.substr(0, web_root_path.size())==web_root_path) {
93+
path=boost::filesystem::canonical(path);
94+
//Check if path is within web_root_path
95+
if(distance(web_root_path.begin(), web_root_path.end())<=distance(path.begin(), path.end()) &&
96+
equal(web_root_path.begin(), web_root_path.end(), path.begin())) {
9497
if(boost::filesystem::is_directory(path))
9598
path/="index.html";
9699
if(boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) {

0 commit comments

Comments
 (0)