WiFi WebServer for ESP8266

Fork of ESP8266_WebServer by Sebastian Schocke

Revision:
8:5573f6f70a82
Parent:
7:f6172ba3e807
--- a/ESP8266_WebServer.cpp	Mon Jan 05 19:22:25 2015 +0000
+++ b/ESP8266_WebServer.cpp	Tue Jan 06 18:44:07 2015 +0000
@@ -60,7 +60,7 @@
 }
 
 void ESP8266_WebServer::readBuffer(void) {
-    strncpy(reply, buffer, 1024);
+    strncpy(reply, buffer, sizeof(buffer));
     while(reqMode == true ) { wait_ms(10); }
     
     rxptr = buffer;
@@ -95,18 +95,19 @@
 }
 
 void ESP8266_WebServer::ResetModule(void) {
-    readBuffer();
-    serial->printf("ATE0\r\n");
-    while( data_waiting() == 0 ) {
-        wait_ms(10);
-    }
-    readBuffer();
-    
-    readBuffer();
-    serial->printf("AT+RST\r\n");
-    wait_ms(1000);
-    while( string_waiting("\r\nready\r\n") == 0 ) {
-        wait_ms(100);
+    if( string_waiting("\r\nready\r\n") == 0 ) {
+        readBuffer();
+        serial->printf("ATE0\r\n");
+        wait_ms(1000);
+        
+        if( string_waiting("\r\nready\r\n") == 0 ) {
+            readBuffer();
+            serial->printf("AT+RST\r\n");
+            wait_ms(1000);
+            while( string_waiting("\r\nready\r\n") == 0 ) {
+                wait_ms(100);
+            }
+        }
     }
     
     readBuffer();
@@ -213,6 +214,9 @@
 }
 void ESP8266_WebServer::SendReply(int linkID, char const* reply, int replySize, const char* mimeType, int maxAge) {
     sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\nCache-Control:max-age=%d\r\n\r\n", mimeType, replySize, maxAge);
+    if( debugSerial != NULL ) {
+        debugSerial->printf("HTTP Reply Header(%d bytes): %s\r\n", strlen(response), response);
+    }
     sendResponse(linkID, strlen(response));
     char const* sendPtr = reply;
     int bytesSent = 0;
@@ -234,6 +238,9 @@
 }
 void ESP8266_WebServer::SendFile(int linkID, FileHandle* file, const char* mimeType, int maxAge) {
     sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\nCache-Control:max-age=%d\r\n\r\n", mimeType, file->flen(), maxAge);
+    if( debugSerial != NULL ) {
+        debugSerial->printf("HTTP Reply Header(%d bytes): %s\r\n", strlen(response), response);
+    }
     sendResponse(linkID, strlen(response));
     int numBytes = file->read(response, sizeof(response));
     while( numBytes > 0) {
@@ -244,6 +251,9 @@
 
 int ESP8266_WebServer::SendStream(int linkID, char const* reply, int StreamSize,  int WindowSize, const char* mimeType, int maxAge) {
     sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\nCache-Control:max-age=%d\r\n\r\n", mimeType, StreamSize, maxAge);
+    if( debugSerial != NULL ) {
+        debugSerial->printf("HTTP Reply Header(%d bytes): %s\r\n", strlen(response), response);
+    }
     sendResponse(linkID, strlen(response));
     return SendStream(linkID, reply, WindowSize);
 }
@@ -488,11 +498,17 @@
         if( debugSerial != NULL ) {
             debugSerial->printf("HTTP Packet: %s\r\n", httpPacket);
         }
+        char* httpMethod = (char*)malloc(16);
+        char* httpURI = (char*)malloc(256);
+        
         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);
             }
+            
+            free(httpMethod);
+            free(httpURI);
             return;
         }
         
@@ -529,6 +545,9 @@
         if( debugSerial != NULL ) {
             debugSerial->printf("HTTP %s %s\r\n", httpMethod, httpURI);
         }
+        
+        free(httpMethod);
+        free(httpURI);
     }
 }