Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-esp32
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-esp32
Commits
3902aa40
Commit
3902aa40
authored
Nov 19, 2018
by
Bob
Committed by
Me No Dev
Nov 19, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding path arguments to WebServer (#1994)
parent
f9d1b24c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
113 additions
and
3 deletions
+113
-3
libraries/WebServer/examples/PathArgServer/PathArgServer.ino
libraries/WebServer/examples/PathArgServer/PathArgServer.ino
+53
-0
libraries/WebServer/src/WebServer.cpp
libraries/WebServer/src/WebServer.cpp
+5
-0
libraries/WebServer/src/WebServer.h
libraries/WebServer/src/WebServer.h
+1
-0
libraries/WebServer/src/detail/RequestHandler.h
libraries/WebServer/src/detail/RequestHandler.h
+12
-0
libraries/WebServer/src/detail/RequestHandlersImpl.h
libraries/WebServer/src/detail/RequestHandlersImpl.h
+42
-3
No files found.
libraries/WebServer/examples/PathArgServer/PathArgServer.ino
0 → 100644
View file @
3902aa40
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
const
char
*
ssid
=
"........"
;
const
char
*
password
=
"........"
;
WebServer
server
(
80
);
void
setup
(
void
)
{
Serial
.
begin
(
9600
);
WiFi
.
mode
(
WIFI_STA
);
WiFi
.
begin
(
ssid
,
password
);
Serial
.
println
(
""
);
// Wait for connection
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
println
(
""
);
Serial
.
print
(
"Connected to "
);
Serial
.
println
(
ssid
);
Serial
.
print
(
"IP address: "
);
Serial
.
println
(
WiFi
.
localIP
());
if
(
MDNS
.
begin
(
"esp32"
))
{
Serial
.
println
(
"MDNS responder started"
);
}
server
.
on
(
"/"
,
[]()
{
server
.
send
(
200
,
"text/plain"
,
"hello from esp32!"
);
});
server
.
on
(
"/users/{}"
,
[]()
{
String
user
=
server
.
pathArg
(
0
);
server
.
send
(
200
,
"text/plain"
,
"User: '"
+
user
+
"'"
);
});
server
.
on
(
"/users/{}/devices/{}"
,
[]()
{
String
user
=
server
.
pathArg
(
0
);
String
device
=
server
.
pathArg
(
1
);
server
.
send
(
200
,
"text/plain"
,
"User: '"
+
user
+
"' and Device: '"
+
device
+
"'"
);
});
server
.
begin
();
Serial
.
println
(
"HTTP server started"
);
}
void
loop
(
void
)
{
server
.
handleClient
();
}
libraries/WebServer/src/WebServer.cpp
View file @
3902aa40
...
...
@@ -509,6 +509,11 @@ void WebServer::_streamFileCore(const size_t fileSize, const String & fileName,
send
(
200
,
contentType
,
""
);
}
String
WebServer
::
pathArg
(
unsigned
int
i
)
{
if
(
_currentHandler
!=
nullptr
)
return
_currentHandler
->
pathArg
(
i
);
return
""
;
}
String
WebServer
::
arg
(
String
name
)
{
for
(
int
j
=
0
;
j
<
_postArgsLen
;
++
j
)
{
...
...
libraries/WebServer/src/WebServer.h
View file @
3902aa40
...
...
@@ -97,6 +97,7 @@ public:
virtual
WiFiClient
client
()
{
return
_currentClient
;
}
HTTPUpload
&
upload
()
{
return
*
_currentUpload
;
}
String
pathArg
(
unsigned
int
i
);
// get request path argument by number
String
arg
(
String
name
);
// get request argument value by name
String
arg
(
int
i
);
// get request argument value by number
String
argName
(
int
i
);
// get request argument name by number
...
...
libraries/WebServer/src/detail/RequestHandler.h
View file @
3902aa40
#ifndef REQUESTHANDLER_H
#define REQUESTHANDLER_H
#include <vector>
#include <assert.h>
class
RequestHandler
{
public:
virtual
~
RequestHandler
()
{
}
...
...
@@ -14,6 +17,15 @@ public:
private:
RequestHandler
*
_next
=
nullptr
;
protected:
std
::
vector
<
String
>
pathArgs
;
public:
const
String
&
pathArg
(
unsigned
int
i
)
{
assert
(
i
<
pathArgs
.
size
());
return
pathArgs
[
i
];
}
};
#endif //REQUESTHANDLER_H
libraries/WebServer/src/detail/RequestHandlersImpl.h
View file @
3902aa40
...
...
@@ -15,16 +15,55 @@ public:
,
_uri
(
uri
)
,
_method
(
method
)
{
int
numParams
=
0
,
start
=
0
;
do
{
start
=
_uri
.
indexOf
(
"{}"
,
start
);
if
(
start
>
0
)
{
numParams
++
;
start
+=
2
;
}
}
while
(
start
>
0
);
pathArgs
.
resize
(
numParams
);
}
bool
canHandle
(
HTTPMethod
requestMethod
,
String
requestUri
)
override
{
if
(
_method
!=
HTTP_ANY
&&
_method
!=
requestMethod
)
return
false
;
if
(
requestUri
!=
_uri
)
return
false
;
if
(
_uri
==
requestUri
)
return
true
;
size_t
uriLength
=
_uri
.
length
();
unsigned
int
pathArgIndex
=
0
;
unsigned
int
requestUriIndex
=
0
;
for
(
unsigned
int
i
=
0
;
i
<
uriLength
;
i
++
,
requestUriIndex
++
)
{
char
uriChar
=
_uri
[
i
];
char
requestUriChar
=
requestUri
[
requestUriIndex
];
if
(
uriChar
==
requestUriChar
)
continue
;
if
(
uriChar
!=
'{'
)
return
false
;
i
+=
2
;
// index of char after '}'
if
(
i
>=
uriLength
)
{
// there is no char after '}'
pathArgs
[
pathArgIndex
]
=
requestUri
.
substring
(
requestUriIndex
);
return
pathArgs
[
pathArgIndex
].
indexOf
(
"/"
)
==
-
1
;
// path argument may not contain a '/'
}
else
{
char
charEnd
=
_uri
[
i
];
int
uriIndex
=
requestUri
.
indexOf
(
charEnd
,
requestUriIndex
);
if
(
uriIndex
<
0
)
return
false
;
pathArgs
[
pathArgIndex
]
=
requestUri
.
substring
(
requestUriIndex
,
uriIndex
);
requestUriIndex
=
(
unsigned
int
)
uriIndex
;
}
pathArgIndex
++
;
}
return
true
;
return
requestUriIndex
>=
requestUri
.
length
()
;
}
bool
canUpload
(
String
requestUri
)
override
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment