fixed typo
[anytun.git] / Sockets / HttpClientSocket.cpp
blobc6f43e48f92b3c8749051c873052995130779fab
1 /**
2 ** \file HttpClientSocket.cpp
3 ** \date 2006-04-20
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 "HttpClientSocket.h"
27 #include "StdLog.h"
28 #include "ISocketHandler.h"
29 #include "Utility.h"
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE {
34 #endif
37 HttpClientSocket::HttpClientSocket(ISocketHandler& h)
38 :HTTPSocket(h)
39 ,m_data_ptr(NULL)
40 ,m_data_size(0)
41 ,m_content_length(0)
42 ,m_data_ptr_set(false)
43 ,m_fil(NULL)
44 ,m_content_ptr(0)
45 ,m_b_complete(false)
46 ,m_b_close_when_complete(false)
51 HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& url_in)
52 :HTTPSocket(h)
53 ,m_data_ptr(NULL)
54 ,m_data_size(0)
55 ,m_content_length(0)
56 ,m_data_ptr_set(false)
57 ,m_fil(NULL)
58 ,m_content_ptr(0)
59 ,m_b_complete(false)
60 ,m_b_close_when_complete(false)
62 std::string url;
63 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
64 SetUrl( url );
68 HttpClientSocket::~HttpClientSocket()
70 if (m_data_ptr && !m_data_ptr_set)
72 delete[] m_data_ptr;
74 if (m_fil)
76 fclose(m_fil);
81 void HttpClientSocket::OnFirst()
83 if (!IsResponse())
85 Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL);
86 SetCloseAndDelete();
88 m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n";
92 void HttpClientSocket::OnHeader(const std::string& key,const std::string& value)
94 m_content += key + ": " + value + "\r\n";
95 if (!strcasecmp(key.c_str(), "content-length"))
97 m_content_length = atoi(value.c_str());
99 else
100 if (!strcasecmp(key.c_str(), "content-type"))
102 m_content_type = value;
107 void HttpClientSocket::OnHeaderComplete()
109 if (m_filename.size())
111 m_fil = fopen(m_filename.c_str(), "wb");
113 else
114 if (!m_data_ptr && m_content_length)
116 m_data_ptr = new unsigned char[m_content_length];
117 m_data_size = m_content_length;
122 void HttpClientSocket::OnData(const char *buf,size_t len)
124 if (m_fil)
126 fwrite(buf, 1, len, m_fil);
128 else
129 if (m_data_ptr)
131 if (m_content_ptr + len > m_data_size)
133 Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR);
135 else
137 memcpy(m_data_ptr + m_content_ptr, buf, len);
140 m_content_ptr += len;
141 if (m_content_ptr == m_content_length && m_content_length)
143 if (m_fil)
145 fclose(m_fil);
146 m_fil = NULL;
148 m_b_complete = true;
149 OnContent();
150 if (m_b_close_when_complete)
152 SetCloseAndDelete();
158 void HttpClientSocket::OnDelete()
160 if (!m_b_complete)
162 if (m_fil)
164 fclose(m_fil);
165 m_fil = NULL;
167 m_b_complete = true;
168 OnContent();
173 void HttpClientSocket::SetFilename(const std::string& x)
175 m_filename = x;
179 void HttpClientSocket::SetDataPtr(unsigned char *buf,size_t len)
181 m_data_ptr = buf;
182 m_data_size = len;
183 m_data_ptr_set = true;
187 const std::string& HttpClientSocket::GetContent()
189 return m_content;
193 size_t HttpClientSocket::GetContentLength()
195 return m_content_length;
199 size_t HttpClientSocket::GetContentPtr()
201 return m_content_ptr;
205 size_t HttpClientSocket::GetPos()
207 return m_content_ptr;
211 bool HttpClientSocket::Complete()
213 return m_b_complete;
217 const unsigned char *HttpClientSocket::GetDataPtr() const
219 return m_data_ptr;
223 void HttpClientSocket::OnContent()
228 void HttpClientSocket::SetCloseOnComplete(bool x)
230 m_b_close_when_complete = x;
234 const std::string& HttpClientSocket::GetUrlProtocol()
236 return m_protocol;
240 const std::string& HttpClientSocket::GetUrlHost()
242 return m_host;
246 port_t HttpClientSocket::GetUrlPort()
248 return m_port;
252 const std::string& HttpClientSocket::GetUrlFilename()
254 return m_url_filename;
258 const std::string& HttpClientSocket::GetContentType()
260 return m_content_type;
264 void HttpClientSocket::Url(const std::string& url_in,std::string& host,port_t& port)
266 std::string url;
267 url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
268 SetUrl(url);
269 host = GetUrlHost();
270 port = GetUrlPort();
274 #ifdef SOCKETS_NAMESPACE
275 } // namespace SOCKETS_NAMESPACE {
276 #endif