Skip to content

Commit

Permalink
support POST in addition to PUT for command endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Mullins committed Apr 14, 2017
1 parent 82382f4 commit c15ee92
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/WebServer/MiLightHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void MiLightHttpServer::begin() {
server.on("/settings", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(SETTINGS_FILE));
server.on("/radio_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
server.onPattern("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_PUT, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
server.onPattern("/raw_commands/:type", HTTP_PUT, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_ANY, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
server.onPattern("/raw_commands/:type", HTTP_ANY, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
server.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
server.on("/web", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
server.on("/about", HTTP_GET, [this]() { handleAbout(); });
Expand Down
46 changes: 23 additions & 23 deletions lib/WebServer/PatternHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
#include <PatternHandler.h>

PatternHandler::PatternHandler(
const String& pattern,
const HTTPMethod method,
const String& pattern,
const HTTPMethod method,
const PatternHandler::TPatternHandlerFn fn
) : method(method), fn(fn), tokenPositions(NULL) {
Vector<StringToken>* tokenPositions = new Vector<StringToken>();
tokenize(pattern, tokenPositions);

numPatternTokens = tokenPositions->size();
patternTokens = new String[numPatternTokens];

for (int i = 0; i < tokenPositions->size(); i++) {
patternTokens[i] = (*tokenPositions)[i].extract(pattern);
}

delete tokenPositions;
}

bool PatternHandler::canHandle(HTTPMethod requestMethod, String requestUri) {
if (requestMethod != HTTP_ANY && requestMethod != this->method) {
if (this->method != HTTP_ANY && requestMethod != this->method) {
return false;
}

if (tokenPositions) {
delete tokenPositions;
}

bool canHandle = true;

tokenPositions = new Vector<StringToken>();
tokenize(requestUri, tokenPositions);

if (numPatternTokens == tokenPositions->size()) {
for (int i = 0; i < numPatternTokens; i++) {
const StringToken urlTokenP = (*tokenPositions)[i];
if (!patternTokens[i].startsWith(":")

if (!patternTokens[i].startsWith(":")
&& patternTokens[i] != urlTokenP.extract(requestUri)) {
canHandle = false;
break;
Expand All @@ -45,41 +45,41 @@ bool PatternHandler::canHandle(HTTPMethod requestMethod, String requestUri) {
} else {
canHandle = false;
}

return canHandle;
}

bool PatternHandler::handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) {
if (! canHandle(requestMethod, requestUri)) {
return false;
}

UrlTokenBindings* bindings = new UrlTokenBindings(patternTokens, tokenPositions, requestUri);
fn(bindings);

delete bindings;
}

void PatternHandler::tokenize(const String& path, Vector<StringToken>* tokenPositions) {
int lastStart = 0;
int currentPosition = 0;

for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == '/' || i == path.length()-1) {
// If we're in the last position, include the last character if it isn't
// a '/'
if (path.charAt(i) != '/') {
currentPosition++;
}

if (lastStart > 0 && currentPosition > lastStart) {
StringToken token(lastStart, currentPosition);
tokenPositions->push_back(token);
}

lastStart = i+1;
}

currentPosition++;
}
}
}
4 changes: 2 additions & 2 deletions lib/WebServer/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void WebServer::disableAuthentication() {
}

void WebServer::_handleRequest() {
if (this->authEnabled
if (this->authEnabled
&& !this->authenticate(this->username.c_str(), this->password.c_str())) {
this->requestAuthentication();
} else {
Expand Down Expand Up @@ -82,4 +82,4 @@ void WebServer::handleClient() {
return;
}
}
}
}

0 comments on commit c15ee92

Please sign in to comment.