Arduino DLNA Server
Url.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "basic/Str.h"
4 #include "basic/Logger.h"
5 
6 namespace tiny_dlna {
7 
18 class Url {
19  public:
20  // empty url
21  Url() {
22  DlnaLogger.log(DlnaDebug,"Url");
23  }
24 
25  ~Url() {
26  DlnaLogger.log(DlnaDebug,"~Url");
27  pathStr.clear();
28  hostStr.clear();
30  urlRootStr.clear();
31  urlStr.clear();
32  }
33 
34  // setup url with string
35  Url(const char *url){
36  DlnaLogger.log(DlnaDebug,"Url %s",url);
37  setUrl(url);
38  }
39 
40  // copy constructor
41  Url(Url &url){
42  DlnaLogger.log(DlnaDebug,"Url %s",url.url());
43  setUrl(url.url());
44  }
45 
46  const char* url() {return urlStr.c_str();}
47  const char* path() { return pathStr.c_str(); }
48  const char* host() { return hostStr.c_str();}
49  const char* protocol() {return protocolStr.c_str();}
50  const char* urlRoot() {return urlRootStr.c_str();} // prefix w/o path -> https://host:port
51  int port() {return portInt;}
52 
53  void setUrl(const char* url){
54  DlnaLogger.log(DlnaDebug,"setUrl %s",url);
55  this->urlStr = url;
56  parse();
57  }
58 
59  operator bool() { return !urlStr.isEmpty();}
60 
61  protected:
62  Str pathStr = Str(40);
63  Str hostStr = Str(20);
66  Str urlStr = Str(40);
67  int portInt;
68 
69  void parse() {
70  DlnaLogger.log(DlnaDebug,"Url::parse");
71 
72  int protocolEnd = urlStr.indexOf("://");
73  if (protocolEnd==-1){
74  return;
75  }
76  protocolStr.substring(urlStr, 0,protocolEnd);
77  int portStart = urlStr.indexOf(":",protocolEnd+3);
78  int hostEnd = portStart!=-1 ? portStart : urlStr.indexOf("/",protocolEnd+4);
79  // we have not path -> so then host end is the end of string
80  if (hostEnd==-1){
81  hostEnd = urlStr.length();
82  }
83  hostStr.substring(urlStr, protocolEnd+3,hostEnd);
84  if (portStart>0){
85  portInt = atoi(urlStr.c_str()+portStart+1);
86  } else {
87  if (protocolStr.startsWith("https"))
88  portInt = 443;
89  else if (protocolStr.startsWith("http"))
90  portInt = 80;
91  else if (protocolStr.startsWith("ftp"))
92  portInt = 21;
93  else
94  portInt = -1; // undefined
95  }
96  int pathStart = urlStr.indexOf("/",protocolEnd+4);
97  if (pathStart==0){
98  // we have no path
99  pathStr = "/";
100  urlRootStr = urlStr;
101  } else {
102  pathStr.substring(urlStr, pathStart, urlStr.length());
103  pathStr.trim();
104  urlRootStr.substring(urlStr, 0, pathStart);
105  }
106  DlnaLogger.log(DlnaDebug,"url-> %s",url());
107  DlnaLogger.log(DlnaDebug,"path-> %s",path());
108 
109  }
110 
111 };
112 
113 }
114 
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
virtual bool isEmpty()
checks if the string is empty
Definition: StrView.h:373
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:267
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition: StrView.h:177
virtual int length()
Definition: StrView.h:370
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:510
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:483
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
void clear() override
clears the string by setting the terminating 0 at the beginning
Definition: Str.h:164
const char * c_str()
provides the string value as const char*
Definition: Str.h:188
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
Url()
Definition: Url.h:21
const char * path()
Definition: Url.h:47
Str protocolStr
Definition: Url.h:64
Url(Url &url)
Definition: Url.h:41
int port()
Definition: Url.h:51
int portInt
Definition: Url.h:67
Str hostStr
Definition: Url.h:63
const char * urlRoot()
Definition: Url.h:50
void setUrl(const char *url)
Definition: Url.h:53
Str urlStr
Definition: Url.h:66
~Url()
Definition: Url.h:25
const char * protocol()
Definition: Url.h:49
const char * url()
Definition: Url.h:46
Str urlRootStr
Definition: Url.h:65
Url(const char *url)
Definition: Url.h:35
Str pathStr
Definition: Url.h:62
const char * host()
Definition: Url.h:48
void parse()
Definition: Url.h:69
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5