Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
Diff: ESP8266_WebServer/ESP8266_WebServer.cpp
- Revision:
- 9:319aeb6e0123
- Parent:
- 7:f15c81074400
- Child:
- 11:3ab606a42227
diff -r f819de1946a7 -r 319aeb6e0123 ESP8266_WebServer/ESP8266_WebServer.cpp
--- a/ESP8266_WebServer/ESP8266_WebServer.cpp	Fri Dec 19 09:08:56 2014 +0000
+++ b/ESP8266_WebServer/ESP8266_WebServer.cpp	Sat Dec 27 05:56:47 2014 +0000
@@ -1,5 +1,6 @@
 #include "ESP8266_WebServer.h"
 #include <string>
+#include <map>
 
 ESP8266_WebServer::ESP8266_WebServer(Serial *espUART) {
     serial = espUART;
@@ -86,7 +87,7 @@
     readBuffer();
 }
 
-bool ESP8266_WebServer::GetRequest(void)
+ESP8266_WebRequest* ESP8266_WebServer::GetRequest(void)
 {
     if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
         if( debugSerial != NULL ) {
@@ -95,17 +96,17 @@
         readBuffer();
 
         char* ipdPacket = strstr(reply, "+IPD");
-        int bytesRecv, ipdLen;
-        int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &LinkID, &bytesRecv, &ipdLen);
+        int bytesRecv, ipdLen, linkID;
+        int numMatched = sscanf(ipdPacket,"+IPD,%d,%d:%n", &linkID, &bytesRecv, &ipdLen);
         if( numMatched != 2 ) {
             if( debugSerial != NULL ) {
-                debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, LinkID, bytesRecv, ipdLen);
+                debugSerial->printf("IPD ERROR : Matched %d, LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", numMatched, linkID, bytesRecv, ipdLen);
             }
             return false;
         }
 
         if( debugSerial != NULL ) {
-            debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", LinkID, bytesRecv, ipdLen);
+            debugSerial->printf("IPD Data: LinkID=%d, BytesRecv=%d, IPD Header Len=%d\r\n", linkID, bytesRecv, ipdLen);
         }
         if( strstr(ipdPacket, "HTTP") != NULL ) {
             if( debugSerial != NULL ) {
@@ -115,26 +116,13 @@
             if( debugSerial != NULL ) {
                 debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
             }
-
-            numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
-            if( numMatched != 2 ) {
-                if( debugSerial != NULL ) {
-                    debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
-                }
-                return false;
-            }
-            if( debugSerial != NULL ) {
-                debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
-            }
-
-            Method = httpMethod;
-            URI = httpURI;
+            ESP8266_WebRequest* request = new ESP8266_WebRequest(linkID, httpPacket, debugSerial); 
             
-            return true;
+            return request;
         }
     }
     
-    return false;
+    return NULL;
 }
 
 void ESP8266_WebServer::sendResponse(int linkID) {
@@ -161,3 +149,50 @@
     sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", reply.length(), reply.c_str());
     sendResponse(linkID);
 }
+
+ESP8266_WebRequest::ESP8266_WebRequest(int linkID, const char* httpPacket, Serial* debugSerial) {
+    int numMatched = sscanf(httpPacket, "%s %s HTTP/%*c.%*c", httpMethod, httpURI);
+    if( numMatched != 2 ) {
+        if( debugSerial != NULL ) {
+            debugSerial->printf("HTTP ERROR : Matched %d, Method=%s, URI=%s\r\n", numMatched, httpMethod, httpURI);
+        }
+        return;
+    }
+    if( debugSerial != NULL ) {
+        debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
+    }
+
+    LinkID = linkID;
+    Method = httpMethod;
+    URI = httpURI;
+    int pos = URI.find('?');
+    if(pos != string::npos ) {
+        string params = URI.substr(pos+1);
+        URI = URI.substr(0,pos);
+        if( debugSerial != NULL ) {
+            debugSerial->printf("HTTP %s %s\r\n", URI.c_str(), params.c_str());
+        }
+        pos = params.find('=');
+        while( pos != string::npos ) {
+            string name = params.substr(0,pos);
+            string value = params.substr(pos+1);
+            pos = params.find('&');
+            if( pos == string::npos )
+            {
+                if( debugSerial != NULL ) {
+                    debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
+                }
+                Parameters[name] = value;
+                break;
+            }
+            
+            value = value.substr(0,value.find('&'));
+            params = params.substr(pos+1);
+            pos = params.find('=');
+            if( debugSerial != NULL ) {
+                debugSerial->printf("HTTP GET Parameter %s=%s\r\n", name.c_str(), value.c_str());
+            }
+            Parameters[name] = value;
+        }
+    }
+}
\ No newline at end of file
    