From 1d05994fe1d9488193f01b47e92dcf11920c8f02 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Dec 2010 00:43:12 +0100 Subject: Misc changes in HTTP code. --- http.cpp | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'http.cpp') diff --git a/http.cpp b/http.cpp index 8d69f93..dbe8145 100644 --- a/http.cpp +++ b/http.cpp @@ -7,7 +7,7 @@ #include HTTPRequest::HTTPRequest(std::istream& is) { - std::string line, firstline; + std::string firstline; std::getline(is, firstline); std::vector 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); } -- cgit v1.2.3