0

I have one base URI like https://stackoverflow.com/questions/ask and a relative URI. I want to combine them all into one absolute URI.

Examples:

  • Relative URI: ../

    Result: https://stackoverflow.com/questions

  • Relative URI: /abc/kk?6

    Result: https://stackoverflow.com/abc/kk?6

  • Relative URI: task.php?ui=4

    Result: https://stackoverflow.com/questions/task.php?ui=4

How can I do this?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
montjet
  • 525
  • 1
  • 4
  • 13
  • What have you tried? Please show your code. – Ted Lyngmo Sep 20 '21 at 08:43
  • C++ does not have URL parsing capability. Do you have a specific 3rd-party library in mind? After you parsed the URL, you can use `std::filesystem::path` capabilities to combine the path parts (simply the `/` operator). – rustyx Sep 20 '21 at 08:58
  • You can check this thread: https://stackoverflow.com/questions/2616011/easy-way-to-parse-a-url-in-c-cross-platform it may help you. Good luck – Recon Sep 20 '21 at 09:43
  • Read [RFC 3986 Section 5](https://datatracker.ietf.org/doc/html/rfc3986#section-5) which defines how to combine and resolve URI segments – Remy Lebeau Sep 20 '21 at 17:22

1 Answers1

2

It seems the proposal to add URI-handling to standard C++, https://isocpp.org/files/papers/n3975.html, is dead and/or stuck in committee.

You therefore have to write your own or use a 3rd party - e.g., Qt has QUrl with https://doc.qt.io/qt-5/qurl.html#resolved

QUrl QUrl::resolved(const QUrl &relative) const 
Hans Olsson
  • 11,123
  • 15
  • 38
  • I expect this result. Unfortunately, Qt is a very large framework. I would prefer a less complex library. I found uriparser library: https://uriparser.github.io/doc/api/latest/#resolution Unfortunately it is written in pure C - modern c ++ would be better – montjet Sep 20 '21 at 09:35