Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added URI resolution according to RFC3986 #897

Merged
merged 6 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Release/include/cpprest/base_uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,18 @@ namespace web {
/// <summary>
/// Returns the full (encoded) URI as a string.
/// </summary>
/// <returns>The full encoded URI string.</returns>
/// <returns>The full encoded URI string.</returns>
utility::string_t to_string() const
{
return m_uri;
}

/// <summary>
/// Returns an URI resolved according to RFC3986, Section 5 (https://tools.ietf.org/html/rfc3986#section-5).
/// </summary>
/// <returns>The new resolved URI.</returns>
_ASYNCRTIMP utility::string_t resolve_uri(const utility::string_t &relativeUri) const;

_ASYNCRTIMP bool operator == (const uri &other) const;

bool operator < (const uri &other) const
Expand Down Expand Up @@ -414,3 +420,4 @@ namespace web {
};

} // namespace web

90 changes: 90 additions & 0 deletions Release/src/uri/uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,53 @@ namespace
return encoded;
}

// 5.2.3. Merge Paths https://tools.ietf.org/html/rfc3986#section-5.2.3
utility::string_t mergePaths(const utility::string_t &base, const utility::string_t &relative)
BillyONeal marked this conversation as resolved.
Show resolved Hide resolved
{
const auto lastSlash = base.rfind(_XPLATSTR('/'));
if (lastSlash == utility::string_t::npos)
{
return base + _XPLATSTR('/') + relative;
}
else if (lastSlash == base.size() - 1)
{
return base + relative;
}
// path contains and does not end with '/', we remove segment after last '/'
return base.substr(0, lastSlash + 1) + relative;
}

// 5.2.4. Remove Dot Segments https://tools.ietf.org/html/rfc3986#section-5.2.4
void removeDotSegments(web::uri_builder &builder)
{
if (builder.path().find(_XPLATSTR('.')) == utility::string_t::npos)
return;

const auto segments = web::uri::split_path(builder.path());
std::vector<std::reference_wrapper<const utility::string_t>> result;
for (auto& segment : segments)
{
if (segment == _XPLATSTR("."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These would be more efficient if you made const string_t instances for . and ..; this form does strlen on those constants a lot.

Copy link
Contributor Author

@toughengineer toughengineer Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, will fix it.

continue;
else if (segment != _XPLATSTR(".."))
result.push_back(segment);
else if (!result.empty())
result.pop_back();
}
if (result.empty())
{
builder.set_path(utility::string_t());
return;
}
utility::stringstream_t path;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this would be better with string_t's operator+ or +=?

Copy link
Contributor Author

@toughengineer toughengineer Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately either the string or the stringstream will manage the buffer growth so it is roughly the same.
I consider stringstream's memory management to be somewhat better because it is supposed to grow by its very purpose, and the interface is more convenient (e.g. you can operator<< numbers, though not in this case).
It's all arguable, however.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can tell you as someone who has maintained MSVC++'s stringstream that it is far less efficient than std::string. Like "virtual function calls per character" less efficient. If you need number formatting or something like that with it then of course stringstream is the only option, but this is just string concat.

Copy link
Contributor Author

@toughengineer toughengineer Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I forgot about that design feature of streams. Will fix it.

path << result.front().get();
for (size_t i = 1; i != result.size(); ++i)
path << _XPLATSTR('/') << result[i].get();
if (segments.back() == _XPLATSTR("..") || segments.back() == _XPLATSTR(".") || builder.path().back() == _XPLATSTR('/'))
path << _XPLATSTR('/');

builder.set_path(path.str());
}
}

utility::string_t uri_components::join()
Expand Down Expand Up @@ -784,4 +831,47 @@ bool uri::operator == (const uri &other) const
return true;
}

//resolving URI according to RFC3986, Section 5 https://tools.ietf.org/html/rfc3986#section-5
utility::string_t uri::resolve_uri(const utility::string_t &relativeUri) const
{
if (relativeUri.empty())
return to_string();

if (relativeUri[0] == _XPLATSTR('/')) // starts with '/'
{
if (relativeUri.size() >= 2 && relativeUri[1] == _XPLATSTR('/')) // starts with '//'
return scheme() + _XPLATSTR(':') + relativeUri;

// otherwise relative to root
auto builder = web::uri_builder(this->authority());
builder.append(relativeUri);
details::removeDotSegments(builder);
return builder.to_string();
}

const auto url = web::uri(relativeUri);
if (!url.scheme().empty())
return relativeUri;

if (!url.authority().is_empty())
return web::uri_builder(url).set_scheme(this->scheme()).to_string();

// relative url
auto builder = web::uri_builder(*this);
if (url.path() == _XPLATSTR("/") || url.path().empty()) // web::uri considers empty path as '/'
{
if (!url.query().empty())
builder.set_query(url.query());
}
else if (!this->path().empty())
{
builder.set_path(details::mergePaths(this->path(), url.path()));
details::removeDotSegments(builder);
builder.set_query(url.query());
}

return builder.set_fragment(url.fragment()).to_string();
}

}

1 change: 1 addition & 0 deletions Release/tests/functional/uri/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(SOURCES
operator_tests.cpp
splitting_tests.cpp
uri_builder_tests.cpp
resolve_uri_tests.cpp
stdafx.cpp
)

Expand Down
69 changes: 69 additions & 0 deletions Release/tests/functional/uri/resolve_uri_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "stdafx.h"

using namespace web;
using namespace utility;

namespace tests { namespace functional { namespace uri_tests {

//testing resolution against examples from Section 5.4 https://tools.ietf.org/html/rfc3986#section-5.4
SUITE(resolve_uri_tests)
{
//5.4.1. Normal Examples https://tools.ietf.org/html/rfc3986#section-5.4.1
TEST(resolve_uri_normal)
{
const uri baseUri = U("http://a/b/c/d;p?q");

VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g:h")), U("g:h"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g")), U("http://a/b/c/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("./g")), U("http://a/b/c/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g/")), U("http://a/b/c/g/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("/g")), U("http://a/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("//g")), U("http://g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("?y")), U("http://a/b/c/d;p?y"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g?y")), U("http://a/b/c/g?y"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("#s")), U("http://a/b/c/d;p?q#s"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g#s")), U("http://a/b/c/g#s"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g?y#s")), U("http://a/b/c/g?y#s"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U(";x")), U("http://a/b/c/;x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g;x")), U("http://a/b/c/g;x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g;x?y#s")), U("http://a/b/c/g;x?y#s"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("")), U("http://a/b/c/d;p?q"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U(".")), U("http://a/b/c/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("./")), U("http://a/b/c/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("..")), U("http://a/b/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../")), U("http://a/b/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../g")), U("http://a/b/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../..")), U("http://a/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../../")), U("http://a/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../../g")), U("http://a/g"));
}
//5.4.2. Abnormal Examples https://tools.ietf.org/html/rfc3986#section-5.4.2
TEST(resolve_uri_abnormal)
{
const uri baseUri = U("http://a/b/c/d;p?q");

VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../../../g")), U("http://a/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("../../../../g")), U("http://a/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("/./g")), U("http://a/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("/../g")), U("http://a/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g.")), U("http://a/b/c/g."));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U(".g")), U("http://a/b/c/.g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g..")), U("http://a/b/c/g.."));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("..g")), U("http://a/b/c/..g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("./../g")), U("http://a/b/g"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("./g/.")), U("http://a/b/c/g/"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g/./h")), U("http://a/b/c/g/h"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g/../h")), U("http://a/b/c/h"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g;x=1/./y")), U("http://a/b/c/g;x=1/y"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g;x=1/../y")), U("http://a/b/c/y"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g?y/./x")), U("http://a/b/c/g?y/./x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g?y/../x")), U("http://a/b/c/g?y/../x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g#s/./x")), U("http://a/b/c/g#s/./x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("g#s/../x")), U("http://a/b/c/g#s/../x"));
VERIFY_ARE_EQUAL(baseUri.resolve_uri(U("http:g")), U("http:g"));
}

} // SUITE(resolve_uri_tests)

}}}