The lib with which to make LPC1768 a simple HTTP server. This have not yet implemented. fopen() DOESN'T WORK after EthernetInterface::connect() is called as using mbed-os 5.4~. See also https://os.mbed.com/questions/80658/HardFault-occurs-when-fopen-is-called-af/ or https://github.com/ARMmbed/mbed-os/issues/6578 and https://github.com/ARMmbed/mbed-os/issues/6624

Fork of HTTP_SERVER by Akifumi Takahashi

Revision:
15:9b2cfbaf1c12
Parent:
14:a16cdcd098d7
Child:
16:c3920b5b8572
--- a/HTTP_SERVER.cpp	Fri Mar 16 22:45:47 2018 +0000
+++ b/HTTP_SERVER.cpp	Fri Mar 16 23:17:28 2018 +0000
@@ -7,7 +7,7 @@
 LOG:
 "sys_thread_new number error" is occurred when an instance of this class is instantiated in main() in main.cpp
 This case wasn't occured with the APIs of mbed os 2.
-So, inserting some DEBUG_PRINT_LINE, and finding where is wrong. 
+So, inserting some DEBUG_PRINT_LINE, and finding where is wrong.
 
 The error seems to be occurred in init() in the above case.
 
@@ -27,7 +27,7 @@
 
 bool deep-alive was also not uesed so deleted it.
 
-After doing those above, the error massage was DERAYED but 
+After doing those above, the error massage was DERAYED but
 it even now occur.
 and when some array stack(such like char buffer[1024] -> [256])'s size was decreased,
 the message was more derayed.
@@ -80,6 +80,7 @@
     backlog  = 1;
     //keep_alive = (false);
     listening_flag = (false);
+    socket_connection = false;
     DEBUG_PRINT_LINE("A HTTP_SERVER has been inistantiated.");
 }
 
@@ -88,7 +89,7 @@
 }
 
 bool HttpServer::init()
-{    
+{
     DEBUG_PRINT_LINE("The HTTP SERVER is being initiated");
     //  Ethernet Initialization
     //  Ethernet Connecting setup
@@ -98,7 +99,7 @@
     } else {
         printf("(HTTP_SERVER) IP Address is %s\r\n", net->get_ip_address());
     }
-    
+
     //  TCP Socket setup
     //  To open Server-side PORT
     if(server.open(net) < 0) {
@@ -121,16 +122,15 @@
         listening_flag = true;
         printf("(HTTP_SERVER) tcp server is listening...\r\n");
     }
-    
+
     return true;
 }
 
 bool HttpServer::run()
 {
-    
+
     DigitalOut led1(LED1);
     DigitalOut led2(LED1);
-    bool socket_connection = false;
 
 
     DEBUG_PRINT_LINE("The HTTP SERVER is starting running");
@@ -155,105 +155,17 @@
             char* httpmethod    = NULL;
             char* filepath      = NULL;
             char* http_ver      = NULL;
-            char* header_field_name = NULL;
-            char* header_field_val  = NULL;
+            //char* header_field_name = NULL;
+            //char* header_field_val  = NULL;
 
             //
             //  Request Analysis
             //
-            DEBUG_PRINT_LINE("DEBUG MODE@Request Analysis");
-            switch(client_socket.recv(buffer, 255)) {
-                case 0:
-                    DEBUG_PRINT_LINE("recieved buffer is empty.");
-                    msger.setStatusLine(400, "No Request");
-                    if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                    httpmethod    = NULL;
-                    filepath      = NULL;
-                    http_ver      = NULL;
-                    socket_connection = false;
-                    break;
-                case -1:
-                    DEBUG_PRINT_LINE("failed to read data from client.");
-                    msger.setStatusLine(500, "Internal Server Error");
-                    if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                    httpmethod    = NULL;
-                    filepath      = NULL;
-                    http_ver      = NULL;
-                    socket_connection = false;
-                    break;
-                default:
-                    DEBUG_PRINT_LINE("Recieved Data: %d",strlen(buffer));
-                    DEBUG_PRINT_LINE("-->\r\n");
-                    DEBUG_PRINT_LINE("%.*s[End of Request]",strlen(buffer),buffer);
-                    //  get HTTP method, File path, HTTP version
-                    httpmethod = strtok(buffer," ");
-                    filepath = strtok(NULL, " ");
-                    http_ver = strtok(NULL, "\r\n");
-                    DEBUG_PRINT_LINE("httpmethod: %s", httpmethod);
-                    DEBUG_PRINT_LINE("file path:  %s", filepath);
-                    DEBUG_PRINT_LINE("http ver :  %s", http_ver);
-                    break;
-            }
-
+            analyzeRequest(buffer, 255, httpmethod, filepath, http_ver);
             //
             //  Response
             //
-            if (strcmp(httpmethod,"GET") == 0 ) {
-                DEBUG_PRINT_LINE("GET request incomming.");
-
-                //  file calibration
-                DEBUG_PRINT_LINE("file opening");
-                fhndl.open(filepath,"rb");
-                if(fhndl.arrival()) {
-                    msger.setStatusLine(200, "OK");
-                    if(msger.setHeaderField("Content-Length", fhndl.getFileSize()))     DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                    if(msger.setHeaderField("Connection", "keep-alive"))                DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else {
-                    if(msger.setStatusLine(404, "NOT FOUND"))                           DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                    if(msger.setHeaderField("Connection", "Close"))                     DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                    DEBUG_PRINT_LINE("NOT FOUND");
-                }
-                if(         !strcmp(fhndl.getSuffix(), "htm" ) ||
-                            !strcmp(fhndl.getSuffix(), "HTM" ) ||
-                            !strcmp(fhndl.getSuffix(), "html") ||
-                            !strcmp(fhndl.getSuffix(), "HTML")) {
-                    if(msger.setHeaderField("Content-Type", "text/html"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else if(  !strcmp(fhndl.getSuffix(), "js"  )) {
-                    if(msger.setHeaderField("Content-Type", "text/javascript"))         DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else if ( !strcmp(fhndl.getSuffix(), "ico" )) {
-                    if(msger.setHeaderField("Content-Type", "image/png"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else if ( !strcmp(fhndl.getSuffix(), "png" ) ||
-                            !strcmp(fhndl.getSuffix(), "PNG" )) {
-                    if(msger.setHeaderField("Content-Type", "image/png"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else if ( !strcmp(fhndl.getSuffix(), "jpg" ) ||
-                            !strcmp(fhndl.getSuffix(), "JPG" )) {
-                    if(msger.setHeaderField("Content-Type", "image/jpg"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-                } else {
-                    msger.setStatusLine(406, "not acceptable");
-                }
-
-                //  Connection timeout field
-                if(msger.setHeaderField("Keep-Alive", "timeouit=15"))                   DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
-
-                //  send response
-                msger.sendHTTPResponse(client_socket, fhndl);
-
-                //file close
-                if( fhndl.close()== 0)
-                    DEBUG_PRINT_LINE("file has closed");
-                else if(EOF)
-                    DEBUG_PRINT_LINE("failed to close the file");
-
-                msger.resetHeader();
-                DEBUG_PRINT_LINE("echo back done.");
-            }
-            if (httpmethod == NULL) {
-                msger.sendHTTPResponse(client_socket);
-                msger.resetHeader();
-                DEBUG_PRINT_LINE("echo back done.");
-                socket_connection = false;
-            }
-            printf("(HTTP_SERVER) Response to Request has done\r\n");
+            sendResponse(httpmethod, filepath, http_ver);
             //
             //
             //
@@ -266,5 +178,104 @@
     listening_flag = false;
     led1 = false;
     return 0;
-    
+
+}
+
+bool HttpServer::analyzeRequest(char* buffer, int buffer_size, char* httpmethod, char* filepath, char* http_ver)
+{
+
+    DEBUG_PRINT_LINE("Request Analysis");
+    switch(client_socket.recv(buffer, buffer_size)) {
+        case 0:
+            DEBUG_PRINT_LINE("recieved buffer is empty.");
+            msger.setStatusLine(400, "No Request");
+            if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+            httpmethod    = NULL;
+            filepath      = NULL;
+            http_ver      = NULL;
+            socket_connection = false;
+            break;
+        case -1:
+            DEBUG_PRINT_LINE("failed to read data from client.");
+            msger.setStatusLine(500, "Internal Server Error");
+            if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+            httpmethod    = NULL;
+            filepath      = NULL;
+            http_ver      = NULL;
+            socket_connection = false;
+            break;
+        default:
+            DEBUG_PRINT_LINE("Recieved Data: %d",strlen(buffer));
+            DEBUG_PRINT_LINE("-->\r\n");
+            DEBUG_PRINT_LINE("%.*s[End of Request]",strlen(buffer),buffer);
+            //  get HTTP method, File path, HTTP version
+            httpmethod = strtok(buffer," ");
+            filepath = strtok(NULL, " ");
+            http_ver = strtok(NULL, "\r\n");
+            DEBUG_PRINT_LINE("httpmethod: %s", httpmethod);
+            DEBUG_PRINT_LINE("file path:  %s", filepath);
+            DEBUG_PRINT_LINE("http ver :  %s", http_ver);
+            break;
+    }
+    return true;
 }
+
+bool HttpServer::sendResponse(char* httpmethod, char*filepath, char* http_ver)
+{
+    if (strcmp(httpmethod,"GET") == 0 ) {
+        DEBUG_PRINT_LINE("GET request incomming.");
+
+        //  file calibration
+        DEBUG_PRINT_LINE("file opening");
+        fhndl.open(filepath,"rb");
+        if(fhndl.arrival()) {
+            msger.setStatusLine(200, "OK");
+            if(msger.setHeaderField("Content-Length", fhndl.getFileSize()))     DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+            if(msger.setHeaderField("Connection", "keep-alive"))                DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else {
+            if(msger.setStatusLine(404, "NOT FOUND"))                           DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+            if(msger.setHeaderField("Connection", "Close"))                     DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+            DEBUG_PRINT_LINE("NOT FOUND");
+        }
+        if(         !strcmp(fhndl.getSuffix(), "htm" ) ||
+                    !strcmp(fhndl.getSuffix(), "HTM" ) ||
+                    !strcmp(fhndl.getSuffix(), "html") ||
+                    !strcmp(fhndl.getSuffix(), "HTML")) {
+            if(msger.setHeaderField("Content-Type", "text/html"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else if(  !strcmp(fhndl.getSuffix(), "js"  )) {
+            if(msger.setHeaderField("Content-Type", "text/javascript"))         DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else if ( !strcmp(fhndl.getSuffix(), "ico" )) {
+            if(msger.setHeaderField("Content-Type", "image/png"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else if ( !strcmp(fhndl.getSuffix(), "png" ) ||
+                    !strcmp(fhndl.getSuffix(), "PNG" )) {
+            if(msger.setHeaderField("Content-Type", "image/png"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else if ( !strcmp(fhndl.getSuffix(), "jpg" ) ||
+                    !strcmp(fhndl.getSuffix(), "JPG" )) {
+            if(msger.setHeaderField("Content-Type", "image/jpg"))               DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+        } else {
+            msger.setStatusLine(406, "not acceptable");
+        }
+
+        //  Connection timeout field
+        if(msger.setHeaderField("Keep-Alive", "timeouit=15"))                   DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger");
+
+        //  send response
+        msger.sendHTTPResponse(client_socket, fhndl);
+
+        //file close
+        if( fhndl.close()== 0)
+            DEBUG_PRINT_LINE("file has closed");
+        else if(EOF)
+            DEBUG_PRINT_LINE("failed to close the file");
+
+        msger.resetHeader();
+        DEBUG_PRINT_LINE("echo back done.");
+    }
+    if (httpmethod == NULL) {
+        msger.sendHTTPResponse(client_socket);
+        msger.resetHeader();
+        DEBUG_PRINT_LINE("echo back done.");
+        socket_connection = false;
+    }
+    printf("(HTTP_SERVER) Response to Request has done\r\n");
+}