Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_HttpServer by EmbeddedArtists AB

Revision:
6:7b3320c34654
Parent:
4:1b6b021ee21d
Child:
8:5779cee2e94a
--- a/HTTPRequestHandler.cpp	Sat Feb 22 05:51:59 2014 +0000
+++ b/HTTPRequestHandler.cpp	Tue Dec 02 15:04:43 2014 +0000
@@ -22,6 +22,7 @@
 #endif
 
 #include "HTTPRequestHandler.h"
+#include "DMBoard.h"
 
 #include <string.h>
 
@@ -36,14 +37,14 @@
     m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false)
 {
 #ifdef _DEBUG_REQUEST_HANDLER
-    printf("+++(HTTPRequestHandler) init \r\n");
+    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) init \r\n");
 #endif
     //Read & parse headers
     readHeaders();
 //*  m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
 //*  setTimeout(HTTP_REQUEST_TIMEOUT);
 #ifdef _DEBUG_REQUEST_HANDLER
-    printf("+++(HTTPRequestHandler) init end \r\n");
+    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) init end \r\n");
 #endif
 }
 
@@ -51,7 +52,7 @@
 {
     close();
 #ifdef _DEBUG_REQUEST_HANDLER
-    printf("+++(HTTPRequestHandler) Destroy end\r\n");
+    DMBoard::instance().logger()->printf("+++(HTTPRequestHandler) Destroy end\r\n");
 #endif
 }
 
@@ -111,7 +112,7 @@
 void HTTPRequestHandler::setContentLen(int len)
 {
     char len_str[6] = {0};
-    sprintf(len_str, "%d", len);
+    snprintf(len_str, 6, "%d", len);
     respHeaders()["Content-Length"] = len_str;
 }
 
@@ -152,7 +153,7 @@
         int n = sscanf(line, "%[^:]: %[^\n]", key, value);
         if ( n == 2 ) {
 #ifdef _DEBUG_REQUEST_HANDLER
-            printf("\r\n+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value);
+            DMBoard::instance().logger()->printf("\r\n+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value);
 #endif
             m_reqHeaders[key] = value;
         }
@@ -165,15 +166,15 @@
     static char line[128];
 
     //Response line
-    sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
+    snprintf(line, 128, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
     m_pTCPSocketConnection->send(line, strlen(line));
 
     map<string,string>::iterator it;
     while( !m_respHeaders.empty() ) {
         it = m_respHeaders.begin();
-        sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
+        snprintf(line, 128, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
 #ifdef _DEBUG_REQUEST_HANDLER
-        printf("\r\n+++(HTTPRequestHandler)%s", line);
+        DMBoard::instance().logger()->printf("\r\n+++(HTTPRequestHandler)%s", line);
 #endif
         m_pTCPSocketConnection->send(line, strlen(line));
         m_respHeaders.erase(it);
@@ -236,4 +237,4 @@
     break;
   }
 }
-**/
\ No newline at end of file
+**/