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 clear();
25 }
26
27 // setup url with string
28 Url(const char* url) {
29 DlnaLogger.log(DlnaLogLevel::Debug, "Url %s", url);
30 setUrl(url);
31 }
32
33 // copy constructor
35 DlnaLogger.log(DlnaLogLevel::Debug, "Url %s", url.url());
36 setUrl(url.url());
37 }
38
39 const char* url() { return urlStr.c_str(); }
40 const char* path() { return pathStr.c_str(); }
41 const char* host() { return hostStr.c_str(); }
42 const char* protocol() { return protocolStr.c_str(); }
43 const char* urlRoot() {
44 return urlRootStr.c_str();
45 } // prefix w/o path -> https://host:port
46 int port() { return portInt; }
47
48 void setUrl(const char* url) {
49 DlnaLogger.log(DlnaLogLevel::Debug, "setUrl %s", url);
50 this->urlStr = url;
51 parse();
52 }
53
54 operator bool() { return !urlStr.isEmpty(); }
55
56 bool operator ==(Url& other) {
57 return this->urlStr.equals(other.urlStr.c_str());
58 }
59 bool operator !=(Url& other) {
60 return !(*this == other);
61 }
62
63 void clear() {
64 pathStr.clear();
65 hostStr.clear();
68 urlStr.clear();
69 portInt = -1;
70 }
71
72 protected:
78 int portInt = 0;
79
80 void parse() {
81 DlnaLogger.log(DlnaLogLevel::Debug, "Url::parse()");
82
83 int protocolEnd = urlStr.indexOf("://");
84 if (protocolEnd == -1) {
85 return;
86 }
87 protocolStr.substrView(urlStr, 0, protocolEnd);
88 int portStart = urlStr.indexOf(":", protocolEnd + 3);
89 int hostEnd =
90 portStart != -1 ? portStart : urlStr.indexOf("/", protocolEnd + 4);
91 // we have not path -> so then host end is the end of string
92 if (hostEnd == -1) {
93 hostEnd = urlStr.length();
94 }
95 hostStr.substrView(urlStr, protocolEnd + 3, hostEnd);
96 if (portStart > 0) {
97 portInt = atoi(urlStr.c_str() + portStart + 1);
98 } else {
99 if (protocolStr.startsWith("https"))
100 portInt = 443;
101 else if (protocolStr.startsWith("http"))
102 portInt = 80;
103 else if (protocolStr.startsWith("ftp"))
104 portInt = 21;
105 else
106 portInt = -1; // undefined
107 }
108 int pathStart = urlStr.indexOf("/", protocolEnd + 4);
109 if (pathStart == 0) {
110 // we have no path
111 pathStr = "/";
113 } else {
114 pathStr.substrView(urlStr, pathStart, urlStr.length());
115 pathStr.trim();
116 urlRootStr.substrView(urlStr, 0, pathStart);
117 }
118 DlnaLogger.log(DlnaLogLevel::Debug, "url-> %s", url());
119 DlnaLogger.log(DlnaLogLevel::Debug, "path-> %s", path());
120 }
121};
122
123} // namespace tiny_dlna
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
bool equals(const char *other) const
Exact string equality with C-string.
Definition: Str.h:167
int length() const
Current length (int)
Definition: Str.h:57
bool isEmpty() const
True if empty.
Definition: Str.h:54
bool startsWith(const char *prefix) const
True if starts with prefix (case-sensitive)
Definition: Str.h:206
void trim()
Trim spaces on both ends.
Definition: Str.h:235
void substrView(StrView &from, int start, int end)
Assign substring view from StrView [start,end)
Definition: Str.h:69
int indexOf(const char *substr, int start=0) const
Index of substring from position (or -1)
Definition: Str.h:214
void clear()
Clear contents (size -> 0)
Definition: Str.h:93
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
Str protocolStr
Definition: Url.h:75
Url()=default
Url(Url &url)
Definition: Url.h:34
int port()
Definition: Url.h:46
int portInt
Definition: Url.h:78
const char * host()
Definition: Url.h:41
const char * urlRoot()
Definition: Url.h:43
Str hostStr
Definition: Url.h:74
const char * protocol()
Definition: Url.h:42
bool operator==(Url &other)
Definition: Url.h:56
void setUrl(const char *url)
Definition: Url.h:48
Str urlStr
Definition: Url.h:77
~Url()
Definition: Url.h:23
const char * url()
Definition: Url.h:39
bool operator!=(Url &other)
Definition: Url.h:59
Str urlRootStr
Definition: Url.h:76
void clear()
Definition: Url.h:63
Url(const char *url)
Definition: Url.h:28
const char * path()
Definition: Url.h:40
Str pathStr
Definition: Url.h:73
void parse()
Definition: Url.h:80
Definition: Allocator.h:13