Skip to content

Commit b8e3cc3

Browse files
committed
Add setUrlPathCommand method, which adds support for resolving the URL in the function itself, when it doesn't match default or specified paths.
Modified Command prototype, UrlPathCommand, now returns the extra parameter char **url_path. url_path contains pointers to the separate parts of the URL path.
1 parent ee6c197 commit b8e3cc3

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

WebServer.h

+54-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#define WEBDUINO_READ_TIMEOUT_IN_MS 1000
5858
#endif
5959

60+
#ifndef WEBDUINO_URL_PATH_COMMAND_LENGTH
61+
#define WEBDUINO_URL_PATH_COMMAND_LENGTH 8
62+
#endif
63+
6064
#ifndef WEBDUINO_FAIL_MESSAGE
6165
#define WEBDUINO_FAIL_MESSAGE "<h1>EPIC FAIL</h1>"
6266
#endif
@@ -158,6 +162,13 @@ class WebServer: public Print
158162
typedef void Command(WebServer &server, ConnectionType type,
159163
char *url_tail, bool tail_complete);
160164

165+
// Prototype for the optional function which consumes the URL path itself.
166+
// url_path contains pointers to the seperate parts of the URL path where '/'
167+
// was used as the delimiter.
168+
typedef void UrlPathCommand(WebServer &server, ConnectionType type,
169+
char **url_path, char *url_tail,
170+
bool tail_complete);
171+
161172
// constructor for webserver object
162173
WebServer(const char *urlPrefix = "", int port = 80);
163174

@@ -184,6 +195,11 @@ class WebServer: public Print
184195
// add a new command to be run at the URL specified by verb
185196
void addCommand(const char *verb, Command *cmd);
186197

198+
// Set command that's run if default command or URL specified commands do
199+
// not run, uses extra url_path parameter to allow resolving the URL in the
200+
// function.
201+
void setUrlPathCommand(UrlPathCommand *cmd);
202+
187203
// utility function to output CRLF pair
188204
void printCRLF();
189205

@@ -296,6 +312,7 @@ class WebServer: public Print
296312
Command *cmd;
297313
} m_commands[8];
298314
char m_cmdCount;
315+
UrlPathCommand *m_urlPathCmd;
299316

300317
void reset();
301318
void getRequest(WebServer::ConnectionType &type, char *request, int *length);
@@ -329,7 +346,8 @@ WebServer::WebServer(const char *urlPrefix, int port) :
329346
m_cmdCount(0),
330347
m_contentLength(0),
331348
m_failureCmd(&defaultFailCmd),
332-
m_defaultCmd(&defaultFailCmd)
349+
m_defaultCmd(&defaultFailCmd),
350+
m_urlPathCmd(NULL)
333351
{
334352
}
335353

@@ -357,6 +375,11 @@ void WebServer::addCommand(const char *verb, Command *cmd)
357375
}
358376
}
359377

378+
void WebServer::setUrlPathCommand(UrlPathCommand *cmd)
379+
{
380+
m_urlPathCmd = cmd;
381+
}
382+
360383
size_t WebServer::write(uint8_t ch)
361384
{
362385
return m_client.write(ch);
@@ -472,6 +495,36 @@ bool WebServer::dispatchCommand(ConnectionType requestType, char *verb,
472495
return true;
473496
}
474497
}
498+
// Check if UrlPathCommand is assigned.
499+
if (m_urlPathCmd != NULL)
500+
{
501+
// Initialize with null bytes, so number of parts can be determined.
502+
char *url_path[WEBDUINO_URL_PATH_COMMAND_LENGTH] = {0};
503+
int part = 0;
504+
505+
// URL path should be terminated with null byte.
506+
*(verb + verb_len) = 0;
507+
508+
// First URL path part is at the start of verb.
509+
url_path[part++] = verb;
510+
// Replace all slashes ('/') with a null byte so every part of the URL
511+
// path is a seperate string. Add every char following a '/' as a new
512+
// part of the URL, even if that char is a '/' (which will be replaced
513+
// with a null byte).
514+
for (char * p = verb; p < verb + verb_len; p++)
515+
{
516+
if (*p == '/')
517+
{
518+
*p = 0;
519+
url_path[part++] = p + 1;
520+
// Don't try to assign out of array bounds.
521+
if (part == WEBDUINO_URL_PATH_COMMAND_LENGTH) break;
522+
}
523+
}
524+
m_urlPathCmd(*this, requestType, url_path,
525+
verb + verb_len + qm_offset, tail_complete);
526+
return true;
527+
}
475528
}
476529
return false;
477530
}

0 commit comments

Comments
 (0)