cpp-netlib/uri

Error on removing adjacent slashes

Closed this issue · 5 comments

Hello,

Adjacent slashes are not removed in some cases.
Please find the test case for the problem below:

TEST(uri_normalization_test, path_double_dash2) {
  network::uri instance("http://www.example.com/abc//elementary");
  ASSERT_EQ("http://www.example.com/abc/elementary",
            instance.normalize(network::uri_comparison_level::syntax_based).string());
}

When I run make test, CTest is failing with following output:

/home/user/cpp-netlib-uri_test/test/uri_normalization_test.cpp:143: Failure
Value of: instance.normalize(network::uri_comparison_level::syntax_based).string()
Actual: "http://www.example.com/abc//elementary"
Expected: "http://www.example.com/abc/elementary"
[ FAILED ] uri_normalization_test.path_double_dash2 (0 ms)

If we try to normalize http://www.example.com/abc//elementary, the normalized value should be http://www.example.com/abc/elementary.

Thanks.

@ncnxc: thanks for the report. Can you review this PR to confirm that it fixes your issue?

@glynos I confirm that this PR fixes this issue.

But I think the problem lies in how normalize_path_segments is implemented. In the current version:

  • First single dots are removed.
  • Then double dots are evaluated.
  • And the empty segments are erased.

Unrelated to the specific case of this issue, above approach doesn't work the URIs like "http://www.example.com/abc//.//../elementary". So maybe, it could be better to rewrite normalize_pah_segments function to walk the path. For example;

let normalized_segments be empty
for each segment in path_segments:
    if segment is empty or equal to ".", then
          skip the segment and continue
    if segment is "..", then
          if normalized_segments empty, then
                  throw an exception
          otherwise
                  remove the last element from normalized_segments
    otherwise
          add segment to the end of normalized_segments

let normalized_path be "/" + normalized_segments[0] ... + "/" + normalized_segments[N - 1]

What is your thought?

I took this suggestion and indeed it is much cleaner. You can find the code in the last commit.

Thank you very much. I am looking forward for v1.0.4 release :)

There you go.