mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	Add util::GetWebuiDataDir(), HTTP parsing utilities and tests.
This commit is contained in:
		
							parent
							
								
									6857dcb911
								
							
						
					
					
						commit
						e3b891de41
					
				
					 6 changed files with 138 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -11,6 +11,7 @@ set(CORE_SRC
 | 
			
		|||
    "util/base64.cpp"
 | 
			
		||||
    "util/util.cpp"
 | 
			
		||||
    "util/Log.cpp"
 | 
			
		||||
    "util/HTTP.cpp"
 | 
			
		||||
    "tunnel/TransitTunnel.cpp"
 | 
			
		||||
    "tunnel/Tunnel.cpp"
 | 
			
		||||
    "tunnel/TunnelGateway.cpp"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										60
									
								
								core/util/HTTP.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								core/util/HTTP.cpp
									
										
									
									
									
										Normal 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
									
								
							
							
						
						
									
										42
									
								
								core/util/HTTP.h
									
										
									
									
									
										Normal 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__
 | 
			
		||||
| 
						 | 
				
			
			@ -193,6 +193,11 @@ namespace filesystem
 | 
			
		|||
        return pathTunnelsConfigFile;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    boost::filesystem::path GetWebuiDataDir()
 | 
			
		||||
    {
 | 
			
		||||
        return GetDataDir() / "webui";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    boost::filesystem::path GetDefaultDataDir()
 | 
			
		||||
    {
 | 
			
		||||
        // Custom path, or default path:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue