forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient2.cpp
More file actions
275 lines (232 loc) · 6.93 KB
/
HttpClient2.cpp
File metadata and controls
275 lines (232 loc) · 6.93 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
/*
* Copyright (C) 2012 Yee Young Han <[email protected]> (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 "SipPlatformDefine.h"
#include "HttpClient2.h"
#include "Log.h"
#include "FileUtility.h"
#include "MemoryDebug.h"
CHttpClient2::CHttpClient2() : m_iPort(0), m_hSocket(INVALID_SOCKET), m_psttSsl(NULL)
{
InitNetwork();
}
CHttpClient2::~CHttpClient2()
{
Close();
}
/**
* @ingroup HttpStack
* @brief HTTP GET 명령을 실행한다.
* @param pszUrl HTTP URL (예:http://www.naver.com)
* @param strOutputContentType 수신 Content-Type
* @param strOutputBody 수신 body
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool CHttpClient2::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 );
clsRequest.AddHeader( "Connection", "keep-alive" );
if( Execute( &clsUri, &clsRequest, &clsPacket ) )
{
CHttpMessage * pclsMessage = clsPacket.GetHttpMessage();
strOutputContentType = pclsMessage->m_strContentType;
strOutputBody = pclsMessage->m_strBody;
return true;
}
return false;
}
/**
* @ingroup HttpStack
* @brief 소켓을 종료한다.
*/
void CHttpClient2::Close()
{
if( m_psttSsl )
{
SSLClose( m_psttSsl );
m_psttSsl = NULL;
}
if( m_hSocket != INVALID_SOCKET )
{
closesocket( m_hSocket );
m_hSocket = INVALID_SOCKET;
}
}
/**
* @ingroup HttpStack
* @brief HTTP 응답 메시지 수신 timeout 시간을 설정한다.
* @param iRecvTimeout HTTP 응답 메시지 수신 timeout 시간 (초단위)
*/
void CHttpClient2::SetRecvTimeout( int iRecvTimeout )
{
m_iRecvTimeout = iRecvTimeout;
}
/**
* @ingroup HttpStack
* @brief HTTP 응답 status code 를 리턴한다.
* @returns HTTP 응답 status code 를 리턴한다.
*/
int CHttpClient2::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 CHttpClient2::Execute( CHttpUri * pclsUri, CHttpMessage * pclsRequest, CHttpPacket * pclsPacket )
{
char * pszBuf = NULL;
int iBufLen, n;
bool bRes = false;
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;
}
if( strcmp( m_strHost.c_str(), pclsUri->m_strHost.c_str() ) || m_iPort != pclsUri->m_iPort )
{
Close();
m_hSocket = TcpConnect( pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
if( m_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( m_hSocket, &m_psttSsl ) == false )
{
CLog::Print( LOG_ERROR, "%s SSLConnect error", __FUNCTION__ );
Close();
goto FUNC_END;
}
CLog::Print( LOG_NETWORK, "SSLConnect(%s:%d) success", pclsUri->m_strHost.c_str(), pclsUri->m_iPort );
}
m_strHost = pclsUri->m_strHost;
m_iPort = pclsUri->m_iPort;
}
if( m_psttSsl )
{
if( SSLSend( m_psttSsl, pszBuf, iBufLen ) != iBufLen )
{
CLog::Print( LOG_ERROR, "%s SSLSend error", __FUNCTION__ );
Close();
goto FUNC_END;
}
}
else
{
if( TcpSend( m_hSocket, pszBuf, iBufLen ) != iBufLen )
{
CLog::Print( LOG_ERROR, "%s TcpSend error", __FUNCTION__ );
Close();
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( m_psttSsl )
{
n = SSLRecv( m_psttSsl, pszBuf, iNewBufLen );
}
else
{
n = TcpRecv( m_hSocket, pszBuf, iNewBufLen, m_iRecvTimeout );
}
if( n <= 0 )
{
Close();
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( 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;
}