Skip to content

Commit

Permalink
libcore: Added a helper for splitting URI components
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 0ed1262 commit 7596b81
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doomsday/libs/core/include/de/net/webrequest.h
Expand Up @@ -55,6 +55,17 @@ class DE_PUBLIC WebRequest

Block result() const;

public:
static bool splitUriComponents(const String &uri,
String * scheme = nullptr,
String * authority = nullptr,
String * path = nullptr,
String * query = nullptr,
String * fragment = nullptr);

static String hostNameFromUri(const String &uri);
static String pathFromUri(const String &uri);

private:
DE_PRIVATE(d)
};
Expand Down
44 changes: 44 additions & 0 deletions doomsday/libs/core/src/net/webrequest.cpp
Expand Up @@ -17,6 +17,7 @@
*/

#include "de/WebRequest"
#include "de/RegExp"
#include "de/Async"

#include <c_plus/webrequest.h>
Expand Down Expand Up @@ -142,4 +143,47 @@ Block WebRequest::result() const
return result_WebRequest(d->web);
}

bool WebRequest::splitUriComponents(const String &uri,
String * scheme,
String * authority,
String * path,
String * query,
String * fragment)
{
static const RegExp reComps(R"(^(([A-Za-z0-9.-]+):)?(//([^/\?#]*))?([^\?#]*)(\?([^#]*))?(#(.*))?)");
RegExpMatch m;
if (reComps.match(uri, m))
{
if (scheme) *scheme = m.captured(2);
if (authority) *authority = m.captured(4);
if (path) *path = m.captured(5);
if (query) *query = m.captured(7);
if (fragment) *fragment = m.captured(9);
return true;
}
return false;
}

String WebRequest::hostNameFromUri(const String &uri)
{
String authority;
if (splitUriComponents(uri, nullptr, &authority))
{
static const RegExp reAuth(R"(([^@:]+@)?(\[[0-9A-Za-z:#]+\]|[^:]+)(:([0-9]+))?)");
RegExpMatch m;
if (reAuth.match(authority, m))
{
return m.captured(2);
}
}
return {};
}

String WebRequest::pathFromUri(const String &uri)
{
String path;
splitUriComponents(uri, nullptr, nullptr, &path);
return path;
}

} // namespace de

0 comments on commit 7596b81

Please sign in to comment.