Browse Source

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

pull/271/head
EinMByte 9 years ago
parent
commit
e3b891de41
  1. 1
      core/CMakeLists.txt
  2. 60
      core/util/HTTP.cpp
  3. 42
      core/util/HTTP.h
  4. 5
      core/util/util.cpp
  5. 4
      core/util/util.h
  6. 26
      tests/Utility.cpp

1
core/CMakeLists.txt

@ -11,6 +11,7 @@ set(CORE_SRC
"util/base64.cpp" "util/base64.cpp"
"util/util.cpp" "util/util.cpp"
"util/Log.cpp" "util/Log.cpp"
"util/HTTP.cpp"
"tunnel/TransitTunnel.cpp" "tunnel/TransitTunnel.cpp"
"tunnel/Tunnel.cpp" "tunnel/Tunnel.cpp"
"tunnel/TunnelGateway.cpp" "tunnel/TunnelGateway.cpp"

60
core/util/HTTP.cpp

@ -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

@ -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__

5
core/util/util.cpp

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

4
core/util/util.h

@ -80,6 +80,10 @@ namespace util
*/ */
boost::filesystem::path GetDefaultDataDir(); 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. * Read a configuration file and store its contents in the given maps.

26
tests/Utility.cpp

@ -1,5 +1,6 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include "util/util.h" #include "util/util.h"
#include "util/HTTP.h"
BOOST_AUTO_TEST_SUITE(UtilityTests) BOOST_AUTO_TEST_SUITE(UtilityTests)
@ -88,4 +89,29 @@ BOOST_AUTO_TEST_CASE(ParseUrlPassword)
BOOST_CHECK_EQUAL(url("").pass_, ""); BOOST_CHECK_EQUAL(url("").pass_, "");
} }
BOOST_AUTO_TEST_CASE(ParseHTTPRequestNoHeaders)
{
Request req1("GET /index.html HTTP/1.1");
Request req2("POST / HTTP/1.0\r\n");
BOOST_CHECK_EQUAL(req1.getMethod(), "GET");
BOOST_CHECK_EQUAL(req1.getUri(), "/index.html");
BOOST_CHECK_EQUAL(req2.getMethod(), "POST");
BOOST_CHECK_EQUAL(req2.getUri(), "/");
}
BOOST_AUTO_TEST_CASE(ParseHTTPRequestWithHeaders)
{
Request req1(
"GET /index.html HTTP/1.1\r\n"
"Host: localhost\r\n"
);
Request req2(
"POST / HTTP/1.1\r\n"
"Host: localhost:123 \r\n"
);
BOOST_CHECK_EQUAL(req1.getHeader("Host"), "localhost");
BOOST_CHECK_EQUAL(req2.getHeader("Host"), "localhost:123");
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save