Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method SetAcceptEncoding for customized Accept-Encoding header (#683
- Loading branch information
1 parent
e17dec0
commit 49432fc
Showing
10 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
add_library(cpr | ||
accept_encoding.cpp | ||
async.cpp | ||
auth.cpp | ||
bearer.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "cpr/accept_encoding.h" | ||
|
||
namespace cpr { | ||
|
||
void mapMethodsToString(const std::initializer_list<cpr::AcceptEncodingMethods>& from, std::vector<std::string>& to) { | ||
const auto* first = from.begin(); | ||
const auto* last = from.end(); | ||
auto output = std::back_inserter(to); | ||
while (first != last) { | ||
*output++ = AcceptEncodingMethodsStringMap.at(*first); | ||
first = std::next(first); | ||
} | ||
} | ||
|
||
std::string concatenateMethodsWithComma(const std::vector<std::string>& methods) { | ||
auto first = std::next(methods.begin()); | ||
auto last = methods.end(); | ||
std::string init = methods[0]; | ||
|
||
for (; first != last; ++first) { | ||
init = std::move(init) + ", " + *first; | ||
} | ||
return init; | ||
} | ||
|
||
AcceptEncoding::AcceptEncoding(const std::initializer_list<AcceptEncodingMethods>& methods) { | ||
methods_.clear(); | ||
mapMethodsToString(methods, methods_); | ||
} | ||
|
||
AcceptEncoding::AcceptEncoding(const std::initializer_list<std::string>& string_methods) : methods_{string_methods} {} | ||
|
||
bool AcceptEncoding::empty() const noexcept { | ||
return methods_.empty(); | ||
} | ||
|
||
const std::string AcceptEncoding::getString() const { | ||
return concatenateMethodsWithComma(methods_); | ||
} | ||
|
||
} // namespace cpr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef CPR_ACCEPT_ENCODING_H | ||
#define CPR_ACCEPT_ENCODING_H | ||
|
||
#include <curl/curlver.h> | ||
#include <initializer_list> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace cpr { | ||
|
||
enum class AcceptEncodingMethods { | ||
identity, | ||
deflate, | ||
zlib, | ||
gzip, | ||
}; | ||
|
||
static const std::map<AcceptEncodingMethods, std::string> AcceptEncodingMethodsStringMap{{AcceptEncodingMethods::identity, "identity"}, {AcceptEncodingMethods::deflate, "deflate"}, {AcceptEncodingMethods::zlib, "zlib"}, {AcceptEncodingMethods::gzip, "gzip"}}; | ||
|
||
class AcceptEncoding { | ||
public: | ||
AcceptEncoding() = default; | ||
AcceptEncoding(const std::initializer_list<AcceptEncodingMethods>& methods); | ||
AcceptEncoding(const std::initializer_list<std::string>& methods); | ||
|
||
bool empty() const noexcept; | ||
const std::string getString() const; | ||
|
||
private: | ||
std::vector<std::string> methods_; | ||
}; | ||
|
||
} // namespace cpr | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters