[d7b7f5e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Martin Sucha
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup uri
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <stdlib.h>
|
---|
[582a0b8] | 38 | #include <stddef.h>
|
---|
[d7b7f5e] | 39 | #include <str.h>
|
---|
| 40 | #include <ctype.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 |
|
---|
| 43 | #include "uri.h"
|
---|
[dbbbe75b] | 44 | #include "internal/ctype.h"
|
---|
[d7b7f5e] | 45 |
|
---|
| 46 | static char *cut_str(const char *start, const char *end)
|
---|
| 47 | {
|
---|
| 48 | size_t size = end - start;
|
---|
| 49 | return str_ndup(start, size);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | uri_t *uri_parse(const char *str)
|
---|
| 53 | {
|
---|
| 54 | uri_t *uri = malloc(sizeof(uri_t));
|
---|
| 55 | if (uri == NULL)
|
---|
| 56 | return NULL;
|
---|
| 57 | memset(uri, 0, sizeof(uri_t));
|
---|
[a35b458] | 58 |
|
---|
[d7b7f5e] | 59 | /* scheme ":" */
|
---|
| 60 | const char *scheme = str;
|
---|
[3bacee1] | 61 | while (*str != 0 && *str != ':')
|
---|
| 62 | str++;
|
---|
[d7b7f5e] | 63 | if (*str == 0) {
|
---|
| 64 | uri_destroy(uri);
|
---|
| 65 | return NULL;
|
---|
| 66 | }
|
---|
| 67 | uri->scheme = cut_str(scheme, str);
|
---|
[a35b458] | 68 |
|
---|
[d7b7f5e] | 69 | /* skip the colon */
|
---|
| 70 | str++;
|
---|
[a35b458] | 71 |
|
---|
[d7b7f5e] | 72 | if (*str == '/' && str[1] == '/') {
|
---|
| 73 | /* "//" [user-info [":" user-credential] "@"] host [":" port] */
|
---|
| 74 | str++;
|
---|
| 75 | str++;
|
---|
| 76 | const char *authority_start = str;
|
---|
[a35b458] | 77 |
|
---|
[d7b7f5e] | 78 | char *host_or_user_info = NULL;
|
---|
| 79 | char *port_or_user_credential = NULL;
|
---|
[a35b458] | 80 |
|
---|
[3bacee1] | 81 | while (*str != 0 && *str != '?' && *str != '#' && *str != '@' &&
|
---|
| 82 | *str != ':' && *str != '/')
|
---|
[d7b7f5e] | 83 | str++;
|
---|
[a35b458] | 84 |
|
---|
[d7b7f5e] | 85 | host_or_user_info = cut_str(authority_start, str);
|
---|
| 86 | if (*str == ':') {
|
---|
| 87 | str++;
|
---|
| 88 | const char *second_part = str;
|
---|
| 89 | while (*str != 0 && *str != '?' && *str != '#' &&
|
---|
[3bacee1] | 90 | *str != '@' && *str != '/')
|
---|
| 91 | str++;
|
---|
[d7b7f5e] | 92 | port_or_user_credential = cut_str(second_part, str);
|
---|
| 93 | }
|
---|
[a35b458] | 94 |
|
---|
[d7b7f5e] | 95 | if (*str == '@') {
|
---|
| 96 | uri->user_info = host_or_user_info;
|
---|
| 97 | uri->user_credential = port_or_user_credential;
|
---|
[a35b458] | 98 |
|
---|
[d7b7f5e] | 99 | str++;
|
---|
| 100 | const char *host_start = str;
|
---|
[3bacee1] | 101 | while (*str != 0 && *str != '?' && *str != '#' &&
|
---|
| 102 | *str != ':' && *str != '/')
|
---|
| 103 | str++;
|
---|
[d7b7f5e] | 104 | uri->host = cut_str(host_start, str);
|
---|
[a35b458] | 105 |
|
---|
[d7b7f5e] | 106 | if (*str == ':') {
|
---|
| 107 | str++;
|
---|
| 108 | const char *port_start = str;
|
---|
| 109 | while (*str != 0 && *str != '?' && *str != '#' && *str != '/')
|
---|
| 110 | str++;
|
---|
| 111 | uri->port = cut_str(port_start, str);
|
---|
| 112 | }
|
---|
[3bacee1] | 113 | } else {
|
---|
[d7b7f5e] | 114 | uri->host = host_or_user_info;
|
---|
| 115 | uri->port = port_or_user_credential;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[a35b458] | 118 |
|
---|
[d7b7f5e] | 119 | const char *path_start = str;
|
---|
[3bacee1] | 120 | while (*str != 0 && *str != '?' && *str != '#')
|
---|
| 121 | str++;
|
---|
[d7b7f5e] | 122 | uri->path = cut_str(path_start, str);
|
---|
[a35b458] | 123 |
|
---|
[d7b7f5e] | 124 | if (*str == '?') {
|
---|
| 125 | str++;
|
---|
| 126 | const char *query_start = str;
|
---|
[3bacee1] | 127 | while (*str != 0 && *str != '#')
|
---|
| 128 | str++;
|
---|
[d7b7f5e] | 129 | uri->query = cut_str(query_start, str);
|
---|
| 130 | }
|
---|
[a35b458] | 131 |
|
---|
[d7b7f5e] | 132 | if (*str == '#') {
|
---|
| 133 | str++;
|
---|
| 134 | const char *fragment_start = str;
|
---|
[3bacee1] | 135 | while (*str != 0)
|
---|
| 136 | str++;
|
---|
[d7b7f5e] | 137 | uri->fragment = cut_str(fragment_start, str);
|
---|
| 138 | }
|
---|
[a35b458] | 139 |
|
---|
[d7b7f5e] | 140 | assert(*str == 0);
|
---|
| 141 | return uri;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Parse the URI scheme
|
---|
| 145 | *
|
---|
| 146 | * @param endptr The position of the first character after the scheme
|
---|
| 147 | * is stored there.
|
---|
| 148 | * @return EOK on success
|
---|
| 149 | */
|
---|
[b7fd2a0] | 150 | errno_t uri_scheme_parse(const char *str, const char **endptr)
|
---|
[d7b7f5e] | 151 | {
|
---|
| 152 | if (*str == 0) {
|
---|
| 153 | *endptr = str;
|
---|
| 154 | return ELIMIT;
|
---|
| 155 | }
|
---|
[a35b458] | 156 |
|
---|
[d7b7f5e] | 157 | if (!isalpha(*str)) {
|
---|
| 158 | *endptr = str;
|
---|
| 159 | return EINVAL;
|
---|
| 160 | }
|
---|
[a35b458] | 161 |
|
---|
[d7b7f5e] | 162 | while (isalpha(*str) || isdigit(*str) ||
|
---|
| 163 | *str == '+' || *str == '-' || *str == '.') {
|
---|
| 164 | str++;
|
---|
| 165 | }
|
---|
[a35b458] | 166 |
|
---|
[d7b7f5e] | 167 | *endptr = str;
|
---|
| 168 | return EOK;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Determine if the URI scheme is valid
|
---|
| 172 | *
|
---|
| 173 | */
|
---|
| 174 | bool uri_scheme_validate(const char *str)
|
---|
| 175 | {
|
---|
| 176 | const char *endptr = NULL;
|
---|
| 177 | if (uri_scheme_parse(str, &endptr) != EOK)
|
---|
| 178 | return false;
|
---|
| 179 | return *endptr == 0;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[b7fd2a0] | 182 | errno_t uri_percent_parse(const char *str, const char **endptr,
|
---|
[d7b7f5e] | 183 | uint8_t *decoded)
|
---|
| 184 | {
|
---|
| 185 | *endptr = str;
|
---|
| 186 | if (str[0] == 0 || str[1] == 0 || str[2] == 0)
|
---|
| 187 | return ELIMIT;
|
---|
[a35b458] | 188 |
|
---|
[d7b7f5e] | 189 | if (str[0] != '%' || !is_hexdig(str[1]) || !is_hexdig(str[2]))
|
---|
| 190 | return EINVAL;
|
---|
[a35b458] | 191 |
|
---|
[d7b7f5e] | 192 | if (decoded != NULL) {
|
---|
[b7fd2a0] | 193 | errno_t rc = str_uint8_t(str + 1, NULL, 16, true, decoded);
|
---|
[d7b7f5e] | 194 | if (rc != EOK)
|
---|
| 195 | return rc;
|
---|
| 196 | }
|
---|
[a35b458] | 197 |
|
---|
[d7b7f5e] | 198 | *endptr = str + 3;
|
---|
| 199 | return EOK;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[b7fd2a0] | 202 | errno_t uri_user_info_parse(const char *str, const char **endptr)
|
---|
[d7b7f5e] | 203 | {
|
---|
| 204 | while (*str != 0) {
|
---|
[3bacee1] | 205 | while (is_unreserved(*str) || is_subdelim(*str) || *str == ':')
|
---|
| 206 | str++;
|
---|
[d7b7f5e] | 207 | if (*str == 0)
|
---|
| 208 | break;
|
---|
[b7fd2a0] | 209 | errno_t rc = uri_percent_parse(str, &str, NULL);
|
---|
[d7b7f5e] | 210 | if (rc != EOK) {
|
---|
| 211 | *endptr = str;
|
---|
| 212 | return rc;
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
[a35b458] | 215 |
|
---|
[d7b7f5e] | 216 | *endptr = str;
|
---|
| 217 | return EOK;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | bool uri_user_info_validate(const char *str)
|
---|
| 221 | {
|
---|
| 222 | const char *endptr = NULL;
|
---|
| 223 | if (uri_user_info_parse(str, &endptr) != EOK)
|
---|
| 224 | return false;
|
---|
| 225 | return *endptr == 0;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[b7fd2a0] | 228 | errno_t uri_port_parse(const char *str, const char **endptr)
|
---|
[d7b7f5e] | 229 | {
|
---|
| 230 | if (*str == 0)
|
---|
| 231 | return ELIMIT;
|
---|
| 232 | if (!isdigit(*str))
|
---|
| 233 | return EINVAL;
|
---|
[3bacee1] | 234 | while (isdigit(*str))
|
---|
| 235 | str++;
|
---|
[d7b7f5e] | 236 | *endptr = str;
|
---|
| 237 | return EOK;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | bool uri_port_validate(const char *str)
|
---|
| 241 | {
|
---|
| 242 | const char *endptr = NULL;
|
---|
| 243 | if (uri_port_parse(str, &endptr) != EOK)
|
---|
| 244 | return false;
|
---|
| 245 | return *endptr == 0;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | bool uri_validate(uri_t *uri)
|
---|
| 249 | {
|
---|
| 250 | if (uri->scheme && !uri_scheme_validate(uri->scheme))
|
---|
| 251 | return false;
|
---|
[a35b458] | 252 |
|
---|
[d7b7f5e] | 253 | if (uri->user_info && !uri_user_info_validate(uri->user_info))
|
---|
| 254 | return false;
|
---|
[a35b458] | 255 |
|
---|
[d7b7f5e] | 256 | if (uri->user_credential && !uri_user_info_validate(uri->user_credential))
|
---|
| 257 | return false;
|
---|
[a35b458] | 258 |
|
---|
[d7b7f5e] | 259 | if (uri->port && !uri_port_validate(uri->port))
|
---|
| 260 | return false;
|
---|
[a35b458] | 261 |
|
---|
[d7b7f5e] | 262 | return true;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | void uri_destroy(uri_t *uri)
|
---|
| 266 | {
|
---|
| 267 | free(uri->scheme);
|
---|
| 268 | free(uri->user_info);
|
---|
| 269 | free(uri->user_credential);
|
---|
| 270 | free(uri->host);
|
---|
| 271 | free(uri->port);
|
---|
| 272 | free(uri->path);
|
---|
| 273 | free(uri->query);
|
---|
| 274 | free(uri->fragment);
|
---|
| 275 | free(uri);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /** @}
|
---|
| 279 | */
|
---|