4 #include <boost/algorithm/hex.hpp>
5 #include <boost/algorithm/string.hpp>
6 #include <boost/filesystem.hpp>
12 #include "json/json.h"
14 #include "crypto/crypto.h"
15 #include "http/httpinterface.h"
16 #include "logging/logging.h"
18 #include "utilities/utils.h"
23 HttpFake(
const boost::filesystem::path &test_dir_in, std::string flavor =
"",
24 const boost::filesystem::path &meta_dir_in =
"")
25 : test_dir(test_dir_in), flavor_(std::move(flavor)), meta_dir(meta_dir_in) {
26 if (meta_dir.empty()) {
27 meta_dir = temp_meta_dir.Path();
34 void setCerts(
const std::string &ca, CryptoSource ca_source,
const std::string &cert, CryptoSource cert_source,
35 const std::string &pkey, CryptoSource pkey_source)
override {
45 bool rewrite(std::string &url,
const std::string &pattern) {
46 size_t pat_pos = url.find(pattern);
47 if (pat_pos == std::string::npos) {
50 size_t ext_pos = pattern.find(
".json");
51 if (ext_pos == std::string::npos) {
52 LOG_ERROR <<
"Invalid pattern";
57 url.replace(pat_pos + ext_pos, std::string(
".json").size(),
"_" + flavor_ +
".json");
61 virtual HttpResponse handle_event(
const std::string &url,
const Json::Value &
data) {
68 HttpResponse get(
const std::string &url, int64_t maxsize)
override {
70 std::cout <<
"URL requested: " << url <<
"\n";
72 std::string new_url = url;
73 if (!flavor_.empty()) {
74 rewrite(new_url,
"director/targets.json") || rewrite(new_url,
"repo/timestamp.json") ||
75 rewrite(new_url,
"repo/targets.json") || rewrite(new_url,
"snapshot.json");
78 std::cout <<
"Rewritten to: " << new_url <<
"\n";
82 const boost::filesystem::path path = meta_dir / new_url.substr(tls_server.size());
84 std::cout <<
"file served: " << path <<
"\n";
86 if (boost::filesystem::exists(path)) {
87 return HttpResponse(Utils::readFile(path), 200, CURLE_OK,
"");
89 std::cout <<
"not found: " << path <<
"\n";
94 HttpResponse post(
const std::string &url,
const std::string &content_type,
const std::string &
data)
override {
101 HttpResponse post(
const std::string &url,
const Json::Value &
data)
override {
102 if (url.find(
"/devices") != std::string::npos || url.find(
"/director/ecus") != std::string::npos || url.empty()) {
103 Utils::writeFile((test_dir /
"post.json").
string(),
data);
104 return HttpResponse(Utils::readFile(
"tests/test_data/cred.p12"), 200, CURLE_OK,
"");
105 }
else if (url.find(
"/events") != std::string::npos) {
106 return handle_event(url,
data);
111 HttpResponse put(
const std::string &url,
const std::string &content_type,
const std::string &
data)
override {
118 HttpResponse put(
const std::string &url,
const Json::Value &
data)
override {
119 last_manifest =
data;
123 std::future<HttpResponse> downloadAsync(
const std::string &url, curl_write_callback write_cb,
124 curl_xferinfo_callback progress_cb,
void *userp, curl_off_t from,
125 CurlHandler *easyp)
override {
131 std::cout <<
"URL requested: " << url <<
"\n";
132 const boost::filesystem::path path = meta_dir / url.substr(tls_server.size());
133 std::cout <<
"file served: " << path <<
"\n";
135 std::promise<HttpResponse> resp_promise;
136 auto resp_future = resp_promise.get_future();
138 [path, write_cb, progress_cb, userp, url](std::promise<HttpResponse> promise) {
139 const std::string content = Utils::readFile(path.string());
140 for (
unsigned int i = 0; i < content.size(); ++i) {
141 write_cb(
const_cast<char *
>(&content[i]), 1, 1, userp);
142 progress_cb(userp, 0, 0, 0, 0);
143 if (url.find(
"downloads/repo/targets/primary_firmware.txt") != std::string::npos) {
144 std::this_thread::sleep_for(std::chrono::milliseconds(100));
147 promise.set_value(
HttpResponse(content, 200, CURLE_OK,
""));
149 std::move(resp_promise))
155 HttpResponse download(
const std::string &url, curl_write_callback write_cb, curl_xferinfo_callback progress_cb,
156 void *userp, curl_off_t from)
override {
157 return downloadAsync(url, write_cb, progress_cb, userp, from,
nullptr).get();
160 const std::string tls_server =
"https://tlsserver.com";
161 Json::Value last_manifest;
164 boost::filesystem::path test_dir;
166 boost::filesystem::path meta_dir;
170 #endif // HTTPFAKE_H_