forked from jimloco/Csocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurlSock.cc
233 lines (213 loc) · 8.31 KB
/
CurlSock.cc
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
/**
* @file CurlSock.cc
* @author Jim Hull <[email protected]>
*
* Copyright (c) 1999-2012 Jim Hull <[email protected]>
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Redistributions in any form must be accompanied by information on how to obtain
* complete source code for this software and any accompanying software that uses this software.
* The source code must either be included in the distribution or be available for no more than
* the cost of distribution plus a nominal fee, and must be freely redistributable
* under reasonable conditions. For an executable file, complete source code means the source
* code for all modules it contains. It does not include source code for modules or files
* that typically accompany the major components of the operating system on which the executable file runs.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
* OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CurlSock.h"
#ifndef _NO_CSOCKET_NS
namespace Csocket
{
#endif /* _NO_CSOCKET_NS */
CCurlSock::CCurlSock()
{
m_iTimeout_ms = -1;
m_pMultiHandle = NULL;
}
CCurlSock::~CCurlSock()
{
for( std::map< CURL *, bool >::iterator it = m_pcbCurlHandles.begin(); it != m_pcbCurlHandles.end(); ++it )
{
CURL * pCurl = it->first;
if( m_pMultiHandle )
curl_multi_remove_handle( m_pMultiHandle, pCurl );
curl_easy_cleanup( pCurl );
}
m_pcbCurlHandles.clear();
if( m_pMultiHandle )
{
curl_multi_cleanup( m_pMultiHandle );
m_pMultiHandle = NULL;
}
}
bool CCurlSock::GatherFDsForSelect( std::map< int, int16_t > & miiReadyFds, long & iTimeoutMS )
{
if( m_pMultiHandle )
{
// look for any timeout changes, and any fd changes
int iRunningHandles = 0;
curl_multi_socket_action( m_pMultiHandle, CURL_SOCKET_TIMEOUT, 0, &iRunningHandles );
if( iRunningHandles >= 1 )
{
// this means there is a request working
size_t uNumFDs = m_miiMonitorFDs.size();
if( uNumFDs > 0 )
{
// the call back came through and there are fd's to work on
int * aiFDs = ( int * )malloc( sizeof( int ) * uNumFDs );
int * pTmp = aiFDs;
// cycle through the available fd's and get the specific actions needed to proceed, need to copy as m_miiMonitorFDs might change during the callback
for( std::map< int, int16_t >::iterator it = m_miiMonitorFDs.begin(); it != m_miiMonitorFDs.end(); ++it, ++pTmp )
*pTmp = it->first;
pTmp = aiFDs;
for( size_t uCount = 0; uCount < uNumFDs; ++uCount, ++pTmp )
curl_multi_socket_action( m_pMultiHandle, aiFDs[uCount], 0, &iRunningHandles );
free( aiFDs );
// cycle through any additional fd's and set them into Csocket to monitor
for( std::map< int, int16_t >::iterator it = m_miiMonitorFDs.begin(); it != m_miiMonitorFDs.end(); ++it )
{
if( it->second > 0 )
miiReadyFds[it->first] = it->second;
}
}
// change the timeout if reqested to do so
if( m_iTimeout_ms >= 0 )
iTimeoutMS = m_iTimeout_ms;
}
// check for anything complete
CURLMsg * pMSG = NULL;
int iNumMsgQueue = 0;
do
{
pMSG = curl_multi_info_read( m_pMultiHandle, &iNumMsgQueue );
if( pMSG && pMSG->msg == CURLMSG_DONE )
{
OnCURLComplete( pMSG->easy_handle );
m_pcbCurlHandles.at( pMSG->easy_handle ) = false;
}
}
while( pMSG && iNumMsgQueue > 0 );
}
return( m_bEnabled );
}
CURL * CCurlSock::Retr( const CS_STRING & sURL, const CS_STRING & sReferrer )
{
CURL * pCURL = NULL;
if( !m_pMultiHandle )
{
m_pMultiHandle = curl_multi_init();
// assign the next functions to get information about the internal fd's and the suggested timeout
curl_multi_setopt( m_pMultiHandle, CURLMOPT_SOCKETFUNCTION, CCurlSock::SetupSock );
curl_multi_setopt( m_pMultiHandle, CURLMOPT_SOCKETDATA, this );
curl_multi_setopt( m_pMultiHandle, CURLMOPT_TIMERFUNCTION, CCurlSock::SetupTimer );
curl_multi_setopt( m_pMultiHandle, CURLMOPT_TIMERDATA, this );
}
for( std::map< CURL *, bool >::iterator it = m_pcbCurlHandles.begin(); it != m_pcbCurlHandles.end(); ++it )
{
if( !it->second )
{
pCURL = it->first;
curl_easy_reset( pCURL );
break;
}
}
if( !pCURL )
{
pCURL = curl_easy_init();
}
m_pcbCurlHandles[pCURL] = false;
// prepare the handle, this is just a proof of concept, so doing it right here
// you can create a CURL handle for each query, re-use them (to persist connections), drop them off, etc
// the easiest method if you are serially retrieving documents is to do them through one CURL handle
// otherwise you can make a pool of handles, etc using curl_multi_info_read to see which is ready and so forth
// curl_easy_setopt( pCURL, CURLOPT_VERBOSE, 1 );
curl_easy_setopt( pCURL, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt( pCURL, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_easy_setopt( pCURL, CURLOPT_ENCODING, "" );
curl_easy_setopt( pCURL, CURLOPT_SSL_VERIFYPEER, 0 );
curl_easy_setopt( pCURL, CURLOPT_WRITEFUNCTION, CCurlSock::WriteData );
curl_easy_setopt( pCURL, CURLOPT_HEADERFUNCTION, CCurlSock::WriteHeader );
curl_easy_setopt( pCURL, CURLOPT_COOKIEFILE, "" ); // empty string means enable cookie handling
// send curl back as the argument to the functions,
// and tie this class as a reference to that object for function calls
curl_easy_setopt( pCURL, CURLOPT_WRITEDATA, pCURL );
curl_easy_setopt( pCURL, CURLOPT_WRITEHEADER, pCURL );
curl_easy_setopt( pCURL, CURLOPT_PRIVATE, this );
curl_multi_add_handle( m_pMultiHandle, pCURL );
if( curl_easy_setopt( pCURL, CURLOPT_URL, sURL.c_str() ) != CURLE_OK )
return( NULL );
if( !sReferrer.empty() && curl_easy_setopt( pCURL, CURLOPT_REFERER, sReferrer.c_str() ) != CURLE_OK )
return( NULL );
m_pcbCurlHandles[pCURL] = true;
return( pCURL );
}
size_t CCurlSock::WriteData( void * pData, size_t uSize, size_t uNemb, void * pCBPtr )
{
CURL * pCURL = static_cast< CURL * >( pCBPtr );
CCurlSock * pManager = NULL;
if( curl_easy_getinfo( pCURL, CURLINFO_PRIVATE, &pManager ) != CURLE_OK )
return( 0 );
assert( pManager );
size_t uBytes = uSize * uNemb;
//cout.write( (const char *)pData, uBytes );
return( pManager->OnBody( pCURL, ( const char * )pData, uBytes ) );
}
size_t CCurlSock::WriteHeader( void * pData, size_t uSize, size_t uNemb, void * pCBPtr )
{
CURL * pCURL = static_cast< CURL * >( pCBPtr );
CCurlSock * pManager = NULL;
if( curl_easy_getinfo( pCURL, CURLINFO_PRIVATE, &pManager ) != CURLE_OK )
return( 0 );
assert( pManager );
size_t uBytes = uSize * uNemb;
return( pManager->OnHeader( pCURL, ( const char * )pData, uBytes ) );
}
int CCurlSock::SetupSock( CURL * pCurl, curl_socket_t iFD, int iWhat, void * pCBPtr, void * pSockPtr )
{
CCurlSock * pManager = static_cast< CCurlSock * >( pCBPtr );
if( iWhat == CURL_POLL_IN )
{
pManager->Add( iFD, CSocketManager::ECT_Read );
}
else if( iWhat == CURL_POLL_OUT )
{
pManager->Add( iFD, CSocketManager::ECT_Write );
}
else if( iWhat == CURL_POLL_INOUT )
{
pManager->Add( iFD, CSocketManager::ECT_Write|CSocketManager::ECT_Read );
}
else if( iWhat == CURL_POLL_REMOVE )
{
pManager->Remove( iFD );
}
else
{
pManager->Add( iFD, 0 );
}
return( 0 );
}
int CCurlSock::SetupTimer( CURLM * pMulti, long iTimeoutMS, void * pCBPtr )
{
CCurlSock * pManager = static_cast< CCurlSock * >( pCBPtr );
pManager->SetTimeoutMS( iTimeoutMS );
return( 0 );
}
#ifndef _NO_CSOCKET_NS
};
#endif /* _NO_CSOCKET_NS */