cpp-netlib/uri

Copy assignment operator invalidates URI path

Closed this issue · 4 comments

Hello,

I have noticed a difference in behavior between the copy constructor and the copy assignment operator in the uri class. This is the code I used:

#include <iostream>
#include <string>

#include "network/uri.hpp"

using network::uri;

void PrintParts(const uri &u) {
  std::cout << "scheme    : " << u.scheme() << std::endl;
  std::cout << "user_info : " << u.user_info() << std::endl;
  std::cout << "host      : " << u.host() << std::endl;
  std::cout << "port      : " << u.port() << std::endl;
  std::cout << "path      : " << u.path() << std::endl;
  std::cout << "query     : " << u.query() << std::endl;
  std::cout << "fragment  : " << u.fragment() << std::endl;
}

int main(int argc, char *argv[])
try {
  // Using copy constructor
  uri u1 = uri(argv[1]);
  PrintParts(u1);

  std::cout << "==============================" << std::endl;
  
  // Using copy assignment
  uri u2;
  u2 = uri(argv[1]);
  PrintParts(u2);

  return 0;
} catch (std::exception &e) {
  std::cerr << "error: " << e.what() << std::endl;
  return 1;
}

Then, when I run it with a URI beginning with file://, I get the following result:

$ ./test file:///path/to/file.txt
scheme    : file
user_info : 
host      : 
port      : 
path      : /path/to/file.txt
query     : 
fragment  : 
==============================
scheme    : file
user_info : 
host      : 
port      : 
path      : path/to/file.txt
query     : 
fragment  : 

Notice that the copy assignment operator suppresses the leading forward slash in the path. Pull request #93 doesn't seem to solve this problem.

$ ./test "file:///path/to/file.txt?query=value#foo"
scheme    : file
user_info : 
host      : 
port      : 
path      : /path/to/file.txt
query     : query=value
fragment  : foo
==============================
scheme    : file
user_info : 
host      : 
port      : 
path      : path/to/file.txt?
query     : uery=value#
fragment  : oo

Apparently, the copy assignment operator shifts the boundaries of each part of the URI by one character.

Thank you for the report. I can confirm that I can reproduce this issue. I'll take a look at the fix.

@andrecunha, can you see if the fixes I applied in PR #99 fixes your issue?

@glynos , I've run some tests here, and everything seems to work fine. Thank you very much for the prompt fix!