rippled
StringUtilities.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/contract.h>
21 #include <ripple/basics/Slice.h>
22 #include <ripple/basics/StringUtilities.h>
23 #include <ripple/basics/ToString.h>
24 #include <ripple/beast/core/LexicalCast.h>
25 #include <boost/algorithm/string.hpp>
26 #include <ripple/beast/net/IPEndpoint.h>
27 #include <boost/regex.hpp>
28 #include <algorithm>
29 #include <cstdarg>
30 
31 namespace ripple {
32 
33 uint64_t uintFromHex (std::string const& strSrc)
34 {
35  uint64_t uValue (0);
36 
37  if (strSrc.size () > 16)
38  Throw<std::invalid_argument> ("overlong 64-bit value");
39 
40  for (auto c : strSrc)
41  {
42  int ret = charUnHex (c);
43 
44  if (ret == -1)
45  Throw<std::invalid_argument> ("invalid hex digit");
46 
47  uValue = (uValue << 4) | ret;
48  }
49 
50  return uValue;
51 }
52 
53 bool parseUrl (parsedURL& pUrl, std::string const& strUrl)
54 {
55  // scheme://username:password@hostname:port/rest
56  static boost::regex reUrl (
57  "(?i)\\`\\s*"
58  // required scheme
59  "([[:alpha:]][-+.[:alpha:][:digit:]]*?):"
60  // We choose to support only URIs whose `hier-part` has the form
61  // `"//" authority path-abempty`.
62  "//"
63  // optional userinfo
64  "(?:([^:@/]*?)(?::([^@/]*?))?@)?"
65  // optional host
66  "([[:digit:]:]*[[:digit:]]|\\[[^]]+\\]|[^:/?#]*?)"
67  // optional port
68  "(?::([[:digit:]]+))?"
69  // optional path
70  "(/.*)?"
71  "\\s*?\\'");
72  boost::smatch smMatch;
73 
74  // Bail if there is no match.
75  try {
76  if (! boost::regex_match (strUrl, smMatch, reUrl))
77  return false;
78  } catch (...) {
79  return false;
80  }
81 
82  pUrl.scheme = smMatch[1];
83  boost::algorithm::to_lower (pUrl.scheme);
84  pUrl.username = smMatch[2];
85  pUrl.password = smMatch[3];
86  const std::string domain = smMatch[4];
87  // We need to use Endpoint to parse the domain to
88  // strip surrounding brackets from IPv6 addresses,
89  // e.g. [::1] => ::1.
90  const auto result = beast::IP::Endpoint::from_string_checked (domain);
91  pUrl.domain = result
92  ? result->address().to_string()
93  : domain;
94  const std::string port = smMatch[5];
95  if (!port.empty())
96  {
97  pUrl.port = beast::lexicalCast <std::uint16_t> (port);
98  }
99  pUrl.path = smMatch[6];
100 
101  return true;
102 }
103 
105 {
106  boost::trim (str);
107  return str;
108 }
109 
110 boost::optional<std::uint64_t>
111 to_uint64(std::string const& s)
112 {
113  std::uint64_t result;
114  if (beast::lexicalCastChecked (result, s))
115  return result;
116  return boost::none;
117 }
118 
119 } // ripple
cstdarg
std::string
STL class.
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:26
ripple::parsedURL
Definition: StringUtilities.h:122
ripple::parsedURL::password
std::string password
Definition: StringUtilities.h:128
ripple::uintFromHex
uint64_t uintFromHex(std::string const &strSrc)
Definition: StringUtilities.cpp:33
std::string::size
T size(T... args)
ripple::to_uint64
boost::optional< std::uint64_t > to_uint64(std::string const &s)
ripple::parsedURL::username
std::string username
Definition: StringUtilities.h:127
ripple::parsedURL::path
std::string path
Definition: StringUtilities.h:131
ripple::trim_whitespace
std::string trim_whitespace(std::string str)
algorithm
ripple::parseUrl
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
Definition: StringUtilities.cpp:53
std::uint64_t
ripple::parsedURL::port
boost::optional< std::uint16_t > port
Definition: StringUtilities.h:130
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::lexicalCastChecked
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
Definition: LexicalCast.h:262
std::string::empty
T empty(T... args)
ripple::parsedURL::scheme
std::string scheme
Definition: StringUtilities.h:126
ripple::parsedURL::domain
std::string domain
Definition: StringUtilities.h:129
beast::IP::Endpoint::from_string_checked
static boost::optional< Endpoint > from_string_checked(std::string const &s)
Create an Endpoint from a string.
Definition: IPEndpoint.cpp:37