arduino-audio-tools
Loading...
Searching...
No Matches
Url.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioBasic/Str.h"
4#include "AudioTools/CoreAudio/AudioLogger.h"
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:
51 Str pathStr{0};
52 Str hostStr{0};
53 Str protocolStr{0};
54 Str urlRootStr{0};
55 Str urlStr{0};
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 }
65 protocolStr.substring(urlStr, 0, protocolEnd);
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
69 if (pathStart>=0) portStart = portStart < pathStart ? portStart : -1;
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) {
73 hostEnd = urlStr.length();
74 }
75 hostStr.substring(urlStr, protocolEnd + 3, hostEnd);
76 if (portStart > 0) {
77 portInt = atoi(urlStr.c_str() + portStart + 1);
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 = "/";
91 urlRootStr = urlStr;
92 } else {
93 pathStr.substring(urlStr, pathStart, urlStr.length());
94 pathStr.trim();
95 urlRootStr.substring(urlStr, 0, pathStart);
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
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
Url(const char *url)
setup url from string
Definition Url.h:28
Url()=default
Allow Empty constructor.
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10