Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
Url.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Url.h
8  *
9  * @author Feb 2019 mikee47 <mike@sillyhouse.net>
10  *
11  * Support added for escaped URLs and standard schemes.
12  *
13  * @see https://en.m.wikipedia.org/wiki/URL
14  * @see https://en.m.wikipedia.org/wiki/URL_normalization
15  *
16  ****/
17 
25 #ifndef _SMING_CORE_NETWORK_URL_H_
26 #define _SMING_CORE_NETWORK_URL_H_
27 
28 #include "WString.h"
29 #include "Http/HttpParams.h"
30 
31 /*
32  * Common URL schemes (name, scheme, port)
33  * name: Used for string identifier
34  * scheme: String to be used in a URL, normalised (i.e. lower case)
35  * port: Offical default port definition for the scheme, 0 if not applicable
36  */
37 #define URI_SCHEME_MAP(XX) \
38  XX(HTTP, http, 80) \
39  XX(HTTP_SECURE, https, 443) \
40  XX(WEBSOCKET, ws, 80) \
41  XX(WEBSOCKET_SECURE, wss, 443) \
42  XX(MQTT, mqtt, 1883) \
43  XX(MQTT_SECURE, mqtts, 8883) \
44  XX(SMTP, smtp, 25) \
45  XX(SMTP_SECURE, smtps, 465) \
46  XX(FTP, ftp, 21) \
47  XX(MAIL_TO, mailto, 0) \
48  XX(FILE, file, 0) \
49  XX(DATA, data, 0)
50 
54 #define XX(name, str, port) DECLARE_FSTR(URI_SCHEME_##name)
55 URI_SCHEME_MAP(XX)
56 #undef XX
57 
58 class Print;
59 
66 class Url
67 {
68 public:
69  Url() = default;
70 
71  Url(const Url& url) = default;
72 
76  Url(const String& urlString)
77  {
78  *this = urlString;
79  }
80 
84  Url(const char* urlString) : Url(String(urlString))
85  {
86  }
87 
88  Url(const String& scheme, const String& user, const String& password, const String& host, int port = 0,
89  const String& path = nullptr, const String& query = nullptr, const String& fragment = nullptr)
90  : Scheme(scheme), User(user), Password(password), Host(host), Port(port), Path(path), Query(query),
91  Fragment(fragment)
92  {
93  Scheme.toLowerCase();
94  if(Port == 0) {
96  }
97  }
98 
103  Url& operator=(String urlString);
104 
108  Url& operator=(const char* urlString)
109  {
110  *this = String(urlString);
111  return *this;
112  }
113 
117  String toString() const;
118 
119  operator String() const
120  {
121  return toString();
122  }
123 
127  static int getDefaultPort(const String& scheme);
128 
133  int getPort() const
134  {
135  return Port ?: getDefaultPort(Scheme);
136  }
137 
142  String getHostWithPort() const;
143 
148  {
149  return (Path[0] == '/') ? Path.substring(1) : Path;
150  }
151 
156  String getPathWithQuery() const;
157 
161  String getFileName() const;
162 
167  void debugPrintTo(Print& p) const;
168 
169 public:
171  String User;
172  String Password;
174  int Port = 0;
176  HttpParams Query;
178 };
179 
180 typedef Url URL SMING_DEPRECATED;
181 
183 #endif /* _SMING_CORE_NETWORK_URL_H_ */
Url & operator=(const char *urlString)
Copy assignment operator, for C-style strings.
Definition: Url.h:108
Class to manage URL instance.
Definition: Url.h:66
Definition: HttpParams.h:32
String Scheme
without ":" and "//"
Definition: Url.h:170
The string class.
Definition: WString.h:104
String Path
with leading "/"
Definition: Url.h:175
Url URL SMING_DEPRECATED
Definition: Url.h:180
Provides formatted output to stream.
Definition: Print.h:31
#define XX(name, str, port)
Common URI scheme strings.
Definition: Url.h:54
static int getDefaultPort(const String &scheme)
Obtain the default port for a given scheme.
Url(const char *urlString)
Construct a URL object from a regular null-terminated escaped string Escaped URL.
Definition: Url.h:84
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:133
void debugPrintTo(Print &p) const
Printable output for debugging.
Url & operator=(String urlString)
Copy assignment operator.
String getRelativePath() const
Get path without leading separator.
Definition: Url.h:147
Url(const String &urlString)
Construct a URL object from a regular escaped string Escaped URL.
Definition: Url.h:76
String toString() const
Get escaped URL.
String getPathWithQuery() const
Get path with any query parameters attached.
int Port
Undefined by default.
Definition: Url.h:174
String getFileName() const
Obtain the filename part of the URL path.
String getHostWithPort() const
Get hostname+port part of URL string.
String Fragment
Without &#39;#&#39;.
Definition: Url.h:177
String Host
hostname or IP address
Definition: Url.h:173