You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.9 KiB
94 lines
2.9 KiB
#ifndef _BT_HELPERS_HPP_ |
|
#define _BT_HELPERS_HPP_ |
|
|
|
#include <unordered_map> |
|
#include <boost/regex.hpp> |
|
#include <json.hpp> |
|
|
|
namespace i18N { |
|
using namespace std; |
|
static const auto content_type_not_set= "A Content-Type header set to application/json is required."s; |
|
static const auto unable_to_parse_json= "Unable to parse JSON in body into a JSON-object."s; |
|
static const auto session_unavailable= "A server service is down, please try again later."s; |
|
static const auto wrong_format= "A valid JSON request was posted, but the JSON format was wrong."s; |
|
static const auto unable_to_parse_torrent_uri= "The torrent location was not accepted, the format was wrong."s; |
|
static const auto write_error= "Unable to write to "; |
|
}; |
|
|
|
namespace util { |
|
class uri { |
|
public: |
|
auto static parse(const std::string &request_path){ |
|
std::unordered_map<std::string,std::string> options; |
|
std::string option, value; |
|
bool collect=false; |
|
for(auto &c:request_path){ |
|
switch(c){ |
|
case '&': |
|
options.insert(std::make_pair(option,value)); |
|
option=""; |
|
collect=true; |
|
break; |
|
case '?': |
|
collect=true; |
|
break; |
|
case '=': |
|
collect=false; |
|
value=""; |
|
break; |
|
default: |
|
if(collect) |
|
option+=c; |
|
else |
|
value+=c; |
|
break; |
|
} |
|
} |
|
if(!option.empty() || !value.empty()) |
|
options.insert(std::make_pair(option,value)); |
|
return options; |
|
} |
|
}; |
|
const boost::regex regex("@(https?|ftp)://(-\\.)?([^\\s/?\\.#-]+\\.?)+(/[^\\s]*)?$@", boost::regex_constants::perl | boost::regex_constants::icase); |
|
|
|
class json { |
|
public: |
|
/*! @brief Wrapper for nlohmann::json::parse. If the parse fails, result.is_null() will be */ |
|
static nlohmann::json parse(std::istream &istream) { |
|
try { |
|
return nlohmann::json::parse(istream); |
|
} catch(const std::invalid_argument &exp) { |
|
|
|
} |
|
return nlohmann::json(nullptr); |
|
} |
|
/*! @brief wrapper around nlohmann::json::parse, but object returns null on throw */ |
|
static nlohmann::json parse(const std::string &string){ |
|
try { |
|
return nlohmann::json::parse(string); |
|
} catch(const std::invalid_argument &exp) { |
|
|
|
} |
|
return nlohmann::json(nullptr); |
|
} |
|
static bool has_property(const std::string &key, const nlohmann::json &object){ |
|
auto it=object.find(key); |
|
return it!=object.end(); |
|
} |
|
template<class T> |
|
static auto get(const std::string &key, const nlohmann::json &object, const T &default_value){ |
|
if(has_property(key,object)){ |
|
T r; |
|
try{ |
|
r=object[key]; //TODO fix use json parser where a non-throwable exist |
|
} catch(const std::exception&){ |
|
return default_value; |
|
} |
|
return r; |
|
} |
|
return default_value; |
|
} |
|
}; |
|
} |
|
|
|
#endif // _BT_HELPERS_HPP_
|
|
|