Url.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/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 
18 #pragma once
19 
20 #include <WString.h>
21 #include "Http/HttpParams.h"
22 #include <sming_attr.h>
23 
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: Official 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)
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  {
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 
143 
148  {
149  return (Path[0] == '/') ? Path.substring(1) : Path;
150  }
151 
157 
162 
167  void debugPrintTo(Print& p) const;
168 
169 public:
174  int Port = 0;
178 };
179 
180 inline String toString(const Url& url)
181 {
182  return url.toString();
183 }
184 
Handles the query portion of a URI.
Definition: HttpParams.h:35
Provides formatted output to stream.
Definition: Print.h:37
The String class.
Definition: WString.h:137
void toLowerCase(void)
Convert the entire String content to lower case.
String substring(size_t from, size_t to) const
Class to manage URL instance.
Definition: Url.h:67
static int getDefaultPort(const String &scheme)
Obtain the default port for a given scheme.
String getFileName() const
Obtain the filename part of the URL path.
String getPathWithQuery() const
Get path with any query parameters attached.
Url(const String &scheme, const String &user, const String &password, const String &host, int port=0, const String &path=nullptr, const String &query=nullptr, const String &fragment=nullptr)
Definition: Url.h:88
String Fragment
Without '#'.
Definition: Url.h:177
String Password
Definition: Url.h:172
String Host
hostname or IP address
Definition: Url.h:173
String toString() const
Get escaped URL.
String getRelativePath() const
Get path without leading separator.
Definition: Url.h:147
HttpParams Query
Definition: Url.h:176
String Scheme
without ":" and "//"
Definition: Url.h:170
Url & operator=(String urlString)
Copy assignment operator.
int Port
Undefined by default.
Definition: Url.h:174
String getHostWithPort() const
Get hostname+port part of URL string.
Url & operator=(const char *urlString)
Copy assignment operator, for C-style strings.
Definition: Url.h:108
Url()=default
int getPort() const
Obtain the actual port number to be used.
Definition: Url.h:133
Url(const String &urlString)
Construct a URL object from a regular escaped string.
Definition: Url.h:76
String User
Definition: Url.h:171
void debugPrintTo(Print &p) const
Printable output for debugging.
String Path
with leading "/"
Definition: Url.h:175
Url(const Url &url)=default
Url(const char *urlString)
Construct a URL object from a regular null-terminated escaped string.
Definition: Url.h:84
String toString(const Url &url)
Definition: Url.h:180
#define XX(name, str, port)
Common URI scheme strings.
Definition: Url.h:54
#define URI_SCHEME_MAP(XX)
Definition: Url.h:37
Definition: Streams.h:25