arduino-audio-tools
Url.h
1 #pragma once
2 
3 #include "AudioTools/CoreAudio/AudioBasic/Str.h"
4 #include "AudioTools/CoreAudio/AudioLogger.h"
5 
6 namespace audio_tools {
7 
22 class 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  bool isSecure() { return portInt == 443; }
42 
43  void setUrl(const char* url) {
44  LOGD("setUrl %s", url);
45  this->urlStr = url;
46  parse();
47  }
48 
49  protected:
50  Str pathStr{0};
51  Str hostStr{0};
52  Str protocolStr{0};
53  Str urlRootStr{0};
54  Str urlStr{0};
55  int portInt = 0;
56 
57  void parse() {
58  LOGI("Url::parse");
59 
60  int protocolEnd = urlStr.indexOf("://");
61  if (protocolEnd == -1) {
62  return;
63  }
64  protocolStr.substring(urlStr, 0, protocolEnd);
65  int pathStart = urlStr.indexOf("/", protocolEnd + 4);
66  int portStart = urlStr.indexOf(":", protocolEnd + 3);
67  // check if the found portStart isn't part of the path
68  if (pathStart>=0) portStart = portStart < pathStart ? portStart : -1;
69  int hostEnd = portStart != -1 ? portStart : pathStart;
70  // we have not path -> so then host end is the end of string
71  if (hostEnd == -1) {
72  hostEnd = urlStr.length();
73  }
74  hostStr.substring(urlStr, protocolEnd + 3, hostEnd);
75  if (portStart > 0) {
76  portInt = atoi(urlStr.c_str() + portStart + 1);
77  } else {
78  if (protocolStr.startsWith("https"))
79  portInt = 443;
80  else if (protocolStr.startsWith("http"))
81  portInt = 80;
82  else if (protocolStr.startsWith("ftp"))
83  portInt = 21;
84  else
85  portInt = -1; // undefined
86  }
87  if (pathStart <= 0) {
88  // we have no path
89  pathStr = "/";
90  urlRootStr = urlStr;
91  } else {
92  pathStr.substring(urlStr, pathStart, urlStr.length());
93  pathStr.trim();
94  urlRootStr.substring(urlStr, 0, pathStart);
95  }
96  LOGI("url->%s", url());
97  LOGI("host->%s", host());
98  LOGI("protocol->%s", protocol());
99  LOGI("path->%s", path());
100  LOGI("port->%d", port());
101  }
102 };
103 
104 } // 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 const char * c_str()
provides the string value as const char*
Definition: StrView.h:379
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:477
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: AudioConfig.h:823