Add Response type to util::http.

This commit is contained in:
EinMByte 2015-09-06 18:51:19 +02:00
parent e3b891de41
commit 3d30b4bbbc
3 changed files with 94 additions and 0 deletions

View file

@ -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();
}
}
}
}
}

View file

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