-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHttpClient.cpp
More file actions
435 lines (365 loc) · 12.6 KB
/
HttpClient.cpp
File metadata and controls
435 lines (365 loc) · 12.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* 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 3 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
*/
#include "SipStackDefine.h"
#include "SipTcp.h"
#include "TlsFunction.h"
#include "HttpClient.h"
#include "Log.h"
#include "MemoryDebug.h"
CHttpClient::CHttpClient() : m_iRecvTimeout(10), m_iStatusCode(0)
{
InitNetwork();
}
CHttpClient::~CHttpClient()
{
}
/**
* @ingroup HttpStack
* @brief HTTP GET 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://www.naver.com)
* @param strOutputContentType 수신 Content-Type
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoGet( const char * pszUrl, std::string & strOutputContentType, std::string & strOutputBody )
{
strOutputContentType.clear();
strOutputBody.clear();
if( pszUrl == NULL )
{
CLog::Print( LOG_ERROR, "%s pszUrl is null", __FUNCTION__ );
return false;
}
CHttpUri clsUri;
int iUrlLen = strlen( pszUrl );
if( clsUri.Parse( pszUrl, iUrlLen ) == -1 )
{
CLog::Print( LOG_ERROR, "%s clsUri.Parse(%s) error", __FUNCTION__, pszUrl );
return false;
}
CHttpMessage clsRequest;
CHttpPacket clsPacket;
clsRequest.SetRequest( "GET", &clsUri );
if( Execute( &clsUri, &clsRequest, &clsPacket ) )
{
CHttpMessage * pclsMessage = clsPacket.GetHttpMessage();
strOutputContentType = pclsMessage->m_strContentType;
strOutputBody = pclsMessage->m_strBody;
return true;
}
return false;
}
/**
* @ingroup HttpStack
* @brief HTTP GET 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://wsf.cdyne.com/WeatherWS/Weather.asmx)
* @param pszInputContentType 전송 Content-Type
* @param pszInputBody 전송 body
* @param strOutputContentType 수신 Content-Type
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoGet( const char * pszUrl, const char * pszInputContentType, const char * pszInputBody, std::string & strOutputContentType, std::string & strOutputBody )
{
strOutputContentType.clear();
strOutputBody.clear();
if( pszUrl == NULL || pszInputContentType == NULL || pszInputBody == NULL )
{
CLog::Print( LOG_ERROR, "%s pszUrl or input content type or input body is null", __FUNCTION__ );
return false;
}
CHttpUri clsUri;
int iUrlLen = strlen( pszUrl );
int iContentLength = strlen( pszInputBody );
if( iContentLength <= 0 )
{
CLog::Print( LOG_ERROR, "%s pszInputBody's length(%d) error", __FUNCTION__, iContentLength );
return false;
}
if( clsUri.Parse( pszUrl, iUrlLen ) == -1 )
{
CLog::Print( LOG_ERROR, "%s clsUri.Parse(%s) error", __FUNCTION__, pszUrl );
return false;
}
CHttpMessage clsRequest;
CHttpPacket clsPacket;
clsRequest.SetRequest( "GET", &clsUri );
clsRequest.m_strContentType = pszInputContentType;
clsRequest.m_iContentLength = iContentLength;
clsRequest.m_strBody = pszInputBody;
if( Execute( &clsUri, &clsRequest, &clsPacket ) )
{
CHttpMessage * pclsMessage = clsPacket.GetHttpMessage();
strOutputContentType = pclsMessage->m_strContentType;
strOutputBody = pclsMessage->m_strBody;
return true;
}
return false;
}
/**
* @ingroup HttpStack
* @brief HTTP POST 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://wsf.cdyne.com/WeatherWS/Weather.asmx)
* @param pszInputContentType 전송 Content-Type
* @param pszInputBody 전송 body
* @param strOutputContentType 수신 Content-Type
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoPost( const char * pszUrl, const char * pszInputContentType, const char * pszInputBody, std::string & strOutputContentType, std::string & strOutputBody )
{
return DoPost( pszUrl, NULL, pszInputContentType, pszInputBody, strOutputContentType, strOutputBody );
}
/**
* @ingroup HttpStack
* @brief HTTP POST 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://wsf.cdyne.com/WeatherWS/Weather.asmx)
* @param pclsHeaderList 전송 헤더에 포함될 헤더 항목 리스트
* @param pszInputContentType 전송 Content-Type
* @param pszInputBody 전송 body
* @param strOutputContentType 수신 Content-Type
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoPost( const char * pszUrl, HTTP_HEADER_LIST * pclsHeaderList, const char * pszInputContentType, const char * pszInputBody, std::string & strOutputContentType, std::string & strOutputBody )
{
strOutputContentType.clear();
strOutputBody.clear();
if( pszUrl == NULL || pszInputContentType == NULL || pszInputBody == NULL )
{
CLog::Print( LOG_ERROR, "%s pszUrl or input content type or input body is null", __FUNCTION__ );
return false;
}
CHttpUri clsUri;
int iUrlLen = strlen( pszUrl );
int iContentLength = strlen( pszInputBody );
if( iContentLength <= 0 )
{
CLog::Print( LOG_ERROR, "%s pszInputBody's length(%d) error", __FUNCTION__, iContentLength );
return false;
}
if( clsUri.Parse( pszUrl, iUrlLen ) == -1 )
{
CLog::Print( LOG_ERROR, "%s clsUri.Parse(%s) error", __FUNCTION__, pszUrl );
return false;
}
CHttpMessage clsRequest;
CHttpPacket clsPacket;
clsRequest.SetRequest( "POST", &clsUri );
clsRequest.m_strContentType = pszInputContentType;
clsRequest.m_iContentLength = iContentLength;
clsRequest.m_strBody = pszInputBody;
if( pclsHeaderList )
{
clsRequest.m_clsHeaderList.insert( clsRequest.m_clsHeaderList.end(), pclsHeaderList->begin(), pclsHeaderList->end() );
}
if( Execute( &clsUri, &clsRequest, &clsPacket ) )
{
CHttpMessage * pclsMessage = clsPacket.GetHttpMessage();
strOutputContentType = pclsMessage->m_strContentType;
strOutputBody = pclsMessage->m_strBody;
return true;
}
return false;
}
/**
* @ingroup HttpClientApi
* @brief HTTP POST 기반으로 SOAP 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://wsf.cdyne.com/WeatherWS/Weather.asmx)
* @param pszSoapAction HTTP SOAPAction 헤더에 저장될 문자열 (예:http://ws.cdyne.com/WeatherWS/GetWeatherInformation)
* @param pszInputBody 전송 body
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoSoap( const char * pszUrl, const char * pszSoapAction, const char * pszInputBody, std::string & strOutputBody )
{
return DoSoap( pszUrl, pszSoapAction, "text/xml;charset=UTF-8", pszInputBody, strOutputBody );
}
/**
* @ingroup HttpClientApi
* @brief HTTP POST 기반으로 SOAP 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://wsf.cdyne.com/WeatherWS/Weather.asmx)
* @param pszSoapAction HTTP SOAPAction 헤더에 저장될 문자열 (예:http://ws.cdyne.com/WeatherWS/GetWeatherInformation)
* @param pszInputContentType HTTP Content-Type
* @param pszInputBody 전송 body
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::DoSoap( const char * pszUrl, const char * pszSoapAction, const char * pszInputContentType, const char * pszInputBody, std::string & strOutputBody )
{
std::string strOutputContentType;
strOutputBody.clear();
if( pszSoapAction && strlen(pszSoapAction) > 0 )
{
HTTP_HEADER_LIST clsHeaderList;
CHttpHeader clsHeader( "SOAPAction", pszSoapAction );
clsHeaderList.push_back( clsHeader );
return DoPost( pszUrl, &clsHeaderList, pszInputContentType, pszInputBody, strOutputContentType, strOutputBody );
}
return DoPost( pszUrl, NULL, pszInputContentType, pszInputBody, strOutputContentType, strOutputBody );
}
/**
* @ingroup HttpStack
* @brief HTTP 응답 메시지 수신 timeout 시간을 설정한다.
* @param iRecvTimeout HTTP 응답 메시지 수신 timeout 시간 (초단위)
*/
void CHttpClient::SetRecvTimeout( int iRecvTimeout )
{
m_iRecvTimeout = iRecvTimeout;
}
/**
* @ingroup HttpStack
* @brief HTTP 응답 status code 를 리턴한다.
* @returns HTTP 응답 status code 를 리턴한다.
*/
int CHttpClient::GetStatusCode()
{
return m_iStatusCode;
}
/**
* @ingroup HttpStack
* @brief HTTP 서버에 연결하여서 HTTP 요청 메시지를 전송한 후, HTTP 응답 메시지를 수신한다.
* @param pclsUri HTTP request URI
* @param pclsRequest HTTP request
* @param pclsPacket HTTP response 를 저장할 패킷 객체
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient::Execute( CHttpUri * pclsUri, CHttpMessage * pclsRequest, CHttpPacket * pclsPacket )
{
char * pszBuf = NULL;
int iBufLen, n;
bool bRes = false;
Socket hSocket = INVALID_SOCKET;
SSL * psttSsl = NULL;
CHttpMessage * pclsResponse = pclsPacket->GetHttpMessage();
int iNewBufLen = 8192 + pclsRequest->m_strBody.length();
pszBuf = (char *)malloc( iNewBufLen );
if( pszBuf == NULL )
{
CLog::Print( LOG_ERROR, "%s malloc error", __FUNCTION__ );
return false;
}
memset( pszBuf, 0, iNewBufLen );
pclsRequest->m_strHttpVersion = "HTTP/1.1";
iBufLen = pclsRequest->ToString( pszBuf, iNewBufLen );
if( iBufLen <= 0 )
{
CLog::Print( LOG_ERROR, "%s clsRequest.ToString() error", __FUNCTION__ );
goto FUNC_END;
}
hSocket = TcpConnect( pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
if( hSocket == INVALID_SOCKET )
{
CLog::Print( LOG_ERROR, "%s TcpConnect(%s:%d) error", __FUNCTION__, pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
goto FUNC_END;
}
CLog::Print( LOG_NETWORK, "TcpConnect(%s:%d) success", pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
// https 프로토콜이면 TLS 로 연결한다.
if( !strcmp( pclsUri->m_strProtocol.c_str(), "https" ) )
{
if( SSLConnect( hSocket, &psttSsl ) == false )
{
CLog::Print( LOG_ERROR, "%s SSLConnect error", __FUNCTION__ );
goto FUNC_END;
}
CLog::Print( LOG_NETWORK, "SSLConnect(%s:%d) success", pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
}
if( psttSsl )
{
if( SSLSend( psttSsl, pszBuf, iBufLen ) != iBufLen )
{
CLog::Print( LOG_ERROR, "%s SSLSend error", __FUNCTION__ );
goto FUNC_END;
}
}
else
{
if( TcpSend( hSocket, pszBuf, iBufLen ) != iBufLen )
{
CLog::Print( LOG_ERROR, "%s TcpSend error", __FUNCTION__ );
goto FUNC_END;
}
}
CLog::Print( LOG_NETWORK, "TcpSend(%s:%d) [%s]", pclsUri->m_strHost.c_str(), pclsUri->m_iPort, pszBuf );
while( 1 )
{
memset( pszBuf, 0, iNewBufLen );
if( psttSsl )
{
n = SSLRecv( psttSsl, pszBuf, iNewBufLen );
}
else
{
n = TcpRecv( hSocket, pszBuf, iNewBufLen, m_iRecvTimeout );
}
if( n <= 0 )
{
break;
}
CLog::Print( LOG_NETWORK, "TcpRecv(%s:%d) [%.*s]", pclsUri->m_strHost.c_str(), pclsUri->m_iPort, n, pszBuf );
if( pclsPacket->AddPacket( pszBuf, n ) == false )
{
CLog::Print( LOG_ERROR, "%s clsPacket.AddPacket error", __FUNCTION__ );
break;
}
if( pclsPacket->IsCompleted() ) break;
}
m_iStatusCode = pclsResponse->m_iStatusCode;
if( pclsResponse->m_iStatusCode / 100 == 2 )
{
bRes = true;
}
FUNC_END:
if( psttSsl )
{
SSLClose( psttSsl );
psttSsl = NULL;
}
if( hSocket != INVALID_SOCKET )
{
closesocket( hSocket );
hSocket = INVALID_SOCKET;
}
if( pszBuf )
{
free( pszBuf );
pszBuf = NULL;
}
// 3XX 응답에서 Location 헤더가 존재하면 해당 URL 로 다시 시도한다.
if( pclsResponse->m_iStatusCode / 100 == 3 )
{
CHttpHeader * pclsHeader = pclsResponse->GetHeader( "Location" );
if( pclsHeader && pclsHeader->m_strValue.empty() == false )
{
CHttpUri clsUri;
if( clsUri.Parse( pclsHeader->m_strValue.c_str(), pclsHeader->m_strValue.length() ) )
{
if( clsUri.m_strHost.empty() )
{
clsUri.m_strHost = pclsUri->m_strHost;
clsUri.m_iPort = pclsUri->m_iPort;
clsUri.m_strProtocol = pclsUri->m_strProtocol;
clsUri.m_strPath = pclsHeader->m_strValue;
}
pclsRequest->SetRequest( pclsRequest->m_strHttpMethod.c_str(), &clsUri );
return Execute( &clsUri, pclsRequest, pclsPacket );
}
}
}
return bRes;
}