Skip to content

Commit

Permalink
Fix #1747
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jan 27, 2024
1 parent 4ef9ed8 commit 2ce7c22
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_HTTPLIB_H

#define CPPHTTPLIB_VERSION "0.14.3"
#define CPPHTTPLIB_VERSION "0.15.0"

/*
* Configuration
Expand Down Expand Up @@ -4637,28 +4637,31 @@ inline std::string to_lower(const char *beg, const char *end) {
return out;
}

inline std::string make_multipart_data_boundary() {
inline std::string random_string(size_t length) {
static const char data[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

// std::random_device might actually be deterministic on some
// platforms, but due to lack of support in the c++ standard library,
// 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
std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};
std::mt19937 engine(seed_sequence);
static std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};

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)];
}

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) {
auto valid = true;
for (size_t i = 0; i < boundary.size(); i++) {
Expand Down Expand Up @@ -5133,20 +5136,6 @@ inline bool parse_www_authenticate(const Response &res,
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 {
public:
explicit ContentProviderAdapter(
Expand Down

0 comments on commit 2ce7c22

Please sign in to comment.