Kurlyk
Loading...
Searching...
No Matches
utility_functions_example.cpp
Go to the documentation of this file.
1#include <kurlyk.hpp>
2#include <iostream>
3
4int main() {
5 try {
6
7 // Example 1: Percent encode and decode
8 std::string original = "Hello World! How are you?";
9 std::string encoded = kurlyk::utils::percent_encode(original);
10 std::string decoded = kurlyk::utils::percent_decode(encoded);
11 KURLYK_PRINT << "Original: " << original << std::endl;
12 KURLYK_PRINT << "Encoded: " << encoded << std::endl;
13 KURLYK_PRINT << "Decoded: " << decoded << std::endl;
14
15 // Example 2: Validate email
16 std::vector<std::string> emails = {"valid@example.com", "invalid-email", "user@domain.ruf"};
17 for (const auto& email : emails) {
18 KURLYK_PRINT << "Email: " << email << " is valid: " << std::boolalpha
19 << kurlyk::utils::is_valid_email_id(email) << std::endl;
20 }
21
22 // Example 3: Extract protocol from URL
23 std::string url = "https://example.com/path?query=value";
24 std::string protocol = kurlyk::utils::extract_protocol(url);
25 KURLYK_PRINT << "Protocol in URL: " << protocol << std::endl;
26
27 // Example 4: Convert query parameters to a query string
28 kurlyk::QueryParams query = {{"key1", "value1"}, {"key2", "value2"}};
29 std::string query_string = kurlyk::utils::to_query_string(query);
30 KURLYK_PRINT << "Query string: " << query_string << std::endl;
31
32 // Example 5: Validate domain and path
33 std::string domain = "example.com";
34 std::string path = "/valid/path";
35 KURLYK_PRINT << "Domain " << domain << " is valid: " << std::boolalpha
36 << kurlyk::utils::is_valid_domain(domain) << std::endl;
37 KURLYK_PRINT << "Path " << path << " is valid: " << std::boolalpha
38 << kurlyk::utils::is_valid_path(path) << std::endl;
39
40 // Example 6: Validate URL
41 std::vector<std::string> protocols = {"http", "https"};
42 KURLYK_PRINT << "URL " << url << " is valid: " << std::boolalpha
43 << kurlyk::utils::is_valid_url(url, protocols) << std::endl;
44
45 // Example 7: Convert User-Agent to sec-ch-ua
46 std::string user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
47 std::string sec_ch_ua = kurlyk::utils::convert_user_agent_to_sec_ch_ua(user_agent);
48 KURLYK_PRINT << "sec-ch-ua header: " << sec_ch_ua << std::endl;
49
50 // Example 8: Get executable path
51 std::string exe_path = kurlyk::utils::get_exec_dir();
52 KURLYK_PRINT << "Executable path: " << exe_path << std::endl;
53
54 } catch (const std::exception& e) {
55 KURLYK_PRINT << "Error: " << e.what() << std::endl;
56 }
57
58 return 0;
59}
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
std::string percent_decode(const std::string &value) noexcept
Decodes a Percent-Encoded string.
bool is_valid_url(const std::string &url, const std::vector< std::string > &protocol)
Validates if a URL is correctly formatted.
bool is_valid_email_id(const std::string &str)
Validates an email address format.
std::string extract_protocol(const std::string &url)
Extracts the protocol from a URL.
Definition url_utils.hpp:13
std::string to_query_string(const QueryParams &query, const std::string &prefix=std::string()) noexcept
Converts a map of query parameters into a URL query string.
bool is_valid_path(const std::string &path)
Checks if a path is correctly formatted.
Definition url_utils.hpp:89
std::string convert_user_agent_to_sec_ch_ua(const std::string &user_agent)
Converts a User-Agent string to a sec-ch-ua header value.
std::string get_exec_dir()
Retrieves the directory of the executable file.
std::string percent_encode(const std::string &value) noexcept
Encodes a string using Percent Encoding according to RFC 3986.
bool is_valid_domain(const std::string &domain)
Validates if a domain name is correctly formatted.
Definition url_utils.hpp:62
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT