Clinton Lee Taylor / Mbed 2 deprecated WiFiLamp

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Revision:
11:3ab606a42227
Parent:
9:319aeb6e0123
Child:
13:1e8f27da036a
Child:
14:416fff90cd0f
--- a/ESP8266_WebServer/ESP8266_WebServer.cpp	Sat Dec 27 05:56:47 2014 +0000
+++ b/ESP8266_WebServer/ESP8266_WebServer.cpp	Sun Dec 28 16:04:22 2014 +0000
@@ -125,29 +125,40 @@
     return NULL;
 }
 
-void ESP8266_WebServer::sendResponse(int linkID) {
-    int bytes = strlen(response);
+void ESP8266_WebServer::sendResponse(int linkID, int numBytes) {
     if( debugSerial != NULL ) {
-        debugSerial->printf("HTTP Reply Packet(%d bytes): %s\r\n", bytes, response);
+        debugSerial->printf("HTTP Reply Packet(%d bytes): %s\r\n", numBytes, response);
     }
-    serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, bytes);
+    serial->printf("AT+CIPSEND=%d,%d\r\n", linkID, numBytes);
     wait_ms(500);
     if( (string_waiting("\r\n>") == 1) ) {
-        serial->printf(response);
+        for( int i=0; i<numBytes; i++ ) {
+            serial->putc(response[i]);
+        }
     }
     while( string_waiting("\r\nSEND OK\r\n") == 0 ) {
         wait_ms(10);
     }
 }
 
-void ESP8266_WebServer::Send404Reply(int linkID) {
-    sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: 13\r\n\r\n404 Not Found");
-    sendResponse(linkID);
+void ESP8266_WebServer::SendError(int linkID, std::string error) {
+    SendError(linkID, error.c_str());
+}
+void ESP8266_WebServer::SendError(int linkID, const char* error) {
+    sprintf(response, "HTTP/1.1 %s\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", error, strlen(error), error);
+    sendResponse(linkID, strlen(response));
+}
+void ESP8266_WebServer::Send404Error(int linkID) {
+    SendError(linkID, "404 Not Found");
 }
 
-void ESP8266_WebServer::SendHTMLReply(int linkID, std::string reply) {
-    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);
+void ESP8266_WebServer::SendReply(int linkID, std::string reply, const char* mimeType) {
+    SendReply(linkID, reply.c_str(), reply.length(), mimeType);
+}
+
+void ESP8266_WebServer::SendReply(int linkID, const char* reply, int replySize, const char* mimeType) {
+    sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type:%s\r\nContent-Length: %d\r\n\r\n%s", mimeType, replySize, reply);
+    sendResponse(linkID, strlen(response));
 }
 
 ESP8266_WebRequest::ESP8266_WebRequest(int linkID, const char* httpPacket, Serial* debugSerial) {