arduino-audio-tools
Loading...
Searching...
No Matches
Url.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace audio_tools {
7
22class Url {
23 public:
25 Url() = default;
26
28 Url(const char* url) {
29 LOGD("Url %s", url);
30 setUrl(url);
31 }
32
33 const char* url() { return urlStr.c_str(); }
34 const char* path() { return pathStr.c_str(); }
35 const char* host() { return hostStr.c_str(); }
36 const char* protocol() { return protocolStr.c_str(); }
37 const char* urlRoot() {
38 return urlRootStr.c_str();
39 } // prefix w/o path -> https://host:port
40 int port() { return portInt; }
41 // support https on custom ports
42 bool isSecure() { return portInt == 443 || protocolStr.startsWith("https"); }
43
44 void setUrl(const char* url) {
45 LOGD("setUrl %s", url);
46 this->urlStr = url;
47 parse();
48 }
49
50 protected:
56 int portInt = 0;
57
58 void parse() {
59 LOGI("Url::parse");
60
61 int protocolEnd = urlStr.indexOf("://");
62 if (protocolEnd == -1) {
63 return;
64 }
66 int pathStart = urlStr.indexOf("/", protocolEnd + 4);
67 int portStart = urlStr.indexOf(":", protocolEnd + 3);
68 // check if the found portStart isn't part of the path
70 int hostEnd = portStart != -1 ? portStart : pathStart;
71 // we have not path -> so then host end is the end of string
72 if (hostEnd == -1) {
74 }
76 if (portStart > 0) {
78 } else {
79 if (protocolStr.startsWith("https"))
80 portInt = 443;
81 else if (protocolStr.startsWith("http"))
82 portInt = 80;
83 else if (protocolStr.startsWith("ftp"))
84 portInt = 21;
85 else
86 portInt = -1; // undefined
87 }
88 if (pathStart <= 0) {
89 // we have no path
90 pathStr = "/";
92 } else {
94 pathStr.trim();
96 }
97 LOGI("url->%s", url());
98 LOGI("host->%s", host());
99 LOGI("protocol->%s", protocol());
100 LOGI("path->%s", path());
101 LOGI("port->%d", port());
102 }
103};
104
105} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition Str.h:24
virtual int length()
Definition StrView.h:383
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition StrView.h:171
virtual void trim()
remove leading and traling spaces
Definition StrView.h:504
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition StrView.h:477
virtual const char * c_str()
provides the string value as const char*
Definition StrView.h:379
virtual int indexOf(const char c, int start=0)
Definition StrView.h:260
URL parser which breaks a full url string up into its individual parts.
Definition Url.h:22
int port()
Definition Url.h:40
Str urlRootStr
Definition Url.h:54
Str hostStr
Definition Url.h:52
Str protocolStr
Definition Url.h:53
const char * url()
Definition Url.h:33
const char * path()
Definition Url.h:34
bool isSecure()
Definition Url.h:42
int portInt
Definition Url.h:56
const char * host()
Definition Url.h:35
Url(const char *url)
setup url from string
Definition Url.h:28
void setUrl(const char *url)
Definition Url.h:44
Str urlStr
Definition Url.h:55
const char * protocol()
Definition Url.h:36
void parse()
Definition Url.h:58
Str pathStr
Definition Url.h:51
const char * urlRoot()
Definition Url.h:37
Url()=default
Allow Empty constructor.
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512