2014-01-30 03:28:07 +00:00
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2014-12-23 02:20:39 +00:00
|
|
|
#include <iostream>
|
2014-10-31 18:17:52 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-02-01 13:28:30 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2014-01-30 03:28:07 +00:00
|
|
|
|
2014-02-01 03:09:55 +00:00
|
|
|
#define PAIRTYPE(t1, t2) std::pair<t1, t2>
|
|
|
|
|
2014-01-30 03:28:07 +00:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
2015-07-16 21:29:52 +00:00
|
|
|
namespace config
|
|
|
|
{
|
|
|
|
extern std::map<std::string, std::string> mapArgs;
|
|
|
|
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses command line arguments, i.e. stores them in config::mapArgs.
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
void OptionParser(int argc, const char* const argv[]);
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a command line argument from config::mapArgs as an int
|
|
|
|
* @param nDefault the default value to be returned
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
int GetArg(const std::string& strArg, int nDefault);
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a command line argument from config::mapArgs as a std::string
|
|
|
|
* @param strDefault the default value to be returned
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
std::string GetArg(const std::string& strArg, const std::string& strDefault);
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a command line argument from config::mapArgs as a C-style string
|
|
|
|
* @param nDefault the default value to be returned
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
const char* GetCharArg(const std::string& strArg, const std::string& nDefault);
|
2015-09-18 12:19:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if the argument is set, false otherwise
|
|
|
|
*/
|
|
|
|
bool HasArg(const std::string& strArg);
|
2015-07-16 21:29:52 +00:00
|
|
|
}
|
2014-02-01 03:09:55 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
namespace filesystem
|
|
|
|
{
|
2015-07-19 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* Change the application name.
|
|
|
|
*/
|
|
|
|
void SetAppName(const std::string& name);
|
2014-11-17 20:28:52 +00:00
|
|
|
|
2015-07-19 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* @return the application name.
|
|
|
|
*/
|
|
|
|
std::string GetAppName();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the path of the i2pd directory
|
|
|
|
*/
|
|
|
|
const boost::filesystem::path& GetDataDir();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the full path of a file within the i2pd directory
|
|
|
|
*/
|
|
|
|
std::string GetFullPath(const std::string& filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the path of the configuration file
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
boost::filesystem::path GetConfigFile();
|
2015-07-19 15:52:28 +00:00
|
|
|
|
2015-08-11 21:19:25 +00:00
|
|
|
/**
|
|
|
|
* @return the path of the tunnels configuration file
|
|
|
|
*/
|
|
|
|
boost::filesystem::path GetTunnelsConfigFile();
|
|
|
|
|
2015-07-19 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* @return the default directory for i2pd data
|
|
|
|
*/
|
|
|
|
boost::filesystem::path GetDefaultDataDir();
|
|
|
|
|
2015-09-06 14:15:46 +00:00
|
|
|
/**
|
|
|
|
* @return the default directory for webui data
|
|
|
|
*/
|
|
|
|
boost::filesystem::path GetWebuiDataDir();
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a configuration file and store its contents in the given maps.
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
|
2014-02-01 03:09:55 +00:00
|
|
|
std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the path of the certificates directory
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
boost::filesystem::path GetCertificatesDir();
|
2015-09-18 12:19:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the webui files.
|
|
|
|
* @throw std::runtime_error when installation fails
|
|
|
|
*/
|
|
|
|
void InstallFiles();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies all files and directories in src to dest.
|
|
|
|
* @warning overrides existing files
|
|
|
|
*/
|
|
|
|
void CopyDir(const boost::filesystem::path& src, const boost::filesystem::path& dest);
|
2015-07-16 21:29:52 +00:00
|
|
|
}
|
2014-02-01 03:09:55 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
namespace http
|
|
|
|
{
|
|
|
|
const char ETAG[] = "ETag";
|
|
|
|
const char IF_NONE_MATCH[] = "If-None-Match";
|
|
|
|
const char IF_MODIFIED_SINCE[] = "If-Modified-Since";
|
|
|
|
const char LAST_MODIFIED[] = "Last-Modified";
|
|
|
|
const char TRANSFER_ENCODING[] = "Transfer-Encoding";
|
2014-12-22 21:45:50 +00:00
|
|
|
|
2015-07-19 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* Perform an HTTP request.
|
|
|
|
* @return the result of the request, or an empty string if it fails
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
std::string httpRequest(const std::string& address);
|
2015-07-19 15:52:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the content of the given HTTP stream without headers
|
|
|
|
*/
|
|
|
|
std::string GetHttpContent(std::istream& response);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge chunks of a HTTP response into the gien std:ostream object.
|
|
|
|
*/
|
|
|
|
void MergeChunkedResponse(std::istream& response, std::ostream& merged);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an HTTP request through the i2p proxy.
|
|
|
|
* @return the HTTP status code
|
|
|
|
*/
|
|
|
|
int httpRequestViaI2pProxy(const std::string& address, std::string &content);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the decoded url
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
std::string urlDecode(const std::string& data);
|
|
|
|
|
2015-07-19 13:51:02 +00:00
|
|
|
/**
|
|
|
|
* Provides functionality for parsing URLs.
|
|
|
|
*/
|
2015-07-16 21:29:52 +00:00
|
|
|
struct url {
|
2015-07-19 13:51:02 +00:00
|
|
|
/**
|
|
|
|
* Parse a url given as a string.
|
|
|
|
*/
|
|
|
|
url(const std::string& url_s);
|
|
|
|
private:
|
|
|
|
void parse(const std::string& url_s);
|
|
|
|
public:
|
|
|
|
std::string protocol_, host_, path_, query_;
|
|
|
|
std::string portstr_;
|
|
|
|
unsigned int port_;
|
|
|
|
std::string user_;
|
|
|
|
std::string pass_;
|
2015-07-16 21:29:52 +00:00
|
|
|
};
|
|
|
|
}
|
2014-10-31 18:17:52 +00:00
|
|
|
|
2015-07-16 21:29:52 +00:00
|
|
|
namespace net
|
|
|
|
{
|
2015-07-19 15:52:28 +00:00
|
|
|
/**
|
|
|
|
* @return the maximum transmission unit, or 576 on failure
|
|
|
|
*/
|
|
|
|
int GetMTU(const boost::asio::ip::address& localAddress);
|
2015-07-16 21:29:52 +00:00
|
|
|
}
|
2014-01-30 03:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|