@@ -1442,7 +1442,7 @@ class ClientImpl {
1442
1442
void set_keep_alive (bool on);
1443
1443
void set_follow_location (bool on);
1444
1444
1445
- void set_url_encode (bool on);
1445
+ void set_path_encode (bool on);
1446
1446
1447
1447
void set_compress (bool on);
1448
1448
@@ -1554,7 +1554,7 @@ class ClientImpl {
1554
1554
bool keep_alive_ = false ;
1555
1555
bool follow_location_ = false ;
1556
1556
1557
- bool url_encode_ = true ;
1557
+ bool path_encode_ = true ;
1558
1558
1559
1559
int address_family_ = AF_UNSPEC;
1560
1560
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
@@ -1792,6 +1792,7 @@ class Client {
1792
1792
void set_keep_alive (bool on);
1793
1793
void set_follow_location (bool on);
1794
1794
1795
+ void set_path_encode (bool on);
1795
1796
void set_url_encode (bool on);
1796
1797
1797
1798
void set_compress (bool on);
@@ -2246,6 +2247,16 @@ std::string hosted_at(const std::string &hostname);
2246
2247
2247
2248
void hosted_at (const std::string &hostname, std::vector<std::string> &addrs);
2248
2249
2250
+ std::string encode_uri_component (const std::string &value);
2251
+
2252
+ std::string encode_uri (const std::string &value);
2253
+
2254
+ std::string decode_uri_component (const std::string &value);
2255
+
2256
+ std::string decode_uri (const std::string &value);
2257
+
2258
+ std::string encode_query_param (const std::string &value);
2259
+
2249
2260
std::string append_query_params (const std::string &path, const Params ¶ms);
2250
2261
2251
2262
std::pair<std::string, std::string> make_range_header (const Ranges &ranges);
@@ -2287,9 +2298,7 @@ struct FileStat {
2287
2298
int ret_ = -1 ;
2288
2299
};
2289
2300
2290
- std::string encode_query_param (const std::string &value);
2291
-
2292
- std::string decode_url (const std::string &s, bool convert_plus_to_space);
2301
+ std::string decode_path (const std::string &s, bool convert_plus_to_space);
2293
2302
2294
2303
std::string trim_copy (const std::string &s);
2295
2304
@@ -2759,28 +2768,7 @@ inline bool FileStat::is_dir() const {
2759
2768
return ret_ >= 0 && S_ISDIR (st_.st_mode );
2760
2769
}
2761
2770
2762
- inline std::string encode_query_param (const std::string &value) {
2763
- std::ostringstream escaped;
2764
- escaped.fill (' 0' );
2765
- escaped << std::hex;
2766
-
2767
- for (auto c : value) {
2768
- if (std::isalnum (static_cast <uint8_t >(c)) || c == ' -' || c == ' _' ||
2769
- c == ' .' || c == ' !' || c == ' ~' || c == ' *' || c == ' \' ' || c == ' (' ||
2770
- c == ' )' ) {
2771
- escaped << c;
2772
- } else {
2773
- escaped << std::uppercase;
2774
- escaped << ' %' << std::setw (2 )
2775
- << static_cast <int >(static_cast <unsigned char >(c));
2776
- escaped << std::nouppercase;
2777
- }
2778
- }
2779
-
2780
- return escaped.str ();
2781
- }
2782
-
2783
- inline std::string encode_url (const std::string &s) {
2771
+ inline std::string encode_path (const std::string &s) {
2784
2772
std::string result;
2785
2773
result.reserve (s.size ());
2786
2774
@@ -2812,8 +2800,8 @@ inline std::string encode_url(const std::string &s) {
2812
2800
return result;
2813
2801
}
2814
2802
2815
- inline std::string decode_url (const std::string &s,
2816
- bool convert_plus_to_space) {
2803
+ inline std::string decode_path (const std::string &s,
2804
+ bool convert_plus_to_space) {
2817
2805
std::string result;
2818
2806
2819
2807
for (size_t i = 0 ; i < s.size (); i++) {
@@ -4537,7 +4525,7 @@ inline bool parse_header(const char *beg, const char *end, T fn) {
4537
4525
case_ignore::equal (key, " Referer" )) {
4538
4526
fn (key, val);
4539
4527
} else {
4540
- fn (key, decode_url (val, false ));
4528
+ fn (key, decode_path (val, false ));
4541
4529
}
4542
4530
4543
4531
return true ;
@@ -5102,7 +5090,7 @@ inline std::string params_to_query_str(const Params ¶ms) {
5102
5090
if (it != params.begin ()) { query += " &" ; }
5103
5091
query += it->first ;
5104
5092
query += " =" ;
5105
- query += encode_query_param (it->second );
5093
+ query += httplib::encode_uri_component (it->second );
5106
5094
}
5107
5095
return query;
5108
5096
}
@@ -5125,7 +5113,7 @@ inline void parse_query_text(const char *data, std::size_t size,
5125
5113
});
5126
5114
5127
5115
if (!key.empty ()) {
5128
- params.emplace (decode_url (key, true ), decode_url (val, true ));
5116
+ params.emplace (decode_path (key, true ), decode_path (val, true ));
5129
5117
}
5130
5118
});
5131
5119
}
@@ -5435,7 +5423,7 @@ class MultipartFormDataParser {
5435
5423
5436
5424
std::smatch m2;
5437
5425
if (std::regex_match (it->second , m2, re_rfc5987_encoding)) {
5438
- file_.filename = decode_url (m2[1 ], false ); // override...
5426
+ file_.filename = decode_path (m2[1 ], false ); // override...
5439
5427
} else {
5440
5428
is_valid_ = false ;
5441
5429
return false ;
@@ -6258,6 +6246,94 @@ inline void hosted_at(const std::string &hostname,
6258
6246
}
6259
6247
}
6260
6248
6249
+ inline std::string encode_uri_component (const std::string &value) {
6250
+ std::ostringstream escaped;
6251
+ escaped.fill (' 0' );
6252
+ escaped << std::hex;
6253
+
6254
+ for (auto c : value) {
6255
+ if (std::isalnum (static_cast <uint8_t >(c)) || c == ' -' || c == ' _' ||
6256
+ c == ' .' || c == ' !' || c == ' ~' || c == ' *' || c == ' \' ' || c == ' (' ||
6257
+ c == ' )' ) {
6258
+ escaped << c;
6259
+ } else {
6260
+ escaped << std::uppercase;
6261
+ escaped << ' %' << std::setw (2 )
6262
+ << static_cast <int >(static_cast <unsigned char >(c));
6263
+ escaped << std::nouppercase;
6264
+ }
6265
+ }
6266
+
6267
+ return escaped.str ();
6268
+ }
6269
+
6270
+ inline std::string encode_uri (const std::string &value) {
6271
+ std::ostringstream escaped;
6272
+ escaped.fill (' 0' );
6273
+ escaped << std::hex;
6274
+
6275
+ for (auto c : value) {
6276
+ if (std::isalnum (static_cast <uint8_t >(c)) || c == ' -' || c == ' _' ||
6277
+ c == ' .' || c == ' !' || c == ' ~' || c == ' *' || c == ' \' ' || c == ' (' ||
6278
+ c == ' )' || c == ' ;' || c == ' /' || c == ' ?' || c == ' :' || c == ' @' ||
6279
+ c == ' &' || c == ' =' || c == ' +' || c == ' $' || c == ' ,' || c == ' #' ) {
6280
+ escaped << c;
6281
+ } else {
6282
+ escaped << std::uppercase;
6283
+ escaped << ' %' << std::setw (2 )
6284
+ << static_cast <int >(static_cast <unsigned char >(c));
6285
+ escaped << std::nouppercase;
6286
+ }
6287
+ }
6288
+
6289
+ return escaped.str ();
6290
+ }
6291
+
6292
+ inline std::string decode_uri_component (const std::string &value) {
6293
+ std::string result;
6294
+
6295
+ for (size_t i = 0 ; i < value.size (); i++) {
6296
+ if (value[i] == ' %' && i + 2 < value.size ()) {
6297
+ auto val = 0 ;
6298
+ if (detail::from_hex_to_i (value, i + 1 , 2 , val)) {
6299
+ result += static_cast <char >(val);
6300
+ i += 2 ;
6301
+ } else {
6302
+ result += value[i];
6303
+ }
6304
+ } else {
6305
+ result += value[i];
6306
+ }
6307
+ }
6308
+
6309
+ return result;
6310
+ }
6311
+
6312
+ inline std::string decode_uri (const std::string &value) {
6313
+ std::string result;
6314
+
6315
+ for (size_t i = 0 ; i < value.size (); i++) {
6316
+ if (value[i] == ' %' && i + 2 < value.size ()) {
6317
+ auto val = 0 ;
6318
+ if (detail::from_hex_to_i (value, i + 1 , 2 , val)) {
6319
+ result += static_cast <char >(val);
6320
+ i += 2 ;
6321
+ } else {
6322
+ result += value[i];
6323
+ }
6324
+ } else {
6325
+ result += value[i];
6326
+ }
6327
+ }
6328
+
6329
+ return result;
6330
+ }
6331
+
6332
+ [[deprecated (" Use encode_uri_component instead" )]]
6333
+ inline std::string encode_query_param (const std::string &value) {
6334
+ return encode_uri_component (value);
6335
+ }
6336
+
6261
6337
inline std::string append_query_params (const std::string &path,
6262
6338
const Params ¶ms) {
6263
6339
std::string path_with_query = path;
@@ -7063,7 +7139,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) const {
7063
7139
detail::divide (req.target , ' ?' ,
7064
7140
[&](const char *lhs_data, std::size_t lhs_size,
7065
7141
const char *rhs_data, std::size_t rhs_size) {
7066
- req.path = detail::decode_url (
7142
+ req.path = detail::decode_path (
7067
7143
std::string (lhs_data, lhs_size), false );
7068
7144
detail::parse_query_text (rhs_data, rhs_size, req.params );
7069
7145
});
@@ -7958,7 +8034,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
7958
8034
#endif
7959
8035
keep_alive_ = rhs.keep_alive_ ;
7960
8036
follow_location_ = rhs.follow_location_ ;
7961
- url_encode_ = rhs.url_encode_ ;
8037
+ path_encode_ = rhs.path_encode_ ;
7962
8038
address_family_ = rhs.address_family_ ;
7963
8039
tcp_nodelay_ = rhs.tcp_nodelay_ ;
7964
8040
ipv6_v6only_ = rhs.ipv6_v6only_ ;
@@ -8323,7 +8399,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
8323
8399
if (next_host.empty ()) { next_host = host_; }
8324
8400
if (next_path.empty ()) { next_path = " /" ; }
8325
8401
8326
- auto path = detail::decode_url (next_path, true ) + next_query;
8402
+ auto path = detail::decode_path (next_path, true ) + next_query;
8327
8403
8328
8404
// Same host redirect - use current client
8329
8405
if (next_scheme == scheme && next_host == host_ && next_port == port_) {
@@ -8418,7 +8494,7 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) {
8418
8494
client.set_keep_alive (keep_alive_);
8419
8495
client.set_follow_location (
8420
8496
true ); // Enable redirects to handle multi-step redirects
8421
- client.set_url_encode (url_encode_ );
8497
+ client.set_path_encode (path_encode_ );
8422
8498
client.set_compress (compress_);
8423
8499
client.set_decompress (decompress_);
8424
8500
@@ -8612,7 +8688,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
8612
8688
: append_query_params (req.path , req.params );
8613
8689
8614
8690
const auto &path =
8615
- url_encode_ ? detail::encode_url (path_with_query) : path_with_query;
8691
+ path_encode_ ? detail::encode_path (path_with_query) : path_with_query;
8616
8692
8617
8693
detail::write_request_line (bstrm, req.method , path);
8618
8694
@@ -9658,7 +9734,7 @@ inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; }
9658
9734
9659
9735
inline void ClientImpl::set_follow_location (bool on) { follow_location_ = on; }
9660
9736
9661
- inline void ClientImpl::set_url_encode (bool on) { url_encode_ = on; }
9737
+ inline void ClientImpl::set_path_encode (bool on) { path_encode_ = on; }
9662
9738
9663
9739
inline void
9664
9740
ClientImpl::set_hostname_addr_map (std::map<std::string, std::string> addr_map) {
@@ -11134,7 +11210,12 @@ inline void Client::set_follow_location(bool on) {
11134
11210
cli_->set_follow_location (on);
11135
11211
}
11136
11212
11137
- inline void Client::set_url_encode (bool on) { cli_->set_url_encode (on); }
11213
+ inline void Client::set_path_encode (bool on) { cli_->set_path_encode (on); }
11214
+
11215
+ [[deprecated (" Use set_path_encode instead" )]]
11216
+ inline void Client::set_url_encode (bool on) {
11217
+ cli_->set_path_encode (on);
11218
+ }
11138
11219
11139
11220
inline void Client::set_compress (bool on) { cli_->set_compress (on); }
11140
11221
0 commit comments