| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Adrien Devresse <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <iomanip> |
| 23 |
|
| 24 |
#include <davix_internal.hpp> |
| 25 |
#include <utils/davix_uri.hpp> |
| 26 |
|
| 27 |
|
| 28 |
namespace Davix { |
| 29 |
|
| 30 |
/** |
| 31 |
* @cond HIDDEN_SYMBOLS |
| 32 |
*/ |
| 33 |
|
| 34 |
const std::string void_str(""); |
| 35 |
|
| 36 |
int davix_uri_parse(const std::string & uri_str, UriPrivate & res); |
| 37 |
int davix_uri_cmp(const UriPrivate &u1, const UriPrivate &u2); |
| 38 |
std::string davix_path_escape(const std::string & str, bool escapeSlashes); |
| 39 |
std::string davix_path_unescape(const std::string & str); |
| 40 |
std::string davix_userinfo_backslash_escape(const std::string & str); |
| 41 |
|
| 42 |
struct UriPrivate{ |
| 43 |
UriPrivate() : |
| 44 |
code(StatusCode::UriParsingError), |
| 45 |
proto(), |
| 46 |
userinfo(), |
| 47 |
path(), |
| 48 |
host(), |
| 49 |
query(), |
| 50 |
fragment(), |
| 51 |
port(0), |
| 52 |
_uri_string() {} |
| 53 |
|
| 54 |
UriPrivate(const UriPrivate & orig): |
| 55 |
code(orig.code), |
| 56 |
proto(orig.proto), |
| 57 |
userinfo(orig.userinfo), |
| 58 |
path(orig.path), |
| 59 |
host(orig.host), |
| 60 |
query(orig.query), |
| 61 |
fragment(orig.fragment), |
| 62 |
port(orig.port), |
| 63 |
_uri_string(orig._uri_string) {} |
| 64 |
|
| 65 |
~UriPrivate(){ |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
void parsing(const std::string & uri_string){ |
| 70 |
_uri_string = uri_string; |
| 71 |
if( davix_uri_parse(_uri_string, *this) == 0 |
| 72 |
&& proto.size() != 0 |
| 73 |
&& port != UINT_MAX){ |
| 74 |
code = StatusCode::OK; |
| 75 |
}else{ |
| 76 |
clear(); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void addParam(const std::string & key, const std::string & value, std::string & target) { |
| 81 |
if(target.size() == 0) { |
| 82 |
target = key; |
| 83 |
target.append("="); |
| 84 |
target.append(value); |
| 85 |
} |
| 86 |
else { |
| 87 |
target.append("&"); |
| 88 |
target.append(key); |
| 89 |
target.append("="); |
| 90 |
target.append(value); |
| 91 |
} |
| 92 |
_update_string(); |
| 93 |
} |
| 94 |
|
| 95 |
void addQueryParam(const std::string & key, const std::string & value) { |
| 96 |
addParam(key, value, query); |
| 97 |
} |
| 98 |
|
| 99 |
void addFragmentParam(const std::string & key, const std::string & value) { |
| 100 |
addParam(key, value, fragment); |
| 101 |
} |
| 102 |
|
| 103 |
void addPathSegment(const std::string &seg) { |
| 104 |
ensureTrailingSlash(); |
| 105 |
path += seg; |
| 106 |
_update_string(); |
| 107 |
} |
| 108 |
|
| 109 |
void ensureTrailingSlash() { |
| 110 |
if(path.size() == 0 || path[path.size() - 1] != '/') { |
| 111 |
path += "/"; |
| 112 |
_update_string(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void removeTrailingSlash() { |
| 117 |
if(path.size() != 0 && path[path.size() - 1] == '/') { |
| 118 |
path.erase(path.size()-1, 1); |
| 119 |
_update_string(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void clear(){ |
| 124 |
proto.clear(); |
| 125 |
userinfo.clear(); |
| 126 |
path.clear(); |
| 127 |
host.clear(); |
| 128 |
query.clear(); |
| 129 |
fragment.clear(); |
| 130 |
port = 0; |
| 131 |
} |
| 132 |
|
| 133 |
void _update_string() { |
| 134 |
std::ostringstream ss; |
| 135 |
ss << proto << "://"; |
| 136 |
if(userinfo.size() > 0) ss << "@" << userinfo; |
| 137 |
ss << host; |
| 138 |
if(port != 0) ss << ":" << port; |
| 139 |
ss << path; |
| 140 |
if(query.size() > 0) ss << "?" << query; |
| 141 |
if(fragment.size() > 0) ss << "#" << fragment; |
| 142 |
|
| 143 |
_uri_string = ss.str(); |
| 144 |
} |
| 145 |
|
| 146 |
StatusCode::Code code; |
| 147 |
std::string proto, userinfo, path, host, query, fragment; |
| 148 |
unsigned int port; |
| 149 |
std::string _uri_string; |
| 150 |
std::unique_ptr<std::string> query_and_path; |
| 151 |
|
| 152 |
}; |
| 153 |
|
| 154 |
Uri::Uri() : |
| 155 |
d_ptr(new UriPrivate()){ |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
Uri::Uri(const std::string & uri) : |
| 160 |
d_ptr(new UriPrivate()){ |
| 161 |
d_ptr->parsing(uri); |
| 162 |
} |
| 163 |
|
| 164 |
Uri::Uri(const Uri & uri) : |
| 165 |
d_ptr(new UriPrivate(*(uri.d_ptr))){ |
| 166 |
} |
| 167 |
|
| 168 |
void Uri::addQueryParam(const std::string & key, const std::string & value) { |
| 169 |
d_ptr->addQueryParam(Uri::queryParamEscape(key), Uri::queryParamEscape(value)); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
void Uri::addFragmentParam(const std::string & key, const std::string & value) { |
| 174 |
d_ptr->addFragmentParam(key, value); |
| 175 |
} |
| 176 |
|
| 177 |
void Uri::addPathSegment(const std::string &seg) { |
| 178 |
d_ptr->addPathSegment(seg); |
| 179 |
} |
| 180 |
|
| 181 |
void Uri::ensureTrailingSlash() { |
| 182 |
d_ptr->ensureTrailingSlash(); |
| 183 |
} |
| 184 |
|
| 185 |
void Uri::removeTrailingSlash() { |
| 186 |
d_ptr->removeTrailingSlash(); |
| 187 |
} |
| 188 |
|
| 189 |
bool Uri::queryParamExists(const std::string &key) const { |
| 190 |
ParamVec queryVec = this->getQueryVec(); |
| 191 |
for(ParamVec::iterator it = queryVec.begin(); it != queryVec.end(); it++) { |
| 192 |
if(it->first == key) |
| 193 |
return true; |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
Uri::~Uri(){ |
| 199 |
delete d_ptr; |
| 200 |
} |
| 201 |
|
| 202 |
int Uri::getPort() const{ |
| 203 |
if(d_ptr->code != StatusCode::OK) |
| 204 |
return -1; |
| 205 |
return static_cast<int>(d_ptr->port); |
| 206 |
} |
| 207 |
|
| 208 |
const std::string & Uri::getHost() const{ |
| 209 |
return d_ptr->host; |
| 210 |
} |
| 211 |
|
| 212 |
const std::string & Uri::getString() const{ |
| 213 |
return d_ptr->_uri_string; |
| 214 |
} |
| 215 |
|
| 216 |
const std::string & Uri::getProtocol() const { |
| 217 |
return d_ptr->proto; |
| 218 |
} |
| 219 |
|
| 220 |
const std::string & Uri::getUserInfo() const{ |
| 221 |
return d_ptr->userinfo; |
| 222 |
} |
| 223 |
|
| 224 |
const std::string & Uri::getPath() const { |
| 225 |
return d_ptr->path; |
| 226 |
} |
| 227 |
|
| 228 |
void Uri::setPath(const std::string & path) { |
| 229 |
d_ptr->path = path; |
| 230 |
d_ptr->_update_string(); |
| 231 |
} |
| 232 |
|
| 233 |
void Uri::setProtocol(const std::string & protocol) { |
| 234 |
d_ptr->proto = protocol; |
| 235 |
d_ptr->_update_string(); |
| 236 |
} |
| 237 |
|
| 238 |
void Uri::httpizeProtocol() { |
| 239 |
if(d_ptr->proto == "s3" || d_ptr->proto == "dav" || d_ptr->proto == "gcloud" || d_ptr->proto == "swift" || d_ptr->proto == "cs3") { |
| 240 |
setProtocol("http"); |
| 241 |
} |
| 242 |
if(d_ptr->proto == "s3s" || d_ptr->proto == "davs" || d_ptr->proto == "gclouds" || d_ptr->proto == "swifts" || d_ptr->proto == "cs3s") { |
| 243 |
setProtocol("https"); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
const std::string & Uri::getPathAndQuery() const { |
| 248 |
if(d_ptr->query_and_path.get() == NULL){ |
| 249 |
if(d_ptr->query.size() > 0){ |
| 250 |
d_ptr->query_and_path.reset(new std::string(d_ptr->path + "?" + d_ptr->query)); |
| 251 |
}else{ |
| 252 |
d_ptr->query_and_path.reset(new std::string(d_ptr->path)); |
| 253 |
} |
| 254 |
} |
| 255 |
return *(d_ptr->query_and_path); |
| 256 |
} |
| 257 |
|
| 258 |
static ParamVec splitParams(const std::string &str) { |
| 259 |
ParamVec params; |
| 260 |
const std::string paramSeparator = "&"; |
| 261 |
const std::string pairSeparator = "="; |
| 262 |
|
| 263 |
std::string::size_type curr = 0; |
| 264 |
while(curr < str.length()) { |
| 265 |
std::string::size_type next = str.find(paramSeparator, curr); |
| 266 |
if(next == std::string::npos) next = str.length(); |
| 267 |
std::string param = str.substr(curr, next-curr); |
| 268 |
|
| 269 |
// separate the two parts between '=' |
| 270 |
std::string::size_type eq = param.find(pairSeparator); |
| 271 |
if(eq == std::string::npos) { |
| 272 |
params.push_back(ParamLine(param, "")); |
| 273 |
} else { |
| 274 |
params.push_back(ParamLine(param.substr(0, eq), param.substr(eq+1, param.length()))); |
| 275 |
} |
| 276 |
|
| 277 |
curr = next+1; |
| 278 |
} |
| 279 |
return params; |
| 280 |
} |
| 281 |
|
| 282 |
const std::string & Uri::getFragment() const { |
| 283 |
return d_ptr->fragment; |
| 284 |
} |
| 285 |
|
| 286 |
bool Uri::fragmentParamExists(const std::string ¶m) const { |
| 287 |
ParamVec fragmentVec = splitParams(getFragment()); |
| 288 |
for(ParamVec::iterator it = fragmentVec.begin(); it != fragmentVec.end(); it++) { |
| 289 |
if(it->first == param) |
| 290 |
return true; |
| 291 |
} |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
const std::string Uri::getFragmentParam(const std::string ¶m) const { |
| 296 |
ParamVec fragmentVec = splitParams(getFragment()); |
| 297 |
for(ParamVec::iterator it = fragmentVec.begin(); it != fragmentVec.end(); it++) { |
| 298 |
if(it->first == param) |
| 299 |
return it->second; |
| 300 |
} |
| 301 |
return ""; |
| 302 |
} |
| 303 |
|
| 304 |
ParamVec Uri::getQueryVec() const { |
| 305 |
return splitParams(getQuery()); |
| 306 |
} |
| 307 |
|
| 308 |
const std::string & Uri::getQuery() const{ |
| 309 |
return d_ptr->query; |
| 310 |
} |
| 311 |
|
| 312 |
Uri & Uri::operator =(const Uri & orig){ |
| 313 |
if(this == &orig) |
| 314 |
return *this; |
| 315 |
|
| 316 |
if (d_ptr) delete d_ptr; |
| 317 |
d_ptr = new UriPrivate(*(orig.d_ptr)); |
| 318 |
return *this; |
| 319 |
} |
| 320 |
|
| 321 |
StatusCode::Code Uri::getStatus() const{ |
| 322 |
return d_ptr->code; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
bool Uri::equal(const Uri &u2) const{ |
| 327 |
if(this->getStatus() != Davix::StatusCode::OK || u2.getStatus() != Davix::StatusCode::OK) |
| 328 |
return false; |
| 329 |
|
| 330 |
return davix_uri_cmp(*d_ptr, *(u2.d_ptr))==0; |
| 331 |
} |
| 332 |
|
| 333 |
bool Uri::operator ==(const Uri & u2) const{ |
| 334 |
return this->equal(u2); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
std::string Uri::escapeString(const std::string & str){ |
| 339 |
return davix_path_escape(str, false); |
| 340 |
} |
| 341 |
|
| 342 |
std::string Uri::unescapeString(const std::string & str){ |
| 343 |
return davix_path_unescape(str); |
| 344 |
} |
| 345 |
|
| 346 |
std::string Uri::queryParamEscape(const std::string & str) { |
| 347 |
return davix_path_escape(str, true); |
| 348 |
} |
| 349 |
|
| 350 |
// Determine whether this is a URL, and if so, URI-escape the right part. |
| 351 |
// Otherwise, simple string concatenation. |
| 352 |
std::string Uri::join(const std::string &left, const std::string &right) { |
| 353 |
Uri leftUri(left); |
| 354 |
|
| 355 |
if(leftUri.getStatus() != StatusCode::OK) { |
| 356 |
if(left.size() == 0 || left[left.size()-1] != '/') { |
| 357 |
return left + "/" + right; |
| 358 |
} |
| 359 |
return left + right; |
| 360 |
} |
| 361 |
|
| 362 |
leftUri.ensureTrailingSlash(); |
| 363 |
return leftUri.getString() + Uri::escapeString(right); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
// FIX IT : does not manage properly ../ |
| 369 |
Uri Uri::fromRelativePath(const Uri &uri, const std::string &relPath){ |
| 370 |
std::ostringstream ss; |
| 371 |
if(relPath.size() >= 2){ |
| 372 |
// test if not absolute |
| 373 |
std::string::const_iterator it; |
| 374 |
if( ( it = std::find(relPath.begin(), relPath.end(), '/')) != relPath.end() |
| 375 |
&& it != relPath.begin() && *(it-1) == ':' |
| 376 |
&& (it+1) != relPath.end() && *(it+1) == '/'){ |
| 377 |
return Uri(relPath); |
| 378 |
} |
| 379 |
|
| 380 |
// RFC 3986 network-path reference” |
| 381 |
if(relPath[0] == '/' && relPath[1] == '/'){ |
| 382 |
|
| 383 |
ss << uri.getProtocol() << ":" << relPath; |
| 384 |
return Uri(ss.str()); |
| 385 |
} |
| 386 |
|
| 387 |
// RFC 3986 relative ” |
| 388 |
if(relPath[0] == '.' && relPath[1] == '/'){ |
| 389 |
ss << uri.getString() << "/"; |
| 390 |
std::copy(relPath.begin()+2,relPath.end(), std::ostreambuf_iterator<char>(ss)); |
| 391 |
return Uri(ss.str()); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// RFC 3986 abs path ” |
| 396 |
if( relPath.size() >= 1){ |
| 397 |
if(relPath[0] == '/'){ |
| 398 |
ss << uri.getProtocol() << "://"; |
| 399 |
if(uri.getUserInfo().size() >0){ |
| 400 |
ss << uri.getUserInfo() << '@'; |
| 401 |
} |
| 402 |
ss << uri.getHost(); |
| 403 |
if(uri.getPort() != 0){ |
| 404 |
ss << ':' << uri.getPort(); |
| 405 |
} |
| 406 |
ss << relPath; |
| 407 |
return Uri(ss.str()); |
| 408 |
} |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
ss << uri.getString() <<'/'<< relPath; |
| 413 |
return Uri(ss.str()); |
| 414 |
} |
| 415 |
|
| 416 |
bool uriCheckError(const Uri &uri, DavixError **err){ |
| 417 |
if(uri.getStatus() == StatusCode::OK) |
| 418 |
return true; |
| 419 |
DavixError::setupError(err, davix_scope_uri_parser(), uri.getStatus(), std::string("Uri syntax Invalid : ").append(uri.getString())); |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
unsigned int httpUriGetPort(const Uri & uri){ |
| 426 |
int port = uri.getPort(); |
| 427 |
if( port ==0){ |
| 428 |
const std::string & scheme = uri.getProtocol(); |
| 429 |
if(*scheme.rbegin() == 's') // davs, https or s3s |
| 430 |
port = 443; |
| 431 |
else |
| 432 |
port =80; |
| 433 |
} |
| 434 |
return port; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 439 |
/// part imported from neon parser ne_uri, converted C++ |
| 440 |
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 441 |
|
| 442 |
|
| 443 |
/* URI ABNF from RFC 3986: */ |
| 444 |
|
| 445 |
#define PS (0x0001) /* "+" */ |
| 446 |
#define PC (0x0002) /* "%" */ |
| 447 |
#define DS (0x0004) /* "-" */ |
| 448 |
#define DT (0x0008) /* "." */ |
| 449 |
#define US (0x0010) /* "_" */ |
| 450 |
#define TD (0x0020) /* "~" */ |
| 451 |
#define FS (0x0040) /* "/" */ |
| 452 |
#define CL (0x0080) /* ":" */ |
| 453 |
#define AT (0x0100) /* "@" */ |
| 454 |
#define QU (0x0200) /* "?" */ |
| 455 |
|
| 456 |
#define DG (0x0400) /* DIGIT */ |
| 457 |
#define AL (0x0800) /* ALPHA */ |
| 458 |
|
| 459 |
#define GD (0x1000) /* gen-delims = "#" / "[" / "]" |
| 460 |
* ... except ":", "/", "@", and "?" */ |
| 461 |
|
| 462 |
#define SD (0x2000) /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
| 463 |
* / "*" / "+" / "," / ";" / "=" |
| 464 |
* ... except "+" which is PS */ |
| 465 |
|
| 466 |
#define OT (0x4000) /* others */ |
| 467 |
|
| 468 |
#define URI_ALPHA (AL) |
| 469 |
#define URI_DIGIT (DG) |
| 470 |
|
| 471 |
/* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */ |
| 472 |
#define URI_UNRESERVED (AL | DG | DS | DT | US | TD) |
| 473 |
/* scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) */ |
| 474 |
#define URI_SCHEME (AL | DG | PS | DS | DT) |
| 475 |
/* real sub-delims definition, including "+" */ |
| 476 |
#define URI_SUBDELIM (PS | SD) |
| 477 |
/* real gen-delims definition, including ":", "/", "@" and "?" */ |
| 478 |
#define URI_GENDELIM (GD | CL | FS | AT | QU) |
| 479 |
/* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */ |
| 480 |
#define URI_USERINFO (URI_UNRESERVED | PC | URI_SUBDELIM | CL) |
| 481 |
/* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" */ |
| 482 |
#define URI_PCHAR (URI_UNRESERVED | PC | URI_SUBDELIM | CL | AT) |
| 483 |
/* invented: segchar = pchar / "/" */ |
| 484 |
#define URI_SEGCHAR (URI_PCHAR | FS) |
| 485 |
/* query = *( pchar / "/" / "?" ) */ |
| 486 |
#define URI_QUERY (URI_PCHAR | FS | QU) |
| 487 |
/* fragment == query */ |
| 488 |
#define URI_FRAGMENT URI_QUERY |
| 489 |
|
| 490 |
/* any characters which should be path-escaped: */ |
| 491 |
#define URI_ESCAPE ((URI_GENDELIM & ~(FS)) | URI_SUBDELIM | OT | PC) |
| 492 |
|
| 493 |
static const unsigned int uri_chars[256] = { |
| 494 |
/* 0xXX x0 x2 x4 x6 x8 xA xC xE */ |
| 495 |
/* 0x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 496 |
/* 1x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 497 |
/* 2x */ OT, SD, OT, GD, SD, PC, SD, SD, SD, SD, SD, PS, SD, DS, DT, FS, |
| 498 |
/* 3x */ DG, DG, DG, DG, DG, DG, DG, DG, DG, DG, CL, SD, OT, SD, OT, QU, |
| 499 |
/* 4x */ AT, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, |
| 500 |
/* 5x */ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, GD, OT, GD, OT, US, |
| 501 |
/* 6x */ OT, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, |
| 502 |
/* 7x */ AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, AL, OT, OT, OT, TD, OT, |
| 503 |
/* 8x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 504 |
/* 9x */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 505 |
/* Ax */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 506 |
/* Bx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 507 |
/* Cx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 508 |
/* Dx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 509 |
/* Ex */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, |
| 510 |
/* Fx */ OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT, OT |
| 511 |
}; |
| 512 |
|
| 513 |
#define uri_lookup(ch) (uri_chars[(unsigned char)ch]) |
| 514 |
|
| 515 |
|
| 516 |
int davix_uri_parse(const std::string & uri_str, UriPrivate & res) |
| 517 |
{ |
| 518 |
const char *p, *s, *uri; |
| 519 |
p = s = uri = uri_str.c_str(); |
| 520 |
|
| 521 |
/* => s = p = URI-reference */ |
| 522 |
|
| 523 |
if (uri_lookup(*p) & URI_ALPHA) { |
| 524 |
while (uri_lookup(*p) & URI_SCHEME) |
| 525 |
p++; |
| 526 |
|
| 527 |
if (*p == ':') { |
| 528 |
res.proto.assign(uri, p -s); |
| 529 |
s = p + 1; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
/* => s = heir-part, or s = relative-part */ |
| 534 |
|
| 535 |
if (s[0] == '/' && s[1] == '/') { |
| 536 |
const char *pa; |
| 537 |
bool escape_userinfo = false; |
| 538 |
|
| 539 |
/* => s = "//" authority path-abempty (from expansion of |
| 540 |
* either heir-part of relative-part) */ |
| 541 |
|
| 542 |
/* authority = [ userinfo "@" ] host [ ":" port ] */ |
| 543 |
|
| 544 |
s = pa = s + 2; /* => s = authority */ |
| 545 |
|
| 546 |
while (*pa != '/' && *pa != '\0') |
| 547 |
pa++; |
| 548 |
/* => pa = path-abempty */ |
| 549 |
|
| 550 |
p = s; |
| 551 |
while (p < pa && |
| 552 |
((uri_lookup(*p) & URI_USERINFO) || (*p == '\\'))) { |
| 553 |
if (*p == '\\') { /* Dealing with "\" */ |
| 554 |
/* Look-up next character for possible escaping */ |
| 555 |
if (p + 1 < pa && uri_lookup(*(p+1)) & URI_GENDELIM) { |
| 556 |
escape_userinfo = true; |
| 557 |
p++; |
| 558 |
} |
| 559 |
} |
| 560 |
p++; |
| 561 |
} |
| 562 |
|
| 563 |
if (*p == '@') { |
| 564 |
res.userinfo.assign(s, p-s); |
| 565 |
s = p + 1; |
| 566 |
|
| 567 |
if (escape_userinfo) { |
| 568 |
res.userinfo = davix_userinfo_backslash_escape(res.userinfo); |
| 569 |
} |
| 570 |
} |
| 571 |
/* => s = host */ |
| 572 |
|
| 573 |
if (s[0] == '[') { |
| 574 |
p = s + 1; |
| 575 |
|
| 576 |
while (*p != ']' && p < pa) |
| 577 |
p++; |
| 578 |
|
| 579 |
if (p == pa || (p + 1 != pa && p[1] != ':')) { |
| 580 |
/* Ill-formed IP-literal. */ |
| 581 |
return -1; |
| 582 |
} |
| 583 |
|
| 584 |
p++; /* => p = colon */ |
| 585 |
} else { |
| 586 |
/* Find the colon. */ |
| 587 |
p = pa; |
| 588 |
while (*p != ':' && p > s) |
| 589 |
p--; |
| 590 |
} |
| 591 |
|
| 592 |
if (p == s) { |
| 593 |
p = pa; |
| 594 |
/* No colon; => p = path-abempty */ |
| 595 |
} else if (p + 1 != pa) { |
| 596 |
/* => p = colon */ |
| 597 |
if(*p == ':') { |
| 598 |
res.port = atoi(p + 1); |
| 599 |
if( res.port == 0) |
| 600 |
res.port = UINT_MAX; |
| 601 |
} |
| 602 |
} |
| 603 |
res.host.assign(s, p - s); |
| 604 |
|
| 605 |
s = pa; |
| 606 |
|
| 607 |
if (*s == '\0') { |
| 608 |
s = "/"; /* FIXME: scheme-specific. */ |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
/* => s = path-abempty / path-absolute / path-rootless |
| 613 |
* / path-empty / path-noscheme */ |
| 614 |
|
| 615 |
p = s; |
| 616 |
|
| 617 |
while (uri_lookup(*p) & URI_SEGCHAR) |
| 618 |
p++; |
| 619 |
|
| 620 |
/* => p = [ "?" query ] [ "#" fragment ] */ |
| 621 |
|
| 622 |
res.path.assign(s, p - s); |
| 623 |
|
| 624 |
if (*p != '\0') { |
| 625 |
s = p++; |
| 626 |
|
| 627 |
while (uri_lookup(*p) & URI_QUERY) |
| 628 |
p++; |
| 629 |
|
| 630 |
/* => p = [ "#" fragment ] */ |
| 631 |
/* => s = [ "?" query ] [ "#" fragment ] */ |
| 632 |
|
| 633 |
if (*s == '?') { |
| 634 |
res.query.assign(s + 1, p - s - 1); |
| 635 |
|
| 636 |
if (*p != '\0') { |
| 637 |
s = p++; |
| 638 |
|
| 639 |
while (uri_lookup(*p) & URI_FRAGMENT) |
| 640 |
p++; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
/* => p now points to the next character after the |
| 645 |
* URI-reference; which should be the NUL byte. */ |
| 646 |
|
| 647 |
if (*s == '#') { |
| 648 |
res.fragment.assign(s + 1, p - s - 1); |
| 649 |
} |
| 650 |
else if (*p || *s != '?') { |
| 651 |
return -1; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
// As specified by RFC 2616, section 3.2.3. |
| 660 |
int davix_uri_cmp(const UriPrivate & u1, const UriPrivate & u2) |
| 661 |
{ |
| 662 |
|
| 663 |
int diff; |
| 664 |
if( (diff= u1.path.compare(u2.path)) != 0) |
| 665 |
return diff; |
| 666 |
if( (diff= strcasecmp(u1.host.c_str(), u2.host.c_str())) != 0) |
| 667 |
return diff; |
| 668 |
if( (diff= strcasecmp(u1.proto.c_str(), u2.proto.c_str())) != 0) |
| 669 |
return diff; |
| 670 |
if( (diff= u1.query.compare(u2.query)) != 0) |
| 671 |
return diff; |
| 672 |
if( (diff= u1.fragment.compare(u2.fragment)) != 0) |
| 673 |
return diff; |
| 674 |
if( (diff= u1.userinfo.compare(u2.userinfo)) != 0) |
| 675 |
return diff; |
| 676 |
return u2.port - u1.port; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
std::string davix_path_unescape(const std::string & str) |
| 682 |
{ |
| 683 |
const char *pnt, *uri = (const char*)str.c_str(); |
| 684 |
char *retpos, buf[5] = { "0x00" }; |
| 685 |
|
| 686 |
char buffer[str.size() + 1]; |
| 687 |
retpos = buffer; |
| 688 |
for (pnt = uri; *pnt != '\0'; pnt++) { |
| 689 |
if (*pnt == '%') { |
| 690 |
if (!isxdigit((unsigned char) pnt[1]) || |
| 691 |
!isxdigit((unsigned char) pnt[2])) { |
| 692 |
/* Invalid URI */ |
| 693 |
return std::string(); |
| 694 |
} |
| 695 |
buf[2] = *++pnt; buf[3] = *++pnt; /* bit faster than memcpy */ |
| 696 |
*retpos++ = (char)strtol(buf, NULL, 16); |
| 697 |
} else { |
| 698 |
*retpos++ = *pnt; |
| 699 |
} |
| 700 |
} |
| 701 |
*retpos = '\0'; |
| 702 |
return std::string(buffer); |
| 703 |
} |
| 704 |
|
| 705 |
/* CH must be an unsigned char; evaluates to 1 if CH should be |
| 706 |
* percent-encoded. */ |
| 707 |
inline bool path_escape_ch(const char ch, bool escapeSlashes) { |
| 708 |
return (uri_lookup(ch) & URI_ESCAPE) || (escapeSlashes && ch == '/'); |
| 709 |
} |
| 710 |
|
| 711 |
std::string davix_path_escape(const std::string & str, bool escapeSlashes) |
| 712 |
{ |
| 713 |
const unsigned char *pnt, *path= (const unsigned char*)(str.c_str()); |
| 714 |
char *p; |
| 715 |
size_t count = 0; |
| 716 |
|
| 717 |
for (pnt = (const unsigned char *)path; *pnt != '\0'; pnt++) { |
| 718 |
count += path_escape_ch(*pnt, escapeSlashes); |
| 719 |
} |
| 720 |
|
| 721 |
if (count == 0) { |
| 722 |
return std::string((const char*) path); |
| 723 |
} |
| 724 |
|
| 725 |
char buffer[str.size() + 2 * count + 1]; |
| 726 |
p = buffer; |
| 727 |
for (pnt = (const unsigned char *)path; *pnt != '\0'; pnt++) { |
| 728 |
if (path_escape_ch(*pnt, escapeSlashes)) { |
| 729 |
/* Escape it - %<hex><hex> */ |
| 730 |
sprintf(p, "%%%02X", (unsigned char) *pnt); |
| 731 |
p += 3; |
| 732 |
} else { |
| 733 |
*p++ = *pnt; |
| 734 |
} |
| 735 |
} |
| 736 |
*p = '\0'; |
| 737 |
return std::string(buffer); |
| 738 |
} |
| 739 |
|
| 740 |
#undef path_escape_ch |
| 741 |
|
| 742 |
std::string davix_userinfo_backslash_escape(const std::string & str) |
| 743 |
{ |
| 744 |
std::ostringstream oss; |
| 745 |
oss << std::uppercase << std::hex; |
| 746 |
|
| 747 |
for (size_t i = 0; i < str.size(); i++) { |
| 748 |
char p = str.at(i); |
| 749 |
bool skip = false; |
| 750 |
|
| 751 |
if (i + 1 < str.size() && p == '\\') { |
| 752 |
char q = str.at(i + 1); |
| 753 |
if (uri_lookup(q) & URI_GENDELIM) { |
| 754 |
oss << "%" << std::setw(2) << std::setfill('0') |
| 755 |
<< static_cast<int>(q); |
| 756 |
skip = true; |
| 757 |
i++; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
if (!skip) { |
| 762 |
oss << p; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
return oss.str(); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
std::ostream& operator<< (std::ostream& stream, const Davix::Uri & _u){ |
| 772 |
stream << _u.getString(); |
| 773 |
return stream; |
| 774 |
} |
| 775 |
|
| 776 |
} // namespace Davix |
| 777 |
|
| 778 |
|
| 779 |
std::ostream& operator<< (std::ostream& stream, const Davix::Uri & _u){ |
| 780 |
stream << _u.getString(); |
| 781 |
return stream; |
| 782 |
} |