This commit is contained in:
yhirose 2019-07-19 11:38:06 -04:00
parent 3d1ae3a3af
commit eaafa5d55c
3 changed files with 57 additions and 7 deletions

View file

@ -529,6 +529,38 @@ inline size_t to_utf8(int code, char *buff) {
return 0;
}
// NOTE: This code came up with the following stackoverflow post:
// https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
inline std::string base64_encode(const std::string &in) {
static const auto lookup =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string out;
out.reserve(in.size());
int val = 0;
int valb = -6;
for (uint8_t c : in) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
out.push_back(lookup[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) {
out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]);
}
while (out.size() % 4) {
out.push_back('=');
}
return out;
}
inline bool is_file(const std::string &path) {
struct stat st;
return stat(path.c_str(), &st) >= 0 && S_ISREG(st.st_mode);
@ -1439,6 +1471,12 @@ inline std::pair<std::string, std::string> make_range_header(uint64_t value,
return std::make_pair("Range", field);
}
inline std::pair<std::string, std::string>
make_basic_authentication_header(const std::string& username, const std::string& password) {
auto field = "Basic " + detail::base64_encode(username + ":" + password);
return std::make_pair("Authorization", field);
}
// Request implementation
inline bool Request::has_header(const char *key) const {
return detail::has_header(headers, key);