Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
utility_functions_example.cpp
Go to the documentation of this file.
1
#include <
kurlyk.hpp
>
2
#include <iostream>
3
4
int
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
}
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::utils::percent_decode
std::string percent_decode(const std::string &value) noexcept
Decodes a Percent-Encoded string.
Definition
percent_encoding.hpp:39
kurlyk::utils::is_valid_url
bool is_valid_url(const std::string &url, const std::vector< std::string > &protocol)
Validates if a URL is correctly formatted.
Definition
url_utils.hpp:126
kurlyk::utils::is_valid_email_id
bool is_valid_email_id(const std::string &str)
Validates an email address format.
Definition
email_utils.hpp:13
kurlyk::utils::extract_protocol
std::string extract_protocol(const std::string &url)
Extracts the protocol from a URL.
Definition
url_utils.hpp:13
kurlyk::utils::to_query_string
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.
Definition
http_parser.hpp:76
kurlyk::utils::is_valid_path
bool is_valid_path(const std::string &path)
Checks if a path is correctly formatted.
Definition
url_utils.hpp:89
kurlyk::utils::convert_user_agent_to_sec_ch_ua
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.
Definition
user_agent_utils.hpp:13
kurlyk::utils::get_exec_dir
std::string get_exec_dir()
Retrieves the directory of the executable file.
Definition
path_utils.hpp:16
kurlyk::utils::percent_encode
std::string percent_encode(const std::string &value) noexcept
Encodes a string using Percent Encoding according to RFC 3986.
Definition
percent_encoding.hpp:17
kurlyk::utils::is_valid_domain
bool is_valid_domain(const std::string &domain)
Validates if a domain name is correctly formatted.
Definition
url_utils.hpp:62
kurlyk::QueryParams
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
Definition
CaseInsensitiveMultimap.hpp:65
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
main
int main()
Definition
utility_functions_example.cpp:4
examples
utility_functions_example.cpp
Generated by
1.17.0