This commit is contained in:
yhirose 2024-01-27 12:55:59 -05:00
parent 4ef9ed80cd
commit 2ce7c22218

View File

@ -8,7 +8,7 @@
#ifndef CPPHTTPLIB_HTTPLIB_H #ifndef CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_VERSION "0.14.3" #define CPPHTTPLIB_VERSION "0.15.0"
/* /*
* Configuration * Configuration
@ -4637,28 +4637,31 @@ inline std::string to_lower(const char *beg, const char *end) {
return out; return out;
} }
inline std::string make_multipart_data_boundary() { inline std::string random_string(size_t length) {
static const char data[] = static const char data[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// std::random_device might actually be deterministic on some // std::random_device might actually be deterministic on some
// platforms, but due to lack of support in the c++ standard library, // platforms, but due to lack of support in the c++ standard library,
// doing better requires either some ugly hacks or breaking portability. // doing better requires either some ugly hacks or breaking portability.
std::random_device seed_gen; static std::random_device seed_gen;
// Request 128 bits of entropy for initialization // Request 128 bits of entropy for initialization
std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()}; static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};
std::mt19937 engine(seed_sequence);
std::string result = "--cpp-httplib-multipart-data-"; static std::mt19937 engine(seed_sequence);
for (auto i = 0; i < 16; i++) { std::string result;
for (size_t i = 0; i < length; i++) {
result += data[engine() % (sizeof(data) - 1)]; result += data[engine() % (sizeof(data) - 1)];
} }
return result; return result;
} }
inline std::string make_multipart_data_boundary() {
return "--cpp-httplib-multipart-data-" + detail::random_string(16);
}
inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { inline bool is_multipart_boundary_chars_valid(const std::string &boundary) {
auto valid = true; auto valid = true;
for (size_t i = 0; i < boundary.size(); i++) { for (size_t i = 0; i < boundary.size(); i++) {
@ -5133,20 +5136,6 @@ inline bool parse_www_authenticate(const Response &res,
return false; return false;
} }
// https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240
inline std::string random_string(size_t length) {
auto randchar = []() -> char {
const char charset[] = "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[static_cast<size_t>(std::rand()) % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
class ContentProviderAdapter { class ContentProviderAdapter {
public: public:
explicit ContentProviderAdapter( explicit ContentProviderAdapter(