uri.hpp (18763B)
1 // Copyright (C) 2015 Ben Lewis <benjf5+github@gmail.com> 2 // Licensed under the MIT license. 3 4 #pragma once 5 #include <cctype> 6 #include <map> 7 #include <string> 8 #include <stdexcept> 9 #include <utility> 10 11 class uri 12 { 13 /* URIs are broadly divided into two categories: hierarchical and 14 * non-hierarchical. Both hierarchical URIs and non-hierarchical URIs have a 15 * few elements in common; all URIs have a scheme of one or more alphanumeric 16 * characters followed by a colon, and they all may optionally have a query 17 * component preceded by a question mark, and a fragment component preceded by 18 * an octothorpe (hash mark: '#'). The query consists of stanzas separated by 19 * either ampersands ('&') or semicolons (';') (but only one or the other), 20 * and each stanza consists of a key and an optional value; if the value 21 * exists, the key and value must be divided by an equals sign. 22 * 23 * The following is an example from Wikipedia of a hierarchical URI: 24 * scheme:[//[user:password@]domain[:port]][/]path[?query][#fragment] 25 */ 26 27 public: 28 29 enum class scheme_category 30 { 31 Hierarchical, 32 NonHierarchical 33 }; 34 35 enum class component 36 { 37 Scheme, 38 Content, 39 Username, 40 Password, 41 Host, 42 Port, 43 Path, 44 Query, 45 Fragment 46 }; 47 48 enum class query_argument_separator 49 { 50 ampersand, 51 semicolon 52 }; 53 54 uri(char const *uri_text, scheme_category category = scheme_category::Hierarchical, 55 query_argument_separator separator = query_argument_separator::ampersand) : 56 m_category(category), 57 m_port(0), 58 m_path_is_rooted(false), 59 m_separator(separator) 60 { 61 setup(std::string(uri_text)); 62 }; 63 64 uri(std::string const &uri_text, scheme_category category = scheme_category::Hierarchical, 65 query_argument_separator separator = query_argument_separator::ampersand) : 66 m_category(category), 67 m_port(0), 68 m_path_is_rooted(false), 69 m_separator(separator) 70 { 71 setup(uri_text); 72 }; 73 74 uri(std::map<component, std::string> const &components, 75 scheme_category category, 76 bool rooted_path, 77 query_argument_separator separator = query_argument_separator::ampersand) : 78 m_category(category), 79 m_path_is_rooted(rooted_path), 80 m_separator(separator) 81 { 82 if (components.count(component::Scheme)) 83 { 84 if (components.at(component::Scheme).length() == 0) 85 { 86 throw std::invalid_argument("Scheme cannot be empty."); 87 } 88 m_scheme = components.at(component::Scheme); 89 } 90 else 91 { 92 throw std::invalid_argument("A URI must have a scheme."); 93 } 94 95 if (category == scheme_category::Hierarchical) 96 { 97 if (components.count(component::Content)) 98 { 99 throw std::invalid_argument("The content component is only for use in non-hierarchical URIs."); 100 } 101 102 bool has_username = components.count(component::Username); 103 bool has_password = components.count(component::Password); 104 if (has_username && has_password) 105 { 106 m_username = components.at(component::Username); 107 m_password = components.at(component::Password); 108 } 109 else if ((has_username && !has_password) || (!has_username && has_password)) 110 { 111 throw std::invalid_argument("If a username or password is supplied, both must be provided."); 112 } 113 114 if (components.count(component::Host)) 115 { 116 m_host = components.at(component::Host); 117 } 118 119 if (components.count(component::Port)) 120 { 121 m_port = std::stoul(components.at(component::Port)); 122 } 123 124 if (components.count(component::Path)) 125 { 126 m_path = components.at(component::Path); 127 } 128 else 129 { 130 throw std::invalid_argument("A path is required on a hierarchical URI, even an empty path."); 131 } 132 } 133 else 134 { 135 if (components.count(component::Username) 136 || components.count(component::Password) 137 || components.count(component::Host) 138 || components.count(component::Port) 139 || components.count(component::Path)) 140 { 141 throw std::invalid_argument("None of the hierarchical components are allowed in a non-hierarchical URI."); 142 } 143 144 if (components.count(component::Content)) 145 { 146 m_content = components.at(component::Content); 147 } 148 else 149 { 150 throw std::invalid_argument("Content is a required component for a non-hierarchical URI, even an empty string."); 151 } 152 } 153 154 if (components.count(component::Query)) 155 { 156 m_query = components.at(component::Query); 157 } 158 159 if (components.count(component::Fragment)) 160 { 161 m_fragment = components.at(component::Fragment); 162 } 163 } 164 165 uri(uri const &other, std::map<component, std::string> const &replacements) : 166 m_category(other.m_category), 167 m_path_is_rooted(other.m_path_is_rooted), 168 m_separator(other.m_separator) 169 { 170 m_scheme = (replacements.count(component::Scheme)) 171 ? replacements.at(component::Scheme) : other.m_scheme; 172 173 m_username = (replacements.count(component::Username)) 174 ? replacements.at(component::Username) : other.m_username; 175 176 m_password = (replacements.count(component::Password)) 177 ? replacements.at(component::Password) : other.m_password; 178 179 m_host = (replacements.count(component::Host)) 180 ? replacements.at(component::Host) : other.m_host; 181 182 m_port = (replacements.count(component::Port)) 183 ? std::stoul(replacements.at(component::Port)) : other.m_port; 184 185 m_path = (replacements.count(component::Path)) 186 ? replacements.at(component::Path) : other.m_path; 187 188 m_content = (replacements.count(component::Content)) 189 ? replacements.at(component::Content) : other.m_content; 190 191 m_query = (replacements.count(component::Query)) 192 ? replacements.at(component::Query) : other.m_query; 193 194 m_fragment = (replacements.count(component::Fragment)) 195 ? replacements.at(component::Fragment) : other.m_fragment; 196 } 197 198 // Copy constructor; just use the copy assignment operator internally. 199 uri(uri const &other) 200 { 201 *this = other; 202 }; 203 204 // Copy assignment operator 205 uri &operator=(uri const &other) 206 { 207 if (this != &other) 208 { 209 m_scheme = other.m_scheme; 210 m_content = other.m_content; 211 m_username = other.m_username; 212 m_password = other.m_password; 213 m_host = other.m_host; 214 m_path = other.m_path; 215 m_query = other.m_query; 216 m_fragment = other.m_fragment; 217 m_query_dict = other.m_query_dict; 218 m_category = other.m_category; 219 m_port = other.m_port; 220 m_path_is_rooted = other.m_path_is_rooted; 221 m_separator = other.m_separator; 222 } 223 return *this; 224 } 225 226 ~uri() { }; 227 228 std::string const &get_scheme() const 229 { 230 return m_scheme; 231 }; 232 233 scheme_category get_scheme_category() const 234 { 235 return m_category; 236 }; 237 238 std::string const &get_content() const 239 { 240 if (m_category != scheme_category::NonHierarchical) 241 { 242 throw std::domain_error("The content component is only valid for non-hierarchical URIs."); 243 } 244 return m_content; 245 }; 246 247 std::string const &get_username() const 248 { 249 if (m_category != scheme_category::Hierarchical) 250 { 251 throw std::domain_error("The username component is only valid for hierarchical URIs."); 252 } 253 return m_username; 254 }; 255 256 std::string const &get_password() const 257 { 258 if (m_category != scheme_category::Hierarchical) 259 { 260 throw std::domain_error("The password component is only valid for hierarchical URIs."); 261 } 262 return m_password; 263 }; 264 265 std::string const &get_host() const 266 { 267 if (m_category != scheme_category::Hierarchical) 268 { 269 throw std::domain_error("The host component is only valid for hierarchical URIs."); 270 } 271 return m_host; 272 }; 273 274 unsigned long get_port() const 275 { 276 if (m_category != scheme_category::Hierarchical) 277 { 278 throw std::domain_error("The port component is only valid for hierarchical URIs."); 279 } 280 return m_port; 281 }; 282 283 std::string const &get_path() const 284 { 285 if (m_category != scheme_category::Hierarchical) 286 { 287 throw std::domain_error("The path component is only valid for hierarchical URIs."); 288 } 289 return m_path; 290 }; 291 292 std::string const &get_query() const 293 { 294 return m_query; 295 }; 296 297 std::map<std::string, std::string> const &get_query_dictionary() const 298 { 299 return m_query_dict; 300 }; 301 302 std::string const &get_fragment() const 303 { 304 return m_fragment; 305 }; 306 307 std::string to_string() const 308 { 309 std::string full_uri; 310 full_uri.append(m_scheme); 311 full_uri.append(":"); 312 313 if (m_category == scheme_category::Hierarchical) 314 { 315 full_uri.append("//"); 316 if (!(m_username.empty() || m_password.empty())) 317 { 318 full_uri.append(m_username); 319 full_uri.append(":"); 320 full_uri.append(m_password); 321 full_uri.append("@"); 322 } 323 324 full_uri.append(m_host); 325 326 if (m_port != 0) 327 { 328 full_uri.append(":"); 329 full_uri.append(std::to_string(m_port)); 330 } 331 } 332 else 333 { 334 full_uri.append(m_content); 335 } 336 337 if (m_path_is_rooted) 338 { 339 full_uri.append("/"); 340 } 341 full_uri.append(m_path); 342 343 if (!m_query.empty()) 344 { 345 full_uri.append("?"); 346 full_uri.append(m_query); 347 } 348 349 if (!m_fragment.empty()) 350 { 351 full_uri.append("#"); 352 full_uri.append(m_fragment); 353 } 354 355 return full_uri; 356 }; 357 358 private: 359 360 void setup(std::string const &uri_text) 361 { 362 size_t const uri_length = uri_text.length(); 363 364 if (uri_length == 0) 365 { 366 throw std::invalid_argument("URIs cannot be of zero length."); 367 } 368 369 std::string::const_iterator cursor = parse_scheme(uri_text, 370 uri_text.begin()); 371 // After calling parse_scheme, *cursor == ':'; none of the following parsers 372 // expect a separator character, so we advance the cursor upon calling them. 373 cursor = parse_content(uri_text, (cursor + 1)); 374 375 if ((cursor != uri_text.end()) && (*cursor == '?')) 376 { 377 cursor = parse_query(uri_text, (cursor + 1)); 378 } 379 380 if ((cursor != uri_text.end()) && (*cursor == '#')) 381 { 382 cursor = parse_fragment(uri_text, (cursor + 1)); 383 } 384 385 init_query_dictionary(); // If the query string is empty, this will be empty too. 386 387 }; 388 389 std::string::const_iterator parse_scheme(std::string const &uri_text, 390 std::string::const_iterator scheme_start) 391 { 392 std::string::const_iterator scheme_end = scheme_start; 393 while ((scheme_end != uri_text.end()) && (*scheme_end != ':')) 394 { 395 if (!(std::isalnum(*scheme_end) || (*scheme_end == '-') 396 || (*scheme_end == '+') || (*scheme_end == '.'))) 397 { 398 throw std::invalid_argument("Invalid character found in the scheme component. Supplied URI was: \"" 399 + uri_text + "\"."); 400 } 401 ++scheme_end; 402 } 403 404 if (scheme_end == uri_text.end()) 405 { 406 throw std::invalid_argument("End of URI found while parsing the scheme. Supplied URI was: \"" 407 + uri_text + "\"."); 408 } 409 410 if (scheme_start == scheme_end) 411 { 412 throw std::invalid_argument("Scheme component cannot be zero-length. Supplied URI was: \"" 413 + uri_text + "\"."); 414 } 415 416 m_scheme = std::string(scheme_start, scheme_end); 417 return scheme_end; 418 }; 419 420 std::string::const_iterator parse_content(std::string const &uri_text, 421 std::string::const_iterator content_start) 422 { 423 std::string::const_iterator content_end = content_start; 424 while ((content_end != uri_text.end()) && (*content_end != '?') && (*content_end != '#')) 425 { 426 ++content_end; 427 } 428 429 m_content = std::string(content_start, content_end); 430 431 if ((m_category == scheme_category::Hierarchical) && (m_content.length() > 0)) 432 { 433 // If it's a hierarchical URI, the content should be parsed for the hierarchical components. 434 std::string::const_iterator path_start = m_content.begin(); 435 std::string::const_iterator path_end = m_content.end(); 436 if (!m_content.compare(0, 2, "//")) 437 { 438 // In this case an authority component is present. 439 std::string::const_iterator authority_cursor = (m_content.begin() + 2); 440 441 size_t authority_end = m_content.find('/', 2); 442 if (authority_end == std::string::npos) 443 { 444 authority_end = m_content.length(); 445 } 446 447 size_t at_pos = m_content.find('@', 2); 448 if ((at_pos != std::string::npos) && (at_pos < authority_end)) 449 { 450 std::string::const_iterator userpass_divider = parse_username(uri_text, 451 authority_cursor); 452 authority_cursor = parse_password((userpass_divider + 1)); 453 // After this call, *authority_cursor == '@', so we skip over it. 454 ++authority_cursor; 455 } 456 457 authority_cursor = parse_host(uri_text, m_content, authority_cursor); 458 459 if ((authority_cursor != m_content.end()) && (*authority_cursor == ':')) 460 { 461 authority_cursor = parse_port(uri_text, m_content, (authority_cursor + 1)); 462 } 463 464 if ((authority_cursor != m_content.end()) && (*authority_cursor == '/' )) 465 { 466 // Then the path is rooted, and we should note this. 467 m_path_is_rooted = true; 468 path_start = authority_cursor + 1; 469 } 470 471 // If we've reached the end and no path is present then set path_start 472 // to the end. 473 if (authority_cursor == m_content.end()) { 474 path_start = m_content.end(); 475 } 476 } 477 else if (!m_content.compare(0, 1, "/")) 478 { 479 m_path_is_rooted = true; 480 ++path_start; 481 } 482 483 // We can now build the path based on what remains in the content string, 484 // since that's all that exists after the host and optional port component. 485 m_path = std::string(path_start, path_end); 486 } 487 return content_end; 488 }; 489 490 std::string::const_iterator parse_username(std::string const &uri_text, 491 std::string::const_iterator username_start) 492 { 493 std::string::const_iterator username_end = username_start; 494 // Since this is only reachable when '@' was in the content string, we can 495 // ignore the end-of-string case. 496 while (*username_end != ':') 497 { 498 if (*username_end == '@') 499 { 500 throw std::invalid_argument("Username must be followed by a password. Supplied URI was: \"" 501 + uri_text + "\"."); 502 } 503 ++username_end; 504 } 505 m_username = std::string(username_start, username_end); 506 return username_end; 507 }; 508 509 std::string::const_iterator parse_password(std::string::const_iterator password_start) 510 { 511 std::string::const_iterator password_end = password_start; 512 while (*password_end != '@') 513 { 514 ++password_end; 515 } 516 517 m_password = std::string(password_start, password_end); 518 return password_end; 519 }; 520 521 std::string::const_iterator parse_host(std::string const &uri_text, 522 std::string const &content, 523 std::string::const_iterator host_start) 524 { 525 std::string::const_iterator host_end = host_start; 526 // So, the host can contain a few things. It can be a domain, it can be an 527 // IPv4 address, it can be an IPv6 address, or an IPvFuture literal. In the 528 // case of those last two, it's of the form [...] where what's between the 529 // brackets is a matter of which IPv?? version it is. 530 while (host_end != content.end()) 531 { 532 if (*host_end == '[') 533 { 534 // We're parsing an IPv6 or IPvFuture address, so we should handle that 535 // instead of the normal procedure. 536 while ((host_end != content.end()) && (*host_end != ']')) 537 { 538 ++host_end; 539 } 540 541 if (host_end == content.end()) 542 { 543 throw std::invalid_argument("End of content component encountered " 544 "while parsing the host component. " 545 "Supplied URI was: \"" 546 + uri_text + "\"."); 547 } 548 549 ++host_end; 550 break; 551 // We can stop looping, we found the end of the IP literal, which is the 552 // whole of the host component when one's in use. 553 } 554 else if ((*host_end == ':') || (*host_end == '/')) 555 { 556 break; 557 } 558 else 559 { 560 ++host_end; 561 } 562 } 563 564 m_host = std::string(host_start, host_end); 565 return host_end; 566 }; 567 568 std::string::const_iterator parse_port(std::string const &uri_text, 569 std::string const &content, 570 std::string::const_iterator port_start) 571 { 572 std::string::const_iterator port_end = port_start; 573 while ((port_end != content.end()) && (*port_end != '/')) 574 { 575 if (!std::isdigit(*port_end)) 576 { 577 throw std::invalid_argument("Invalid character while parsing the port. " 578 "Supplied URI was: \"" + uri_text + "\"."); 579 } 580 581 ++port_end; 582 } 583 584 m_port = std::stoul(std::string(port_start, port_end)); 585 return port_end; 586 }; 587 588 std::string::const_iterator parse_query(std::string const &uri_text, 589 std::string::const_iterator query_start) 590 { 591 std::string::const_iterator query_end = query_start; 592 while ((query_end != uri_text.end()) && (*query_end != '#')) 593 { 594 // Queries can contain almost any character except hash, which is reserved 595 // for the start of the fragment. 596 ++query_end; 597 } 598 m_query = std::string(query_start, query_end); 599 return query_end; 600 }; 601 602 std::string::const_iterator parse_fragment(std::string const &uri_text, 603 std::string::const_iterator fragment_start) 604 { 605 m_fragment = std::string(fragment_start, uri_text.end()); 606 return uri_text.end(); 607 }; 608 609 void init_query_dictionary() 610 { 611 if (!m_query.empty()) 612 { 613 // Loop over the query string looking for '&'s, then check each one for 614 // an '=' to find keys and values; if there's not an '=' then the key 615 // will have an empty value in the map. 616 char separator = (m_separator == query_argument_separator::ampersand) ? '&' : ';'; 617 size_t carat = 0; 618 size_t stanza_end = m_query.find_first_of(separator); 619 do 620 { 621 std::string stanza = m_query.substr(carat, ((stanza_end != std::string::npos) ? (stanza_end - carat) : std::string::npos)); 622 size_t key_value_divider = stanza.find_first_of('='); 623 std::string key = stanza.substr(0, key_value_divider); 624 std::string value; 625 if (key_value_divider != std::string::npos) 626 { 627 value = stanza.substr((key_value_divider + 1)); 628 } 629 630 if (m_query_dict.count(key) != 0) 631 { 632 throw std::invalid_argument("Bad key in the query string!"); 633 } 634 635 m_query_dict.emplace(key, value); 636 carat = ((stanza_end != std::string::npos) ? (stanza_end + 1) 637 : std::string::npos); 638 stanza_end = m_query.find_first_of(separator, carat); 639 } 640 while ((stanza_end != std::string::npos) 641 || (carat != std::string::npos)); 642 } 643 } 644 645 std::string m_scheme; 646 std::string m_content; 647 std::string m_username; 648 std::string m_password; 649 std::string m_host; 650 std::string m_path; 651 std::string m_query; 652 std::string m_fragment; 653 654 std::map<std::string, std::string> m_query_dict; 655 656 scheme_category m_category; 657 unsigned long m_port; 658 bool m_path_is_rooted; 659 query_argument_separator m_separator; 660 };