1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2  *@ UniformResourceLocator.
3  *
4  * Copyright (c) 2014 - 2020 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
5  * SPDX-License-Identifier: ISC
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #ifndef mx_URL_H
20 #define mx_URL_H
21 
22 #include <mx/nail.h>
23 
24 #define mx_HEADER
25 #include <su/code-in.h>
26 
27 struct mx_url;
28 
29 #ifdef mx_HAVE_NET
30 enum mx_url_flags{
31    mx_URL_TLS_REQUIRED = 1u<<0, /* Whether protocol always uses SSL/TLS.. */
32    mx_URL_TLS_OPTIONAL = 1u<<1, /* ..may later upgrade to SSL/TLS */
33    mx_URL_TLS_MASK = mx_URL_TLS_REQUIRED | mx_URL_TLS_OPTIONAL,
34    mx_URL_HAD_USER = 1u<<2, /* Whether .url_user was part of the URL */
35    mx_URL_HOST_IS_NAME = 1u<<3 /* .url_host not numeric address */
36 };
37 #endif
38 
39 struct mx_url{ /* XXX not _ctx: later object */
40    char const *url_input; /* Input as given (really) */
41    u32 url_flags;
42    u16 url_portno; /* atoi .url_port or default, host endian */
43    u8 url_cproto; /* enum cproto as given */
44    u8 url_proto_len; /* Length of .url_proto (to first '\0') */
45 #ifdef mx_HAVE_NET
46    char url_proto[16]; /* Communication protocol as 'xy\0://\0' */
47    char const *url_port; /* Port (if given) or NIL */
48    struct str url_user; /* User, exactly as given / looked up */
49    struct str url_user_enc; /* User, url_xenc()oded */
50    struct str url_pass; /* Pass (url_xdec()oded) or NULL */
51    /* TODO we don't know whether .url_host is a name or an address.  Us
52     * TODO Net::IPAddress::fromString() to check that, then set
53     * TODO URL_HOST_IS_NAME solely based on THAT!  Until then,
54     * TODO URL_HOST_IS_NAME ONLY set if n_URL_TLS_MASK+mx_HAVE_GETADDRINFO */
55    struct str url_host; /* Service hostname TODO we don't know */
56    struct str url_path; /* Path suffix or NULL */
57    /* TODO: url_get_component(url *, enum COMPONENT, str *store) */
58    struct str url_h_p; /* .url_host[:.url_port] */
59    /* .url_user@.url_host
60     * Note: for CPROTO_SMTP this may resolve HOST via *smtp-hostname* (->
61     * *hostname*)!  (And may later be overwritten according to *from*!) */
62    struct str url_u_h;
63    struct str url_u_h_p; /* .url_user@.url_host[:.url_port] */
64    struct str url_eu_h_p; /* .url_user_enc@.url_host[:.url_port] */
65    char const *url_p_u_h_p; /* .url_proto://.url_u_h_p */
66    char const *url_p_eu_h_p; /* .url_proto://.url_eu_h_p */
67    char const *url_p_eu_h_p_p; /* .url_proto://.url_eu_h_p[/.url_path] */
68 #endif /* mx_HAVE_NET */
69 };
70 
71 /* URL en- and decoding according to (enough of) RFC 3986 (RFC 1738).
72  * These return a newly autorec_alloc()ated result, or NIL on length excess */
73 EXPORT char *mx_url_xenc(char const *cp, boole ispath  su_DBG_LOC_ARGS_DECL);
74 EXPORT char *mx_url_xdec(char const *cp  su_DBG_LOC_ARGS_DECL);
75 
76 #ifdef su_HAVE_DBG_LOC_ARGS
77 # define mx_url_xenc(CP,P) mx_url_xenc(CP, P  su_DBG_LOC_ARGS_INJ)
78 # define mx_url_xdec(CP) mx_url_xdec(CP  su_DBG_LOC_ARGS_INJ)
79 #endif
80 
81 /* `urlcodec' */
82 EXPORT int c_urlcodec(void *vp);
83 
84 /* Parse a RFC 6058 'mailto' URI to a single to: (TODO yes, for now hacky).
85  * Return NIL or something that can be converted to a struct mx_name */
86 EXPORT char *mx_url_mailto_to_address(char const *mailtop);
87 
88 /* Return port for proto, or NIL if unknown.
89  * Upon success *port_or_nil and *issnd_or_nil will be updated, if set; the
90  * latter states whether protocol is of a sending type (SMTP, file etc.).
91  * For file:// and test:// this returns su_empty, in the former case
92  * *port_or_nil is 0 and in the latter U16_MAX */
93 EXPORT char const *mx_url_servbyname(char const *proto, u16 *port_or_nil,
94       boole *issnd_or_nil);
95 
96 /* Parse data, which must meet the criteria of the protocol cproto, and fill
97  * in the URL structure urlp (URL rather according to RFC 3986) */
98 #ifdef mx_HAVE_NET
99 EXPORT boole mx_url_parse(struct mx_url *urlp, enum cproto cproto,
100       char const *data);
101 #endif /* mx_HAVE_NET */
102 
103 #include <su/code-ou.h>
104 #endif /* mx_URL_H */
105 /* s-it-mode */
106