4 #include <boost/filesystem.hpp>
9 #include <netinet/in.h>
11 #include "json/json.h"
14 static std::string fromBase64(std::string base64_string);
15 static std::string toBase64(
const std::string &tob64);
16 static std::string stripQuotes(
const std::string &value);
17 static std::string addQuotes(
const std::string &value);
18 static std::string extractField(
const std::string &in,
unsigned int field_id);
19 static Json::Value parseJSON(
const std::string &json_str);
20 static Json::Value parseJSONFile(
const boost::filesystem::path &filename);
21 static std::string jsonToStr(
const Json::Value &json);
22 static std::string jsonToCanonicalStr(
const Json::Value &json);
23 static std::string genPrettyName();
24 static std::string readFile(
const boost::filesystem::path &filename,
bool trim =
false);
26 static void writeFile(
const boost::filesystem::path &filename,
const char *content,
size_t size);
27 static void writeFile(
const boost::filesystem::path &filename,
const std::string &content,
28 bool create_directories =
true);
29 static void writeFile(
const boost::filesystem::path &filename,
const Json::Value &content,
30 bool create_directories =
true);
31 static void copyDir(
const boost::filesystem::path &from,
const boost::filesystem::path &to);
32 static std::string readFileFromArchive(std::istream &as,
const std::string &filename,
bool trim =
false);
33 static void writeArchive(
const std::map<std::string, std::string> &entries, std::ostream &as);
34 static void removeFileFromArchive(
const boost::filesystem::path &archive_path,
const std::string &filename);
35 static Json::Value getHardwareInfo();
36 static Json::Value getNetworkInfo();
37 static std::string getHostname();
38 static std::string randomUuid();
39 static sockaddr_storage ipGetSockaddr(
int fd);
40 static std::string ipDisplayName(
const sockaddr_storage &saddr);
41 static int ipPort(
const sockaddr_storage &saddr);
42 static int shell(
const std::string &command, std::string *output,
bool include_stderr =
false);
43 static boost::filesystem::path absolutePath(
const boost::filesystem::path &root,
const boost::filesystem::path &file);
44 static void createDirectories(
const boost::filesystem::path &path, mode_t mode);
45 static bool createSecureDirectory(
const boost::filesystem::path &path);
46 static std::string urlEncode(
const std::string &input);
47 static CURL *curlDupHandleWrapper(CURL *curl_in,
bool using_pkcs11);
48 static std::vector<boost::filesystem::path> getDirEntriesByExt(
const boost::filesystem::path &dir_path,
49 const std::string &ext);
50 static void setStorageRootPath(
const std::string &storage_root_path);
51 static boost::filesystem::path getStorageRootPath();
53 static void setUserAgent(std::string user_agent);
54 static const char *getUserAgent();
56 static void setCaPath(boost::filesystem::path path);
57 static const char *getCaPath();
60 static std::string storage_root_path_;
61 static std::string user_agent_;
62 static boost::filesystem::path ca_path_;
74 void PutContents(
const std::string &contents)
const;
75 boost::filesystem::path Path()
const;
76 std::string PathString()
const;
79 boost::filesystem::path tmp_name_;
88 boost::filesystem::path Path()
const;
89 std::string PathString()
const;
90 boost::filesystem::path operator/(
const boost::filesystem::path &subdir)
const;
93 boost::filesystem::path tmp_name_;
100 template <
typename T>
101 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
102 template <
typename T>
103 using StructGuardInt = std::unique_ptr<T, int (*)(T *)>;
108 Socket(
int fd) : socket_fd_(fd) {}
114 int &operator*() {
return socket_fd_; }
115 std::string toString()
const;
118 void bind(in_port_t port,
bool reuse =
true)
const;
126 ConnectionSocket(
const std::string &ip, in_port_t port, in_port_t bind_port = 0);
133 struct sockaddr_in remote_sock_address_;
139 in_port_t port()
const {
return _port; }
150 CURL *get() {
return handle; }
156 template <
typename... T>
157 static void curlEasySetoptWrapper(CURL *curl_handle, CURLoption option, T &&... args) {
158 const CURLcode retval = curl_easy_setopt(curl_handle, option, std::forward<T>(args)...);
160 throw std::runtime_error(std::string(
"curl_easy_setopt error: ") + curl_easy_strerror(retval));
168 using _Single_object = std::unique_ptr<T>;
173 using _Unknown_bound = std::unique_ptr<T[]>;
176 template <
class T,
size_t N>
178 using _Known_bound = void;
181 template <
class T,
class... Args>
182 typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
183 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
187 typename _Unique_if<T>::_Unknown_bound make_unique(
size_t n) {
188 using U =
typename std::remove_extent<T>::type;
189 return std::unique_ptr<T>(
new U[n]());
192 template <
class T,
class... Args>
193 typename _Unique_if<T>::_Known_bound make_unique(Args &&...) =
delete;