Selaa lähdekoodia

support POST in addition to PUT for command endpoints

Chris Mullins 8 vuotta sitten
vanhempi
commit
c15ee923a4

+ 2 - 2
lib/WebServer/MiLightHttpServer.cpp

@@ -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(); });

+ 23 - 23
lib/WebServer/PatternHandler.cpp

@@ -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;
@@ -45,25 +45,25 @@ 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
@@ -71,15 +71,15 @@ void PatternHandler::tokenize(const String& path, Vector<StringToken>* tokenPosi
       if (path.charAt(i) != '/') {
         currentPosition++;
       }
-      
+
       if (lastStart > 0 && currentPosition > lastStart) {
         StringToken token(lastStart, currentPosition);
         tokenPositions->push_back(token);
       }
-      
+
       lastStart = i+1;
     }
-      
+
     currentPosition++;
   }
-}
+}

+ 2 - 2
lib/WebServer/WebServer.cpp

@@ -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 {
@@ -82,4 +82,4 @@ void WebServer::handleClient() {
       return;
     }
   }
-}
+}