-
Notifications
You must be signed in to change notification settings - Fork 4
/
HttpClientSocket.cpp
312 lines (252 loc) · 5.72 KB
/
HttpClientSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
** \file HttpClientSocket.cpp
** \date 2006-04-20
** \author grymse@alhem.net
**/
/*
Copyright (C) 2007-2011 Anders Hedstrom
This library is made available under the terms of the GNU GPL, with
the additional exemption that compiling, linking, and/or using OpenSSL
is allowed.
If you would like to use this library in a closed-source application,
a separate license agreement is available. For information about
the closed-source license agreement for the C++ sockets library,
please visit http://www.alhem.net/Sockets/license.html and/or
email license@alhem.net.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef _MSC_VER
#pragma warning(disable:4786)
#endif
#include "HttpClientSocket.h"
#include "StdLog.h"
#include "ISocketHandler.h"
#include "File.h"
#ifdef SOCKETS_NAMESPACE
namespace SOCKETS_NAMESPACE {
#endif
HttpClientSocket::HttpClientSocket(ISocketHandler& h)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
}
HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& url_in)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
std::string url;
url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl( url );
}
HttpClientSocket::HttpClientSocket(ISocketHandler& h,const std::string& host, port_t port, const std::string& url_in)
:HTTPSocket(h)
,m_data_ptr(NULL)
,m_data_size(0)
,m_content_length(0)
,m_data_ptr_set(false)
,m_fil(NULL)
,m_content_ptr(0)
,m_b_complete(false)
,m_b_close_when_complete(false)
{
std::string url;
std::string tmp = "http://" + host + ":" + Utility::l2string(port) + url_in;
url_this(tmp, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl( url );
}
HttpClientSocket::~HttpClientSocket()
{
if (m_data_ptr && !m_data_ptr_set)
{
delete[] m_data_ptr;
}
if (m_fil)
{
m_fil -> fclose();
}
}
void HttpClientSocket::OnFirst()
{
if (!IsResponse())
{
Handler().LogError(this, "OnFirst", 0, "Response expected but not received - aborting", LOG_LEVEL_FATAL);
SetCloseAndDelete();
}
m_content = GetHttpVersion() + " " + GetStatus() + " " + GetStatusText() + "\r\n";
}
void HttpClientSocket::OnHeader(const std::string& key,const std::string& value)
{
m_content += key + ": " + value + "\r\n";
if (!strcasecmp(key.c_str(), "content-length"))
{
m_content_length = atoi(value.c_str());
}
else
if (!strcasecmp(key.c_str(), "content-type"))
{
m_content_type = value;
}
}
void HttpClientSocket::OnHeaderComplete()
{
if (m_filename.size())
{
m_fil = new File;
if (!m_fil -> fopen(m_filename, "wb"))
{
delete m_fil;
m_fil = NULL;
}
}
else
if (!m_data_ptr && m_content_length)
{
m_data_ptr = new unsigned char[m_content_length];
m_data_size = m_content_length;
}
}
void HttpClientSocket::OnData(const char *buf,size_t len)
{
if (m_fil)
{
m_fil -> fwrite(buf, 1, len);
}
else
if (m_data_ptr)
{
if (m_content_ptr + len > m_data_size)
{
Handler().LogError(this, "OnData", -1, "content buffer overflow", LOG_LEVEL_ERROR);
}
else
{
memcpy(m_data_ptr + m_content_ptr, buf, len);
}
}
m_content_ptr += len;
if (m_content_ptr == m_content_length && m_content_length)
{
if (m_fil)
{
m_fil -> fclose();
delete m_fil;
m_fil = NULL;
}
m_b_complete = true;
OnContent();
if (m_b_close_when_complete)
{
SetCloseAndDelete();
}
}
}
void HttpClientSocket::OnDelete()
{
if (!m_b_complete)
{
if (m_fil)
{
m_fil -> fclose();
delete m_fil;
m_fil = NULL;
}
m_b_complete = true;
OnContent();
}
}
void HttpClientSocket::SetFilename(const std::string& x)
{
m_filename = x;
}
void HttpClientSocket::SetDataPtr(unsigned char *buf,size_t len)
{
m_data_ptr = buf;
m_data_size = len;
m_data_ptr_set = true;
}
const std::string& HttpClientSocket::GetContent()
{
return m_content;
}
size_t HttpClientSocket::GetContentLength()
{
return m_content_length;
}
size_t HttpClientSocket::GetContentPtr()
{
return m_content_ptr;
}
size_t HttpClientSocket::GetPos()
{
return m_content_ptr;
}
bool HttpClientSocket::Complete()
{
return m_b_complete;
}
const unsigned char *HttpClientSocket::GetDataPtr() const
{
return m_data_ptr;
}
void HttpClientSocket::OnContent()
{
}
void HttpClientSocket::SetCloseOnComplete(bool x)
{
m_b_close_when_complete = x;
}
const std::string& HttpClientSocket::GetUrlProtocol()
{
return m_protocol;
}
const std::string& HttpClientSocket::GetUrlHost()
{
return m_host;
}
port_t HttpClientSocket::GetUrlPort()
{
return m_port;
}
const std::string& HttpClientSocket::GetUrlFilename()
{
return m_url_filename;
}
const std::string& HttpClientSocket::GetContentType()
{
return m_content_type;
}
void HttpClientSocket::Url(const std::string& url_in,std::string& host,port_t& port)
{
std::string url;
url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename);
SetUrl(url);
host = GetUrlHost();
port = GetUrlPort();
}
#ifdef SOCKETS_NAMESPACE
} // namespace SOCKETS_NAMESPACE {
#endif