Examples › General Examples
general_url_parser
Legacy: examples_general/url_parser/testprog.cpp · Modern: examples_modern/general/url_parser/modern.cpp
Modern equivalent of the gxsURL test. A URL is split with std::string_view (no copies, no fixed size buffers) into a struct of std::string_view parts; the port is parsed with std::from_chars and is a std::optional, so "no port given" is not port 0. The standard library has no URL class (C++ networking was not standardized), so this parser is a small function. For untrusted input in production, also check the characters allowed in each part (RFC 3986) or use a URL library.
Usage: modern URL ... (with no arguments it parses a few examples).
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxString / UString | std::string, std::string_view | C++98 / C++17 | string_view looks at text without copying it. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_general/url_parser/testprog.cpp 182 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #endif // __USE_ANSI_CPP__ #include "gxsurl.h" #include "gxs_ver.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif // Version number and program name const double ProgramVersionNumber = gxSocketVersion; const char *ProgramName = "gxsURL test program"; // Author information const char *ProducedBy = "DataReel Software Development"; const char *EmailAddress = "https://datareel.com"; // Program globals int parsing_url = 1; void PrintURLInfo(gxsURLInfo &u) { cout << "Uniform resource locator information" << "\n"; cout << "-------------------------------------------------------" << "\n"; cout << "Unchanged URL = " << u.url << "\n"; cout << "URL protocol = " << u.proto << "\n"; cout << "Extracted hostname = " << u.host << "\n"; cout << "Port number = " << u.port << "\n"; cout << "FTP type = " << u.ftp_type << "\n"; cout << "Path = " << u.path << "\n"; cout << "Directory = " << u.dir << "\n"; cout << "File = " << u.file << "\n"; cout << "Username = " << u.user << "\n"; cout << "Password = " << u.passwd << "\n"; cout << "Local filename of the URL document = " << u.local << "\n"; cout << "Source that requested URI was obtained = " << u.referer << "\n"; if(u.is_dynamic) { cout << "\n"; cout << "Dyanmic page information" << "\n"; cout << "-------------------------------------------------------" << "\n"; cout << "Query string = " << u.query_string.c_str() << "\n"; if(!u.dynamic_bin_dir.is_null()) { cout << "Binary dir = " << u.dynamic_bin_dir.c_str() << "\n"; } if(!u.dynamic_file_extension.is_null()) { cout << "File extension = " << u.dynamic_file_extension.c_str() << "\n"; } cout << "Local query file = " << u.local_query_file.c_str() << "\n"; } if(u.proxy) { // The exact string to pass to proxy server cout << "\n"; cout << "Proxy server information" << "\n"; cout << "-------------------------------------------------------" << "\n"; cout << "Unchanged URL = " << u.proxy->url << "\n"; cout << "URL protocol = " << u.proxy->proto << "\n"; cout << "Extracted hostname = " << u.proxy->host << "\n"; cout << "Port number = " << u.proxy->port << "\n"; cout << "FTP type = " << u.proxy->ftp_type << "\n"; cout << "Path = " << u.proxy->path << "\n"; cout << "Directory = " << u.proxy->dir << "\n"; cout << "File = " << u.proxy->file << "\n"; cout << "Username = " << u.proxy->user << "\n"; cout << "Password = " << u.proxy->passwd << "\n"; cout << "Local filename of the URL document = " << u.proxy->local << "\n"; cout << "Source that requested URI was obtained = " << u.proxy->referer << "\n"; } cout << "\n"; } void ListSupportedProtocols() { gxsURL url; for(int i = 0; i < gxsMAX_PROTOCOL_STRINGS; i++) { cout << url.GetProtocolString(i) << "//" << " Port Number - "; gxString u(url.GetProtocolString(i)); u += "//"; int port; url.GetPortNumber(u, port); cout << port << "\n"; } } void ParseURL(const char *URL) { gxsURLInfo u; gxsURL gxsurl; gxString url_buf = URL; gxsurl.SetDefaultProto(url_buf); if(!gxsurl.ParseURL(url_buf, u)) { cout << "Error parsing URL" << "\n"; return; } PrintURLInfo(u); } void HelpMessage(const char *program_name, const double version_number) { cout << "\n"; cout.setf(ios::showpoint | ios::fixed); cout.precision(3); cout << "\n"; cout << program_name << " version " << version_number << "\n"; cout << "Produced by: " << ProducedBy << ' ' << EmailAddress << "\n"; cout << "\n"; cout << "Usage: " << program_name << " [switches] URL" << "\n"; cout << "Example: " << program_name << " -p http://www.xyz.com/index.html" << "\n"; cout << "Switches: " << "\n"; cout << " -L = List all the protocols supported by this program." << "\n"; cout << " -p = Parse the specified URL (default)" << "\n"; cout << "\n"; return; } int ProcessArgs(char *arg) { switch(arg[1]) { case 'L' : parsing_url = 0; ListSupportedProtocols(); return 0; case 'p' : parsing_url = 1; break; default: cerr << "\n"; cerr << "Unknown switch " << arg << "\n"; cerr << "Exiting..." << "\n"; cerr << "\n"; return 0; } arg[0] = '\0'; return 1; // All command line arguments were valid } int main(int argc, char **argv) { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif if(argc < 2) { HelpMessage(ProgramName, ProgramVersionNumber); return 0; } // Process command ling arguments and files int narg; char *arg = argv[narg = 1]; while (narg < argc) { if(arg[0] != '\0') { if(arg[0] == '-') { // Look for command line arguments if(!ProcessArgs(arg)) return 0; // Exit if argument is not valid } else { if(parsing_url) { ParseURL((const char *)arg); } } arg = argv[++narg]; } } return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++17)
examples_modern/general/url_parser/modern.cpp 96 lines
#include <charconv> #include <cstdint> #include <iostream> #include <optional> #include <string_view> struct UrlInfo { std::string_view proto, user, passwd, host, path, dir, file, query; std::optional<std::uint16_t> port; }; std::optional<UrlInfo> parse_url(std::string_view url) { UrlInfo u; auto scheme = url.find("://"); if(scheme != std::string_view::npos) { u.proto = url.substr(0, scheme); url.remove_prefix(scheme + 3); } else { u.proto = "http"; // gxsURL::SetDefaultProto() } auto q = url.find('?'); if(q != std::string_view::npos) { u.query = url.substr(q + 1); url = url.substr(0, q); } auto slash = url.find('/'); std::string_view authority = url.substr(0, slash); u.path = slash == std::string_view::npos ? "/" : url.substr(slash); auto at = authority.rfind('@'); if(at != std::string_view::npos) { std::string_view cred = authority.substr(0, at); auto colon = cred.find(':'); u.user = cred.substr(0, colon); if(colon != std::string_view::npos) u.passwd = cred.substr(colon + 1); authority.remove_prefix(at + 1); } auto colon = authority.rfind(':'); if(colon != std::string_view::npos && authority.find(']') == std::string_view::npos) { std::string_view p = authority.substr(colon + 1); std::uint16_t port = 0; auto [end, ec] = std::from_chars(p.data(), p.data() + p.size(), port); if(ec != std::errc() || end != p.data() + p.size() || p.empty()) return std::nullopt; // Bad or out of range port u.port = port; authority = authority.substr(0, colon); } if(authority.empty()) return std::nullopt; u.host = authority; auto last = u.path.rfind('/'); u.dir = u.path.substr(0, last + 1); u.file = u.path.substr(last + 1); return u; } std::uint16_t default_port(std::string_view proto) { if(proto == "https") return 443; if(proto == "ftp") return 21; return 80; } void print(std::string_view url) { std::cout << "URL = " << url << "\n"; auto u = parse_url(url); if(!u) { std::cout << "Error parsing URL\n\n"; return; } std::cout << " protocol = " << u->proto << "\n host = " << u->host << "\n port = " << u->port.value_or(default_port(u->proto)) << (u->port ? "" : " (default)") << "\n path = " << u->path << "\n dir = " << u->dir << "\n file = " << u->file << "\n"; if(!u->user.empty()) std::cout << " user = " << u->user << "\n"; if(!u->query.empty()) std::cout << " query = " << u->query << "\n"; std::cout << "\n"; } int main(int argc, char **argv) { if(argc > 1) { for(int i = 1; i < argc; i++) print(argv[i]); return 0; } print("http://www.example.com/docs/index.html"); print("https://user:secret@www.example.com:8443/cgi-bin/q.cgi?a=1&b=2"); print("ftp://ftp.example.com/pub/file.tar.gz"); print("www.example.com"); print("http://www.example.com:99999/too/big/port"); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target general_url_parser ./build-modern/general_url_parser # or without CMake: g++ -std=c++17 -pthread -o general_url_parser examples_modern/general/url_parser/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
URL = http://www.example.com/docs/index.html protocol = http host = www.example.com port = 80 (default) path = /docs/index.html dir = /docs/ file = index.html URL = https://user:secret@www.example.com:8443/cgi-bin/q.cgi?a=1&b=2 protocol = https host = www.example.com port = 8443 path = /cgi-bin/q.cgi dir = /cgi-bin/ file = q.cgi user = user query = a=1&b=2 URL = ftp://ftp.example.com/pub/file.tar.gz protocol = ftp host = ftp.example.com port = 21 (default) path = /pub/file.tar.gz dir = /pub/ file = file.tar.gz URL = www.example.com protocol = http host = www.example.com port = 80 (default) path = / dir = / file = URL = http://www.example.com:99999/too/big/port Error parsing URL