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