| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "proton/url.hpp" |
| 23 |
|
| 24 |
#include "proton/error.hpp" |
| 25 |
|
| 26 |
#include "proton_bits.hpp" |
| 27 |
|
| 28 |
#include <cstdlib> |
| 29 |
#include <cstring> |
| 30 |
#include <iomanip> |
| 31 |
#include <sstream> |
| 32 |
#include <vector> |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
/** URL-encode src and append to dst. */ |
| 37 |
static std::string pni_urlencode(const std::string &src) { |
| 38 |
static const char *bad = "@:/%[]?#"; |
| 39 |
|
| 40 |
std::ostringstream dst; |
| 41 |
dst << std::hex << std::uppercase << std::setfill('0'); |
| 42 |
|
| 43 |
std::size_t i = 0; |
| 44 |
std::size_t j = src.find_first_of(bad); |
| 45 |
while (j!=std::string::npos) { |
| 46 |
dst << src.substr(i, j-i); |
| 47 |
dst << "%" << std::setw(2) << int(src[j]); |
| 48 |
i = j + 1; |
| 49 |
j = src.find_first_of(bad, i); |
| 50 |
} |
| 51 |
dst << src.substr(i); |
| 52 |
return dst.str(); |
| 53 |
} |
| 54 |
|
| 55 |
// Low level url parser |
| 56 |
static void pni_urldecode(const char *src, char *dst) |
| 57 |
{ |
| 58 |
const char *in = src; |
| 59 |
char *out = dst; |
| 60 |
while (*in != '\0') |
| 61 |
{ |
| 62 |
if ('%' == *in) |
| 63 |
{ |
| 64 |
if ((in[1] != '\0') && (in[2] != '\0')) |
| 65 |
{ |
| 66 |
char esc[3]; |
| 67 |
esc[0] = in[1]; |
| 68 |
esc[1] = in[2]; |
| 69 |
esc[2] = '\0'; |
| 70 |
unsigned long d = std::strtoul(esc, NULL, 16); |
| 71 |
*out = (char)d; |
| 72 |
in += 3; |
| 73 |
out++; |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
*out = *in; |
| 78 |
in++; |
| 79 |
out++; |
| 80 |
} |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
*out = *in; |
| 85 |
in++; |
| 86 |
out++; |
| 87 |
} |
| 88 |
} |
| 89 |
*out = '\0'; |
| 90 |
} |
| 91 |
|
| 92 |
void parse_url(char *url, const char **scheme, const char **user, const char **pass, const char **host, const char **port, const char **path) |
| 93 |
{ |
| 94 |
if (!url) return; |
| 95 |
|
| 96 |
char *slash = std::strchr(url, '/'); |
| 97 |
|
| 98 |
if (slash && slash>url) { |
| 99 |
char *scheme_end = std::strstr(slash-1, "://"); |
| 100 |
|
| 101 |
if (scheme_end && scheme_end<slash) { |
| 102 |
*scheme_end = '\0'; |
| 103 |
*scheme = url; |
| 104 |
url = scheme_end + 3; |
| 105 |
slash = std::strchr(url, '/'); |
| 106 |
} |
| 107 |
} else if (0 == strncmp(url, "//", 2)) { |
| 108 |
url += 2; |
| 109 |
slash = std::strchr(url, '/'); |
| 110 |
} |
| 111 |
|
| 112 |
if (slash) { |
| 113 |
*slash = '\0'; |
| 114 |
*path = slash + 1; |
| 115 |
} |
| 116 |
|
| 117 |
char *at = std::strchr(url, '@'); |
| 118 |
if (at) { |
| 119 |
*at = '\0'; |
| 120 |
char *up = url; |
| 121 |
url = at + 1; |
| 122 |
char *colon = std::strchr(up, ':'); |
| 123 |
if (colon) { |
| 124 |
*colon = '\0'; |
| 125 |
char *p = colon + 1; |
| 126 |
pni_urldecode(p, p); |
| 127 |
*pass = p; |
| 128 |
} |
| 129 |
pni_urldecode(up, up); |
| 130 |
*user = up; |
| 131 |
} |
| 132 |
|
| 133 |
*host = url; |
| 134 |
char *open = (*url == '[') ? url : 0; |
| 135 |
if (open) { |
| 136 |
char *close = std::strchr(open, ']'); |
| 137 |
if (close) { |
| 138 |
*host = open + 1; |
| 139 |
*close = '\0'; |
| 140 |
url = close + 1; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
char *colon = std::strchr(url, ':'); |
| 145 |
if (colon) { |
| 146 |
*colon = '\0'; |
| 147 |
*port = colon + 1; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
} // namespace |
| 152 |
|
| 153 |
namespace proton { |
| 154 |
|
| 155 |
struct url::impl { |
| 156 |
static const char* const default_host; |
| 157 |
const char* scheme; |
| 158 |
const char* username; |
| 159 |
const char* password; |
| 160 |
const char* host; |
| 161 |
const char* port; |
| 162 |
const char* path; |
| 163 |
std::vector<char> cstr; |
| 164 |
mutable std::string str; |
| 165 |
|
| 166 |
impl(const std::string& s) : |
| 167 |
scheme(0), username(0), password(0), host(0), port(0), path(0), |
| 168 |
cstr(s.size()+1, '\0') |
| 169 |
{ |
| 170 |
std::copy(s.begin(), s.end(), cstr.begin()); |
| 171 |
parse_url(&cstr[0], &scheme, &username, &password, &host, &port, &path); |
| 172 |
} |
| 173 |
|
| 174 |
void defaults() { |
| 175 |
if (!scheme || *scheme=='\0' ) scheme = proton::url::AMQP.c_str(); |
| 176 |
if (!host || *host=='\0' ) host = default_host; |
| 177 |
if (!port || *port=='\0' ) port = scheme; |
| 178 |
} |
| 179 |
|
| 180 |
operator std::string() const { |
| 181 |
if ( str.empty() ) { |
| 182 |
if (scheme) { |
| 183 |
str += scheme; |
| 184 |
str += "://"; |
| 185 |
} |
| 186 |
if (username) { |
| 187 |
str += pni_urlencode(username); |
| 188 |
} |
| 189 |
if (password) { |
| 190 |
str += ":"; |
| 191 |
str += pni_urlencode(password); |
| 192 |
} |
| 193 |
if (username || password) { |
| 194 |
str += "@"; |
| 195 |
} |
| 196 |
if (host) { |
| 197 |
if (std::strchr(host, ':')) { |
| 198 |
str += '['; |
| 199 |
str += host; |
| 200 |
str += ']'; |
| 201 |
} else { |
| 202 |
str += host; |
| 203 |
} |
| 204 |
} |
| 205 |
if (port) { |
| 206 |
str += ':'; |
| 207 |
str += port; |
| 208 |
} |
| 209 |
if (path) { |
| 210 |
str += '/'; |
| 211 |
str += path; |
| 212 |
} |
| 213 |
} |
| 214 |
return str; |
| 215 |
} |
| 216 |
|
| 217 |
}; |
| 218 |
|
| 219 |
const char* const url::impl::default_host = "localhost"; |
| 220 |
|
| 221 |
|
| 222 |
url_error::url_error(const std::string& s) : error(s) {} |
| 223 |
url_error::~url_error() throw() = default; |
| 224 |
|
| 225 |
url::url(const std::string &s) : impl_(new impl(s)) { impl_->defaults(); } |
| 226 |
|
| 227 |
url::url(const std::string &s, bool d) : impl_(new impl(s)) { if (d) impl_->defaults(); } |
| 228 |
|
| 229 |
url::url(const url& u) : impl_(new impl(u)) {} |
| 230 |
|
| 231 |
url::~url() {} |
| 232 |
|
| 233 |
url& url::operator=(const url& u) { |
| 234 |
if (this != &u) { |
| 235 |
impl_.reset(new impl(*u.impl_)); |
| 236 |
} |
| 237 |
return *this; |
| 238 |
} |
| 239 |
|
| 240 |
url::operator std::string() const { return *impl_; } |
| 241 |
|
| 242 |
std::string url::scheme() const { return str(impl_->scheme); } |
| 243 |
std::string url::user() const { return str(impl_->username); } |
| 244 |
std::string url::password() const { return str(impl_->password); } |
| 245 |
std::string url::host() const { return str(impl_->host); } |
| 246 |
std::string url::port() const { return str(impl_->port); } |
| 247 |
std::string url::path() const { return str(impl_->path); } |
| 248 |
|
| 249 |
std::string url::host_port() const { return host() + ":" + port(); } |
| 250 |
|
| 251 |
bool url::empty() const { return impl_->str.empty(); } |
| 252 |
|
| 253 |
const std::string url::AMQP("amqp"); |
| 254 |
const std::string url::AMQPS("amqps"); |
| 255 |
|
| 256 |
uint16_t url::port_int() const { |
| 257 |
if (port() == AMQP) return 5672; |
| 258 |
if (port() == AMQPS) return 5671; |
| 259 |
std::istringstream is(port()); |
| 260 |
uint16_t result; |
| 261 |
is >> result; |
| 262 |
if (is.fail()) |
| 263 |
throw url_error("invalid port '" + port() + "'"); |
| 264 |
return result; |
| 265 |
} |
| 266 |
|
| 267 |
std::ostream& operator<<(std::ostream& o, const url& u) { |
| 268 |
return o << std::string(u); |
| 269 |
} |
| 270 |
|
| 271 |
std::string to_string(const url& u) { |
| 272 |
return u; |
| 273 |
} |
| 274 |
|
| 275 |
std::istream& operator>>(std::istream& i, url& u) { |
| 276 |
std::string s; |
| 277 |
i >> s; |
| 278 |
if (!i.fail()) { |
| 279 |
url::impl* p = new url::impl(s); |
| 280 |
p->defaults(); |
| 281 |
u.impl_.reset(p); |
| 282 |
} |
| 283 |
return i; |
| 284 |
} |
| 285 |
|
| 286 |
} // namespace proton |