arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
Communication
HTTP
Url.h
Go to the documentation of this file.
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
// 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
LOGI
#define LOGI(...)
Definition
AudioLoggerIDF.h:28
LOGD
#define LOGD(...)
Definition
AudioLoggerIDF.h:27
AudioLogger.h
Str.h
audio_tools::Str
Str which keeps the data on the heap. We grow the allocated memory only if the copy source is not fit...
Definition
Str.h:24
audio_tools::StrView::length
virtual int length()
Definition
StrView.h:383
audio_tools::StrView::startsWith
virtual bool startsWith(const char *str)
checks if the string starts with the indicated substring
Definition
StrView.h:171
audio_tools::StrView::trim
virtual void trim()
remove leading and traling spaces
Definition
StrView.h:504
audio_tools::StrView::substring
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition
StrView.h:477
audio_tools::StrView::c_str
virtual const char * c_str()
provides the string value as const char*
Definition
StrView.h:379
audio_tools::StrView::indexOf
virtual int indexOf(const char c, int start=0)
Definition
StrView.h:260
audio_tools::Url
URL parser which breaks a full url string up into its individual parts.
Definition
Url.h:22
audio_tools::Url::port
int port()
Definition
Url.h:40
audio_tools::Url::urlRootStr
Str urlRootStr
Definition
Url.h:54
audio_tools::Url::hostStr
Str hostStr
Definition
Url.h:52
audio_tools::Url::protocolStr
Str protocolStr
Definition
Url.h:53
audio_tools::Url::url
const char * url()
Definition
Url.h:33
audio_tools::Url::path
const char * path()
Definition
Url.h:34
audio_tools::Url::isSecure
bool isSecure()
Definition
Url.h:42
audio_tools::Url::portInt
int portInt
Definition
Url.h:56
audio_tools::Url::host
const char * host()
Definition
Url.h:35
audio_tools::Url::Url
Url(const char *url)
setup url from string
Definition
Url.h:28
audio_tools::Url::setUrl
void setUrl(const char *url)
Definition
Url.h:44
audio_tools::Url::urlStr
Str urlStr
Definition
Url.h:55
audio_tools::Url::protocol
const char * protocol()
Definition
Url.h:36
audio_tools::Url::parse
void parse()
Definition
Url.h:58
audio_tools::Url::pathStr
Str pathStr
Definition
Url.h:51
audio_tools::Url::urlRoot
const char * urlRoot()
Definition
Url.h:37
audio_tools::Url::Url
Url()=default
Allow Empty constructor.
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition
AudioCodecsBase.h:10
audio_tools::writeData
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition
AudioTypes.h:512
Generated by
1.9.8