2#ifndef KURLYK_HEADER_KURLYK_UTILS_HTTP_PARSER_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_UTILS_HTTP_PARSER_HPP_INCLUDED
21 std::string header(buffer, size);
22 if (header.size() < 3)
return;
23 std::size_t colon_pos = header.find_first_of(
":");
24 std::size_t start_pos = colon_pos + 1;
25 if (colon_pos == std::string::npos || colon_pos == 0)
return;
26 key = header.substr(0, colon_pos);
27 std::size_t end_pos = header.find_first_not_of(
" ", start_pos);
28 value = (end_pos != std::string::npos) ? header.substr(end_pos) : std::string();
31 while (!value.empty() && (value.back() ==
'\r' || value.back() ==
'\n')) {
36# ifdef KURLYK_USE_CURL
44 const std::string &prefix = std::string()) noexcept {
45 if (query.empty())
return std::string();
46 std::string query_string(prefix);
47 CURL *curl = curl_easy_init();
48 if (!curl)
return query_string;
51 const size_t index_end = query.size() - 1;
52 for(
const auto& pair : query) {
53 char *escaped_key = curl_easy_escape(curl, pair.first.c_str(), pair.first.size());
55 query_string += std::string(escaped_key);
56 curl_free(escaped_key);
59 char *escaped_value = curl_easy_escape(curl, pair.second.c_str(), pair.second.size());
61 query_string += std::string(escaped_value);
62 curl_free(escaped_value);
64 if (index != index_end) query_string +=
"&";
67 curl_easy_cleanup(curl);
79 const std::string &prefix = std::string()) noexcept {
80 std::string result(prefix);
83 for (
const auto &field : query) {
102 for (
auto it = cookies.begin(); it != cookies.end(); ++it) {
103 if (it != cookies.begin()) {
106 result += it->first +
"=" + it->second;
118 for (
auto it = cookies.begin(); it != cookies.end(); ++it) {
119 if (it != cookies.begin()) {
122 result += it->second.name +
"=" + it->second.value;
124 if (!it->second.path.empty()) {
125 result +=
"; Path=" + it->second.path;
128 if (it->second.expiration_date != 0) {
129 result +=
"; Expires=" + std::to_string(it->second.expiration_date);
141 std::vector<std::string> list_fragment;
143 const std::string secure_prefix(
"__Secure-");
144 const std::string host_prefix(
"__Host-");
145 std::size_t start_pos = 0;
148 bool is_option =
false;
149 std::size_t separator_pos = cookie.find_first_of(
"=;", start_pos);
150 if (separator_pos != std::string::npos) {
151 std::string name = cookie.substr(start_pos, (separator_pos - start_pos));
153 if (name.size() > host_prefix.size() && name.substr(0, 2) ==
"__") {
154 std::size_t prefix_pos = name.find_first_of(
"-");
155 if(prefix_pos != std::string::npos) {
156 name = name.substr(prefix_pos + 1, name.size() - prefix_pos - 1);
167 start_pos = (cookie[separator_pos] ==
';') ? separator_pos + 2 : separator_pos + 1;
172 std::size_t end_pos = cookie.find(
"; ", separator_pos + 1);
173 if (end_pos == std::string::npos) {
174 std::string value = cookie.substr(separator_pos + 1, cookie.size() - separator_pos - 2);
175 Cookie cookie_obj{name, value};
176 if (!is_option) cookies.emplace(cookie_obj.
name, cookie_obj);
179 std::string value = cookie.substr(separator_pos + 1, end_pos - separator_pos - 1);
180 start_pos = end_pos + 2;
181 Cookie cookie_obj{name, value};
182 if (!is_option) cookies.emplace(cookie_obj.
name, cookie_obj);
Represents an HTTP cookie.
std::string name
The name of the cookie.
std::unordered_multimap< std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual > CaseInsensitiveMultimap
A case-insensitive unordered multimap for storing HTTP headers.
bool case_insensitive_equal(const std::string &str1, const std::string &str2) noexcept
Compares two strings case-insensitively.
std::string to_cookie_string(const CaseInsensitiveMultimap &cookies)
Converts a CaseInsensitiveMultimap to a string format suitable for HTTP Cookie headers.
void parse_http_header_pair(const char *buffer, const size_t &size, std::string &key, std::string &value)
Parses a header pair from a buffer.
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.
Cookies parse_cookie(std::string cookie)
Parses a cookie string into a Cookies object.
std::string percent_encode(const std::string &value) noexcept
Encodes a string using Percent Encoding according to RFC 3986.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
utils::CaseInsensitiveCookieMultimap Cookies
Alias for HTTP cookies, stored case-insensitively.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.