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
|
/* httpmessage.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/httpmessage.h>
#include <cxxtools/log.h>
#include <list>
#include <sstream>
namespace tnt
{
size_t HttpMessage::maxRequestSize = 0;
////////////////////////////////////////////////////////////////////////
// HttpMessage
//
log_define("tntnet.httpmessage")
void HttpMessage::clear()
{
method.clear();
url.clear();
queryString.clear();
header.clear();
body.clear();
majorVersion = 1;
minorVersion = 0;
contentSize = 0;
}
std::string HttpMessage::getHeader(const std::string& key, const std::string& def) const
{
header_type::const_iterator i = header.find(key);
return i == header.end() ? def : i->second;
}
void HttpMessage::setHeader(const std::string& key, const std::string& value)
{
log_debug("HttpMessage::setHeader(\"" << key << "\", \"" << value << "\")");
std::string k = key;
if (k.size() > 0 && k.at(k.size() - 1) != ':')
k += ':';
header.insert(header_type::value_type(k, value));
}
std::string HttpMessage::dumpHeader() const
{
std::ostringstream h;
dumpHeader(h);
return h.str();
}
void HttpMessage::dumpHeader(std::ostream& out) const
{
for (header_type::const_iterator it = header.begin();
it != header.end(); ++it)
out << it->first << ' ' << it->second << '\n';
}
std::string HttpMessage::htdate(time_t t)
{
struct ::tm tm;
gmtime_r(&t, &tm);
return htdate(&tm);
}
std::string HttpMessage::htdate(struct ::tm* tm)
{
const char* wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const char* monthn[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char buffer[80];
sprintf(buffer, "%s, %02d %s %d %02d:%02d:%02d GMT",
wday[tm->tm_wday], tm->tm_mday, monthn[tm->tm_mon], tm->tm_year + 1900,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return buffer;
}
std::string HttpMessage::htdateCurrent()
{
static struct ::tm lastTm;
static time_t lastDay = 0;
static cxxtools::Mutex mutex;
/*
* we cache the last split tm-struct here, because it is pretty expensive
* to calculate the date with gmtime_r.
*/
time_t t;
struct ::tm tm;
// get current time
time(&t);
time_t day = t / (24*60*60);
// check lastDay
if (day == lastDay)
{
// We can use the cached tm-struct and calculate hour, minute and
// seconds. No locking was needed at all. This is the common case.
memcpy(&tm, &lastTm, sizeof(struct ::tm));
tm.tm_sec = t % 60;
t /= 60;
tm.tm_min = t % 60;
t /= 24;
tm.tm_hour = t % 24;
}
// We recheck the last day here to ensure, our lastTm was valid even
// after the check. There is a small chance, that the day has changed
// and someone was just setting the lastTm.
if (day != lastDay)
{
// Day differs, we calculate new date.
cxxtools::MutexLock lock(mutex);
// Check again with lock. Another thread might have computed it already.
if (day != lastDay)
{
// still differs
// We set lastDay to zero first, so that other threads will reach
// the lock above. That way we avoid racing-conditions when accessing
// lastTm without locking in the normal case.
lastDay = 0;
gmtime_r(&t, &tm);
memcpy(&lastTm, &tm, sizeof(struct ::tm));
lastDay = day;
}
}
return htdate(&tm);
}
namespace
{
class Pstr
{
const char* start;
const char* end;
public:
typedef size_t size_type;
Pstr(const char* s, const char* e)
: start(s), end(e)
{ }
void setStart(const char* s)
{
start = s;
}
void setEnd(const char* e)
{
end = e;
}
size_type size() const { return end - start; }
bool empty() const { return start == end; }
bool operator== (const Pstr& s) const
{
return size() == s.size()
&& std::equal(start, end, s.start);
}
bool operator== (const char* str) const
{
size_type i;
for (i = 0; i < size() && str[i] != '\0'; ++i)
if (start[i] != str[i])
return false;
return i == size() && str[i] == '\0';
}
};
}
bool HttpMessage::checkUrl(const std::string& url)
{
unsigned level = 0;
const char* p = url.data();
const char* e = p + url.size();
Pstr str(p, p);
for (; p != e; ++p)
{
if (*p == '/')
{
str.setEnd(p);
if (!str.empty())
{
if (str == ".")
;
else if (str == "..")
{
if (level == 0)
return false;
--level;
}
else
++level;
}
str.setStart(p + 1);
}
}
if (level == 0 && (str.setEnd(p), str == ".."))
return false;
return true;
}
}
|