Difference between
modified post e0b45e18 by Anonymous on Wed 24th Jan 2024 16:57
original post d7e by Anonymous on Wed 24th Jan 2024 16:00
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* urlparser1.c */ | |
| 2 | 2 | /* | |
| 3 | 3 | * MIT License | |
| 4 | 4 | * | |
| 5 | 5 | * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org> | |
| 6 | 6 | * | |
| 7 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 8 | 8 | * of this software and associated documentation files (the "Software"), to deal | |
| 9 | 9 | * in the Software without restriction, including without limitation the rights | |
| 10 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 11 | 11 | * copies of the Software, and to permit persons to whom the Software is | |
| 12 | 12 | * furnished to do so, subject to the following conditions: | |
| 13 | 13 | * | |
| 14 | 14 | * The above copyright notice and this permission notice shall be included in all | |
| 15 | 15 | * copies or substantial portions of the Software. | |
| 16 | 16 | * | |
| 17 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 18 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 19 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 20 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 21 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 22 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 23 | 23 | * SOFTWARE. | |
| 24 | 24 | */ | |
| 25 | 25 | ||
| 26 | 26 | /* urlparser1.c - simple URL parser */ | |
| 27 | 27 | ||
| 28 | 28 | #include <stdlib.h> | |
| 29 | 29 | #include <stdbool.h> | |
| 30 | 30 | #include <string.h> | |
| 31 | 31 | #include <stdio.h> | |
| 32 | 32 | ||
| 33 | 33 | #include "urlparser1.h" | |
| 34 | 34 | ||
| 35 | 35 | /* | |
| 36 | 36 | * Original extended regular expression: | |
| 37 | 37 | * | |
| 38 | 38 | * "^" | |
| 39 | 39 | * "(.+?)" // scheme | |
| 40 | 40 | * "://" // '://' | |
| 41 | 41 | * "(" // login | |
| 42 | 42 | * "(?:" | |
| 43 | 43 | * "(.+?)" // user (optional) | |
| 44 | 44 | * "(?::(.+))?" // password (optional) | |
| 45 | 45 | * "@" | |
| 46 | 46 | * ")?" | |
| 47 | 47 | * "(" // hostport | |
| 48 | 48 | * "(.+?)" // host | |
| 49 | 49 | * "(?::([[:digit:]]+))?" // port (optional) | |
| 50 | 50 | * ")" | |
| 51 | 51 | * ")" | |
| 52 | 52 | * "(?:/(.*?))?" // path (optional) | |
| 53 | 53 | * "$" | |
| 54 | 54 | */ | |
| 55 | 55 | ||
| 56 | 56 | #define DBGNULLSTR(s) (((s)!=NULL)?(s):"<NULL>") | |
| 57 | 57 | #if 1 | |
| 58 | 58 | #define D(x) x | |
| 59 | 59 | #else | |
| 60 | 60 | #define D(x) | |
| 61 | 61 | #endif | |
| 62 | 62 | ||
| 63 | 63 | url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags) | |
| 64 | 64 | { | |
| 65 | 65 | url_parser_context *uctx; | |
| 66 | 66 | char *s; | |
| 67 | 67 | size_t in_url_len; | |
| 68 | 68 | size_t context_len; | |
| 69 | 69 | ||
| 70 | 70 | if (!in_url) | |
| 71 | 71 | return NULL; | |
| 72 | 72 | ||
| 73 | 73 | in_url_len = strlen(in_url); | |
| 74 | 74 | ||
| 75 | 75 | context_len = sizeof(url_parser_context) + | |
| 76 | 76 | ((in_url_len+1)*5); | |
| 77 | 77 | uctx = malloc(context_len); | |
| 78 | 78 | if (!uctx) | |
| 79 | 79 | return NULL; | |
| 80 | 80 | ||
| 81 | 81 | s = (void *)(uctx+1); | |
| 82 | 82 | uctx->in_url = s; s+= in_url_len+1; | |
| 83 | 83 | (void)strcpy(uctx->in_url, in_url); | |
| 84 | 84 | uctx->scheme = s; s+= in_url_len+1; | |
| 85 | 85 | uctx->login.username = s; s+= in_url_len+1; | |
| 86 | 86 | uctx->hostport.hostname = s; s+= in_url_len+1; | |
| 87 | 87 | uctx->path = s; s+= in_url_len+1; | |
| 88 | 88 | uctx->hostport.port = -1; | |
| 89 | 89 | ||
| 90 | 90 | return uctx; | |
| 91 | 91 | } | |
| 92 | 92 | ||
| 93 | 93 | int url_parser_parse(url_parser_context *uctx) | |
| 94 | 94 | { | |
| 95 | 95 | D((void)fprintf(stderr, "## parser in_url='%s'\n", uctx->in_url)); | |
| 96 | 96 | ||
| 97 | 97 | char *s; | |
| 98 | 98 | const char *urlstr = uctx->in_url; | |
| 99 | 99 | size_t slen; | |
| 100 | 100 | ||
| 101 | 101 | s = strstr(urlstr, "://"); | |
| 102 | 102 | if (!s) { | |
| 103 | 103 | D((void)fprintf(stderr, "url_parser: Not an URL\n")); | |
| 104 | 104 | return -1; | |
| 105 | 105 | } | |
| 106 | 106 | ||
| 107 | 107 | slen = s-urlstr; | |
| 108 | 108 | (void)memcpy(uctx->scheme, urlstr, slen*sizeof(char)); | |
| 109 | 109 | uctx->scheme[slen] = '\0'; | |
| 110 | 110 | urlstr += slen + 3; | |
| 111 | 111 | ||
| 112 | 112 | D((void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr)); | |
| 113 | 113 | ||
| 114 | 114 | s = strstr(urlstr, "@"); | |
| 115 | 115 | if (s) { | |
| 116 | 116 | /* URL has user/password */ | |
| 117 | 117 | slen = s-urlstr; | |
| 118 | 118 | (void)memcpy(uctx->login.username, urlstr, slen*sizeof(char)); | |
| 119 | 119 | uctx->login.username[slen] = '\0'; | |
| 120 | 120 | urlstr += slen + 1; | |
| 121 | 121 | ||
| 122 | 122 | s = strstr(uctx->login.username, ":"); | |
| 123 | 123 | if (s) { | |
| 124 | 124 | /* found passwd */ | |
| 125 | 125 | uctx->login.passwd = s+1; | |
| 126 | 126 | *s = '\0'; | |
| 127 | 127 | } | |
| 128 | 128 | else | |
| 129 | 129 | { | |
| 130 | 130 | uctx->login.passwd = NULL; | |
| 131 | 131 | } | |
| 132 | 132 | ||
| 133 | 133 | /* catch password-only URLs */ | |
| 134 | 134 | if (uctx->login.username[0] == '\0') | |
| 135 | 135 | uctx->login.username = NULL; | |
| 136 | 136 | } | |
| 137 | 137 | else | |
| 138 | 138 | { | |
| 139 | 139 | uctx->login.username = NULL; | |
| 140 | 140 | uctx->login.passwd = NULL; | |
| 141 | 141 | } | |
| 142 | 142 | ||
| 143 | 143 | D((void)fprintf(stdout, "login='%s', passwd='%s', rest='%s'\n", | |
| 144 | 144 | DBGNULLSTR(uctx->login.username), | |
| 145 | 145 | DBGNULLSTR(uctx->login.passwd), | |
| 146 | 146 | DBGNULLSTR(urlstr))); | |
| 147 | 147 | ||
| 148 | 148 | s = strstr(urlstr, "/"); | |
| 149 | 149 | if (s) { | |
| 150 | 150 | /* URL has hostport */ | |
| 151 | 151 | slen = s-urlstr; | |
| 152 | 152 | (void)memcpy(uctx->hostport.hostname, urlstr, slen*sizeof(char)); | |
| 153 | 153 | uctx->hostport.hostname[slen] = '\0'; | |
| 154 | 154 | urlstr += slen; | |
| 155 | 155 | ||
| 156 | - | s = strstr(uctx->hostport.hostname, ":"); | |
| 156 | + | /* | |
| 157 | - | if (s) { | |
| 157 | + | * check for addresses within '[' and ']', like | |
| 158 | - | /* found port number */ | |
| 158 | + | * IPv6 addresses | |
| 159 | - | uctx->hostport.port = atoi(s+1); | |
| 159 | + | */ | |
| 160 | - | *s = '\0'; | |
| 160 | + | s = uctx->hostport.hostname; | |
| 161 | + | if (s[0] == '[') | |
| 161 | 167 | } | |
| 168 | + | ||
| 162 | 174 | } | |
| 175 | + | } | |
| 163 | 176 | else | |
| 164 | 177 | { | |
| 165 | 178 | (void)strcpy(uctx->hostport.hostname, urlstr); | |
| 166 | 179 | uctx->path = NULL; | |
| 167 | 180 | urlstr = NULL; | |
| 168 | 181 | } | |
| 169 | 182 | ||
| 170 | 183 | D((void)fprintf(stdout, "hostport='%s', port=%d, rest='%s'\n", | |
| 171 | 184 | DBGNULLSTR(uctx->hostport.hostname), | |
| 172 | 185 | uctx->hostport.port, | |
| 173 | 186 | DBGNULLSTR(urlstr))); | |
| 174 | 187 | ||
| 175 | 188 | if (!urlstr) { | |
| 176 | 189 | return 0; | |
| 177 | 190 | } | |
| 178 | 191 | ||
| 179 | 192 | (void)strcpy(uctx->path, urlstr); | |
| 180 | 193 | D((void)fprintf(stdout, "path='%s'\n", uctx->path)); | |
| 181 | 194 | ||
| 182 | 195 | return 0; | |
| 183 | 196 | } | |
| 184 | 197 | ||
| 185 | 198 | void url_parser_free_context(url_parser_context *c) | |
| 186 | 199 | { | |
| 187 | 200 | free(c); | |
| 188 | 201 | } | |
| 189 | 202 | ||
| 190 | 203 | void test_url_parser(const char *instr) | |
| 191 | 204 | { | |
| 192 | 205 | url_parser_context *c; | |
| 193 | 206 | ||
| 194 | 207 | c = url_parser_create_context(instr, 0); | |
| 195 | 208 | ||
| 196 | 209 | (void)url_parser_parse(c); | |
| 197 | 210 | ||
| 198 | 211 | (void)fputc('\n', stdout); | |
| 199 | 212 | ||
| 200 | 213 | url_parser_free_context(c); | |
| 201 | 214 | } | |
| 202 | 215 | ||
| 203 | 216 | int main(int ac, char *av[]) | |
| 204 | 217 | { | |
| 205 | 218 | (void)puts("#start"); | |
| 206 | 219 | ||
| 207 | 220 | (void)setvbuf(stdout, NULL, _IONBF, 0); | |
| 208 | 221 | (void)setvbuf(stderr, NULL, _IONBF, 0); | |
| 209 | 222 | ||
| 210 | 223 | (void)test_url_parser("foo://hostbar/baz"); | |
| 211 | 224 | (void)test_url_parser("foo://myuser@hostbar/baz"); | |
| 212 | 225 | (void)test_url_parser("foo://myuser:mypasswd@hostbar/baz"); | |
| 213 | 226 | (void)test_url_parser("foo://myuser:mypasswd@hostbar:666/baz"); | |
| 227 | + | (void)test_url_parser("foo://myuser:mypasswd@[fe80::21b:1bff:fec3:7713]:666/baz"); | |
| 214 | 228 | (void)test_url_parser("foo://:mypasswd2@hostbar2:667/baf"); | |
| 215 | 229 | (void)test_url_parser("foo://hostbar"); | |
| 216 | 230 | (void)test_url_parser("foo://hostbar:93"); | |
| 217 | 231 | (void)test_url_parser("foo://"); | |
| 218 | 232 | (void)test_url_parser("typo:/hostbar"); | |
| 219 | 233 | (void)test_url_parser("wrong"); | |
| 220 | 234 | ||
| 221 | 235 | (void)puts("#done"); | |
| 222 | 236 | ||
| 223 | 237 | return EXIT_SUCCESS; | |
| 224 | 238 | } | |
| 225 | 239 | /* urlparser1.h */ | |
| 226 | 240 | /* | |
| 227 | 241 | * MIT License | |
| 228 | 242 | * | |
| 229 | 243 | * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org> | |
| 230 | 244 | * | |
| 231 | 245 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 232 | 246 | * of this software and associated documentation files (the "Software"), to deal | |
| 233 | 247 | * in the Software without restriction, including without limitation the rights | |
| 234 | 248 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 235 | 249 | * copies of the Software, and to permit persons to whom the Software is | |
| 236 | 250 | * furnished to do so, subject to the following conditions: | |
| 237 | 251 | * | |
| 238 | 252 | * The above copyright notice and this permission notice shall be included in all | |
| 239 | 253 | * copies or substantial portions of the Software. | |
| 240 | 254 | * | |
| 241 | 255 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 242 | 256 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 243 | 257 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 244 | 258 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 245 | 259 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 246 | 260 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 247 | 261 | * SOFTWARE. | |
| 248 | 262 | */ | |
| 249 | 263 | ||
| 250 | 264 | /* urlparser1.h - header for simple URL parser */ | |
| 251 | 265 | ||
| 252 | 266 | #ifndef __URLPARSER1_H__ | |
| 253 | 267 | #define __URLPARSER1_H__ | |
| 254 | 268 | ||
| 255 | 269 | #include <stdlib.h> | |
| 256 | 270 | ||
| 257 | 271 | typedef | |
| 258 | 272 | struct _url_parser_context { | |
| 259 | 273 | char *in_url; | |
| 260 | 274 | ||
| 261 | 275 | char *scheme; | |
| 262 | 276 | struct { | |
| 263 | 277 | char *username; | |
| 264 | 278 | char *passwd; | |
| 265 | 279 | } login; | |
| 266 | 280 | struct { | |
| 267 | 281 | char *hostname; | |
| 268 | 282 | signed int port; | |
| 269 | 283 | } hostport; | |
| 270 | 284 | char *path; | |
| 271 | 285 | } url_parser_context; | |
| 272 | 286 | ||
| 273 | 287 | /* Prototypes */ | |
| 274 | 288 | url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags); | |
| 275 | 289 | int url_parser_parse(url_parser_context *uctx); | |
| 276 | 290 | void url_parser_free_context(url_parser_context *c); | |
| 277 | 291 | ||
| 278 | 292 | #endif /* !__URLPARSER1_H__ */ | |
| 279 | 293 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
nrubsig.kpaste.net RSS