diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 20:48:35 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-12-27 20:48:35 +0100 |
commit | 1a8f351248c38445189a397035e8a2cb3182ea6a (patch) | |
tree | 507c96dc5fc1e158f799cdeb13cfab382a7a0534 /http.cpp | |
parent | 263097e22bdf0a56007644e4d19605371dc79a8f (diff) |
Added HTTPRequest and HTTPResponse classes.
Diffstat (limited to 'http.cpp')
-rw-r--r-- | http.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/http.cpp b/http.cpp new file mode 100644 index 0000000..8d69f93 --- /dev/null +++ b/http.cpp @@ -0,0 +1,46 @@ +#include "http.h" + +#include <boost/format.hpp> +#include <boost/algorithm/string.hpp> +#include <boost/algorithm/string/regex.hpp> + +#include <vector> + +HTTPRequest::HTTPRequest(std::istream& is) { + std::string line, firstline; + std::getline(is, firstline); + + std::vector<std::string> splitvec; + boost::algorithm::split(splitvec, firstline, boost::algorithm::is_space()); + + type = splitvec[0]; + path = splitvec[1]; + httpver = splitvec[2]; + std::cout << boost::format("%s %s %s\n") % type % path % httpver; + + while(is.good()) { + std::string line; + std::getline(is, line); + boost::trim(line); + if(!line.size()) continue; + std::vector<std::string> v; + boost::algorithm::split_regex(v, line, boost::regex(": ")); + headers[v[0]] = v[1]; + } +} + +HTTPResponse::HTTPResponse() { + httpver = "1.1"; +} + +void HTTPResponse::add_header(std::string key, std::string value) { + headers[key] = value; +} + +void HTTPResponse::write_headers(std::ostream& os) { + os << boost::format("HTTP/%s %d %s\r\n") % httpver % code % status; + for(HTTPHeaders::iterator it = headers.begin(); it != headers.end(); it++) { + os << boost::format("%s: %s\r\n") % it->first % it->second; + } + os << "\r\n"; +} |