8000 Feature/path arguments by Bmooij · Pull Request #5214 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Feature/path arguments #5214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add path args
  • Loading branch information
bmooij-beeliners committed Nov 1, 2019
commit b80ea9021b59583469499573aed564b3ce80ea1c
5 changes: 5 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ void ESP8266WebServerTemplate<ServerType>::_streamFileCore(const size_t fileSize
send(200, contentType, emptyString);
}

String ESP8266WebServer::pathArg(int i) {
if (_currentHandler != nullptr)
return _currentHandler->pathArg(i);
return "";
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::arg(const String& name) const {
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class ESP8266WebServerTemplate
// Allows setting server options (i.e. SSL keys) by the instantiator
ServerType &getServer() { return _server; }

String pathArg(int i); // get request path argument by number
const String& arg(const String& name) const; // get request argument value by name
const String& arg(int i) const; // get request argument value by number
const String& argName(int i) const; // get request argument name by number
Expand Down
10 changes: 10 additions & 0 deletions libraries/ESP8266WebServer/src/detail/RequestHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class RequestHandler {

private:
RequestHandler<ServerType>* _next = nullptr;

protected:
std::vector<String> pathArgs;

public:
String pathArg(int i) {
if (i < pathArgs.size())
return pathArgs[i];
return "";
}
};

#endif //REQUESTHANDLER_H
50 changes: 47 additions & 3 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,60 @@ class FunctionRequestHandler : public RequestHandler<ServerType> {
, _uri(uri)
, _method(method)
{
int numParams = 0, start = 0;
do
{
start = _uri.indexOf("{}", start);
if (start > 0)
{
numParams++;
start += 2;
}
} while (start > 0);
pathArgs = std::vector<String>(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;

int listIndex = 0;
int length = _uri.length();
int j = 0;
for (int i = 0; i < length; i++, j++)
{
char uriChar = _uri[i];
char requestUriChar = requestUri[j];

if (uriChar == requestUriChar)
continue;

if (uriChar != '{')
return false;

i += 2; // index of char after '}'
if (i >= length)
{
// there is no char after '}'
pathArgs[listIndex] = requestUri.substring(j);
return true;
}
else
{
char charEnd = _uri[i];
int uriIndex = requestUri.indexOf(charEnd, j);
if (uriIndex < 0)
return false;
pathArgs[listIndex] = requestUri.substring(j, uriIndex);
j = uriIndex;
}
listIndex++;
}

return true;
return j >= requestUri.length();
}

bool canUpload(String requestUri) override {
Expand Down
0