Skip to content

Commit 0e9cd1d

Browse files
committed
Merge pull request sirleech#25 from polyphemus/UrlPathCommand
Add setUrlPathCommand method, ...
2 parents a29cd68 + b8e3cc3 commit 0e9cd1d

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

@@ -299,6 +315,7 @@ class WebServer: public Print
299315
Command *cmd;
300316
} m_commands[8];
301317
char m_cmdCount;
318+
UrlPathCommand *m_urlPathCmd;
302319

303320
void reset();
304321
void getRequest(WebServer::ConnectionType &type, char *request, int *length);
@@ -332,7 +349,8 @@ WebServer::WebServer(const char *urlPrefix, int port) :
332349
m_cmdCount(0),
333350
m_contentLength(0),
334351
m_failureCmd(&defaultFailCmd),
335-
m_defaultCmd(&defaultFailCmd)
352+
m_defaultCmd(&defaultFailCmd),
353+
m_urlPathCmd(NULL)
336354
{
337355
}
338356

@@ -360,6 +378,11 @@ void WebServer::addCommand(const char *verb, Command *cmd)
360378
}
361379
}
362380

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

0 commit comments

Comments
 (0)