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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/**
* @file http_texture_load.cpp
* @brief Texture download example for core-http library
*
* $LicenseInfo:firstyear=2012&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2012, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#if !defined(WIN32)
#include <pthread.h>
#endif
#include "httpcommon.h"
#include "httprequest.h"
#include "httphandler.h"
#include "httpresponse.h"
#include "bufferarray.h"
#include "_mutex.h"
#include <curl/curl.h>
#include <openssl/crypto.h>
void init_curl();
void term_curl();
unsigned long ssl_thread_id_callback(void);
void ssl_locking_callback(int mode, int type, const char * file, int line);
void usage(std::ostream & out);
// Default command line settings
static int concurrency_limit(40);
static char url_format[1024] = "http://example.com/some/path?texture_id=%s.texture";
#if defined(WIN32)
#define strncpy(_a, _b, _c) strncpy_s(_a, _b, _c)
#define strtok_r(_a, _b, _c) strtok_s(_a, _b, _c)
int getopt(int argc, char * const argv[], const char *optstring);
char *optarg(NULL);
int optind(1);
#endif
class WorkingSet : public LLCore::HttpHandler
{
public:
WorkingSet();
bool reload(LLCore::HttpRequest *);
virtual void onCompleted(LLCore::HttpHandle handle, LLCore::HttpResponse * response);
void loadTextureUuids(FILE * in);
public:
struct Spec
{
std::string mUuid;
int mOffset;
int mLength;
};
typedef std::set<LLCore::HttpHandle> handle_set_t;
typedef std::vector<Spec> texture_list_t;
public:
bool mVerbose;
bool mRandomRange;
int mMaxConcurrency;
handle_set_t mHandles;
int mRemaining;
int mLimit;
int mAt;
std::string mUrl;
texture_list_t mTextures;
int mErrorsApi;
int mErrorsHttp;
int mSuccesses;
long mByteCount;
};
//
//
//
int main(int argc, char** argv)
{
bool do_random(false);
bool do_verbose(false);
int option(-1);
while (-1 != (option = getopt(argc, argv, "u:c:h?Rv")))
{
switch (option)
{
case 'u':
strncpy(url_format, optarg, sizeof(url_format));
url_format[sizeof(url_format) - 1] = '\0';
break;
case 'c':
{
unsigned long value;
char * end;
value = strtoul(optarg, &end, 10);
if (value < 1 || value > 100 || *end != '\0')
{
usage(std::cerr);
return 1;
}
concurrency_limit = value;
}
break;
case 'R':
do_random = true;
break;
case 'v':
do_verbose = true;
break;
case 'h':
case '?':
usage(std::cout);
return 0;
}
}
if ((optind + 1) != argc)
{
usage(std::cerr);
return 1;
}
FILE * uuids(fopen(argv[optind], "r"));
if (! uuids)
{
const char * errstr(strerror(errno));
std::cerr << "Couldn't open UUID file '" << argv[optind] << "'. Reason: "
<< errstr << std::endl;
return 1;
}
// Initialization
init_curl();
LLCore::HttpRequest::createService();
LLCore::HttpRequest::startThread();
// Get service point
LLCore::HttpRequest * hr = new LLCore::HttpRequest();
// Get a handler/working set
WorkingSet ws;
// Fill the working set with work
ws.mUrl = url_format;
ws.loadTextureUuids(uuids);
ws.mRandomRange = do_random;
ws.mVerbose = do_verbose;
ws.mMaxConcurrency = concurrency_limit;
if (! ws.mTextures.size())
{
std::cerr << "No UUIDs found in file '" << argv[optind] << "'." << std::endl;
return 1;
}
// Run it
while (! ws.reload(hr))
{
hr->update(1000);
#if defined(WIN32)
Sleep(5);
#else
usleep(5000);
#endif
}
// Report
std::cout << "HTTP errors: " << ws.mErrorsHttp << " API errors: " << ws.mErrorsApi
<< " Successes: " << ws.mSuccesses << " Byte count: " << ws.mByteCount
<< std::endl;
// Clean up
delete hr;
term_curl();
return 0;
}
void usage(std::ostream & out)
{
out << "\n"
"usage:\thttp_texture_load [options] uuid_file\n"
"\n"
"This is a standalone program to drive the New Platform HTTP Library.\n"
"The program is supplied with a file of texture UUIDs, one per line\n"
"These are fetched sequentially using a pool of concurrent connection\n"
"until all are fetched. The default URL format is only useful from\n"
"within Linden Lab but this can be overriden with a printf-style\n"
"URL formatting string on the command line.\n"
"\n"
"Options:\n"
"\n"
" -u <url_format> printf-style format string for URL generation\n"
" Default: " << url_format << "\n"
" -R Issue GETs with random Range: headers\n"
" -c <limit> Maximum request concurrency. Range: [1..100]\n"
" Default: " << concurrency_limit << "\n"
" -v Verbose mode. Issue some chatter while running\n"
" -h print this help\n"
"\n"
<< std::endl;
}
WorkingSet::WorkingSet()
: LLCore::HttpHandler(),
mVerbose(false),
mRandomRange(false),
mRemaining(200),
mLimit(200),
mAt(0),
mErrorsApi(0),
mErrorsHttp(0),
mSuccesses(0),
mByteCount(0L)
{
mTextures.reserve(30000);
}
bool WorkingSet::reload(LLCore::HttpRequest * hr)
{
int to_do((std::min)(mRemaining, mMaxConcurrency - int(mHandles.size())));
for (int i(0); i < to_do; ++i)
{
char buffer[1024];
#if defined(WIN32)
_snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1, mUrl.c_str(), mTextures[mAt].mUuid.c_str());
#else
snprintf(buffer, sizeof(buffer), mUrl.c_str(), mTextures[mAt].mUuid.c_str());
#endif
int offset(mRandomRange ? ((unsigned long) rand()) % 1000000UL : mTextures[mAt].mOffset);
int length(mRandomRange ? ((unsigned long) rand()) % 1000000UL : mTextures[mAt].mLength);
LLCore::HttpHandle handle;
if (offset || length)
{
handle = hr->requestGetByteRange(0, 0, buffer, offset, length, NULL, NULL, this);
}
else
{
handle = hr->requestGet(0, 0, buffer, NULL, NULL, this);
}
if (! handle)
{
// Fatal. Couldn't queue up something.
std::cerr << "Failed to queue work to HTTP Service. Reason: "
<< hr->getStatus().toString() << std::endl;
exit(1);
}
else
{
mHandles.insert(handle);
}
mAt++;
mRemaining--;
if (mVerbose)
{
static int count(0);
++count;
if (0 == (count %5))
std::cout << "Queued " << count << std::endl;
}
}
// Are we done?
return (! mRemaining) && mHandles.empty();
}
void WorkingSet::onCompleted(LLCore::HttpHandle handle, LLCore::HttpResponse * response)
{
handle_set_t::iterator it(mHandles.find(handle));
if (mHandles.end() == it)
{
// Wha?
std::cerr << "Failed to find handle in request list. Fatal." << std::endl;
exit(1);
}
else
{
LLCore::HttpStatus status(response->getStatus());
if (status)
{
// More success
LLCore::BufferArray * data(response->getBody());
mByteCount += data->size();
++mSuccesses;
}
else
{
// Something in this library or libcurl
if (status.isHttpStatus())
{
++mErrorsHttp;
}
else
{
++mErrorsApi;
}
}
mHandles.erase(it);
}
if (mVerbose)
{
static int count(0);
++count;
if (0 == (count %5))
std::cout << "Handled " << count << std::endl;
}
}
void WorkingSet::loadTextureUuids(FILE * in)
{
char buffer[1024];
while (fgets(buffer, sizeof(buffer), in))
{
WorkingSet::Spec texture;
char * state(NULL);
char * token = strtok_r(buffer, " \t\n,", &state);
if (token && 36 == strlen(token))
{
// Close enough for this function
texture.mUuid = token;
texture.mOffset = 0;
texture.mLength = 0;
token = strtok_r(buffer, " \t\n,", &state);
if (token)
{
int offset(atoi(token));
token = strtok_r(buffer, " \t\n,", &state);
if (token)
{
int length(atoi(token));
texture.mOffset = offset;
texture.mLength = length;
}
}
mTextures.push_back(texture);
}
}
mRemaining = mLimit = mTextures.size();
}
int ssl_mutex_count(0);
LLCoreInt::HttpMutex ** ssl_mutex_list = NULL;
void init_curl()
{
curl_global_init(CURL_GLOBAL_ALL);
ssl_mutex_count = CRYPTO_num_locks();
if (ssl_mutex_count > 0)
{
ssl_mutex_list = new LLCoreInt::HttpMutex * [ssl_mutex_count];
for (int i(0); i < ssl_mutex_count; ++i)
{
ssl_mutex_list[i] = new LLCoreInt::HttpMutex;
}
CRYPTO_set_locking_callback(ssl_locking_callback);
CRYPTO_set_id_callback(ssl_thread_id_callback);
}
}
void term_curl()
{
CRYPTO_set_locking_callback(NULL);
for (int i(0); i < ssl_mutex_count; ++i)
{
delete ssl_mutex_list[i];
}
delete [] ssl_mutex_list;
}
unsigned long ssl_thread_id_callback(void)
{
#if defined(WIN32)
return (unsigned long) GetCurrentThread();
#else
return (unsigned long) pthread_self();
#endif
}
void ssl_locking_callback(int mode, int type, const char * /* file */, int /* line */)
{
if (type >= 0 && type < ssl_mutex_count)
{
if (mode & CRYPTO_LOCK)
{
ssl_mutex_list[type]->lock();
}
else
{
ssl_mutex_list[type]->unlock();
}
}
}
#if defined(WIN32)
// Very much a subset of posix functionality. Don't push
// it too hard...
int getopt(int argc, char * const argv[], const char *optstring)
{
static int pos(0);
while (optind < argc)
{
if (pos == 0)
{
if (argv[optind][0] != '-')
return -1;
pos = 1;
}
if (! argv[optind][pos])
{
++optind;
pos = 0;
continue;
}
const char * thing(strchr(optstring, argv[optind][pos]));
if (! thing)
{
++optind;
return -1;
}
if (thing[1] == ':')
{
optarg = argv[++optind];
++optind;
pos = 0;
}
else
{
optarg = NULL;
++pos;
}
return *thing;
}
return -1;
}
#endif
|