2 ** \file HttpClientSocket.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 "HttpClientSocket.h"
28 #include "ISocketHandler.h"
32 #ifdef SOCKETS_NAMESPACE
33 namespace SOCKETS_NAMESPACE
{
37 HttpClientSocket::HttpClientSocket(ISocketHandler
& h
)
42 ,m_data_ptr_set(false)
46 ,m_b_close_when_complete(false)
51 HttpClientSocket::HttpClientSocket(ISocketHandler
& h
,const std::string
& url_in
)
56 ,m_data_ptr_set(false)
60 ,m_b_close_when_complete(false)
63 url_this(url_in
, m_protocol
, m_host
, m_port
, url
, m_url_filename
);
68 HttpClientSocket::~HttpClientSocket()
70 if (m_data_ptr
&& !m_data_ptr_set
)
81 void HttpClientSocket::OnFirst()
85 Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL
);
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());
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");
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
)
126 fwrite(buf
, 1, len
, m_fil
);
131 if (m_content_ptr
+ len
> m_data_size
)
133 Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR
);
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
)
150 if (m_b_close_when_complete
)
158 void HttpClientSocket::OnDelete()
173 void HttpClientSocket::SetFilename(const std::string
& x
)
179 void HttpClientSocket::SetDataPtr(unsigned char *buf
,size_t len
)
183 m_data_ptr_set
= true;
187 const std::string
& HttpClientSocket::GetContent()
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()
217 const unsigned char *HttpClientSocket::GetDataPtr() const
223 void HttpClientSocket::OnContent()
228 void HttpClientSocket::SetCloseOnComplete(bool x
)
230 m_b_close_when_complete
= x
;
234 const std::string
& HttpClientSocket::GetUrlProtocol()
240 const std::string
& HttpClientSocket::GetUrlHost()
246 port_t
HttpClientSocket::GetUrlPort()
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
)
267 url_this(url_in
, m_protocol
, m_host
, m_port
, url
, m_url_filename
);
274 #ifdef SOCKETS_NAMESPACE
275 } // namespace SOCKETS_NAMESPACE {