1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
/* httpreply.cpp
* Copyright (C) 2003-2005 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <tnt/httpreply.h>
#include <tnt/http.h>
#include <tnt/httpheader.h>
#include <tnt/deflatestream.h>
#include <tnt/httperror.h>
#include <cxxtools/log.h>
#include <cxxtools/md5stream.h>
#include <cxxtools/dynbuffer.h>
#include <zlib.h>
#include <netinet/in.h>
namespace tnt
{
log_define("tntnet.httpreply")
////////////////////////////////////////////////////////////////////////
// HttpReply
//
unsigned HttpReply::keepAliveTimeout = 15000;
unsigned HttpReply::minCompressSize = 1024;
std::string HttpReply::defaultContentType = "text/html; charset=iso-8859-1";
namespace
{
std::string doCompress(const std::string& body)
{
std::string ret;
std::ostringstream b;
char f[] = "\x1f\x8b\x08\x00"
"\x00\x00\x00\x00"
"\x04\x03";
b.write(f, sizeof(f) - 1);
DeflateStream deflator(b);
deflator.write(body.data(), body.size());
deflator.end();
uLong crc = crc32(0, reinterpret_cast<const Bytef*>(body.data()), body.size());
uint32_t u = crc;
b.put(static_cast<char>(u & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
u = body.size();
b.put(static_cast<char>(u & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
b.put(static_cast<char>((u >>= 8) & 0xFF));
return b.str();
}
}
void HttpReply::tryCompress(std::string& body)
{
if (body.size() >= minCompressSize && !hasHeader(httpheader::contentEncoding))
{
if (acceptEncoding.accept("gzip"))
{
log_debug("gzip");
std::string cbody = doCompress(body);
std::string::size_type oldSize = body.size();
// only send compressed data, if the data is compressed more than 1/8th
if (oldSize - (oldSize >> 3) > cbody.size())
{
body = cbody;
log_info("gzip body " << oldSize << " bytes to " << body.size() << " bytes");
setHeader(httpheader::contentEncoding, "gzip");
}
}
}
}
void HttpReply::send(unsigned ret, const char* msg)
{
std::string body = outstream.str();
// complete headers
if (!hasHeader(httpheader::date))
setHeader(httpheader::date, htdateCurrent());
if (!hasHeader(httpheader::server))
setHeader(httpheader::server, httpheader::serverName);
tryCompress(body);
if (!hasHeader(httpheader::connection))
{
if (keepAliveTimeout > 0 && keepAliveCounter > 0)
setKeepAliveHeader(getKeepAliveTimeout() + 999 / 1000);
else
setKeepAliveHeader(0);
}
if (!hasHeader(httpheader::contentLength))
setContentLengthHeader(body.size());
if (!hasHeader(httpheader::contentType))
setHeader(httpheader::contentType, contentType);
// send header
if (sendStatusLine)
{
log_debug("HTTP/" << getMajorVersion() << '.' << getMinorVersion()
<< ' ' << ret << ' ' << msg);
socket << "HTTP/" << getMajorVersion() << '.' << getMinorVersion()
<< ' ' << ret << ' ' << msg << "\r\n";
}
for (header_type::const_iterator it = header.begin();
it != header.end(); ++it)
{
log_debug(it->first << ' ' << it->second);
socket << it->first << ' ' << it->second << "\r\n";
}
if (hasCookies())
{
log_debug(httpheader::setCookie << ' ' << httpcookies);
socket << httpheader::setCookie << ' ' << httpcookies << "\r\n";
}
socket << "\r\n";
// send body
if (getMethod() == "HEAD")
log_debug("HEAD-request - empty body");
else
{
log_debug("send " << body.size()
<< " bytes body, method=" << getMethod());
socket << body;
}
}
HttpReply::HttpReply(std::ostream& s, bool sendStatusLine_)
: contentType(defaultContentType),
socket(s),
current_outstream(&outstream),
save_outstream(outstream),
keepAliveCounter(0),
sendStatusLine(sendStatusLine_)
{ }
void HttpReply::sendReply(unsigned ret, const char* msg)
{
if (!isDirectMode())
{
send(ret, msg);
socket.flush();
}
}
void HttpReply::setMd5Sum()
{
cxxtools::Md5stream md5;
md5 << outstream.str().size();
setHeader(httpheader::contentMD5, md5.getHexDigest());
}
void HttpReply::throwError(unsigned errorCode, const std::string& errorMessage) const
{
throw HttpError(errorCode, errorMessage);
}
void HttpReply::throwError(const std::string& errorMessage) const
{
throw HttpError(errorMessage);
}
void HttpReply::throwNotFound(const std::string& errorMessage) const
{
throw NotFoundException(errorMessage);
}
unsigned HttpReply::redirect(const std::string& newLocation)
{
setHeader(httpheader::location, newLocation);
return HTTP_MOVED_TEMPORARILY;
}
void HttpReply::setContentLengthHeader(size_t size)
{
std::ostringstream s;
s << size;
setHeader(httpheader::contentLength, s.str());
}
void HttpReply::setKeepAliveHeader(unsigned timeout)
{
log_debug("setKeepAliveHeader(" << timeout << ')');
removeHeader(httpheader::connection);
removeHeader(httpheader::keepAlive);
if (timeout > 0 && getKeepAliveCounter() > 0)
{
std::ostringstream s;
s << "timeout=" << timeout << ", max=" << getKeepAliveCounter();
setHeader(httpheader::keepAlive, s.str());
setHeader(httpheader::connection, httpheader::connectionKeepAlive);
}
else
setHeader(httpheader::connection, httpheader::connectionClose);
}
void HttpReply::setDirectMode(unsigned ret, const char* msg)
{
if (!isDirectMode())
{
send(ret, msg);
current_outstream = &socket;
save_outstream.setSink(socket);
}
}
void HttpReply::setDirectModeNoFlush()
{
current_outstream = &socket;
save_outstream.setSink(socket);
}
void HttpReply::setCookie(const std::string& name, const Cookie& value)
{
log_debug("setCookie(\"" << name << "\",\"" << value.getValue() << "\")");
httpcookies.setCookie(name, value);
}
bool HttpReply::keepAlive() const
{
if (getKeepAliveCounter() <= 0
|| getKeepAliveTimeout() <= 0)
return false;
header_type::const_iterator it = header.find(httpheader::connection);
return it != header.end() && it->second == httpheader::connectionKeepAlive;
}
}
|