Browse Source

* HTTPServer.cpp : update response building

pull/503/head
hagen 8 years ago
parent
commit
f10064ce39
  1. 21
      HTTPServer.cpp

21
HTTPServer.cpp

@ -1,4 +1,3 @@
#include <ctime>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
@ -749,7 +748,7 @@ namespace http {
if (needAuth && !CheckAuth(req)) { if (needAuth && !CheckAuth(req)) {
res.code = 401; res.code = 401;
res.headers.insert(std::pair<std::string, std::string>("WWW-Authenticate", "Basic realm=\"WebAdmin\"")); res.add_header("WWW-Authenticate", "Basic realm=\"WebAdmin\"");
SendReply(res, content); SendReply(res, content);
return; return;
} }
@ -763,6 +762,8 @@ namespace http {
else else
ShowStatus (s); ShowStatus (s);
ShowPageTail (s); ShowPageTail (s);
res.code = 200;
content = s.str (); content = s.str ();
SendReply (res, content); SendReply (res, content);
} }
@ -845,21 +846,11 @@ namespace http {
void HTTPConnection::SendReply (HTTPRes& reply, std::string& content) void HTTPConnection::SendReply (HTTPRes& reply, std::string& content)
{ {
std::time_t time_now = std::time(nullptr); reply.add_header("Content-Type", "text/html");
char time_buff[128]; reply.body = content;
std::strftime(time_buff, sizeof(time_buff), "%a, %d %b %Y %H:%M:%S GMT", std::gmtime(&time_now));
reply.status = HTTPCodeToStatus(reply.code);
reply.headers.insert(std::pair<std::string, std::string>("Date", time_buff));
reply.headers.insert(std::pair<std::string, std::string>("Content-Type", "text/html"));
reply.headers.insert(std::pair<std::string, std::string>("Content-Length", std::to_string(content.size())));
std::string res = reply.to_string(); std::string res = reply.to_string();
std::vector<boost::asio::const_buffer> buffers; boost::asio::async_write (*m_Socket, boost::asio::buffer(res),
buffers.push_back(boost::asio::buffer(res));
buffers.push_back(boost::asio::buffer(content));
boost::asio::async_write (*m_Socket, buffers,
std::bind (&HTTPConnection::Terminate, shared_from_this (), std::placeholders::_1)); std::bind (&HTTPConnection::Terminate, shared_from_this (), std::placeholders::_1));
} }

Loading…
Cancel
Save