1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 #ifndef GENLIB_NET_URI_H
33 #define GENLIB_NET_URI_H
34 
35 #include <string>
36 #include <vector>
37 
38 #include "UpnpGlobal.h"
39 #include "UpnpInet.h"
40 
41 #include <string.h>
42 #include <sys/types.h>
43 
44 #ifdef _WIN32
45 #include <Winsock2.h>
46 #else
47 #include <sys/socket.h>
48 #endif
49 
50 /*!
51  * \brief Represents a host port: e.g. "127.127.0.1:80", "www.recoll.org"
52 */
53 struct hostport_type {
hostport_typehostport_type54     hostport_type() {
55         IPaddress = {};
56     }
57     /*! Full "host:port" or "host" text. This is mostly useful when it is
58       separated from the rest of an URL parse_hostport() */
59     std::string text;
60     /*! Separate host copy: original string value, before a possible name resolution. */
61     std::string strhost;
62     /*! Set to true by parse_hostport if strhost is a host name instead of an IP address */
63     bool hostisname{false};
64     /*! Possibly empty separated port string */
65     std::string strport;
66     /* Address as computed by inet_pton() from a numeric address or
67      * getaddrinfo() from a host name. May not be set if
68      * parse_hostport was called with noresolve==true and we had a
69      * host name */
70     struct sockaddr_storage IPaddress;
71 };
72 
73 /*!
74  * \brief Parse a string representing a host and port (e.g. "127.127.0.1:80"
75  * or "localhost"), *possibly followed by the rest of an URL* and fill
76  * out a hostport_type struct.
77  */
78 int parse_hostport(
79     /*! [in] String of characters representing host and port, e.g. 192.168.4.1:49152,
80       [fe80::224:1dff:fede:6868]:49152, www.recoll.org */
81     const char *in,
82     /*! [out] Parsed output. Validated syntax, separate host, port and host:port strings,
83      *  possibly computed binary  address. */
84     hostport_type *out,
85     /*! [in] Do not call the resolver if the input contains a host name */
86     bool noresolve = false
87     );
88 
89 enum uriType  {
90     URITP_ABSOLUTE,
91     URITP_RELATIVE
92 };
93 
94 enum pathType {
95     ABS_PATH,
96     REL_PATH,
97     OPAQUE_PART
98 };
99 
100 /*!
101  * \brief Represents a URI used in parse_uri and elsewhere
102  */
103 struct uri_type {
104     enum uriType type;
105     std::string scheme;
106     enum pathType path_type;
107     std::string path;
108     std::string query;
109     std::string fragment;
110     hostport_type hostport;
111 };
112 
uri_asurlstr(const uri_type & u)113 inline std::string uri_asurlstr(const uri_type& u)
114 {
115     std::string surl(u.scheme);
116     if (!u.scheme.empty()) {
117         surl += ":";
118     }
119     if (!u.hostport.text.empty()) {
120         surl += "//";
121         surl += u.hostport.text;
122     }
123     if (u.path.empty())
124         surl += "/";
125     else
126         surl += u.path;
127     if (!u.query.empty()) {
128         surl += "?";
129         surl += u.query;
130     }
131     return surl;
132 }
133 
134 /*!
135  * Removes http escaped characters such as: "%20" and replaces them with
136  * their character representation. i.e. "hello%20foo" -> "hello foo".
137  */
138 std::string remove_escaped_chars(const std::string& in);
139 
140 /* Removes ".", and ".." from a path.
141  *
142  * If a ".." can not be resolved (i.e. the .. would go past the root of the
143  * path) an error is returned as an empty string.
144  */
145 std::string remove_dots(const std::string& in);
146 
147 /*!
148  * \brief resolves a relative url with a base url returning a new url
149  *
150  * If the base_url is empty, then a copy of the  rel_url is passed back if
151  * the rel_url is absolute then a copy of the rel_url is passed back if neither
152  * the base nor the rel_url are Absolute then NULL is returned. Otherwise it
153  * tries and resolves the relative url with the base as described in
154  * http://www.ietf.org/rfc/rfc2396.txt (RFCs explaining URIs).
155  *
156  */
157 std::string resolve_rel_url(const std::string& base, const std::string& rel);
158 
159 /*!
160  * \brief Parses a uri as defined in http://www.ietf.org/rfc/rfc2396.txt
161  * (RFC explaining URIs).
162  *
163  * Handles absolute, relative, and opaque uris. Parses into the following
164  * pieces: scheme, hostport, pathquery, fragment (path and query are treated
165  * as one token)
166  *
167  * Caller should check for the pieces they require.
168  *
169  * \return UPNP_E_SUCCESS / UPNP_E_OTHER
170  */
171 int parse_uri(const std::string& in, uri_type *out);
172 
173 /* Possibly qualify the address part of the URL with a scope id, if needed */
174 std::string maybeScopeUrlAddr(const char *inurl, const struct sockaddr_storage *remoteaddr);
175 std::string maybeScopeUrlAddr(
176     const char *inurl, uri_type& prsduri, const struct sockaddr_storage *remoteaddr);
177 
178 #endif /* GENLIB_NET_URI_H */
179