| 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 |
#define PN_USE_DEPRECATED_API 1 |
| 23 |
|
| 24 |
#include "core/util.h" |
| 25 |
#include "proton/url.h" |
| 26 |
#include "proton/object.h" |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
static void pni_urldecode(const char *src, char *dst) |
| 32 |
{ |
| 33 |
const char *in = src; |
| 34 |
char *out = dst; |
| 35 |
while (*in != '\0') |
| 36 |
{ |
| 37 |
if ('%' == *in) |
| 38 |
{ |
| 39 |
if ((in[1] != '\0') && (in[2] != '\0')) |
| 40 |
{ |
| 41 |
char esc[3]; |
| 42 |
esc[0] = in[1]; |
| 43 |
esc[1] = in[2]; |
| 44 |
esc[2] = '\0'; |
| 45 |
unsigned long d = strtoul(esc, NULL, 16); |
| 46 |
*out = (char)d; |
| 47 |
in += 3; |
| 48 |
out++; |
| 49 |
} |
| 50 |
else |
| 51 |
{ |
| 52 |
*out = *in; |
| 53 |
in++; |
| 54 |
out++; |
| 55 |
} |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
*out = *in; |
| 60 |
in++; |
| 61 |
out++; |
| 62 |
} |
| 63 |
} |
| 64 |
*out = '\0'; |
| 65 |
} |
| 66 |
|
| 67 |
/* Not static - used by messenger.c */ |
| 68 |
void pni_parse_url(char *url, char **scheme, char **user, char **pass, char **host, char **port, char **path) |
| 69 |
{ |
| 70 |
if (!url) return; |
| 71 |
*scheme = *user = *pass = *host = *port = *path = NULL; |
| 72 |
|
| 73 |
char *slash = strchr(url, '/'); |
| 74 |
|
| 75 |
if (slash && slash>url) { |
| 76 |
char *scheme_end = strstr(slash-1, "://"); |
| 77 |
|
| 78 |
if (scheme_end && scheme_end<slash) { |
| 79 |
*scheme_end = '\0'; |
| 80 |
*scheme = url; |
| 81 |
url = scheme_end + 3; |
| 82 |
slash = strchr(url, '/'); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if (slash) { |
| 87 |
*slash = '\0'; |
| 88 |
*path = slash + 1; |
| 89 |
} |
| 90 |
|
| 91 |
char *at = strchr(url, '@'); |
| 92 |
if (at) { |
| 93 |
*at = '\0'; |
| 94 |
char *up = url; |
| 95 |
*user = up; |
| 96 |
url = at + 1; |
| 97 |
char *colon = strchr(up, ':'); |
| 98 |
if (colon) { |
| 99 |
*colon = '\0'; |
| 100 |
*pass = colon + 1; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
*host = url; |
| 105 |
char *open = (*url == '[') ? url : 0; |
| 106 |
if (open) { |
| 107 |
char *close = strchr(open, ']'); |
| 108 |
if (close) { |
| 109 |
*host = open + 1; |
| 110 |
*close = '\0'; |
| 111 |
url = close + 1; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
char *colon = strrchr(url, ':'); /* Be flexible about unbracketed IPV6 literals like ::1:<port> */ |
| 116 |
if (colon) { |
| 117 |
*colon = '\0'; |
| 118 |
*port = colon + 1; |
| 119 |
} |
| 120 |
|
| 121 |
if (*user) pni_urldecode(*user, *user); |
| 122 |
if (*pass) pni_urldecode(*pass, *pass); |
| 123 |
} |
| 124 |
|
| 125 |
/** URL-encode src and append to dst. */ |
| 126 |
static void pni_urlencode(pn_string_t *dst, const char* src) { |
| 127 |
static const char *bad = "@:/"; |
| 128 |
|
| 129 |
if (!src) return; |
| 130 |
const char *i = src; |
| 131 |
const char *j = strpbrk(i, bad); |
| 132 |
while (j) { |
| 133 |
pn_string_addf(dst, "%.*s", (int)(j-i), i); |
| 134 |
pn_string_addf(dst, "%%%02X", (int)*j); |
| 135 |
i = j + 1; |
| 136 |
j = strpbrk(i, bad); |
| 137 |
} |
| 138 |
pn_string_addf(dst, "%s", i); |
| 139 |
} |
| 140 |
|
| 141 |
struct pn_url_t { |
| 142 |
char *scheme; |
| 143 |
char *username; |
| 144 |
char *password; |
| 145 |
char *host; |
| 146 |
char *port; |
| 147 |
char *path; |
| 148 |
pn_string_t *str; |
| 149 |
}; |
| 150 |
|
| 151 |
/** Internal use only, returns the pn_string_t. Public function is pn_url_str() */ |
| 152 |
static pn_string_t *pn_url_string(pn_url_t* url) |
| 153 |
{ |
| 154 |
pn_url_str(url); /* Make sure str is up to date */ |
| 155 |
return url->str; |
| 156 |
} |
| 157 |
|
| 158 |
static void pn_url_finalize(void *object) |
| 159 |
{ |
| 160 |
pn_url_t *url = (pn_url_t *) object; |
| 161 |
pn_url_clear(url); |
| 162 |
pn_free(url->str); |
| 163 |
} |
| 164 |
|
| 165 |
static uintptr_t pn_url_hashcode(void *object) |
| 166 |
{ |
| 167 |
pn_url_t *url = (pn_url_t *) object; |
| 168 |
return pn_hashcode(pn_url_string(url)); |
| 169 |
} |
| 170 |
|
| 171 |
static intptr_t pn_url_compare(void *oa, void *ob) |
| 172 |
{ |
| 173 |
pn_url_t *a = (pn_url_t *) oa; |
| 174 |
pn_url_t *b = (pn_url_t *) ob; |
| 175 |
return pn_compare(pn_url_string(a), pn_url_string(b)); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static int pn_url_inspect(void *obj, pn_string_t *dst) |
| 180 |
{ |
| 181 |
pn_url_t *url = (pn_url_t *) obj; |
| 182 |
int err = 0; |
| 183 |
err = pn_string_addf(dst, "Url("); if (err) return err; |
| 184 |
err = pn_inspect(pn_url_string(url), dst); if (err) return err; |
| 185 |
return pn_string_addf(dst, ")"); |
| 186 |
} |
| 187 |
|
| 188 |
#define pn_url_initialize NULL |
| 189 |
|
| 190 |
|
| 191 |
pn_url_t *pn_url() { |
| 192 |
static const pn_class_t clazz = PN_CLASS(pn_url); |
| 193 |
pn_url_t *url = (pn_url_t*) pn_class_new(&clazz, sizeof(pn_url_t)); |
| 194 |
if (!url) return NULL; |
| 195 |
memset(url, 0, sizeof(*url)); |
| 196 |
url->str = pn_string(NULL); |
| 197 |
return url; |
| 198 |
} |
| 199 |
|
| 200 |
/** Parse a string URL as a pn_url_t. |
| 201 |
*@param[in] url A URL string. |
| 202 |
*@return The parsed pn_url_t or NULL if url is not a valid URL string. |
| 203 |
*/ |
| 204 |
pn_url_t *pn_url_parse(const char *str) { |
| 205 |
if (!str || !*str) /* Empty string or NULL is illegal. */ |
| 206 |
return NULL; |
| 207 |
|
| 208 |
pn_url_t *url = pn_url(); |
| 209 |
char *str2 = pn_strdup(str); |
| 210 |
pni_parse_url(str2, &url->scheme, &url->username, &url->password, &url->host, &url->port, &url->path); |
| 211 |
url->scheme = pn_strdup(url->scheme); |
| 212 |
url->username = pn_strdup(url->username); |
| 213 |
url->password = pn_strdup(url->password); |
| 214 |
url->host = (url->host && !*url->host) ? NULL : pn_strdup(url->host); |
| 215 |
url->port = pn_strdup(url->port); |
| 216 |
url->path = pn_strdup(url->path); |
| 217 |
|
| 218 |
free(str2); |
| 219 |
return url; |
| 220 |
} |
| 221 |
|
| 222 |
/** Free a URL */ |
| 223 |
void pn_url_free(pn_url_t *url) { pn_free(url); } |
| 224 |
|
| 225 |
/** Clear the contents of the URL. */ |
| 226 |
void pn_url_clear(pn_url_t *url) { |
| 227 |
pn_url_set_scheme(url, NULL); |
| 228 |
pn_url_set_username(url, NULL); |
| 229 |
pn_url_set_password(url, NULL); |
| 230 |
pn_url_set_host(url, NULL); |
| 231 |
pn_url_set_port(url, NULL); |
| 232 |
pn_url_set_path(url, NULL); |
| 233 |
pn_string_clear(url->str); |
| 234 |
} |
| 235 |
|
| 236 |
/** Return the string form of a URL. */ |
| 237 |
const char *pn_url_str(pn_url_t *url) { |
| 238 |
if (pn_string_get(url->str) == NULL) { |
| 239 |
pn_string_set(url->str, ""); |
| 240 |
if (url->scheme) pn_string_addf(url->str, "%s://", url->scheme); |
| 241 |
if (url->username) pni_urlencode(url->str, url->username); |
| 242 |
if (url->password) { |
| 243 |
pn_string_addf(url->str, ":"); |
| 244 |
pni_urlencode(url->str, url->password); |
| 245 |
} |
| 246 |
if (url->username || url->password) pn_string_addf(url->str, "@"); |
| 247 |
if (url->host) { |
| 248 |
if (strchr(url->host, ':')) pn_string_addf(url->str, "[%s]", url->host); |
| 249 |
else pn_string_addf(url->str, "%s", url->host); |
| 250 |
} |
| 251 |
if (url->port) pn_string_addf(url->str, ":%s", url->port); |
| 252 |
if (url->path) pn_string_addf(url->str, "/%s", url->path); |
| 253 |
} |
| 254 |
return pn_string_get(url->str); |
| 255 |
} |
| 256 |
|
| 257 |
const char *pn_url_get_scheme(pn_url_t *url) { return url->scheme; } |
| 258 |
const char *pn_url_get_username(pn_url_t *url) { return url->username; } |
| 259 |
const char *pn_url_get_password(pn_url_t *url) { return url->password; } |
| 260 |
const char *pn_url_get_host(pn_url_t *url) { return url->host; } |
| 261 |
const char *pn_url_get_port(pn_url_t *url) { return url->port; } |
| 262 |
const char *pn_url_get_path(pn_url_t *url) { return url->path; } |
| 263 |
|
| 264 |
#define SET(part) free(url->part); url->part = pn_strdup(part); pn_string_clear(url->str) |
| 265 |
void pn_url_set_scheme(pn_url_t *url, const char *scheme) { SET(scheme); } |
| 266 |
void pn_url_set_username(pn_url_t *url, const char *username) { SET(username); } |
| 267 |
void pn_url_set_password(pn_url_t *url, const char *password) { SET(password); } |
| 268 |
void pn_url_set_host(pn_url_t *url, const char *host) { SET(host); } |
| 269 |
void pn_url_set_port(pn_url_t *url, const char *port) { SET(port); } |
| 270 |
void pn_url_set_path(pn_url_t *url, const char *path) { SET(path); } |
| 271 |
|
| 272 |
#undef PN_USE_DEPRECATED_API |