forked from cpp-netlib/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a function to normalize the URI path and used it to better test…
… URI equality.
- Loading branch information
Showing
5 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Glyn Matthews 2012. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
#ifndef __BOOST_NETWORK_URI_NORMALIZE_INC__ | ||
# define __BOOST_NETWORK_URI_NORMALIZE_INC__ | ||
|
||
# include <network/uri/uri.hpp> | ||
|
||
namespace network { | ||
uri::string_type normalize_path(const uri::const_range_type &path); | ||
|
||
//uri normalize(const uri &uri_); | ||
// | ||
//uri::string_type normalize_scheme(const uri &uri_); | ||
// | ||
//uri::string_type normalize_user_info(const uri &uri_); | ||
// | ||
//uri::string_type normalize_host(const uri &uri_); | ||
// | ||
//uri::string_type normalize_port(const uri &uri_); | ||
// | ||
//uri::string_type normalize_path(const uri &uri_); | ||
// | ||
//uri::string_type normalize_fragment(const uri &uri_); | ||
// | ||
//uri::string_type normalize_query(const uri &uri_); | ||
} // namespace network | ||
|
||
#endif // __BOOST_NETWORK_URI_NORMALIZE_INC__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Glyn Matthews 2012. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
|
||
# include <network/uri/normalize.hpp> | ||
# include <vector> | ||
# include <stack> | ||
# include <boost/algorithm/string/split.hpp> | ||
# include <boost/algorithm/string/join.hpp> | ||
# include <boost/algorithm/string/predicate.hpp> | ||
# include <boost/algorithm/string/classification.hpp> | ||
# include <boost/range/as_literal.hpp> | ||
# include <boost/range/algorithm_ext/erase.hpp> | ||
# include <boost/range/algorithm/for_each.hpp> | ||
|
||
namespace network { | ||
uri::string_type normalize_path(const uri::const_range_type &path) { | ||
using namespace boost; | ||
using namespace boost::algorithm; | ||
|
||
// add trailing / | ||
if (empty(path)) { | ||
return uri::string_type("/"); | ||
} | ||
|
||
std::vector<uri::string_type> path_segments; | ||
split(path_segments, path, is_any_of("/")); | ||
|
||
// remove single dot segments | ||
remove_erase_if(path_segments, [] (const uri::string_type &s) { | ||
return equal(s, as_literal(".")); | ||
}); | ||
|
||
// remove double dot segments | ||
std::vector<std::string> normalized_segments; | ||
auto depth = 0; | ||
for_each(path_segments, [&normalized_segments, &depth] (const uri::string_type &s) { | ||
assert(depth >= 0); | ||
if (equal(s, as_literal(".."))) { | ||
normalized_segments.pop_back(); | ||
} | ||
else { | ||
normalized_segments.push_back(s); | ||
} | ||
}); | ||
|
||
if (!empty(normalized_segments.back()) && | ||
!contains(normalized_segments.back(), as_literal("."))) { | ||
normalized_segments.push_back(uri::string_type()); | ||
} | ||
|
||
return join(normalized_segments, "/"); | ||
} | ||
} // namespace network |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters