|
| 1 | +/* |
| 2 | + * ***** BEGIN LICENSE BLOCK ***** |
| 3 | + * Version: MPL 1.1/GPL 2.0 |
| 4 | + * |
| 5 | + * The contents of this file are subject to the Mozilla Public License |
| 6 | + * Version 1.1 (the "License"); you may not use this file except in |
| 7 | + * compliance with the License. You may obtain a copy of the License |
| 8 | + * at http://www.mozilla.org/MPL/ |
| 9 | + * |
| 10 | + * Software distributed under the License is distributed on an "AS IS" |
| 11 | + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 12 | + * the License for the specific language governing rights and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * The Original Code is librabbitmq. |
| 16 | + * |
| 17 | + * The Initial Developer of the Original Code is VMware, Inc. |
| 18 | + * Portions created by VMware are Copyright (c) 2007-2011 VMware, Inc. |
| 19 | + * |
| 20 | + * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010 |
| 21 | + * VMware, Inc. and Tony Garnock-Jones. |
| 22 | + * |
| 23 | + * All rights reserved. |
| 24 | + * |
| 25 | + * Alternatively, the contents of this file may be used under the terms |
| 26 | + * of the GNU General Public License Version 2 or later (the "GPL"), in |
| 27 | + * which case the provisions of the GPL are applicable instead of those |
| 28 | + * above. If you wish to allow use of your version of this file only |
| 29 | + * under the terms of the GPL, and not to allow others to use your |
| 30 | + * version of this file under the terms of the MPL, indicate your |
| 31 | + * decision by deleting the provisions above and replace them with the |
| 32 | + * notice and other provisions required by the GPL. If you do not |
| 33 | + * delete the provisions above, a recipient may use your version of |
| 34 | + * this file under the terms of any one of the MPL or the GPL. |
| 35 | + * |
| 36 | + * ***** END LICENSE BLOCK ***** |
| 37 | + */ |
| 38 | + |
| 39 | +#include <string.h> |
| 40 | +#include <stdio.h> |
| 41 | +#include <stdlib.h> |
| 42 | +#include <stdint.h> |
| 43 | + |
| 44 | +#include "amqp.h" |
| 45 | +#include "amqp_framing.h" |
| 46 | +#include "amqp_private.h" |
| 47 | + |
| 48 | +void amqp_default_connection_info(struct amqp_connection_info *ci) |
| 49 | +{ |
| 50 | +/* Apply defaults */ |
| 51 | +ci->user = "guest"; |
| 52 | +ci->password = "guest"; |
| 53 | +ci->host = "localhost"; |
| 54 | +ci->port = 5672; |
| 55 | +ci->vhost = "/"; |
| 56 | +} |
| 57 | + |
| 58 | +/* Scan for the next delimiter, handling percent-encodings on the way. */ |
| 59 | +static char find_delim(char **pp, int colon_and_at_sign_are_delims) |
| 60 | +{ |
| 61 | +char *from = *pp; |
| 62 | +char *to = from; |
| 63 | + |
| 64 | +for (;;) { |
| 65 | +char ch = *from++; |
| 66 | + |
| 67 | +switch (ch) { |
| 68 | +case ':': |
| 69 | +case '@': |
| 70 | +if (!colon_and_at_sign_are_delims) { |
| 71 | +*to++ = ch; |
| 72 | +break; |
| 73 | +} |
| 74 | + |
| 75 | +/* fall through */ |
| 76 | +case 0: |
| 77 | +case '/': |
| 78 | +case '?': |
| 79 | +case '#': |
| 80 | +case '[': |
| 81 | +case ']': |
| 82 | +*to = 0; |
| 83 | +*pp = from; |
| 84 | +return ch; |
| 85 | + |
| 86 | +case '%': { |
| 87 | +unsigned int val; |
| 88 | +int chars; |
| 89 | +int res = sscanf(from, "%2x%n", &val, &chars); |
| 90 | + |
| 91 | +if (res == EOF || res < 1 || chars != 2) |
| 92 | +/* Return a surprising delimiter to |
| 93 | + force an error. */ |
| 94 | +return '%'; |
| 95 | + |
| 96 | +*to++ = val; |
| 97 | +from += 2; |
| 98 | +break; |
| 99 | +} |
| 100 | + |
| 101 | +default: |
| 102 | +*to++ = ch; |
| 103 | +break; |
| 104 | +} |
| 105 | +} |
| 106 | +} |
| 107 | + |
| 108 | +/* Parse an AMQP URL into its component parts. */ |
| 109 | +int amqp_parse_url(char *url, struct amqp_connection_info *parsed) |
| 110 | +{ |
| 111 | +int res = -ERROR_BAD_AMQP_URL; |
| 112 | +char delim; |
| 113 | +char *start; |
| 114 | +char *host; |
| 115 | +char *port = NULL; |
| 116 | + |
| 117 | +/* check the prefix */ |
| 118 | +if (strncmp(url, "amqp://", 7)) |
| 119 | +goto out; |
| 120 | + |
| 121 | +host = start = url += 7; |
| 122 | +delim = find_delim(&url, 1); |
| 123 | + |
| 124 | +if (delim == ':') { |
| 125 | +/* The colon could be introducing the port or the |
| 126 | + password part of the userinfo. We don't know yet, |
| 127 | + so stash the preceding component. */ |
| 128 | +port = start = url; |
| 129 | +delim = find_delim(&url, 1); |
| 130 | +} |
| 131 | + |
| 132 | +if (delim == '@') { |
| 133 | +/* What might have been the host and port were in fact |
| 134 | + the username and password */ |
| 135 | +parsed->user = host; |
| 136 | +if (port) |
| 137 | +parsed->password = port; |
| 138 | + |
| 139 | +port = NULL; |
| 140 | +host = start = url; |
| 141 | +delim = find_delim(&url, 1); |
| 142 | +} |
| 143 | + |
| 144 | +if (delim == '[') { |
| 145 | +/* IPv6 address. The bracket should be the first |
| 146 | + character in the host. */ |
| 147 | +if (host != start || *host != 0) |
| 148 | +goto out; |
| 149 | + |
| 150 | +start = url; |
| 151 | +delim = find_delim(&url, 0); |
| 152 | + |
| 153 | +if (delim != ']') |
| 154 | +goto out; |
| 155 | + |
| 156 | +parsed->host = start; |
| 157 | +start = url; |
| 158 | +delim = find_delim(&url, 1); |
| 159 | + |
| 160 | +/* Closing bracket should be the last character in the |
| 161 | + host. */ |
| 162 | +if (*start != 0) |
| 163 | +goto out; |
| 164 | +} |
| 165 | +else { |
| 166 | +/* If we haven't seen the host yet, this is it. */ |
| 167 | +if (*host != 0) |
| 168 | +parsed->host = host; |
| 169 | +} |
| 170 | + |
| 171 | +if (delim == ':') { |
| 172 | +port = start = url; |
| 173 | +delim = find_delim(&url, 1); |
| 174 | +} |
| 175 | + |
| 176 | +if (port) { |
| 177 | +char *end; |
| 178 | +long portnum = strtol(port, &end, 10); |
| 179 | + |
| 180 | +if (port == end || *end != 0 || portnum < 0 || portnum > 65535) |
| 181 | +goto out; |
| 182 | + |
| 183 | +parsed->port = portnum; |
| 184 | +} |
| 185 | + |
| 186 | +if (delim == '/') { |
| 187 | +start = url; |
| 188 | +delim = find_delim(&url, 1); |
| 189 | + |
| 190 | +if (delim != 0) |
| 191 | +goto out; |
| 192 | + |
| 193 | +parsed->vhost = start; |
| 194 | +res = 0; |
| 195 | +} |
| 196 | +else if (delim == 0) { |
| 197 | +res = 0; |
| 198 | +} |
| 199 | + |
| 200 | +/* Any other delimiter is bad, and we will return |
| 201 | + ERROR_BAD_AMQP_URL. */ |
| 202 | + |
| 203 | + out: |
| 204 | +return res; |
| 205 | +} |
0 commit comments