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