shrpx: Add content-length header field to SPDY upstream error page

create_error_html() is rewritten without std::stringstream.
This commit is contained in:
Tatsuhiro Tsujikawa 2013-01-16 22:43:11 +09:00
parent 7342ce7145
commit c48fb56d3f
3 changed files with 35 additions and 11 deletions

View File

@ -24,8 +24,6 @@
*/
#include "shrpx_http.h"
#include <sstream>
#include "shrpx_config.h"
#include "shrpx_log.h"
#include "util.h"
@ -86,16 +84,20 @@ const char* get_status_string(int status_code)
std::string create_error_html(int status_code)
{
std::stringstream ss;
std::string res;
res.reserve(512);
const char *status = http::get_status_string(status_code);
ss << "<html><head><title>" << status << "</title></head><body>"
<< "<h1>" << status << "</h1>"
<< "<hr>"
<< "<address>" << get_config()->server_name << " at port "
<< get_config()->port
<< "</address>"
<< "</body></html>\n";
return ss.str();
res += "<html><head><title>";
res += status;
res += "</title></head><body><h1>";
res += status;
res += "</h1><hr><address>";
res += get_config()->server_name;
res += " at port ";
res += util::utos(get_config()->port);
res += "</address>";
res += "</body></html>";
return res;
}
std::string create_via_header_value(int major, int minor)

View File

@ -680,11 +680,14 @@ int SpdyUpstream::error_reply(Downstream *downstream, int status_code)
data_prd.source.ptr = downstream;
data_prd.read_callback = spdy_data_read_callback;
std::string content_length = util::utos(html.size());
const char *nv[] = {
":status", http::get_status_string(status_code),
":version", "http/1.1",
"content-type", "text/html; charset=UTF-8",
"server", get_config()->server_name,
"content-length", content_length.c_str(),
0
};

View File

@ -298,6 +298,25 @@ char upcase(char c);
char lowcase(char c);
template<typename T>
std::string utos(T n)
{
std::string res;
if(n == 0) {
res = "0";
return res;
}
int i = 0;
T t = n;
for(; t; t /= 10, ++i);
res.resize(i);
--i;
for(; n; --i, n /= 10) {
res[i] = (n%10) + '0';
}
return res;
}
} // namespace util
} // namespace spdylay