Newer
Older
/**
* @file lluri.cpp
* @author Phoenix
* @date 2006-02-08
* @brief Implementation of the LLURI class.
*
* Copyright (c) 2006-$CurrentYear$, Linden Research, Inc.
* $License$
*/
#include "linden_common.h"
#include <iomanip>
#include "../llmath/lluuid.h"
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
// static
std::string LLURI::escape(const std::string& str, const std::string & allowed)
{
std::ostringstream ostr;
std::string::const_iterator it = str.begin();
std::string::const_iterator end = str.end();
for(; it != end; ++it)
{
std::string::value_type c = *it;
if(allowed.find(c) == std::string::npos)
{
ostr << "%"
<< std::uppercase << std::hex << std::setw(2) << std::setfill('0')
<< static_cast<U32>(c);
}
else
{
ostr << c;
}
}
return ostr.str();
}
// static
std::string LLURI::unescape(const std::string& str)
{
std::ostringstream ostr;
std::string::const_iterator it = str.begin();
std::string::const_iterator end = str.end();
for(; it != end; ++it)
{
if((*it) == '%')
{
++it;
if(it == end) break;
U8 c = hex_as_nybble(*it++);
c = c << 4;
if (it == end) break;
c |= hex_as_nybble(*it);
ostr.put((char)c);
}
else
{
ostr.put(*it);
}
}
return ostr.str();
}
namespace
const std::string unreserved()
{
static const std::string s =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
"0123456789"
"-._~";
return s;
}
const std::string sub_delims()
{
static const std::string s = "!$&'()*+,;=";
return s;
}
std::string escapeHostAndPort(const std::string& s)
{ return LLURI::escape(s, unreserved() + sub_delims() +":"); }
std::string escapePathComponent(const std::string& s)
{ return LLURI::escape(s, unreserved() + sub_delims() + ":@"); }
std::string escapeQueryVariable(const std::string& s)
{ return LLURI::escape(s, unreserved() + ":@!$'()*+,"); } // sub_delims - "&;=" + ":@"
std::string escapeQueryValue(const std::string& s)
{ return LLURI::escape(s, unreserved() + ":@!$'()*+,="); } // sub_delims - "&;" + ":@"
}
Don Kjer
committed
// TODO: USE CURL!! After http textures gets merged everywhere.
// static
std::string LLURI::escape(const std::string& str)
{
return escape(str,unreserved() + ":@!$'()*+,=");
}
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
LLURI::LLURI()
{
}
LLURI::LLURI(const std::string& escaped_str)
{
std::string::size_type delim_pos, delim_pos2;
delim_pos = escaped_str.find(':');
std::string temp;
if (delim_pos == std::string::npos)
{
mScheme = "";
mEscapedOpaque = escaped_str;
}
else
{
mScheme = escaped_str.substr(0, delim_pos);
mEscapedOpaque = escaped_str.substr(delim_pos+1);
}
if (mScheme == "http" || mScheme == "https" || mScheme == "ftp")
{
if (mEscapedOpaque.substr(0,2) != "//")
{
return;
}
delim_pos = mEscapedOpaque.find('/', 2);
delim_pos2 = mEscapedOpaque.find('?', 2);
// no path, no query
if (delim_pos == std::string::npos &&
delim_pos2 == std::string::npos)
{
mEscapedAuthority = mEscapedOpaque.substr(2);
mEscapedPath = "";
}
// path exist, no query
else if (delim_pos2 == std::string::npos)
{
mEscapedAuthority = mEscapedOpaque.substr(2,delim_pos-2);
mEscapedPath = mEscapedOpaque.substr(delim_pos);
}
// no path, only query
else if (delim_pos == std::string::npos ||
delim_pos2 < delim_pos)
{
mEscapedAuthority = mEscapedOpaque.substr(2,delim_pos2-2);
// query part will be broken out later
mEscapedPath = mEscapedOpaque.substr(delim_pos2);
}
// path and query
else
{
mEscapedAuthority = mEscapedOpaque.substr(2,delim_pos-2);
// query part will be broken out later
mEscapedPath = mEscapedOpaque.substr(delim_pos);
}
}
delim_pos = mEscapedPath.find('?');
if (delim_pos != std::string::npos)
{
mEscapedQuery = mEscapedPath.substr(delim_pos+1);
mEscapedPath = mEscapedPath.substr(0,delim_pos);
}
}
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
static BOOL isDefault(const std::string& scheme, U16 port)
{
if (scheme == "http")
return port == 80;
if (scheme == "https")
return port == 443;
if (scheme == "ftp")
return port == 21;
return FALSE;
}
LLURI::LLURI(const std::string& scheme,
const std::string& userName,
const std::string& password,
const std::string& hostName,
U16 port,
const std::string& escapedPath,
const std::string& escapedQuery)
: mScheme(scheme),
mEscapedPath(escapedPath),
mEscapedQuery(escapedQuery)
{
std::ostringstream auth;
std::ostringstream opaque;
opaque << "//";
if (!userName.empty())
{
auth << escape(userName);
if (!password.empty())
{
auth << ':' << escape(password);
}
auth << '@';
}
auth << hostName;
if (!isDefault(scheme, port))
{
auth << ':' << port;
}
mEscapedAuthority = auth.str();
opaque << mEscapedAuthority << escapedPath << escapedQuery;
mEscapedOpaque = opaque.str();
}
// static
LLURI LLURI::buildHTTP(const std::string& prefix,
if (prefix.find("://") != prefix.npos)
// it is a prefix
result = LLURI(prefix);
// it is just a host and optional port
result.mScheme = "http";
result.mEscapedAuthority = escapeHostAndPort(prefix);
if (path.isArray())
{
// break out and escape each path component
for (LLSD::array_const_iterator it = path.beginArray();
it != path.endArray();
++it)
{
lldebugs << "PATH: inserting " << it->asString() << llendl;
result.mEscapedPath += "/" + escapePathComponent(it->asString());
Don Kjer
committed
else if(path.isString())
{
result.mEscapedPath += "/" + escapePathComponent(path.asString());
}
else if(path.isUndefined())
{
// do nothing
}
else
{
llwarns << "Valid path arguments to buildHTTP are array, string, or undef, you passed type"
<< path.type() << llendl;
}
result.mEscapedOpaque = "//" + result.mEscapedAuthority +
result.mEscapedPath;
return result;
}
// static
LLURI LLURI::buildHTTP(const std::string& prefix,
Don Kjer
committed
LLURI uri = buildHTTP(prefix, path);
Kartic Krishnamurthy
committed
uri.mEscapedQuery = mapToQueryString(query);
uri.mEscapedOpaque += uri.mEscapedQuery ;
uri.mEscapedQuery.erase(0,1); // trim the leading '?'
Don Kjer
committed
return uri;
LLURI LLURI::buildHTTP(const std::string& host,
const U32& port,
const LLSD& path)
return LLURI::buildHTTP(llformat("%s:%u", host.c_str(), port), path);
}
// static
LLURI LLURI::buildHTTP(const std::string& host,
const U32& port,
const LLSD& path,
const LLSD& query)
{
return LLURI::buildHTTP(llformat("%s:%u", host.c_str(), port), path, query);
}
Aaron Brashears
committed
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
namespace {
LLURI buildBackboneURL(LLApp* app,
const std::string& p1 = "",
const std::string& p2 = "",
const std::string& p3 = "")
{
std::string host = "localhost:12040";
if (app)
{
host = app->getOption("backbone-host-port").asString();
}
LLSD path = LLSD::emptyArray();
if (!p1.empty()) path.append(p1);
if (!p2.empty()) path.append(p2);
if (!p3.empty()) path.append(p3);
return LLURI::buildHTTP(host, path);
}
}
#if LL_ENABLE_JANKY_DEPRECATED_WEB_SERVICE_CALLS
// static
LLURI LLURI::buildBulkAgentNamesURI(LLApp* app)
{
std::string host = "localhost:12040";
if (app)
{
host = app->getOption("backbone-host-port").asString();
}
LLSD path = LLSD::emptyArray();
path.append("agent");
path.append("names");
return buildHTTP(host, path);
}
// static
LLURI LLURI::buildBulkAgentNamesURI(LLApp* app)
{
std::string host = "localhost:12040";
if (app)
{
host = app->getOption("backbone-host-port").asString();
}
LLSD path = LLSD::emptyArray();
path.append("agent");
path.append("names");
return buildHTTP(host, path);
}
// static
LLURI LLURI::buildAgentSessionURI(const LLUUID& agent_id, LLApp* app)
{
return buildBackboneURL(app, "agent", agent_id.asString(), "session");
}
// static
LLURI LLURI::buildAgentNameURI(const LLUUID& agent_id, LLApp* app)
{
std::string host = "localhost:12040";
if (app)
{
host = app->getOption("backbone-host-port").asString();
}
LLSD path = LLSD::emptyArray();
path.append("agent");
path.append(agent_id);
path.append("name");
return buildHTTP(host, path);
}
// static
LLURI LLURI::buildAgentNameURI(const LLUUID& agent_id, LLApp* app)
{
std::string host = "localhost:12040";
if (app)
{
host = app->getOption("backbone-host-port").asString();
}
LLSD path = LLSD::emptyArray();
path.append("agent");
path.append(agent_id);
path.append("name");
return buildHTTP(host, path);
}
// static
LLURI LLURI::buildAgentLoginInfoURI(const LLUUID& agent_id, const std::string& dataserver)
{
LLSD path = LLSD::emptyArray();
path.append("agent");
path.append(agent_id);
path.append("logininfo");
return buildHTTP(dataserver, path);
}
#endif // LL_ENABLE_JANKY_DEPRECATED_WEB_SERVICE_CALLS
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
std::string LLURI::asString() const
{
if (mScheme.empty())
{
return mEscapedOpaque;
}
else
{
return mScheme + ":" + mEscapedOpaque;
}
}
std::string LLURI::scheme() const
{
return mScheme;
}
std::string LLURI::opaque() const
{
return unescape(mEscapedOpaque);
}
std::string LLURI::authority() const
{
return unescape(mEscapedAuthority);
}
namespace {
void findAuthorityParts(const std::string& authority,
std::string& user,
std::string& host,
std::string& port)
{
std::string::size_type start_pos = authority.find('@');
if (start_pos == std::string::npos)
{
user = "";
start_pos = 0;
}
else
{
user = authority.substr(0, start_pos);
start_pos += 1;
}
std::string::size_type end_pos = authority.find(':', start_pos);
if (end_pos == std::string::npos)
{
host = authority.substr(start_pos);
port = "";
}
else
{
host = authority.substr(start_pos, end_pos - start_pos);
port = authority.substr(end_pos + 1);
}
}
}
std::string LLURI::hostName() const
{
std::string user, host, port;
findAuthorityParts(mEscapedAuthority, user, host, port);
return unescape(host);
}
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
std::string LLURI::userName() const
{
std::string user, userPass, host, port;
findAuthorityParts(mEscapedAuthority, userPass, host, port);
std::string::size_type pos = userPass.find(':');
if (pos != std::string::npos)
{
user = userPass.substr(0, pos);
}
return unescape(user);
}
std::string LLURI::password() const
{
std::string pass, userPass, host, port;
findAuthorityParts(mEscapedAuthority, userPass, host, port);
std::string::size_type pos = userPass.find(':');
if (pos != std::string::npos)
{
pass = userPass.substr(pos + 1);
}
return unescape(pass);
}
BOOL LLURI::defaultPort() const
{
return isDefault(mScheme, hostPort());
}
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
U16 LLURI::hostPort() const
{
std::string user, host, port;
findAuthorityParts(mEscapedAuthority, user, host, port);
if (port.empty())
{
if (mScheme == "http")
return 80;
if (mScheme == "https")
return 443;
if (mScheme == "ftp")
return 21;
return 0;
}
return atoi(port.c_str());
}
std::string LLURI::path() const
{
return unescape(mEscapedPath);
}
std::string LLURI::query() const
{
return unescape(mEscapedQuery);
}
LLSD LLURI::queryMap() const
{
return queryMap(mEscapedQuery);
}
// static
LLSD LLURI::queryMap(std::string escaped_query_string)
{
lldebugs << "LLURI::queryMap query params: " << escaped_query_string << llendl;
LLSD result = LLSD::emptyArray();
while(!escaped_query_string.empty())
{
// get tuple first
std::string tuple;
std::string::size_type tuple_begin = escaped_query_string.find('&');
if (tuple_begin != std::string::npos)
{
tuple = escaped_query_string.substr(0, tuple_begin);
escaped_query_string = escaped_query_string.substr(tuple_begin+1);
}
else
{
tuple = escaped_query_string;
escaped_query_string = "";
}
if (tuple.empty()) continue;
// parse tuple
std::string::size_type key_end = tuple.find('=');
if (key_end != std::string::npos)
{
std::string key = unescape(tuple.substr(0,key_end));
std::string value = unescape(tuple.substr(key_end+1));
lldebugs << "inserting key " << key << " value " << value << llendl;
result[key] = value;
}
else
{
lldebugs << "inserting key " << unescape(tuple) << " value true" << llendl;
result[unescape(tuple)] = true;
}
}
return result;
}
Don Kjer
committed
std::string LLURI::mapToQueryString(const LLSD& queryMap)
{
std::string query_string;
if (queryMap.isMap())
{
Kartic Krishnamurthy
committed
bool first_element = true;
LLSD::map_const_iterator iter = queryMap.beginMap();
LLSD::map_const_iterator end = queryMap.endMap();
std::ostringstream ostr;
for (; iter != end; ++iter)
Don Kjer
committed
{
Kartic Krishnamurthy
committed
if(first_element)
{
ostr << "?";
first_element = false;
}
else
{
ostr << "&";
}
ostr << escapeQueryVariable(iter->first);
if(iter->second.isDefined())
{
ostr << "=" << escapeQueryValue(iter->second.asString());
}
Don Kjer
committed
}
Kartic Krishnamurthy
committed
query_string = ostr.str();
Don Kjer
committed
}
return query_string;
}