Difference between
modified post 544ee372 by Anonymous on Wed 24th Jan 2024 10:56
original post db6ed32 by Anonymous on Wed 24th Jan 2024 10:29
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* | |
| 2 | 2 | * MIT License | |
| 3 | 3 | * | |
| 4 | 4 | * Copyright (c) 2024 Roland Mainz <roland.mainz@nrubsig.org> | |
| 5 | 5 | * | |
| 6 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 7 | 7 | * of this software and associated documentation files (the "Software"), to deal | |
| 8 | 8 | * in the Software without restriction, including without limitation the rights | |
| 9 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 10 | 10 | * copies of the Software, and to permit persons to whom the Software is | |
| 11 | 11 | * furnished to do so, subject to the following conditions: | |
| 12 | 12 | * | |
| 13 | 13 | * The above copyright notice and this permission notice shall be included in all | |
| 14 | 14 | * copies or substantial portions of the Software. | |
| 15 | 15 | * | |
| 16 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 17 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 18 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 19 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 20 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 22 | 22 | * SOFTWARE. | |
| 23 | 23 | */ | |
| 24 | 24 | ||
| 25 | + | /* urlparser1.c - simple URL parser */ | |
| 25 | 27 | #include <stdlib.h> | |
| 26 | 28 | #include <stdbool.h> | |
| 27 | 29 | #include <string.h> | |
| 28 | 30 | #include <stdio.h> | |
| 29 | 31 | ||
| 30 | 32 | /* | |
| 31 | 33 | * Original extended regular expression: | |
| 32 | 34 | * | |
| 33 | 35 | * "^" | |
| 34 | 36 | * "(.+?)" // scheme | |
| 35 | 37 | * "://" // '://' | |
| 36 | 38 | * "(" // login | |
| 37 | 39 | * "(?:" | |
| 38 | 40 | * "(.+?)" // user (optional) | |
| 39 | 41 | * "(?::(.+))?" // password (optional) | |
| 40 | 42 | * "@" | |
| 41 | 43 | * ")?" | |
| 42 | 44 | * "(" // hostport | |
| 43 | 45 | * "(.+?)" // host | |
| 44 | 46 | * "(?::([[:digit:]]+))?" // port (optional) | |
| 45 | 47 | * ")" | |
| 46 | 48 | * ")" | |
| 47 | 49 | * "(?:/(.*?))?" // path (optional) | |
| 48 | 50 | * "$" | |
| 49 | 51 | */ | |
| 50 | 52 | ||
| 53 | + | #define DBGNULLSTR(s) (((s)!=NULL)?(s):"<NULL>") | |
| 51 | 56 | typedef | |
| 52 | 57 | struct _url_parser_context { | |
| 53 | 58 | char *in_url; | |
| 54 | 59 | ||
| 55 | 60 | char *scheme; | |
| 56 | 61 | struct { | |
| 57 | - | char *login; | |
| 62 | + | char *username; | |
| 63 | + | char *passwd; | |
| 58 | 64 | } login; | |
| 59 | 65 | struct { | |
| 60 | 66 | char *hostname; | |
| 61 | 67 | signed int port; | |
| 62 | 68 | } hostport; | |
| 63 | 69 | char *path; | |
| 64 | 70 | } url_parser_context; | |
| 65 | 71 | ||
| 66 | - | url_parser_context *url_parser_create_context(const char *in_url) | |
| 72 | + | url_parser_context *url_parser_create_context(const char *in_url, unsigned int flags) | |
| 67 | 73 | { | |
| 68 | 74 | url_parser_context *uctx; | |
| 69 | 75 | char *s; | |
| 70 | 76 | size_t in_url_len; | |
| 71 | 77 | size_t context_len; | |
| 72 | 78 | ||
| 73 | 79 | if (!in_url) | |
| 74 | 80 | return NULL; | |
| 75 | 81 | ||
| 76 | 82 | in_url_len = strlen(in_url); | |
| 77 | 83 | ||
| 78 | 84 | context_len = sizeof(url_parser_context) + | |
| 79 | 85 | ((in_url_len+1)*5); | |
| 80 | 86 | uctx = malloc(context_len); | |
| 81 | 87 | if (!uctx) | |
| 82 | 88 | return NULL; | |
| 83 | 89 | ||
| 84 | 90 | s = (void *)(uctx+1); | |
| 85 | - | uctx->in_url = s; s+= in_url_len+1; | |
| 91 | + | uctx->in_url = s; s+= in_url_len+1; | |
| 86 | 92 | (void)strcpy(uctx->in_url, in_url); | |
| 87 | - | uctx->scheme = s; s+= in_url_len+1; | |
| 93 | + | uctx->scheme = s; s+= in_url_len+1; | |
| 88 | - | uctx->login.login = s; s+= in_url_len+1; | |
| 94 | + | uctx->login.username = s; s+= in_url_len+1; | |
| 89 | 95 | uctx->hostport.hostname = s; s+= in_url_len+1; | |
| 90 | - | uctx->path = s; s+= in_url_len+1; | |
| 96 | + | uctx->path = s; s+= in_url_len+1; | |
| 97 | + | uctx->hostport.port = -1; | |
| 91 | 98 | ||
| 92 | 99 | return uctx; | |
| 93 | 100 | } | |
| 94 | 101 | ||
| 95 | - | int url_parser(url_parser_context *uctx) | |
| 102 | + | int url_parser_parse(url_parser_context *uctx) | |
| 96 | 103 | { | |
| 97 | 104 | (void)fprintf(stderr, "## parser in_url='%s'\n", uctx->in_url); | |
| 98 | 105 | ||
| 99 | 106 | char *s; | |
| 100 | 107 | const char *urlstr = uctx->in_url; | |
| 101 | 108 | size_t slen; | |
| 102 | 109 | ||
| 103 | 110 | s = strstr(urlstr, "://"); | |
| 104 | 111 | if (!s) { | |
| 105 | 112 | (void)fprintf(stderr, "url_parser: Not an URL\n"); | |
| 106 | 113 | return -1; | |
| 107 | 114 | } | |
| 108 | 115 | ||
| 109 | 116 | slen = s-urlstr; | |
| 110 | 117 | (void)memcpy(uctx->scheme, urlstr, slen); | |
| 111 | 118 | uctx->scheme[slen] = '\0'; | |
| 112 | 119 | urlstr += slen + 3; | |
| 113 | 120 | ||
| 114 | 121 | (void)fprintf(stdout, "scheme='%s', rest='%s'\n", uctx->scheme, urlstr); | |
| 115 | 122 | ||
| 116 | 123 | s = strstr(urlstr, "@"); | |
| 117 | 124 | if (s) { | |
| 118 | 125 | /* URL has user/password */ | |
| 119 | 126 | slen = s-urlstr; | |
| 120 | - | (void)memcpy(uctx->login.login, urlstr, slen); | |
| 127 | + | (void)memcpy(uctx->login.username, urlstr, slen); | |
| 121 | - | uctx->login.login[slen] = '\0'; | |
| 128 | + | uctx->login.username[slen] = '\0'; | |
| 122 | 129 | urlstr += slen + 1; | |
| 123 | 130 | ||
| 124 | - | (void)fprintf(stdout, "login='%s', rest='%s'\n", uctx->login.login, urlstr); | |
| 131 | + | s = strstr(uctx->login.username, ":"); | |
| 132 | + | if (s) { | |
| 125 | 136 | } | |
| 126 | 137 | else | |
| 127 | 138 | { | |
| 128 | - | uctx->login.login = NULL; | |
| 139 | + | uctx->login.passwd = NULL; | |
| 129 | 140 | } | |
| 130 | 141 | ||
| 131 | - | s = strstr(urlstr, "/"); | |
| 142 | + | /* catch password-only URLs */ | |
| 132 | - | if (s) { | |
| 143 | + | if (uctx->login.username[0] == '\0') | |
| 133 | - | /* URL has hostport */ | |
| 144 | + | uctx->login.username = NULL; | |
| 134 | - | slen = s-urlstr; | |
| 145 | + | } | |
| 135 | - | (void)memcpy(uctx->hostport.hostname, urlstr, slen); | |
| 146 | + | else | |
| 136 | - | uctx->hostport.hostname[slen] = '\0'; | |
| 147 | + | { | |
| 137 | - | urlstr += slen + 1; | |
| 148 | + | uctx->login.username = NULL; | |
| 149 | + | } | |
| 138 | 150 | ||
| 139 | - | (void)fprintf(stdout, "hostport='%s', rest='%s'\n", uctx->hostport.hostname, urlstr); | |
| 151 | + | (void)fprintf(stdout, "login='%s', passwd='%s', rest='%s'\n", | |
| 152 | + | DBGNULLSTR(uctx->login.username), | |
| 140 | 155 | ||
| 156 | + | s = strstr(urlstr, "/"); | |
| 142 | 157 | if (s) { | |
| 158 | + | /* URL has hostport */ | |
| 143 | 166 | /* found port number */ | |
| 144 | 167 | uctx->hostport.port = atoi(s+1); | |
| 145 | 168 | *s = '\0'; | |
| 146 | 169 | } | |
| 170 | + | } | |
| 147 | 171 | else | |
| 148 | 172 | { | |
| 149 | - | uctx->hostport.port = -1; | |
| 173 | + | (void)strcpy(uctx->hostport.hostname, urlstr); | |
| 174 | + | uctx->path = NULL; | |
| 150 | 176 | } | |
| 151 | 177 | ||
| 178 | + | (void)fprintf(stdout, "hostport='%s', port=%d, rest='%s'\n", |
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