forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpExecute.cpp
More file actions
292 lines (250 loc) · 7.32 KB
/
HttpExecute.cpp
File metadata and controls
292 lines (250 loc) · 7.32 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
/*
* Copyright (C) 2021 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 "HttpSimulator.h"
#include "HtmlElementUrl.h"
#include "HttpClient2.h"
#include "FileUtility.h"
#include "StringUtility.h"
#include "Log.h"
/**
* @ingroup HttpSimulator
* @brief 프로그램을 실행시켜서 stdout 으로 출력된 내용을 저장한다.
* @param strExecute 프로그램 full path
* @param strPostBody [out] 프로그램의 출력 내용을 저장하는 변수
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
static bool GetPostBody( std::string & strExecute, std::string & strPostBody )
{
strPostBody.clear();
FILE * fd = popen( strExecute.c_str(), "rb" );
if( fd == NULL )
{
CLog::Print( LOG_ERROR, "popen(%s) error(%d)", strExecute.c_str(), GetError() );
return false;
}
char szBuf[8192];
int n;
while( 1 )
{
n = fread( szBuf, 1, sizeof(szBuf), fd );
if( n <= 0 ) break;
strPostBody.append( szBuf, n );
}
pclose( fd );
if( strPostBody.empty() ) return false;
return true;
}
/**
* @ingroup HttpSimulator
* @brief 수신한 HTTP body 를 파일에 저장한다.
* @param strRecvFileName 파일 이름
* @param strOutputContentType HTTP Content-Type
* @param strOutputBody HTTP body
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
static bool SaveBodyToFile( std::string & strRecvFileName, std::string & strOutputContentType, std::string & strOutputBody )
{
#ifdef WIN32
const char * pszContentType = strOutputContentType.c_str();
if( strstr( pszContentType, "UTF-8" ) )
{
std::string strOutput;
if( Utf8ToAnsi( strOutputBody.c_str(), strOutput ) )
{
strOutputBody = strOutput;
}
}
#endif
std::string strFolder;
if( GetFolderPathOfFilePath( strRecvFileName.c_str(), strFolder ) )
{
CDirectory::Create( strFolder.c_str() );
}
FILE * fd = fopen( strRecvFileName.c_str(), "wb" );
if( fd == NULL )
{
CLog::Print( LOG_ERROR, "fopen(%s) error(%d)", strRecvFileName.c_str(), GetError() );
return false;
}
fwrite( strOutputBody.c_str(), 1, strOutputBody.length(), fd );
fclose( fd );
return true;
}
/**
* @ingroup HttpSimulator
* @brief HTML 문자열에서 URL 리스트를 가져온다.
* @param strHtml HTML 문자열
* @param clsUrlList [out] URL 리스트
* @returns URL 리스트가 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool GetHtmlUrlList( std::string & strHtml, STRING_LIST & clsUrlList )
{
CHtmlElementUrl clsHtml;
if( clsHtml.Parse( strHtml, E_HEO_NOT_CHECK_END_TAG ) == -1 )
{
return false;
}
return clsHtml.GetUrlList( clsUrlList );
}
/**
* @ingroup HttpSimulator
* @brief URL 에서 최상위 URL 과 현재 URL 의 부모 URL 를 가져온다.
* @param strUrl URL (예: http://127.0.0.1/test/index.html )
* @param strRootUrl [out] 최상위 URL (예: http://127.0.0.1 )
* @param strParentUrl [out] 부모 URL (예: http://127.0.0.1/test )
* @returns true 를 리턴한다.
*/
bool GetRootParentUrl( std::string & strUrl, std::string & strRootUrl, std::string & strParentUrl )
{
const char * pszUrl = strUrl.c_str();
int iLen = (int)strUrl.length();
int iParentUrlEnd = 0;
char cType = 0;
for( int i = 4; i < iLen; ++i )
{
if( cType == 0 )
{
if( pszUrl[i-2] == ':' && pszUrl[i-1] == '/' && pszUrl[i] == '/' )
{
cType = 1;
}
}
else if( cType == 1 )
{
if( pszUrl[i] == '/' )
{
strRootUrl.append( pszUrl, i );
cType = 2;
}
}
else if( cType == 2 )
{
if( pszUrl[i] == '/' )
{
iParentUrlEnd = i;
}
else if( pszUrl[i] == '?' )
{
break;
}
}
}
if( iParentUrlEnd )
{
strParentUrl.append( pszUrl, iParentUrlEnd );
}
if( strRootUrl.empty() )
{
strRootUrl = strUrl;
}
if( strParentUrl.empty() )
{
strParentUrl = strUrl;
}
return true;
}
/**
* @ingroup HttpSimulator
* @brief HTTP 시뮬레이션 명령을 모두 실행한다.
* @param clsCommandList HTTP 시뮬레이션 명령들을 저장하는 자료구조
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool Execute( CHttpClient2 & clsClient, HTTP_SIMULATOR_COMMAND_LIST & clsCommandList )
{
HTTP_SIMULATOR_COMMAND_LIST::iterator itCL;
std::string strOutputContentType, strOutputBody;
bool bRes;
for( itCL = clsCommandList.begin(); itCL != clsCommandList.end(); ++itCL )
{
if( !strcasecmp( itCL->m_strMethod.c_str(), "POST" ) )
{
if( itCL->m_strBodyExecute.empty() == false )
{
if( GetPostBody( itCL->m_strBodyExecute, itCL->m_strBody ) == false ) break;
}
bRes = clsClient.DoPost( itCL->m_strUrl.c_str(), "application/x-www-form-urlencoded", itCL->m_strBody.c_str(), itCL->m_strBody.length(), strOutputContentType, strOutputBody );
}
else
{
bRes = clsClient.DoGet( itCL->m_strUrl.c_str(), strOutputContentType, strOutputBody );
}
if( itCL->m_strRecvFileName.empty() == false )
{
if( SaveBodyToFile( itCL->m_strRecvFileName, strOutputContentType, strOutputBody ) == false ) break;
}
if( bRes == false )
{
CLog::Print( LOG_ERROR, "url[%s] error - status code(%d)", itCL->m_strUrl.c_str(), clsClient.GetStatusCode() );
break;
}
CLog::Print( LOG_INFO, "url[%s] ok", itCL->m_strUrl.c_str() );
if( itCL->m_bDownloadAll )
{
STRING_LIST clsUrlList;
STRING_LIST::iterator itSL;
if( GetHtmlUrlList( strOutputBody, clsUrlList ) )
{
std::string strRootUrl, strParentUrl, strNewUrl;
GetRootParentUrl( itCL->m_strUrl, strRootUrl, strParentUrl );
for( itSL = clsUrlList.begin(); itSL != clsUrlList.end(); ++itSL )
{
if( (*itSL)[0] == 'h' )
{
continue;
}
else if( (*itSL)[0] == '/' )
{
strNewUrl = strRootUrl + *itSL;
}
else
{
strNewUrl = strParentUrl + *itSL;
}
*itSL = strNewUrl;
}
for( itSL = clsUrlList.begin(); itSL != clsUrlList.end(); ++itSL )
{
if( clsClient.DoGet( itSL->c_str(), strOutputContentType, strOutputBody ) == false )
{
break;
}
}
}
}
}
return bRes;
}
/**
* @ingroup HttpSimulator
* @brief HTTP 시뮬레이션 명령을 모두 실행한다.
* @param clsCommandList HTTP 시뮬레이션 명령들을 저장하는 자료구조
* @param iLoopCount 반복 횟수
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool Execute( HTTP_SIMULATOR_COMMAND_LIST & clsCommandList, int iLoopCount )
{
CHttpClient2 clsClient;
bool bRes;
for( int i = 0; i < iLoopCount; ++i )
{
bRes = Execute( clsClient, clsCommandList );
if( bRes == false ) break;
}
return bRes;
}