amqp_url.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #ifdef HAVE_CONFIG_H
  4. #include "config.h"
  5. #endif
  6. #ifdef _MSC_VER
  7. #define _CRT_SECURE_NO_WARNINGS
  8. #endif
  9. #include "amqp_private.h"
  10. #include <limits.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. void amqp_default_connection_info(struct amqp_connection_info *ci) {
  16. /* Apply defaults */
  17. ci->user = "guest";
  18. ci->password = "guest";
  19. ci->host = "localhost";
  20. ci->port = 5672;
  21. ci->vhost = "/";
  22. ci->ssl = 0;
  23. }
  24. /* Scan for the next delimiter, handling percent-encodings on the way. */
  25. static char find_delim(char **pp, int colon_and_at_sign_are_delims) {
  26. char *from = *pp;
  27. char *to = from;
  28. for (;;) {
  29. char ch = *from++;
  30. switch (ch) {
  31. case ':':
  32. case '@':
  33. if (!colon_and_at_sign_are_delims) {
  34. *to++ = ch;
  35. break;
  36. }
  37. /* fall through */
  38. case 0:
  39. case '/':
  40. case '?':
  41. case '#':
  42. case '[':
  43. case ']':
  44. *to = 0;
  45. *pp = from;
  46. return ch;
  47. case '%': {
  48. unsigned int val;
  49. int chars;
  50. int res = sscanf(from, "%2x%n", &val, &chars);
  51. if (res == EOF || res < 1 || chars != 2 || val > CHAR_MAX)
  52. /* Return a surprising delimiter to
  53. force an error. */
  54. {
  55. return '%';
  56. }
  57. *to++ = (char)val;
  58. from += 2;
  59. break;
  60. }
  61. default:
  62. *to++ = ch;
  63. break;
  64. }
  65. }
  66. }
  67. /* Parse an AMQP URL into its component parts. */
  68. int amqp_parse_url(char *url, struct amqp_connection_info *parsed) {
  69. int res = AMQP_STATUS_BAD_URL;
  70. char delim;
  71. char *start;
  72. char *host;
  73. char *port = NULL;
  74. amqp_default_connection_info(parsed);
  75. /* check the prefix */
  76. if (!strncmp(url, "amqp://", 7)) {
  77. /* do nothing */
  78. } else if (!strncmp(url, "amqps://", 8)) {
  79. parsed->port = 5671;
  80. parsed->ssl = 1;
  81. } else {
  82. goto out;
  83. }
  84. host = start = url += (parsed->ssl ? 8 : 7);
  85. delim = find_delim(&url, 1);
  86. if (delim == ':') {
  87. /* The colon could be introducing the port or the
  88. password part of the userinfo. We don't know yet,
  89. so stash the preceding component. */
  90. port = start = url;
  91. delim = find_delim(&url, 1);
  92. }
  93. if (delim == '@') {
  94. /* What might have been the host and port were in fact
  95. the username and password */
  96. parsed->user = host;
  97. if (port) {
  98. parsed->password = port;
  99. }
  100. port = NULL;
  101. host = start = url;
  102. delim = find_delim(&url, 1);
  103. }
  104. if (delim == '[') {
  105. /* IPv6 address. The bracket should be the first
  106. character in the host. */
  107. if (host != start || *host != 0) {
  108. goto out;
  109. }
  110. start = url;
  111. delim = find_delim(&url, 0);
  112. if (delim != ']') {
  113. goto out;
  114. }
  115. parsed->host = start;
  116. start = url;
  117. delim = find_delim(&url, 1);
  118. /* Closing bracket should be the last character in the
  119. host. */
  120. if (*start != 0) {
  121. goto out;
  122. }
  123. } else {
  124. /* If we haven't seen the host yet, this is it. */
  125. if (*host != 0) {
  126. parsed->host = host;
  127. }
  128. }
  129. if (delim == ':') {
  130. port = url;
  131. delim = find_delim(&url, 1);
  132. }
  133. if (port) {
  134. char *end;
  135. long portnum = strtol(port, &end, 10);
  136. if (port == end || *end != 0 || portnum < 0 || portnum > 65535) {
  137. goto out;
  138. }
  139. parsed->port = portnum;
  140. }
  141. if (delim == '/') {
  142. start = url;
  143. delim = find_delim(&url, 1);
  144. if (delim != 0) {
  145. goto out;
  146. }
  147. parsed->vhost = start;
  148. res = AMQP_STATUS_OK;
  149. } else if (delim == 0) {
  150. res = AMQP_STATUS_OK;
  151. }
  152. /* Any other delimiter is bad, and we will return AMQP_STATUS_BAD_AMQP_URL. */
  153. out:
  154. return res;
  155. }