NeoMutt  2025-12-11-1073-gf33cdb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
url.c File Reference

Parse and identify different URL schemes. More...

#include "config.h"
#include <stdbool.h>
#include <string.h>
#include "mutt/lib.h"
#include "url.h"
#include "mime.h"
Include dependency graph for url.c:

Go to the source code of this file.

Functions

static bool parse_query_string (struct UrlQueryList *list, char *src)
 Parse a URL query string.
static enum UrlScheme get_scheme (const char *src, const regmatch_t *match)
 Extract the scheme part from a matched URL.
static struct Url * url_new (void)
 Create a Url.
void url_free (struct Url **ptr)
 Free the contents of a URL.
void url_pct_encode (char *buf, size_t buflen, const char *src)
 Percent-encode a string.
int url_pct_decode (char *s)
 Decode a percent-encoded string.
enum UrlScheme url_check_scheme (const char *str)
 Check the protocol of a URL.
struct Url * url_parse (const char *src)
 Fill in Url.
int url_tobuffer (const struct Url *url, struct Buffer *buf, uint8_t flags)
 Output the URL string for a given Url object.
int url_tostring (const struct Url *url, char *dest, size_t len, uint8_t flags)
 Output the URL string for a given Url object.

Variables

static const struct Mapping UrlMap []
 Constants for URL protocols.

Detailed Description

Parse and identify different URL schemes.

Authors
  • Richard Russon
  • Pietro Cerutti
  • Thomas Klausner

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file url.c.

Function Documentation

◆ parse_query_string()

bool parse_query_string ( struct UrlQueryList * list,
char * src )
static

Parse a URL query string.

Parameters
listList to store the results
srcString to parse
Return values
trueSuccess
falseError

Definition at line 56 of file url.c.

57{
58 if (!src || (*src == '\0'))
59 return false;
60
61 bool again = true;
62 while (again)
63 {
64 regmatch_t *match = mutt_prex_capture(PREX_URL_QUERY_KEY_VAL, src);
65 if (!match)
66 return false;
67
68 regmatch_t *mkey = &match[PREX_URL_QUERY_KEY_VAL_MATCH_KEY];
69 regmatch_t *mval = &match[PREX_URL_QUERY_KEY_VAL_MATCH_VAL];
70
71 again = src[mutt_regmatch_end(mval)] != '\0';
72
73 char *key = src + mutt_regmatch_start(mkey);
74 char *val = src + mutt_regmatch_start(mval);
75 src[mutt_regmatch_end(mkey)] = '\0';
76 src[mutt_regmatch_end(mval)] = '\0';
77 if ((url_pct_decode(key) < 0) || (url_pct_decode(val) < 0))
78 return false;
79
80 struct UrlQuery *qs = MUTT_MEM_CALLOC(1, struct UrlQuery);
81 qs->name = key;
82 qs->value = val;
83 STAILQ_INSERT_TAIL(list, qs, entries);
84
85 src += mutt_regmatch_end(mval) + again;
86 }
87
88 return true;
89}
#define MUTT_MEM_CALLOC(n, T)
Definition memory.h:68
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition prex.c:301
@ PREX_URL_QUERY_KEY_VAL_MATCH_VAL
key=[val]
Definition prex.h:85
@ PREX_URL_QUERY_KEY_VAL_MATCH_KEY
[key]=val
Definition prex.h:84
@ PREX_URL_QUERY_KEY_VAL
https://example.com/?[q=foo]
Definition prex.h:35
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
static regoff_t mutt_regmatch_end(const regmatch_t *match)
Return the end of a match.
Definition regex3.h:66
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition regex3.h:56
Parsed Query String.
Definition url.h:58
char * name
Query name.
Definition url.h:59
char * value
Query value.
Definition url.h:60
int url_pct_decode(char *s)
Decode a percent-encoded string.
Definition url.c:195
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_scheme()

enum UrlScheme get_scheme ( const char * src,
const regmatch_t * match )
static

Extract the scheme part from a matched URL.

Parameters
srcOriginal string that was matched
matchResult from a matched regex
Return values
enumScheme

Definition at line 97 of file url.c.

98{
99 enum UrlScheme rc = U_UNKNOWN;
100 if (src && match)
101 {
103 if (rc == -1)
104 rc = U_UNKNOWN;
105 }
106 return rc;
107}
int mutt_map_get_value_n(const char *name, size_t len, const struct Mapping *map)
Lookup the constant for a string.
Definition mapping.c:62
@ PREX_URL_MATCH_SCHEME
[imaps]://...
Definition prex.h:55
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition regex3.h:76
static const struct Mapping UrlMap[]
Constants for URL protocols.
Definition url.c:41
UrlScheme
All recognised Url types.
Definition url.h:34
@ U_UNKNOWN
Url wasn't recognised.
Definition url.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_new()

struct Url * url_new ( void )
static

Create a Url.

Return values
ptrNew Url

Definition at line 113 of file url.c.

114{
115 struct Url *url = MUTT_MEM_CALLOC(1, struct Url);
117 return url;
118}
#define STAILQ_INIT(head)
Definition queue.h:410
A parsed URL proto://user:password@host:port/path?a=1&b=2.
Definition url.h:69
struct UrlQueryList query_strings
List of query strings.
Definition url.h:76
Here is the caller graph for this function:

◆ url_free()

void url_free ( struct Url ** ptr)

Free the contents of a URL.

Parameters
ptrUrl to free

Definition at line 124 of file url.c.

125{
126 if (!ptr || !*ptr)
127 return;
128
129 struct Url *url = *ptr;
130
131 struct UrlQueryList *l = &url->query_strings;
132 while (!STAILQ_EMPTY(l))
133 {
134 struct UrlQuery *np = STAILQ_FIRST(l);
135 STAILQ_REMOVE_HEAD(l, entries);
136 // Don't free 'name', 'value': they are pointers into the 'src' string
137 FREE(&np);
138 }
139
140 FREE(&url->src);
141 FREE(ptr);
142}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:92
#define STAILQ_REMOVE_HEAD(head, field)
Definition queue.h:461
#define STAILQ_FIRST(head)
Definition queue.h:388
#define STAILQ_EMPTY(head)
Definition queue.h:382
char * src
Raw URL string.
Definition url.h:77
Here is the caller graph for this function:

◆ url_pct_encode()

void url_pct_encode ( char * buf,
size_t buflen,
const char * src )

Percent-encode a string.

Parameters
bufBuffer for the result
buflenLength of buffer
srcString to encode

e.g. turn "hello world" into "hello%20world"

Control characters, the URL delimiters and the characters that RFC3986 doesn't allow anywhere in a URL are encoded, so that url_parse() can read the result back, e.g. an Exchange user name "domain\user".

Definition at line 156 of file url.c.

157{
158 static const char *hex = "0123456789ABCDEF";
159
160 if (!buf)
161 return;
162
163 *buf = '\0';
164 buflen--;
165 while (src && *src && (buflen != 0))
166 {
167 const unsigned char c = *src;
168 if ((c < 0x20) || (c == 0x7f) || strchr(" \"#%&/:<=>?\\^`{|}", c))
169 {
170 if (buflen < 3)
171 break;
172
173 *buf++ = '%';
174 *buf++ = hex[(c >> 4) & 0xf];
175 *buf++ = hex[c & 0xf];
176 src++;
177 buflen -= 3;
178 continue;
179 }
180 *buf++ = *src++;
181 buflen--;
182 }
183 *buf = '\0';
184}
Here is the caller graph for this function:

◆ url_pct_decode()

int url_pct_decode ( char * s)

Decode a percent-encoded string.

Parameters
sString to decode
Return values
0Success
-1Error

e.g. turn "hello%20world" into "hello world" The string is decoded in-place.

Definition at line 195 of file url.c.

196{
197 if (!s)
198 return -1;
199
200 char *d = NULL;
201
202 for (d = s; *s; s++)
203 {
204 if (*s == '%')
205 {
206 if ((s[1] != '\0') && (s[2] != '\0') && mutt_isxdigit(s[1]) &&
207 mutt_isxdigit(s[2]) && (hexval(s[1]) >= 0) && (hexval(s[2]) >= 0))
208 {
209 unsigned char c = (hexval(s[1]) << 4) | (hexval(s[2]));
210 if (c == '\0')
211 return -1;
212 *d++ = c;
213 s += 2;
214 }
215 else
216 {
217 return -1;
218 }
219 }
220 else
221 {
222 *d++ = *s;
223 }
224 }
225 *d = '\0';
226 return 0;
227}
bool mutt_isxdigit(int arg)
Wrapper for isxdigit(3).
Definition ctype.c:111
#define hexval(ch)
Convert hexadecimal character to its integer value.
Definition mime.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_check_scheme()

enum UrlScheme url_check_scheme ( const char * str)

Check the protocol of a URL.

Parameters
strString to check
Return values
enumUrlScheme, e.g. U_IMAPS

Definition at line 234 of file url.c.

235{
236 return get_scheme(str, mutt_prex_capture(PREX_URL, str));
237}
@ PREX_URL
[imaps://user:pass@example.com/INBOX?foo=bar]
Definition prex.h:34
static enum UrlScheme get_scheme(const char *src, const regmatch_t *match)
Extract the scheme part from a matched URL.
Definition url.c:97
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_parse()

struct Url * url_parse ( const char * src)

Fill in Url.

Parameters
srcString to parse
Return values
ptrParsed URL
NULLError
Note
Caller must free returned Url with url_free()

Definition at line 247 of file url.c.

248{
249 const regmatch_t *match = mutt_prex_capture(PREX_URL, src);
250 if (!match)
251 return NULL;
252
253 enum UrlScheme scheme = get_scheme(src, match);
254 if (scheme == U_UNKNOWN)
255 return NULL;
256
257 const regmatch_t *userinfo = &match[PREX_URL_MATCH_USERINFO];
258 const regmatch_t *user = &match[PREX_URL_MATCH_USER];
259 const regmatch_t *pass = &match[PREX_URL_MATCH_PASS];
260 const regmatch_t *host = &match[PREX_URL_MATCH_HOSTNAME];
261 const regmatch_t *ipvx = &match[PREX_URL_MATCH_HOSTIPVX];
262 const regmatch_t *port = &match[PREX_URL_MATCH_PORT];
263 const regmatch_t *path = &match[PREX_URL_MATCH_PATH];
264 const regmatch_t *query = &match[PREX_URL_MATCH_QUERY];
265 const regmatch_t *pathonly = &match[PREX_URL_MATCH_PATH_ONLY];
266
267 struct Url *url = url_new();
268 url->scheme = scheme;
269 url->src = mutt_str_dup(src);
270
271 /* If the scheme is not followed by two forward slashes, then it's a simple
272 * path (see https://tools.ietf.org/html/rfc3986#section-3). */
273 if (mutt_regmatch_start(pathonly) != -1)
274 {
275 url->src[mutt_regmatch_end(pathonly)] = '\0';
276 url->path = url->src + mutt_regmatch_start(pathonly);
277 if (url_pct_decode(url->path) < 0)
278 goto err;
279 }
280
281 /* separate userinfo part */
282 if (mutt_regmatch_end(userinfo) != -1)
283 {
284 url->src[mutt_regmatch_end(userinfo) - 1] = '\0';
285 }
286
287 /* user */
288 if (mutt_regmatch_end(user) != -1)
289 {
290 url->src[mutt_regmatch_end(user)] = '\0';
291 url->user = url->src + mutt_regmatch_start(user);
292 if (url_pct_decode(url->user) < 0)
293 goto err;
294 }
295
296 /* pass */
297 if (mutt_regmatch_end(pass) != -1)
298 {
299 url->pass = url->src + mutt_regmatch_start(pass);
300 if (url_pct_decode(url->pass) < 0)
301 goto err;
302 }
303
304 /* host */
305 if (mutt_regmatch_len(host) != 0)
306 {
307 url->host = url->src + mutt_regmatch_start(host);
308 url->src[mutt_regmatch_end(host)] = '\0';
309 }
310 else if (mutt_regmatch_end(ipvx) != -1)
311 {
312 url->host = url->src + mutt_regmatch_start(ipvx) + 1; /* skip opening '[' */
313 url->src[mutt_regmatch_end(ipvx) - 1] = '\0'; /* skip closing ']' */
314 }
315
316 /* port */
317 if (mutt_regmatch_end(port) != -1)
318 {
319 url->src[mutt_regmatch_end(port)] = '\0';
320 const char *ports = url->src + mutt_regmatch_start(port);
321 unsigned short num = 0;
322 if (!mutt_str_atous_full(ports, &num))
323 {
324 goto err;
325 }
326 url->port = num;
327 }
328
329 /* path */
330 if (mutt_regmatch_end(path) != -1)
331 {
332 url->src[mutt_regmatch_end(path)] = '\0';
333 url->path = url->src + mutt_regmatch_start(path);
334 if (!url->host)
335 {
336 /* If host is not provided, restore the '/': this is an absolute path */
337 *(--url->path) = '/';
338 }
339 if (url_pct_decode(url->path) < 0)
340 goto err;
341 }
342
343 /* query */
344 if (mutt_regmatch_end(query) != -1)
345 {
346 char *squery = url->src + mutt_regmatch_start(query);
347 if (!parse_query_string(&url->query_strings, squery))
348 goto err;
349 }
350
351 return url;
352
353err:
354 url_free(&url);
355 return NULL;
356}
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
@ PREX_URL_MATCH_USER
...//[user]:pass@...
Definition prex.h:60
@ PREX_URL_MATCH_QUERY
...Inbox?[foo=bar&baz=value]
Definition prex.h:72
@ PREX_URL_MATCH_HOSTNAME
imaps://...[host.com]...
Definition prex.h:64
@ PREX_URL_MATCH_PORT
imaps://host.com:[993]/...
Definition prex.h:67
@ PREX_URL_MATCH_PATH_ONLY
mailto:[me@example.com]?foo=bar
Definition prex.h:70
@ PREX_URL_MATCH_USERINFO
...//[user:pass@]...
Definition prex.h:59
@ PREX_URL_MATCH_PATH
...:993/[Inbox]
Definition prex.h:69
@ PREX_URL_MATCH_HOSTIPVX
imaps://...[127.0.0.1]...
Definition prex.h:65
@ PREX_URL_MATCH_PASS
...//user:[pass]@...
Definition prex.h:62
char * user
Username.
Definition url.h:71
unsigned short port
Port.
Definition url.h:74
char * host
Host.
Definition url.h:73
char * pass
Password.
Definition url.h:72
char * path
Path.
Definition url.h:75
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
Definition url.h:70
static bool parse_query_string(struct UrlQueryList *list, char *src)
Parse a URL query string.
Definition url.c:56
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition url.c:124
static struct Url * url_new(void)
Create a Url.
Definition url.c:113
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_tobuffer()

int url_tobuffer ( const struct Url * url,
struct Buffer * buf,
uint8_t flags )

Output the URL string for a given Url object.

Parameters
urlUrl to turn into a string
bufBuffer for the result
flagsFlags, e.g. U_PATH
Return values
0Success
-1Error

Definition at line 366 of file url.c.

367{
368 if (!url || !buf)
369 return -1;
370 if (url->scheme == U_UNKNOWN)
371 return -1;
372
373 buf_printf(buf, "%s:", mutt_map_get_name(url->scheme, UrlMap));
374
375 if (url->host)
376 {
377 if (!(flags & U_PATH))
378 buf_addstr(buf, "//");
379
380 if (url->user && (url->user[0] || !(flags & U_PATH)))
381 {
382 char str[256] = { 0 };
383 url_pct_encode(str, sizeof(str), url->user);
384 buf_add_printf(buf, "%s@", str);
385 }
386
387 if (strchr(url->host, ':'))
388 buf_add_printf(buf, "[%s]", url->host);
389 else
390 buf_addstr(buf, url->host);
391
392 if (url->port)
393 buf_add_printf(buf, ":%hu/", url->port);
394 else
395 buf_addstr(buf, "/");
396 }
397
398 if (url->path)
399 buf_addstr(buf, url->path);
400
401 if (STAILQ_FIRST(&url->query_strings))
402 {
403 buf_addstr(buf, "?");
404
405 char str[256] = { 0 };
406 struct UrlQuery *np = NULL;
407 STAILQ_FOREACH(np, &url->query_strings, entries)
408 {
409 url_pct_encode(str, sizeof(str), np->name);
410 buf_addstr(buf, str);
411 buf_addstr(buf, "=");
412 url_pct_encode(str, sizeof(str), np->value);
413 buf_addstr(buf, str);
414 if (STAILQ_NEXT(np, entries))
415 buf_addstr(buf, "&");
416 }
417 }
418
419 return 0;
420}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:168
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:211
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:233
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition mapping.c:42
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
void url_pct_encode(char *buf, size_t buflen, const char *src)
Percent-encode a string.
Definition url.c:156
#define U_PATH
Path is included in URL.
Definition url.h:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_tostring()

int url_tostring ( const struct Url * url,
char * dest,
size_t len,
uint8_t flags )

Output the URL string for a given Url object.

Parameters
urlUrl to turn into a string
destBuffer for the result
lenLength of buffer
flagsFlags, e.g. U_PATH
Return values
0Success
-1Error

Definition at line 431 of file url.c.

432{
433 if (!url || !dest)
434 return -1;
435
436 struct Buffer *dest_buf = buf_pool_get();
437
438 int rc = url_tobuffer(url, dest_buf, flags);
439 if (rc == 0)
440 mutt_str_copy(dest, buf_string(dest_buf), len);
441
442 buf_pool_release(&dest_buf);
443
444 return rc;
445}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:99
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination).
Definition string.c:587
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:37
int url_tobuffer(const struct Url *url, struct Buffer *buf, uint8_t flags)
Output the URL string for a given Url object.
Definition url.c:366
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ UrlMap

const struct Mapping UrlMap[]
static
Initial value:
= {
{ "file", U_FILE }, { "imap", U_IMAP }, { "imaps", U_IMAPS },
{ "pop", U_POP }, { "pops", U_POPS }, { "news", U_NNTP },
{ "nntp", U_NNTP }, { "snews", U_NNTPS }, { "nntps", U_NNTPS },
{ "mailto", U_MAILTO }, { "notmuch", U_NOTMUCH }, { "smtp", U_SMTP },
{ "smtps", U_SMTPS }, { NULL, U_UNKNOWN },
}
@ U_NOTMUCH
Url is notmuch://.
Definition url.h:46
@ U_FILE
Url is file://.
Definition url.h:36
@ U_NNTPS
Url is nntps://.
Definition url.h:42
@ U_MAILTO
Url is mailto://.
Definition url.h:45
@ U_SMTPS
Url is smtps://.
Definition url.h:44
@ U_SMTP
Url is smtp://.
Definition url.h:43
@ U_NNTP
Url is nntp://.
Definition url.h:41
@ U_IMAP
Url is imap://.
Definition url.h:39
@ U_POPS
Url is pops://.
Definition url.h:38
@ U_IMAPS
Url is imaps://.
Definition url.h:40
@ U_POP
Url is pop://.
Definition url.h:37

Constants for URL protocols.

Definition at line 41 of file url.c.

41 {
42 { "file", U_FILE }, { "imap", U_IMAP }, { "imaps", U_IMAPS },
43 { "pop", U_POP }, { "pops", U_POPS }, { "news", U_NNTP },
44 { "nntp", U_NNTP }, { "snews", U_NNTPS }, { "nntps", U_NNTPS },
45 { "mailto", U_MAILTO }, { "notmuch", U_NOTMUCH }, { "smtp", U_SMTP },
46 { "smtps", U_SMTPS }, { NULL, U_UNKNOWN },
47};