Skip to content

Commit

Permalink
fix path_normalize corner case
Browse files Browse the repository at this point in the history
fix #674
  • Loading branch information
alandefreitas committed Mar 8, 2023
1 parent 23e0109 commit e549f40
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
45 changes: 42 additions & 3 deletions include/boost/url/impl/url_base.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,12 @@ normalize_path()
auto skip_dot = 0;
bool encode_colons = false;
string_view first_seg;

//------------------------------------------------
//
// Determine unnecessary initial dot segments to skip and
// if we need to encode colons in the first segment
//
if (
!has_authority() &&
p.starts_with("/./"))
Expand Down Expand Up @@ -1596,6 +1602,11 @@ normalize_path()
encode_colons = first_seg.contains(':');
}
}

//------------------------------------------------
//
// Encode colons in the first segment
//
if (encode_colons)
{
// prepend with "./"
Expand Down Expand Up @@ -1649,20 +1660,34 @@ normalize_path()
p_dest = s_ + impl_.offset(id_path);
p_end = s_ + impl_.offset(id_path + 1);
}

//------------------------------------------------
//
// Remove "." and ".." segments
//
p.remove_prefix(skip_dot);
p_dest += skip_dot;
auto n = detail::remove_dot_segments(
p_dest, p_end, p);

//------------------------------------------------
//
// Update path parameters
//
if (n != pn)
{
BOOST_ASSERT(n < pn);
shrink_impl(id_path, n + skip_dot, op);
p = encoded_path();
if (!p.empty())
if (p == "/")
impl_.nseg_ = 0;
else if (!p.empty())
impl_.nseg_ = std::count(
p.begin() + 1, p.end(), '/') + 1;
else
impl_.nseg_ = 0;
impl_.decoded_[id_path] =
detail::decode_bytes_unsafe(impl_.get(id_path));
}
return *this;
}
Expand Down Expand Up @@ -2113,6 +2138,7 @@ edit_segments(
std::size_t nchar = 0;
std::size_t prefix = 0;
bool encode_colons = false;
bool cp_src_prefix = false;
if(it0.index > 0)
{
// first segment unchanged
Expand All @@ -2125,7 +2151,20 @@ edit_segments(
{
if( src.front == "." &&
src.fast_nseg > 1)
prefix = 2 + absolute;
if (src.s.empty())
{
// if front is ".", we need the extra "." in the prefix
// which will maintain the invariant that segments represent
// {"."}
prefix = 2 + absolute;
}
else
{
// if the "." prefix is explicitly required from set_path
// we do not include an extra "." segment
prefix = absolute;
cp_src_prefix = true;
}
else if(absolute)
prefix = 1;
else if(has_scheme() ||
Expand Down Expand Up @@ -2290,7 +2329,7 @@ edit_segments(
BOOST_ASSERT(size() == new_size);
end = dest + nchar;
impl_.nseg_ = impl_.nseg_ + nseg - (
it1.index - it0.index);
it1.index - it0.index) - cp_src_prefix;
if(s_)
s_[size()] = '\0';
}
Expand Down
43 changes: 32 additions & 11 deletions test/unit/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
#include <iomanip>
#include <sstream>

#ifdef BOOST_TEST_CSTR_EQ
#undef BOOST_TEST_CSTR_EQ
#define BOOST_TEST_CSTR_EQ(expr1,expr2) \
BOOST_TEST_EQ( boost::urls::detail::to_sv(expr1), boost::urls::detail::to_sv(expr2) )
#endif

/* Legend
'#' 0x23 ':' 0x3a
Expand Down Expand Up @@ -303,19 +309,34 @@ struct url_test
BOOST_TEST_EQ( u.buffer(), "./kyle:xy" );
}
{
// issue 674: 1
auto ok = [](string_view u0, string_view p)
// issue 674
{
auto ok = [](string_view u0, string_view p)
{
urls::url u(u0);
u.set_path(p);
BOOST_TEST_CSTR_EQ(u.buffer(), p);
u.normalize();
BOOST_TEST_CSTR_EQ(u.buffer(), p);
};
ok("/", "/");
ok("/", "");
ok("", "/");
ok("", "");
}
{
urls::url u(u0);
u.set_path(p);
BOOST_TEST_CSTR_EQ(u.buffer(), p);
urls::url u;
BOOST_TEST_EQ(u.encoded_segments().size(), 0);
u.set_path("/");
BOOST_TEST_EQ(u.encoded_segments().size(), 0);
u.set_path("/./");
BOOST_TEST_EQ(u.encoded_segments().size(), 1);
BOOST_TEST_CSTR_EQ(u.buffer(), "/./");
u.normalize();
BOOST_TEST_CSTR_EQ(u.buffer(), p);
};
ok("/", "/");
ok("/", "");
ok("", "/");
ok("", "");
BOOST_TEST_CSTR_EQ(u.buffer(), "/");
BOOST_TEST_CSTR_EQ(u.encoded_target(), "/");
BOOST_TEST_EQ(u.encoded_segments().size(), 0);
}
}

// set_encoded_path
Expand Down

0 comments on commit e549f40

Please sign in to comment.