summaryrefslogtreecommitdiff
path: root/http.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'http.cpp')
-rw-r--r--http.cpp29
1 files changed, 23 insertions, 6 deletions
diff --git a/http.cpp b/http.cpp
index 8d69f93..dbe8145 100644
--- a/http.cpp
+++ b/http.cpp
@@ -7,7 +7,7 @@
#include <vector>
HTTPRequest::HTTPRequest(std::istream& is) {
- std::string line, firstline;
+ std::string firstline;
std::getline(is, firstline);
std::vector<std::string> splitvec;
@@ -29,18 +29,35 @@ HTTPRequest::HTTPRequest(std::istream& is) {
}
}
-HTTPResponse::HTTPResponse() {
+HTTPResponse::HTTPResponse(boost::asio::ip::tcp::socket& socket_) : socket(socket_){
httpver = "1.1";
+ headers_written = false;
}
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;
+void HTTPResponse::write_headers() {
+ write(boost::str(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;
+ write(boost::str(boost::format("%s: %s\r\n") % it->first % it->second));
}
- os << "\r\n";
+ write("\r\n");
+}
+
+void HTTPResponse::write(char *data, unsigned int len) {
+ write(std::string(data, len));
+}
+
+void HTTPResponse::write(std::string str) {
+ if(!headers_written) {
+ // make sure to set headers_written before calling write_headers
+ headers_written = true;
+ write_headers();
+ }
+ boost::asio::streambuf b;
+ std::ostream os(&b);
+ os << str;
+ boost::asio::write(socket, b);
}