big svn cleanup
[anytun.git] / src / Sockets / HttpdSocket.cpp
blob777dd0759654121603c4d9889dc6c393baedb12a
1 /** \file HttpdSocket.cpp
2 */
3 /*
4 Copyright (C) 2001-2007 Anders Hedstrom (grymse@alhem.net)
6 This library is made available under the terms of the GNU GPL.
8 If you would like to use this library in a closed-source application,
9 a separate license agreement is available. For information about
10 the closed-source license agreement for the C++ sockets library,
11 please visit http://www.alhem.net/Sockets/license.html and/or
12 email license@alhem.net.
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #ifdef _MSC_VER
29 #pragma warning(disable:4786)
30 #endif
31 #include "Utility.h"
32 #include "HttpdCookies.h"
33 #include "HttpdForm.h"
34 #include "MemFile.h"
35 #include "HttpdSocket.h"
36 #include "Base64.h"
39 #ifdef SOCKETS_NAMESPACE
40 namespace SOCKETS_NAMESPACE {
41 #endif
43 #define TMPSIZE 10000
46 // statics
47 int HttpdSocket::m_request_count = 0;
48 std::string HttpdSocket::m_start = "";
51 HttpdSocket::HttpdSocket(ISocketHandler& h)
52 :HTTPSocket(h)
53 ,m_content_length(0)
54 ,m_file(NULL)
55 ,m_received(0)
56 ,m_request_id(++m_request_count)
57 ,m_cookies(NULL)
58 ,m_form(NULL)
60 m_http_date = datetime2httpdate(GetDate());
61 if (!m_start.size())
62 m_start = m_http_date;
66 HttpdSocket::~HttpdSocket()
68 if (m_file)
70 delete m_file;
72 if (m_cookies)
73 delete m_cookies;
74 if (m_form)
75 delete m_form;
79 void HttpdSocket::OnFirst()
84 void HttpdSocket::OnHeader(const std::string& key,const std::string& value)
86 if (!strcasecmp(key.c_str(),"content-length"))
88 m_content_length = atoi(value.c_str());
89 m_content_length_str = value;
91 else
92 if (!strcasecmp(key.c_str(),"cookie"))
94 m_http_cookie = value;
96 else
97 if (!strcasecmp(key.c_str(),"content-type"))
99 m_content_type = value;
101 else
102 if (!strcasecmp(key.c_str(),"if-modified-since"))
104 m_if_modified_since = value;
109 void HttpdSocket::OnHeaderComplete()
111 m_cookies = new HttpdCookies(m_http_cookie);
113 if (GetMethod() == "GET")
115 Utility::SetEnv("QUERY_STRING", GetQueryString());
117 Utility::SetEnv("REQUEST_METHOD", GetMethod());
118 Utility::SetEnv("HTTP_COOKIE", m_http_cookie);
119 Utility::SetEnv("CONTENT_TYPE", m_content_type);
120 Utility::SetEnv("CONTENT_LENGTH", m_content_length_str);
122 #if (defined(SOLARIS8) || defined(SOLARIS))
124 char slask[TMPSIZE];
125 if (GetMethod() == "GET")
127 sprintf(slask,"QUERY_STRING=%s", GetQueryString().c_str());
128 putenv(slask);
130 sprintf(slask,"REQUEST_METHOD=%s", GetMethod().c_str());
131 putenv(slask);
132 sprintf(slask,"HTTP_COOKIE=%s", m_http_cookie.c_str());
133 putenv(slask);
134 sprintf(slask,"CONTENT_TYPE=%s", m_content_type.c_str());
135 putenv(slask);
136 sprintf(slask,"CONTENT_LENGTH=%s", m_content_length_str.c_str());
137 putenv(slask);
139 #elif defined _WIN32
141 char slask[TMPSIZE];
142 if (GetMethod() == "GET")
144 sprintf(slask,"QUERY_STRING=%s", GetQueryString().c_str());
145 _putenv(slask);
147 sprintf(slask,"REQUEST_METHOD=%s", GetMethod().c_str());
148 _putenv(slask);
149 sprintf(slask,"HTTP_COOKIE=%s", m_http_cookie.c_str());
150 _putenv(slask);
151 sprintf(slask,"CONTENT_TYPE=%s", m_content_type.c_str());
152 _putenv(slask);
153 sprintf(slask,"CONTENT_LENGTH=%s", m_content_length_str.c_str());
154 _putenv(slask);
156 #else
157 if (GetMethod() == "GET")
159 setenv("QUERY_STRING", GetQueryString().c_str(), 1);
161 setenv("REQUEST_METHOD", GetMethod().c_str(), 1);
162 setenv("HTTP_COOKIE", m_http_cookie.c_str(), 1);
163 setenv("CONTENT_TYPE", m_content_type.c_str(), 1);
164 setenv("CONTENT_LENGTH", m_content_length_str.c_str(), 1);
165 #endif
167 if (GetMethod() == "POST")
169 m_file = new MemFile;
171 else
172 if (GetMethod() == "GET")
174 m_form = new HttpdForm(GetQueryString(), GetQueryString().size() );
175 AddResponseHeader("Date", datetime2httpdate(GetDate()) );
176 if (GetUri() == "/image")
178 std::string str64 =
179 "iVBORw0KGgoAAAANSUhEUgAAAGAAAABeCAIAAABTioayAAAACXBIWXMAAABkAAAAZAAPlsXdAAAAUHRF"
180 "WHRSYXcgcHJvZmlsZSB0eXBlIEFQUDEyAApnZW5lcmljIHByb2ZpbGUKICAgICAgMTUKNDQ3NTYzNmI3"
181 "OTAwMDEwMDA0MDAwMDAwM2MwMDAwCg2F1B0AABb4SURBVHja7Xx7rGVXed/3WGs/zvPeuTPXMzbjcUpq"
182 "HAwlpQlYoUlDaJsAiVuFFKSKGlUlKgXqIKJAKxfRR4IUZEpSt1FoQEK4oYQEErlSFAoKIEoo2BQasBmc"
183 "2NjyYzxz5957Hnufvfd6fF//2Oecez2hvbXvmZkg3U9b556Zc85ea//291rf+n0bVRWO5P8udLUn8Jdd"
184 "jgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QMxVHHtfoUWx"
185 "/dP+C5fvABGv4gwBAK9qPWg5NLbvrjIY302ujgapAmKrQQiqC3xAQQF0H1AIcyXSq4XeZdegfedfWs78"
186 "jYKAAgDi3sVri4mqzH+D9DSTu+I2dyVNTEFRl34GUTUikiqotrgoAqqIAhBxqzj7ZocAighXWJUuI0Cq"
187 "injJ+SMAIZKolkXlGrOzM61mjfckCoN+X9UjorU26wgb7HTSPAfDRlVVFQAR21NeOT26XAC1p10CpKoI"
188 "iKTO+ckkF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OxVrLGMIJToB//"
225 "2z93zbUveN6NL/GvelOMs43jJ6whZgYAUcny1MUmRveyv/X31teve+73v3Bra5JkTlW982nWAQUEBERmTVKMIsv8bCVu+hmXXJel1dYxey/eh6ZxVdNs7QZrNpLE"
226 "MhMxAmhTBwhU"
227 "eDMa7wxz7PWstY21lCZp0zREtDtGHzuzWspxk3XTTm7zxB8/Dnlm5gOBzup44byrfVo3ShAsl3mPCCnG2FaL8jzzPjDJcDAb9oeJpcRaZiZCxMPuxj4zDbp0N1nb"
228 "/wQRReRQz8qwneVZr9dLOQHQKN4FCUFOHM97nTSxIU1SQxo8MkdgyDLV2Yyk6fWU2Tvv+r0OmVxViAgBQwQmO1hT2fWMIc0MU56kxrkmhIgISnFnvBMa6XZ0Y70b"
229 "JBplFQReDaPmUKt5XToiRRFNU5zNJiJxe3tre3u7KIrZrDQGT2z0+13D7NfWkk996g/f855f3tiwSWKtMYNB3uvRqVPZ5qY9ccKcvKY/HOS+abwPTdOUsxJQAcQa"
230 "3Niwv/4f7rh48dGN4700tb1+59ix9SxNsyzPsk6ScJoRES+LVIvjsLKiehBgjJLmJkmwqmaIEKOv6zgcDpxzdTUeDrKsY/MMyun2nz14v4+QWOyvZ9NCT5ywk0kw"
231 "hGxoPK0Iw/qgU85ciLC+1i3ruH7c7OzG8U519ptfrsvbmAFJCEkAu71OFLVJXnG0SSNRwPJyi2QlnKJnpkFPN+aWyoLzaSARcmIBxK2tHcvzPM1s42rF2Ol0vnX/"
232 "N19766t+6idv/dR//7RNekUDX/of99766p/+qZff8uZ/+iZj+MMf/M3f/ujdx453Bv38Xe+648v33hsovPGNb3jlT7z0H/z9n/3zBx6KkhCa7dHk4siNp6WPMai4"
233 "GIJGwKDqDBgFBMW2ugmEK/HShzAxvfStqnY6eaebpanpdPJer9fr9YbD9aIs3vGOn7/x+T/0mte99fOf+0JZlI88svvuf/3OW156653v/y+TIv7Ld75z89RzP/yh"
234 "3woOzn774XvuuefMDX/17W+9vdM99f67fv+vv/j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HSBHAB0gRwAdIEcAHSBHAB0gRwAdIP8HqHccFP4Rq4QAAAAASUVORK5CYII=";
261 Send64(str64, "image/png");
263 else
265 Exec();
267 Reset(); // prepare for next request
269 else
271 AddResponseHeader("Date", GetHttpDate());
272 AddResponseHeader("Connection", "close");
273 SetStatus("405");
274 SetStatusText("Method not allowed");
275 SendResponse();
280 void HttpdSocket::OnData(const char *p,size_t l)
282 if (m_file)
284 m_file -> fwrite(p,1,l);
286 m_received += l;
287 if (m_received >= m_content_length && m_content_length)
289 // all done
290 if (m_file && !m_form)
292 m_form = new HttpdForm(m_file, m_content_type, m_content_length);
293 AddResponseHeader("Date", datetime2httpdate(GetDate()) );
294 if (GetUri() == "/image")
296 std::string str64 =
297 "iVBORw0KGgoAAAANSUhEUgAAAGAAAABeCAIAAABTioayAAAACXBIWXMAAABkAAAAZAAPlsXdAAAAUHRF"
298 "WHRSYXcgcHJvZmlsZSB0eXBlIEFQUDEyAApnZW5lcmljIHByb2ZpbGUKICAgICAgMTUKNDQ3NTYzNmI3"
299 "OTAwMDEwMDA0MDAwMDAwM2MwMDAwCg2F1B0AABb4SURBVHja7Xx7rGVXed/3WGs/zvPeuTPXMzbjcUpq"
300 "HAwlpQlYoUlDaJsAiVuFFKSKGlUlKgXqIKJAKxfRR4IUZEpSt1FoQEK4oYQEErlSFAoKIEoo2BQasBmc"
301 "2NjyYzxz5957Hnufvfd6fF//2Oecez2hvbXvmZkg3U9b556Zc85ea//291rf+n0bVRWO5P8udLUn8Jdd"
302 "jgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QI4AOkCOADpAjgA6QMxVHHtfoUWx"
303 "/dP+C5fvABGv4gwBAK9qPWg5NLbvrjIY302ujgapAmKrQQiqC3xAQQF0H1AIcyXSq4XeZdegfedfWs78"
304 "jYKAAgDi3sVri4mqzH+D9DSTu+I2dyVNTEFRl34GUTUikiqotrgoAqqIAhBxqzj7ZocAighXWJUuI0Cq"
305 "injJ+SMA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F7X4f/12/sUrC9KVfsDSpcPtIbX/sq+aQf1FubpPoPoekKOHvB0gRwAdIEcAHSBHAB0gRwAd"
378 "IEcAHSBHAB0gRwAdIEcAHSBHAB0gRwAdIP8HqHccFP4Rq4QAAAAASUVORK5CYII=";
379 Send64(str64, "image/png");
381 else
383 Exec();
385 Reset(); // prepare for next request
391 void HttpdSocket::Send64(const std::string& str64, const std::string& type)
393 Base64 bb;
395 if (!strcasecmp(m_start.c_str(), m_if_modified_since.c_str()))
397 SetStatus("304");
398 SetStatusText("Not Modified");
399 SendResponse();
401 else
403 size_t len = bb.decode_length(str64);
404 unsigned char *buf = new unsigned char[len];
406 SetStatus("200");
407 SetStatusText("OK");
409 AddResponseHeader("Content-length", Utility::l2string( (long)len) );
410 AddResponseHeader("Content-type", type );
411 AddResponseHeader("Last-modified", m_start);
412 SendResponse();
414 bb.decode(str64, buf, len);
415 SendBuf( (char *)buf, len);
416 delete[] buf;
421 std::string HttpdSocket::datetime2httpdate(const std::string& dt)
423 struct tm tp;
424 time_t t;
425 const char *days[] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
426 const char *months[] = { "Jan","Feb","Mar","Apr","May","Jun",
427 "Jul","Aug","Sep","Oct","Nov","Dec" };
428 int i;
429 char s[40];
431 /* 1997-12-16 09:50:40 */
433 if (dt.size() == 19)
435 tp.tm_year = atoi(dt.substr(0,4).c_str()) - 1900;
436 i = atoi(dt.substr(5,2).c_str()) - 1;
437 tp.tm_mon = i >= 0 ? i : 0;
438 tp.tm_mday = atoi(dt.substr(8,2).c_str());
439 tp.tm_hour = atoi(dt.substr(11,2).c_str());
440 tp.tm_min = atoi(dt.substr(14,2).c_str());
441 tp.tm_sec = atoi(dt.substr(17,2).c_str());
442 tp.tm_wday = 0;
443 tp.tm_yday = 0;
444 tp.tm_isdst = 0;
445 t = mktime(&tp);
446 if (t == -1)
448 Handler().LogError(this, "datetime2httpdate", 0, "mktime() failed");
451 sprintf(s,"%s, %02d %s %d %02d:%02d:%02d GMT",
452 days[tp.tm_wday],
453 tp.tm_mday,
454 months[tp.tm_mon],
455 tp.tm_year + 1900,
456 tp.tm_hour,tp.tm_min,tp.tm_sec);
458 else
460 *s = 0;
462 return s;
466 std::string HttpdSocket::GetDate()
468 time_t t = time(NULL);
469 struct tm tp;
470 #ifdef _WIN32
471 memcpy(&tp, localtime(&t), sizeof(tp));
472 #else
473 localtime_r(&t, &tp);
474 #endif
475 char slask[40]; // yyyy-mm-dd hh:mm:ss
476 sprintf(slask,"%d-%02d-%02d %02d:%02d:%02d",
477 tp.tm_year + 1900,
478 tp.tm_mon + 1,
479 tp.tm_mday,
480 tp.tm_hour,tp.tm_min,tp.tm_sec);
481 return slask;
485 void HttpdSocket::Reset()
487 HTTPSocket::Reset();
488 m_content_length = 0;
489 if (m_file)
491 delete m_file;
492 m_file = NULL;
494 m_received = 0;
495 m_request_id = ++m_request_count;
496 if (m_cookies)
497 delete m_cookies;
498 m_cookies = NULL;
499 if (m_form)
500 delete m_form;
501 m_form = NULL;
505 const std::string& HttpdSocket::GetHttpDate() const
507 return m_http_date;
511 HttpdCookies *HttpdSocket::GetCookies()
513 return m_cookies;
517 const HttpdForm *HttpdSocket::GetForm() const
519 return m_form;
524 #ifdef SOCKETS_NAMESPACE
526 #endif