| 
4 | 4 | #include "RequestHandler.h"  | 
5 | 5 | #include "mimetable.h"  | 
6 | 6 | #include "WString.h"  | 
 | 7 | +#include "Uri.h"  | 
7 | 8 | 
 
  | 
8 | 9 | using namespace mime;  | 
9 | 10 | 
 
  | 
10 | 11 | class FunctionRequestHandler : public RequestHandler {  | 
11 | 12 | public:  | 
12 |  | -    FunctionRequestHandler(WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn, const String &uri, HTTPMethod method)  | 
 | 13 | +    FunctionRequestHandler(WebServer::THandlerFunction fn, WebServer::THandlerFunction ufn, const Uri &uri, HTTPMethod method)  | 
13 | 14 |     : _fn(fn)  | 
14 | 15 |     , _ufn(ufn)  | 
15 |  | -    , _uri(uri)  | 
 | 16 | +    , _uri(uri.clone())  | 
16 | 17 |     , _method(method)  | 
17 | 18 |     {  | 
18 |  | -        int numParams = 0, start = 0;  | 
19 |  | -        do {  | 
20 |  | -            start = _uri.indexOf("{}", start);  | 
21 |  | -            if (start > 0) {  | 
22 |  | -                numParams++;  | 
23 |  | -                start += 2;  | 
24 |  | -            }  | 
25 |  | -        } while (start > 0);  | 
26 |  | -        pathArgs.resize(numParams);  | 
 | 19 | +        _uri->initPathArgs(pathArgs);  | 
 | 20 | +    }  | 
 | 21 | + | 
 | 22 | +    ~FunctionRequestHandler() {  | 
 | 23 | +        delete _uri;  | 
27 | 24 |     }  | 
28 | 25 | 
 
  | 
29 | 26 |     bool canHandle(HTTPMethod requestMethod, String requestUri) override  {  | 
30 | 27 |         if (_method != HTTP_ANY && _method != requestMethod)  | 
31 | 28 |             return false;  | 
32 | 29 | 
 
  | 
33 |  | -        if (_uri == requestUri)  | 
34 |  | -            return true;  | 
35 |  | - | 
36 |  | -        size_t uriLength = _uri.length();  | 
37 |  | -        unsigned int pathArgIndex = 0;  | 
38 |  | -        unsigned int requestUriIndex = 0;  | 
39 |  | -        for (unsigned int i = 0; i < uriLength; i++, requestUriIndex++) {  | 
40 |  | -            char uriChar = _uri[i];  | 
41 |  | -            char requestUriChar = requestUri[requestUriIndex];  | 
42 |  | - | 
43 |  | -            if (uriChar == requestUriChar)  | 
44 |  | -                continue;  | 
45 |  | -            if (uriChar != '{')  | 
46 |  | -                return false;  | 
47 |  | - | 
48 |  | -            i += 2; // index of char after '}'  | 
49 |  | -            if (i >= uriLength) {  | 
50 |  | -                // there is no char after '}'  | 
51 |  | -                pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex);  | 
52 |  | -                return pathArgs[pathArgIndex].indexOf("/") == -1; // path argument may not contain a '/'  | 
53 |  | -            }  | 
54 |  | -            else  | 
55 |  | -            {  | 
56 |  | -                char charEnd = _uri[i];  | 
57 |  | -                int uriIndex = requestUri.indexOf(charEnd, requestUriIndex);  | 
58 |  | -                if (uriIndex < 0)  | 
59 |  | -                    return false;  | 
60 |  | -                pathArgs[pathArgIndex] = requestUri.substring(requestUriIndex, uriIndex);  | 
61 |  | -                requestUriIndex = (unsigned int) uriIndex;  | 
62 |  | -            }  | 
63 |  | -            pathArgIndex++;  | 
64 |  | -        }  | 
65 |  | - | 
66 |  | -        return requestUriIndex >= requestUri.length();  | 
 | 30 | +        return _uri->canHandle(requestUri, pathArgs);  | 
67 | 31 |     }  | 
68 | 32 | 
 
  | 
69 | 33 |     bool canUpload(String requestUri) override  {  | 
@@ -92,7 +56,7 @@ class FunctionRequestHandler : public RequestHandler {  | 
92 | 56 | protected:  | 
93 | 57 |     WebServer::THandlerFunction _fn;  | 
94 | 58 |     WebServer::THandlerFunction _ufn;  | 
95 |  | -    String _uri;  | 
 | 59 | +    Uri *_uri;  | 
96 | 60 |     HTTPMethod _method;  | 
97 | 61 | };  | 
98 | 62 | 
 
  | 
 | 
0 commit comments