Add util::GetWebuiDataDir(), HTTP parsing utilities and tests.

This commit is contained in:
EinMByte 2015-09-06 16:15:46 +02:00
parent 6857dcb911
commit e3b891de41
6 changed files with 138 additions and 0 deletions

60
core/util/HTTP.cpp Normal file
View file

@ -0,0 +1,60 @@
#include "HTTP.h"
#include <boost/algorithm/string.hpp>
#include <sstream>
namespace i2p {
namespace util {
namespace http {
void Request::parseRequestLine(const std::string& line)
{
std::stringstream ss(line);
ss >> method;
ss >> uri;
}
void Request::parseHeaderLine(const std::string& line)
{
const std::size_t pos = line.find_first_of(':');
headers[boost::trim_copy(line.substr(0, pos))] = boost::trim_copy(line.substr(pos + 1));
}
Request::Request(const std::string& data)
{
std::stringstream ss(data);
std::string line;
std::getline(ss, line);
parseRequestLine(line);
while(std::getline(ss, line))
parseHeaderLine(line);
}
std::string Request::getMethod() const
{
return method;
}
std::string Request::getUri() const
{
return uri;
}
std::string Request::getHost() const
{
return host;
}
int Request::getPort() const
{
return port;
}
std::string Request::getHeader(const std::string& name) const
{
return headers.at(name);
}
}
}
}

42
core/util/HTTP.h Normal file
View file

@ -0,0 +1,42 @@
#ifndef _HTTP_H__
#define _HTTP_H__
#include <string>
#include <map>
namespace i2p {
namespace util {
namespace http {
class Request {
void parseRequestLine(const std::string& line);
void parseHeaderLine(const std::string& line);
public:
Request(const std::string& data);
std::string getMethod() const;
std::string getUri() const;
std::string getHost() const;
int getPort() const;
/**
* @throw std::out_of_range if no such header exists
*/
std::string getHeader(const std::string& name) const;
private:
std::string method;
std::string uri;
std::string host;
int port;
std::map<std::string, std::string> headers;
};
}
}
}
#endif // _HTTP_H__

View file

@ -193,6 +193,11 @@ namespace filesystem
return pathTunnelsConfigFile;
}
boost::filesystem::path GetWebuiDataDir()
{
return GetDataDir() / "webui";
}
boost::filesystem::path GetDefaultDataDir()
{
// Custom path, or default path:

View file

@ -80,6 +80,10 @@ namespace util
*/
boost::filesystem::path GetDefaultDataDir();
/**
* @return the default directory for webui data
*/
boost::filesystem::path GetWebuiDataDir();
/**
* Read a configuration file and store its contents in the given maps.