mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	Add Response type to util::http.
This commit is contained in:
		
							parent
							
								
									e3b891de41
								
							
						
					
					
						commit
						3d30b4bbbc
					
				
					 3 changed files with 94 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -55,6 +55,50 @@ std::string Request::getHeader(const std::string& name) const
 | 
			
		|||
    return headers.at(name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Response::Response(int status)
 | 
			
		||||
    : status(status), headers()
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Response::setHeader(const std::string& name, const std::string& value)
 | 
			
		||||
{
 | 
			
		||||
    headers[name] = value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string Response::toString() const
 | 
			
		||||
{
 | 
			
		||||
    std::stringstream ss;
 | 
			
		||||
    ss << "HTTP/1.1 " << status << ' ' << getStatusMessage() << "\r\n";
 | 
			
		||||
    for(auto& pair : headers)
 | 
			
		||||
        ss << pair.first << ": " << pair.second << "\r\n";
 | 
			
		||||
    ss << "\r\n"; 
 | 
			
		||||
    return ss.str();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::string Response::getStatusMessage() const
 | 
			
		||||
{
 | 
			
		||||
    switch(status) {
 | 
			
		||||
        case 105:
 | 
			
		||||
            return "Name Not Resolved";
 | 
			
		||||
        case 200:
 | 
			
		||||
            return "OK";
 | 
			
		||||
        case 400:
 | 
			
		||||
            return "Bad Request";
 | 
			
		||||
        case 404:
 | 
			
		||||
            return "Not Found";
 | 
			
		||||
        case 408:
 | 
			
		||||
            return "Request Timeout";
 | 
			
		||||
        case 500:
 | 
			
		||||
            return "Internal Server Error";
 | 
			
		||||
        case 502:
 | 
			
		||||
            return "Not Implemented";
 | 
			
		||||
        case 504:
 | 
			
		||||
            return "Gateway Timeout";
 | 
			
		||||
        default:
 | 
			
		||||
            return std::string();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,11 +9,15 @@ namespace util {
 | 
			
		|||
namespace http {
 | 
			
		||||
 | 
			
		||||
class Request {
 | 
			
		||||
 | 
			
		||||
    void parseRequestLine(const std::string& line);
 | 
			
		||||
 | 
			
		||||
    void parseHeaderLine(const std::string& line);
 | 
			
		||||
public:
 | 
			
		||||
    Request(const std::string& data);
 | 
			
		||||
 | 
			
		||||
    Request();
 | 
			
		||||
 | 
			
		||||
    std::string getMethod() const;
 | 
			
		||||
 | 
			
		||||
    std::string getUri() const;
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +39,29 @@ private:
 | 
			
		|||
    std::map<std::string, std::string> headers;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Response {
 | 
			
		||||
public:
 | 
			
		||||
 | 
			
		||||
    Response(int status);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @note overrides existing header values with the same name
 | 
			
		||||
     */
 | 
			
		||||
    void setHeader(const std::string& name, const std::string& value);
 | 
			
		||||
 | 
			
		||||
    std::string toString() const;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return the message associated with the satus of this response, or the
 | 
			
		||||
     *  empty string if the status number is invalid
 | 
			
		||||
     */
 | 
			
		||||
    std::string getStatusMessage() const;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    int status;
 | 
			
		||||
    std::map<std::string, std::string> headers;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue