2 ** \file HttpRequest.cpp
4 ** \author grymse@alhem.net
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.
24 #pragma warning(disable:4786)
26 #include "HttpRequest.h"
29 #include "HttpdForm.h"
30 #include "HttpdCookies.h"
33 #ifdef SOCKETS_NAMESPACE
34 namespace SOCKETS_NAMESPACE
{
38 #define DEB(x) x; fflush(stderr);
44 // --------------------------------------------------------------------------------------
45 HttpRequest::HttpRequest() : HttpTransaction()
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
)
66 , m_body_file(src
.m_body_file
)
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
);
92 // --------------------------------------------------------------------------------------
93 void HttpRequest::SetHttpMethod(const std::string
& value
)
99 const std::string
& HttpRequest::HttpMethod() const
106 // --------------------------------------------------------------------------------------
107 void HttpRequest::SetHttpVersion(const std::string
& value
)
113 const std::string
& HttpRequest::HttpVersion() const
120 // --------------------------------------------------------------------------------------
121 void HttpRequest::SetUri(const std::string
& value
)
123 m_req_uri
= Utility::rfc1738_decode(value
);
127 const std::string
& HttpRequest::Uri() const
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
)
197 bool HttpRequest::IsSsl() const
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())
226 // --------------------------------------------------------------------------------------
227 const std::map
<std::string
, std::string
>& HttpRequest::Attributes() const
233 // --------------------------------------------------------------------------------------
234 void HttpRequest::AddCookie(const std::string
& str
)
236 m_cookies
.add( 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());)
250 // --------------------------------------------------------------------------------------
251 void HttpRequest::InitBody( size_t sz
)
253 if (!m_body_file
.get())
254 m_body_file
= std::auto_ptr
<IFile
>(new MemFile
);
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
);
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();
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() ));
290 if (m_body_file
.get())
292 m_form
= std::auto_ptr
<HttpdForm
>(new HttpdForm( m_body_file
.get(), ContentType(), ContentLength() ));
297 m_form
= std::auto_ptr
<HttpdForm
>(new HttpdForm( "", 0 ));
302 // --------------------------------------------------------------------------------------
303 const HttpdForm
& HttpRequest::Form() const
309 // --------------------------------------------------------------------------------------
310 const HttpdCookies
& HttpRequest::Cookies() const
316 // --------------------------------------------------------------------------------------
317 void HttpRequest::Reset()
319 HttpTransaction::Reset();
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
);
347 while (!m_cookie
.empty())
349 m_cookie
.erase(m_cookie
.begin());
354 #ifdef SOCKETS_NAMESPACE
355 } // namespace SOCKETS_NAMESPACE {