summaryrefslogtreecommitdiff
path: root/librabbitmq/amqp_url.c
blob: 602b5540b18e77dd64f5dabf8305f86fcf07d50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MIT
 *
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
 * Alan Antonuk. All Rights Reserved.
 *
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
 * All Rights Reserved.
 *
 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ***** END LICENSE BLOCK *****
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "amqp_private.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void amqp_default_connection_info(struct amqp_connection_info *ci)
{
  /* Apply defaults */
  ci->user = "guest";
  ci->password = "guest";
  ci->host = "localhost";
  ci->port = 5672;
  ci->vhost = "/";
  ci->ssl = 0;
}

/* Scan for the next delimiter, handling percent-encodings on the way. */
static char find_delim(char **pp, int colon_and_at_sign_are_delims)
{
  char *from = *pp;
  char *to = from;

  for (;;) {
    char ch = *from++;

    switch (ch) {
    case ':':
    case '@':
      if (!colon_and_at_sign_are_delims) {
        *to++ = ch;
        break;
      }

      /* fall through */
    case 0:
    case '/':
    case '?':
    case '#':
    case '[':
    case ']':
      *to = 0;
      *pp = from;
      return ch;

    case '%': {
      unsigned int val;
      int chars;
      int res = sscanf(from, "%2x%n", &val, &chars);

      if (res == EOF || res < 1 || chars != 2)
        /* Return a surprising delimiter to
           force an error. */
      {
        return '%';
      }

      *to++ = val;
      from += 2;
      break;
    }

    default:
      *to++ = ch;
      break;
    }
  }
}

/* Parse an AMQP URL into its component parts. */
int amqp_parse_url(char *url, struct amqp_connection_info *parsed)
{
  int res = AMQP_STATUS_BAD_URL;
  char delim;
  char *start;
  char *host;
  char *port = NULL;

  /* check the prefix */
  if (!strncmp(url, "amqp://", 7)) {
    /* do nothing */
  } else if (!strncmp(url, "amqps://", 8)) {
    parsed->port = 5671;
    parsed->ssl = 1;
  } else {
    goto out;
  }

  host = start = url += (parsed->ssl ? 8 : 7);
  delim = find_delim(&url, 1);

  if (delim == ':') {
    /* The colon could be introducing the port or the
       password part of the userinfo.  We don't know yet,
       so stash the preceding component. */
    port = start = url;
    delim = find_delim(&url, 1);
  }

  if (delim == '@') {
    /* What might have been the host and port were in fact
       the username and password */
    parsed->user = host;
    if (port) {
      parsed->password = port;
    }

    port = NULL;
    host = start = url;
    delim = find_delim(&url, 1);
  }

  if (delim == '[') {
    /* IPv6 address.  The bracket should be the first
       character in the host. */
    if (host != start || *host != 0) {
      goto out;
    }

    start = url;
    delim = find_delim(&url, 0);

    if (delim != ']') {
      goto out;
    }

    parsed->host = start;
    start = url;
    delim = find_delim(&url, 1);

    /* Closing bracket should be the last character in the
       host. */
    if (*start != 0) {
      goto out;
    }
  } else {
    /* If we haven't seen the host yet, this is it. */
    if (*host != 0) {
      parsed->host = host;
    }
  }

  if (delim == ':') {
    port = start = url;
    delim = find_delim(&url, 1);
  }

  if (port) {
    char *end;
    long portnum = strtol(port, &end, 10);

    if (port == end || *end != 0 || portnum < 0 || portnum > 65535) {
      goto out;
    }

    parsed->port = portnum;
  }

  if (delim == '/') {
    start = url;
    delim = find_delim(&url, 1);

    if (delim != 0) {
      goto out;
    }

    parsed->vhost = start;
    res = AMQP_STATUS_OK;
  } else if (delim == 0) {
    res = AMQP_STATUS_OK;
  }

  /* Any other delimiter is bad, and we will return
     AMQP_STATUS_BAD_AMQP_URL. */

out:
  return res;
}