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
|
/**
* @file llcurl.h
* @author Zero / Donovan
* @date 2006-10-15
* @brief Curl wrapper
*
* $LicenseInfo:firstyear=2006&license=viewergpl$
*
* Copyright (c) 2006-2007, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlife.com/developers/opensource/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at http://secondlife.com/developers/opensource/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llcurl.h"
#include <iomanip>
#include "llsdserialize.h"
//////////////////////////////////////////////////////////////////////////////
/*
The trick to getting curl to do keep-alives is to reuse the
same easy handle for the requests. It appears that curl
keeps a pool of connections alive for each easy handle, but
doesn't share them between easy handles. Therefore it is
important to keep a pool of easy handles and reuse them,
rather than create and destroy them with each request. This
code does this.
Furthermore, it would behoove us to keep track of which
hosts an easy handle was used for and pick an easy handle
that matches the next request. This code does not current
do this.
*/
using namespace std;
LLCurl::Responder::Responder()
: mReferenceCount(0)
{
}
LLCurl::Responder::~Responder()
{
}
// virtual
void LLCurl::Responder::error(U32 status, const std::stringstream& content)
{
llinfos << "LLCurl::Responder::error " << status << ": " << content.str() << llendl;
}
// virtual
void LLCurl::Responder::result(const std::stringstream& content)
{
}
// virtual
void LLCurl::Responder::completed(U32 status, const std::stringstream& content)
{
if (200 <= status && status < 300)
{
result(content);
}
else
{
error(status, content);
}
}
namespace boost
{
void intrusive_ptr_add_ref(LLCurl::Responder* p)
{
++p->mReferenceCount;
}
void intrusive_ptr_release(LLCurl::Responder* p)
{
if(p && 0 == --p->mReferenceCount)
{
delete p;
}
}
};
//////////////////////////////////////////////////////////////////////////////
size_t
curlOutputCallback(void* data, size_t size, size_t nmemb, void* user_data)
{
stringstream& output = *(stringstream*)user_data;
size_t n = size * nmemb;
output.write((const char*)data, n);
if (!((istream&)output).good()) {
std::cerr << "WHAT!?!?!? istream side bad" << std::endl;
}
if (!((ostream&)output).good()) {
std::cerr << "WHAT!?!?!? ostream side bad" << std::endl;
}
return n;
}
// Only used if request contained a body (post or put), Not currently implemented.
// size_t
// curlRequestCallback(void* data, size_t size, size_t nmemb, void* user_data)
// {
// stringstream& request = *(stringstream*)user_data;
// size_t n = size * nmemb;
// request.read((char*)data, n);
// return request.gcount();
// }
LLCurl::Easy::Easy()
{
mHeaders = 0;
mHeaders = curl_slist_append(mHeaders, "Connection: keep-alive");
mHeaders = curl_slist_append(mHeaders, "Keep-alive: 300");
mHeaders = curl_slist_append(mHeaders, "Content-Type: application/xml");
// FIXME: shouldn't be there for GET/DELETE
// FIXME: should have ACCEPT headers
mHandle = curl_easy_init();
}
LLCurl::Easy::~Easy()
{
curl_easy_cleanup(mHandle);
curl_slist_free_all(mHeaders);
}
void
LLCurl::Easy::get(const string& url, ResponderPtr responder)
{
prep(url, responder);
curl_easy_setopt(mHandle, CURLOPT_HTTPGET, 1);
}
void
LLCurl::Easy::getByteRange(const string& url, S32 offset, S32 length, ResponderPtr responder)
{
mRange = llformat("Range: bytes=%d-%d", offset,offset+length-1);
mHeaders = curl_slist_append(mHeaders, mRange.c_str());
prep(url, responder);
curl_easy_setopt(mHandle, CURLOPT_HTTPGET, 1);
}
void
LLCurl::Easy::perform()
{
report(curl_easy_perform(mHandle));
}
void
LLCurl::Easy::prep(const std::string& url, ResponderPtr responder)
{
#if !LL_DARWIN
curl_easy_reset(mHandle); // SJB: doesn't exisit on OSX 10.3.9
#else
// SJB: equivalent? fast?
curl_easy_cleanup(mHandle);
mHandle = curl_easy_init();
#endif
curl_easy_setopt(mHandle, CURLOPT_PRIVATE, this);
// curl_easy_setopt(mHandle, CURLOPT_VERBOSE, 1); // usefull for debugging
curl_easy_setopt(mHandle, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(mHandle, CURLOPT_WRITEFUNCTION, &curlOutputCallback);
curl_easy_setopt(mHandle, CURLOPT_WRITEDATA, &mOutput);
#if 1 // For debug
curl_easy_setopt(mHandle, CURLOPT_HEADERFUNCTION, &curlOutputCallback);
curl_easy_setopt(mHandle, CURLOPT_HEADERDATA, &mHeaderOutput);
#endif
curl_easy_setopt(mHandle, CURLOPT_ERRORBUFFER, &mErrorBuffer);
curl_easy_setopt(mHandle, CURLOPT_ENCODING, "");
curl_easy_setopt(mHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(mHandle, CURLOPT_HTTPHEADER, mHeaders);
mOutput.str("");
if (!((istream&)mOutput).good()) {
std::cerr << "WHAT!?!?!? istream side bad" << std::endl;
}
if (!((ostream&)mOutput).good()) {
std::cerr << "WHAT!?!?!? ostream side bad" << std::endl;
}
mURL = url;
curl_easy_setopt(mHandle, CURLOPT_URL, mURL.c_str());
mResponder = responder;
}
void
LLCurl::Easy::report(CURLcode code)
{
if (!mResponder) return;
long responseCode;
if (code == CURLE_OK)
{
curl_easy_getinfo(mHandle, CURLINFO_RESPONSE_CODE, &responseCode);
}
else
{
responseCode = 499;
}
mResponder->completed(responseCode, mOutput);
mResponder = NULL;
}
LLCurl::Multi::Multi()
{
mHandle = curl_multi_init();
}
LLCurl::Multi::~Multi()
{
// FIXME: should clean up excess handles in mFreeEasy
curl_multi_cleanup(mHandle);
}
void
LLCurl::Multi::get(const std::string& url, ResponderPtr responder)
{
LLCurl::Easy* easy = easyAlloc();
easy->get(url, responder);
curl_multi_add_handle(mHandle, easy->mHandle);
}
void
LLCurl::Multi::getByteRange(const std::string& url, S32 offset, S32 length, ResponderPtr responder)
{
LLCurl::Easy* easy = easyAlloc();
easy->getByteRange(url, offset, length, responder);
curl_multi_add_handle(mHandle, easy->mHandle);
}
void
LLCurl::Multi::process()
{
int count;
for (int call_count = 0; call_count < 5; call_count += 1)
{
if (CURLM_CALL_MULTI_PERFORM != curl_multi_perform(mHandle, &count))
{
break;
}
}
CURLMsg* msg;
int msgs_in_queue;
while ((msg = curl_multi_info_read(mHandle, &msgs_in_queue)))
{
if (msg->msg != CURLMSG_DONE) continue;
Easy* easy = 0;
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &easy);
if (!easy) continue;
easy->report(msg->data.result);
curl_multi_remove_handle(mHandle, easy->mHandle);
easyFree(easy);
}
}
LLCurl::Easy*
LLCurl::Multi::easyAlloc()
{
Easy* easy = 0;
if (mFreeEasy.empty())
{
easy = new Easy();
}
else
{
easy = mFreeEasy.back();
mFreeEasy.pop_back();
}
return easy;
}
void
LLCurl::Multi::easyFree(Easy* easy)
{
if (mFreeEasy.size() < 5)
{
mFreeEasy.push_back(easy);
}
else
{
delete easy;
}
}
namespace
{
static LLCurl::Multi* sMainMulti = 0;
LLCurl::Multi*
mainMulti()
{
if (!sMainMulti) {
sMainMulti = new LLCurl::Multi();
}
return sMainMulti;
}
void freeMulti()
{
delete sMainMulti;
sMainMulti = NULL;
}
}
void
LLCurl::get(const std::string& url, ResponderPtr responder)
{
mainMulti()->get(url, responder);
}
void
LLCurl::getByteRange(const std::string& url, S32 offset, S32 length, ResponderPtr responder)
{
mainMulti()->getByteRange(url, offset, length, responder);
}
void
LLCurl::process()
{
mainMulti()->process();
}
void LLCurl::cleanup()
{
freeMulti();
curl_global_cleanup();
}
|