Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame^] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/client_config/uri_parser.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/support/alloc.h> |
| 39 | #include <grpc/support/log.h> |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 40 | #include <grpc/support/port_platform.h> |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
| 42 | |
Craig Tiller | f29a388 | 2015-09-11 12:59:27 -0700 | [diff] [blame] | 43 | /** a size_t default value... maps to all 1's */ |
| 44 | #define NOT_SET (~(size_t)0) |
| 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | static grpc_uri *bad_uri(const char *uri_text, size_t pos, const char *section, |
| 47 | int suppress_errors) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 48 | char *line_prefix; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 49 | size_t pfx_len; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 50 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 51 | if (!suppress_errors) { |
| 52 | gpr_asprintf(&line_prefix, "bad uri.%s: '", section); |
| 53 | pfx_len = strlen(line_prefix) + pos; |
| 54 | gpr_log(GPR_ERROR, "%s%s'", line_prefix, uri_text); |
| 55 | gpr_free(line_prefix); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | line_prefix = gpr_malloc(pfx_len + 1); |
| 58 | memset(line_prefix, ' ', pfx_len); |
| 59 | line_prefix[pfx_len] = 0; |
| 60 | gpr_log(GPR_ERROR, "%s^ here", line_prefix); |
| 61 | gpr_free(line_prefix); |
| 62 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 67 | /** Returns a copy of \a src[begin, end) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 68 | static char *copy_component(const char *src, size_t begin, size_t end) { |
| 69 | char *out = gpr_malloc(end - begin + 1); |
| 70 | memcpy(out, src + begin, end - begin); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 71 | out[end - begin] = 0; |
| 72 | return out; |
| 73 | } |
| 74 | |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 75 | /** Returns how many chars to advance if \a uri_text[i] begins a valid \a pchar |
| 76 | * production. If \a uri_text[i] introduces an invalid \a pchar (such as percent |
Craig Tiller | f29a388 | 2015-09-11 12:59:27 -0700 | [diff] [blame] | 77 | * sign not followed by two hex digits), NOT_SET is returned. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | static size_t parse_pchar(const char *uri_text, size_t i) { |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 79 | /* pchar = unreserved / pct-encoded / sub-delims / ":" / "@" |
| 80 | * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" |
| 81 | * pct-encoded = "%" HEXDIG HEXDIG |
| 82 | * sub-delims = "!" / "$" / "&" / "'" / "(" / ")" |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 83 | / "*" / "+" / "," / ";" / "=" */ |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 84 | char c = uri_text[i]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || |
| 86 | ((c >= '0') && (c <= '9')) || |
| 87 | (c == '-' || c == '.' || c == '_' || c == '~') || /* unreserved */ |
| 88 | (c == '!' || c == '$' || c == '&' || c == '\'' || c == '$' || c == '&' || |
| 89 | c == '(' || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || |
| 90 | c == '=') /* sub-delims */) { |
| 91 | return 1; |
| 92 | } |
| 93 | if (c == '%') { /* pct-encoded */ |
| 94 | size_t j; |
| 95 | if (uri_text[i + 1] == 0 || uri_text[i + 2] == 0) { |
| 96 | return NOT_SET; |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 97 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 98 | for (j = i + 1; j < 2; j++) { |
| 99 | c = uri_text[j]; |
| 100 | if (!(((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'f')) || |
| 101 | ((c >= 'A') && (c <= 'F')))) { |
| 102 | return NOT_SET; |
| 103 | } |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 104 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | return 2; |
| 106 | } |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /* *( pchar / "?" / "/" ) */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | static int parse_fragment_or_query(const char *uri_text, size_t *i) { |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 112 | char c; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | while ((c = uri_text[*i]) != 0) { |
| 114 | const size_t advance = parse_pchar(uri_text, *i); /* pchar */ |
| 115 | switch (advance) { |
| 116 | case 0: /* uri_text[i] isn't in pchar */ |
| 117 | /* maybe it's ? or / */ |
| 118 | if (uri_text[*i] == '?' || uri_text[*i] == '/') { |
| 119 | (*i)++; |
| 120 | break; |
| 121 | } else { |
| 122 | return 1; |
| 123 | } |
yang-g | b063c87 | 2015-10-07 11:40:13 -0700 | [diff] [blame] | 124 | GPR_UNREACHABLE_CODE(return 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | default: |
| 126 | (*i) += advance; |
| 127 | break; |
| 128 | case NOT_SET: /* uri_text[i] introduces an invalid URI */ |
| 129 | return 0; |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 130 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | } |
Craig Tiller | f29a388 | 2015-09-11 12:59:27 -0700 | [diff] [blame] | 132 | /* *i is the first uri_text position past the \a query production, maybe \0 */ |
| 133 | return 1; |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | grpc_uri *grpc_uri_parse(const char *uri_text, int suppress_errors) { |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 137 | grpc_uri *uri; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 138 | size_t scheme_begin = 0; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 139 | size_t scheme_end = NOT_SET; |
| 140 | size_t authority_begin = NOT_SET; |
| 141 | size_t authority_end = NOT_SET; |
| 142 | size_t path_begin = NOT_SET; |
| 143 | size_t path_end = NOT_SET; |
Craig Tiller | f29a388 | 2015-09-11 12:59:27 -0700 | [diff] [blame] | 144 | size_t query_begin = NOT_SET; |
| 145 | size_t query_end = NOT_SET; |
| 146 | size_t fragment_begin = NOT_SET; |
| 147 | size_t fragment_end = NOT_SET; |
Craig Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 148 | size_t i; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 149 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | for (i = scheme_begin; uri_text[i] != 0; i++) { |
| 151 | if (uri_text[i] == ':') { |
| 152 | scheme_end = i; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | if (uri_text[i] >= 'a' && uri_text[i] <= 'z') continue; |
| 156 | if (uri_text[i] >= 'A' && uri_text[i] <= 'Z') continue; |
| 157 | if (i != scheme_begin) { |
| 158 | if (uri_text[i] >= '0' && uri_text[i] <= '9') continue; |
| 159 | if (uri_text[i] == '+') continue; |
| 160 | if (uri_text[i] == '-') continue; |
| 161 | if (uri_text[i] == '.') continue; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 162 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | if (scheme_end == NOT_SET) { |
| 166 | return bad_uri(uri_text, i, "scheme", suppress_errors); |
| 167 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 168 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | if (uri_text[scheme_end + 1] == '/' && uri_text[scheme_end + 2] == '/') { |
| 170 | authority_begin = scheme_end + 3; |
| 171 | for (i = authority_begin; uri_text[i] != 0 && authority_end == NOT_SET; |
| 172 | i++) { |
| 173 | if (uri_text[i] == '/' || uri_text[i] == '?' || uri_text[i] == '#') { |
| 174 | authority_end = i; |
| 175 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 176 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | if (authority_end == NOT_SET && uri_text[i] == 0) { |
| 178 | authority_end = i; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 179 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 180 | if (authority_end == NOT_SET) { |
| 181 | return bad_uri(uri_text, i, "authority", suppress_errors); |
| 182 | } |
| 183 | /* TODO(ctiller): parse the authority correctly */ |
| 184 | path_begin = authority_end; |
| 185 | } else { |
| 186 | path_begin = scheme_end + 1; |
| 187 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 188 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | for (i = path_begin; uri_text[i] != 0; i++) { |
| 190 | if (uri_text[i] == '?' || uri_text[i] == '#') { |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 191 | path_end = i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | break; |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 193 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | } |
| 195 | if (path_end == NOT_SET && uri_text[i] == 0) { |
| 196 | path_end = i; |
| 197 | } |
| 198 | if (path_end == NOT_SET) { |
| 199 | return bad_uri(uri_text, i, "path", suppress_errors); |
| 200 | } |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 201 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 202 | if (uri_text[i] == '?') { |
| 203 | query_begin = ++i; |
| 204 | if (!parse_fragment_or_query(uri_text, &i)) { |
| 205 | return bad_uri(uri_text, i, "query", suppress_errors); |
| 206 | } else if (uri_text[i] != 0 && uri_text[i] != '#') { |
| 207 | /* We must be at the end or at the beginning of a fragment */ |
| 208 | return bad_uri(uri_text, i, "query", suppress_errors); |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 209 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 210 | query_end = i; |
| 211 | } |
| 212 | if (uri_text[i] == '#') { |
| 213 | fragment_begin = ++i; |
| 214 | if (!parse_fragment_or_query(uri_text, &i)) { |
| 215 | return bad_uri(uri_text, i - fragment_end, "fragment", suppress_errors); |
| 216 | } else if (uri_text[i] != 0) { |
| 217 | /* We must be at the end */ |
| 218 | return bad_uri(uri_text, i, "fragment", suppress_errors); |
David Garcia Quintas | 5267891 | 2015-09-07 11:28:58 -0700 | [diff] [blame] | 219 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | fragment_end = i; |
| 221 | } |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 222 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | uri = gpr_malloc(sizeof(*uri)); |
| 224 | memset(uri, 0, sizeof(*uri)); |
| 225 | uri->scheme = copy_component(uri_text, scheme_begin, scheme_end); |
| 226 | uri->authority = copy_component(uri_text, authority_begin, authority_end); |
| 227 | uri->path = copy_component(uri_text, path_begin, path_end); |
| 228 | uri->query = copy_component(uri_text, query_begin, query_end); |
| 229 | uri->fragment = copy_component(uri_text, fragment_begin, fragment_end); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 230 | |
| 231 | return uri; |
| 232 | } |
| 233 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 234 | void grpc_uri_destroy(grpc_uri *uri) { |
| 235 | if (!uri) return; |
| 236 | gpr_free(uri->scheme); |
| 237 | gpr_free(uri->authority); |
| 238 | gpr_free(uri->path); |
| 239 | gpr_free(uri->query); |
| 240 | gpr_free(uri->fragment); |
| 241 | gpr_free(uri); |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 242 | } |