switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / HttpRequest.cpp
blob3909595d9a9b3e132a7e21ed3b646b6848194467
1 /**
2 ** \file HttpRequest.cpp
3 ** \date 2007-10-05
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2007 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifdef _MSC_VER
24 #pragma warning(disable:4786)
25 #endif
26 #include "HttpRequest.h"
27 #include "Utility.h"
28 #include "MemFile.h"
29 #include "HttpdForm.h"
30 #include "HttpdCookies.h"
31 #include "Parse.h"
33 #ifdef SOCKETS_NAMESPACE
34 namespace SOCKETS_NAMESPACE {
35 #endif
37 #ifdef _DEBUG
38 #define DEB(x) x; fflush(stderr);
39 #else
40 #define DEB(x)
41 #endif
44 // --------------------------------------------------------------------------------------
45 HttpRequest::HttpRequest() : HttpTransaction()
46 , m_server_port(0)
47 , m_is_ssl(false)
48 , m_body_file(NULL)
49 , m_form(NULL)
54 // --------------------------------------------------------------------------------------
55 HttpRequest::HttpRequest(const HttpRequest& src) : HttpTransaction(src)
56 , m_method(src.m_method)
57 , m_protocol(src.m_protocol)
58 , m_req_uri(src.m_req_uri)
59 , m_remote_addr(src.m_remote_addr)
60 , m_remote_host(src.m_remote_host)
61 , m_server_name(src.m_server_name)
62 , m_server_port(src.m_server_port)
63 , m_is_ssl(src.m_is_ssl)
64 , m_attribute(src.m_attribute)
65 , m_null(src.m_null)
66 , m_body_file(src.m_body_file)
67 , m_form(src.m_form)
68 , m_cookies(src.m_cookies)
69 , m_cookie(src.m_cookie)
74 // --------------------------------------------------------------------------------------
75 HttpRequest::~HttpRequest()
77 m_body_file = std::auto_ptr<IFile>(NULL);
78 m_form = std::auto_ptr<HttpdForm>(NULL);
80 if (m_body_file)
82 delete m_body_file;
84 if (m_form)
86 delete m_form;
92 // --------------------------------------------------------------------------------------
93 void HttpRequest::SetHttpMethod(const std::string& value)
95 m_method = value;
99 const std::string& HttpRequest::HttpMethod() const
101 return m_method;
106 // --------------------------------------------------------------------------------------
107 void HttpRequest::SetHttpVersion(const std::string& value)
109 m_protocol = value;
113 const std::string& HttpRequest::HttpVersion() const
115 return m_protocol;
120 // --------------------------------------------------------------------------------------
121 void HttpRequest::SetUri(const std::string& value)
123 m_req_uri = Utility::rfc1738_decode(value);
127 const std::string& HttpRequest::Uri() const
129 return m_req_uri;
134 // --------------------------------------------------------------------------------------
135 void HttpRequest::SetRemoteAddr(const std::string& value)
137 m_remote_addr = value;
141 const std::string& HttpRequest::RemoteAddr() const
143 return m_remote_addr;
148 // --------------------------------------------------------------------------------------
149 void HttpRequest::SetRemoteHost(const std::string& value)
151 m_remote_host = value;
155 const std::string& HttpRequest::RemoteHost() const
157 return m_remote_host;
162 // --------------------------------------------------------------------------------------
163 void HttpRequest::SetServerName(const std::string& value)
165 m_server_name = value;
169 const std::string& HttpRequest::ServerName() const
171 return m_server_name;
176 // --------------------------------------------------------------------------------------
177 void HttpRequest::SetServerPort(int value)
179 m_server_port = value;
183 int HttpRequest::ServerPort() const
185 return m_server_port;
190 // --------------------------------------------------------------------------------------
191 void HttpRequest::SetIsSsl(bool value)
193 m_is_ssl = value;
197 bool HttpRequest::IsSsl() const
199 return m_is_ssl;
204 // --------------------------------------------------------------------------------------
205 void HttpRequest::SetAttribute(const std::string& key, const std::string& value)
207 m_attribute[Utility::ToLower(key)] = value;
211 void HttpRequest::SetAttribute(const std::string& key, long value)
213 m_attribute[Utility::ToLower(key)] = Utility::l2string(value);
217 const std::string& HttpRequest::Attribute(const std::string& key) const
219 std::map<std::string, std::string>::const_iterator it;
220 if ( (it = m_attribute.find(Utility::ToLower(key))) != m_attribute.end())
221 return it -> second;
222 return m_null;
226 // --------------------------------------------------------------------------------------
227 const std::map<std::string, std::string>& HttpRequest::Attributes() const
229 return m_attribute;
233 // --------------------------------------------------------------------------------------
234 void HttpRequest::AddCookie(const std::string& str)
236 m_cookies.add( str );
237 Parse pa(str, ";");
238 std::string lstr = pa.getword();
239 while (!lstr.empty())
241 Parse pa2(lstr, "=");
242 std::string name = pa2.getword();
243 m_cookie[name] = lstr;
244 DEB(fprintf(stderr, " *** AddCookie '%s' = '%s'\n", name.c_str(), lstr.c_str());)
245 lstr = pa.getword();
250 // --------------------------------------------------------------------------------------
251 void HttpRequest::InitBody( size_t sz )
253 if (!m_body_file.get())
254 m_body_file = std::auto_ptr<IFile>(new MemFile);
255 DEB( else
256 fprintf(stderr, "Body data file already opened\n");)
260 // --------------------------------------------------------------------------------------
261 void HttpRequest::Write( const char *buf, size_t sz )
263 if (m_body_file.get())
264 m_body_file -> fwrite(buf, 1, sz);
265 DEB( else
266 fprintf(stderr, "Write: Body data file not open\n");)
270 // --------------------------------------------------------------------------------------
271 void HttpRequest::CloseBody()
273 if (m_body_file.get())
274 m_body_file -> fclose();
275 DEB( else
276 fprintf(stderr, "CloseBody: File not open\n");)
280 // --------------------------------------------------------------------------------------
281 void HttpRequest::ParseBody()
283 std::map<std::string, std::string>::const_iterator it;
284 if ( (it = m_attribute.find("query_string")) != m_attribute.end())
286 std::string qs = it -> second;
287 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( qs, qs.size() ));
289 else
290 if (m_body_file.get())
292 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( m_body_file.get(), ContentType(), ContentLength() ));
294 else
296 // dummy
297 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( "", 0 ));
302 // --------------------------------------------------------------------------------------
303 const HttpdForm& HttpRequest::Form() const
305 return *m_form;
309 // --------------------------------------------------------------------------------------
310 const HttpdCookies& HttpRequest::Cookies() const
312 return m_cookies;
316 // --------------------------------------------------------------------------------------
317 void HttpRequest::Reset()
319 HttpTransaction::Reset();
320 m_method = "";
321 m_protocol = "";
322 m_req_uri = "";
323 m_remote_addr = "";
324 m_remote_host = "";
325 m_server_name = "";
326 m_server_port = 0;
327 m_is_ssl = false;
328 while (!m_attribute.empty())
330 m_attribute.erase(m_attribute.begin());
332 m_body_file = std::auto_ptr<IFile>(NULL);
333 m_form = std::auto_ptr<HttpdForm>(NULL);
335 if (m_body_file)
337 delete m_body_file;
338 m_body_file = NULL;
340 if (m_form)
342 delete m_form;
343 m_form = NULL;
346 m_cookies.Reset();
347 while (!m_cookie.empty())
349 m_cookie.erase(m_cookie.begin());
354 #ifdef SOCKETS_NAMESPACE
355 } // namespace SOCKETS_NAMESPACE {
356 #endif