File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,7 @@ using socket_t = int;
203
203
#include < string>
204
204
#include < sys/stat.h>
205
205
#include < thread>
206
+ #include < iomanip>
206
207
207
208
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
208
209
#include < openssl/err.h>
@@ -214,7 +215,6 @@ using socket_t = int;
214
215
#include < openssl/applink.c>
215
216
#endif
216
217
217
- #include < iomanip>
218
218
#include < iostream>
219
219
#include < sstream>
220
220
@@ -1457,6 +1457,33 @@ inline bool is_valid_path(const std::string &path) {
1457
1457
return true ;
1458
1458
}
1459
1459
1460
+ inline std::string encode_query_param (const std::string &value){
1461
+ std::ostringstream escaped;
1462
+ escaped.fill (' 0' );
1463
+ escaped << std::hex;
1464
+
1465
+ for (char const &c: value) {
1466
+ if (std::isalnum (c) ||
1467
+ c == ' -' ||
1468
+ c == ' _' ||
1469
+ c == ' .' ||
1470
+ c == ' !' ||
1471
+ c == ' ~' ||
1472
+ c == ' *' ||
1473
+ c == ' \' ' ||
1474
+ c == ' (' ||
1475
+ c == ' )' ) {
1476
+ escaped << c;
1477
+ } else {
1478
+ escaped << std::uppercase;
1479
+ escaped << ' %' << std::setw (2 ) << static_cast <int >(static_cast <unsigned char >(c));
1480
+ escaped << std::nouppercase;
1481
+ }
1482
+ }
1483
+
1484
+ return escaped.str ();
1485
+ }
1486
+
1460
1487
inline std::string encode_url (const std::string &s) {
1461
1488
std::string result;
1462
1489
@@ -3045,7 +3072,7 @@ inline std::string params_to_query_str(const Params ¶ms) {
3045
3072
if (it != params.begin ()) { query += " &" ; }
3046
3073
query += it->first ;
3047
3074
query += " =" ;
3048
- query += encode_url (it->second );
3075
+ query += encode_query_param (it->second );
3049
3076
}
3050
3077
return query;
3051
3078
}
Original file line number Diff line number Diff line change @@ -46,6 +46,18 @@ TEST(StartupTest, WSAStartup) {
46
46
}
47
47
#endif
48
48
49
+ TEST (EncodeQueryParamTest, ParseUnescapedChararactersTest){
50
+ string unescapedCharacters = " -_.!~*'()" ;
51
+
52
+ EXPECT_EQ (detail::encode_query_param (unescapedCharacters), " -_.!~*'()" );
53
+ }
54
+
55
+ TEST (EncodeQueryParamTest, ParseReservedCharactersTest){
56
+ string reservedCharacters = " ;,/?:@&=+$" ;
57
+
58
+ EXPECT_EQ (detail::encode_query_param (reservedCharacters), " %3B%2C%2F%3F%3A%40%26%3D%2B%24" );
59
+ }
60
+
49
61
TEST (TrimTests, TrimStringTests) {
50
62
EXPECT_EQ (" abc" , detail::trim_copy (" abc" ));
51
63
EXPECT_EQ (" abc" , detail::trim_copy (" abc " ));
You can’t perform that action at this time.
0 commit comments