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:
7:184c6f1ace94
Parent:
6:4eb469f51570
Child:
8:b013075de2e4
--- a/HTTP_SERVER.cpp	Sat Nov 26 19:15:20 2016 +0000
+++ b/HTTP_SERVER.cpp	Mon Nov 28 08:27:42 2016 +0000
@@ -137,15 +137,17 @@
                     if(msger.setHeaderField("Connection", "keep-alive"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
                 } else {
                     if(msger.setStatusLine(404, "NOT FOUND"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
-                    if(msger.setHeaderField("Connection", "close"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
+                    if(msger.setHeaderField("Connection", "Close"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
                     DEBUG_PRINT_LINE("(HTTP_SERVER) NOT FOUND\r\n");
                 }
-                if(     strcmp(fhandl.getSuffix(), "htm" )  ||
-                        strcmp(fhandl.getSuffix(), "HTM" )  ||
-                        strcmp(fhandl.getSuffix(), "html")  ||
-                        strcmp(fhandl.getSuffix(), "HTML")) {
+                if(     !strcmp(fhandl.getSuffix(), "htm" )  ||
+                        !strcmp(fhandl.getSuffix(), "HTM" )  ||
+                        !strcmp(fhandl.getSuffix(), "html")  ||
+                        !strcmp(fhandl.getSuffix(), "HTML")) {
                     if(msger.setHeaderField("Content-Type", "text/html"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
-                } else if (strcmp(fhandl.getSuffix(), "ico" )  ) {
+                } else if(!strcmp(fhandl.getSuffix(), "js" )){
+                    if(msger.setHeaderField("Content-Type", "text/javascript"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
+                } else if (!strcmp(fhandl.getSuffix(), "ico" )  ) {
                     if(msger.setHeaderField("Content-Type", "image/png"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
                 } else {
                     msger.setStatusLine(406, "not acceptable");
@@ -154,104 +156,34 @@
                 //  Connection timeout field
                 if(msger.setHeaderField("Keep-Alive", "timeouit=15"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
 
-                //  Define behaviour of server according to Request Header lines
-                //  Apply request header field to response header field
-                char* field_Connection = NULL;
-                char* field_Upgrade = NULL;
-                char* field_Sec_WebSocket_Key = NULL;
-                char* field_Sec_WebSocket_Version = NULL;
-                char* field_Origin = NULL;
-                do {
-                    //Analyze the header feilds
-                    header_field_name = strtok(NULL, ":");
-                    header_field_name++;
-                    header_field_val  = strtok(NULL, "\r\n");
-                    header_field_val++;
-
-                    if(header_field_name - 1 != NULL) {
-                        if(!strcmp(header_field_name, "Connection")) {
-                            field_Connection = header_field_val;
-                        } else if(!strcmp(header_field_name, "Upgrade")) {
-                            field_Upgrade = header_field_val;
-                        } else if(!strcmp(header_field_name, "Sec-WebSocket-Key") ) {
-                            field_Sec_WebSocket_Key = header_field_val;
-                        } else if(!strcmp(header_field_name, "Sec-WebSocket-Version") ) {
-                            field_Sec_WebSocket_Version = header_field_val;
-                        } else if(!strcmp(header_field_name, "Origin") ) {
-                            field_Origin = header_field_val;
-                        }
-                    } else {
-                        break;
-                    }
-#ifdef DEBUG
-                    printf("(HTTP_SERVER) *header_field_name adr: %d %s\r\n", header_field_name - 1, header_field_name);
-                    printf("(HTTP_SERVER)  header_field_val  adr: %d %s\r\n", header_field_val  - 1, header_field_val);
-#endif
-                } while(1);
-
-                //  if the request is to switching to the WebSocket Server
-                if(!strcmp(field_Connection, "Upgrade")) {
-                    if(     !strcmp(field_Upgrade, "websocket")         &&
-                            field_Sec_WebSocket_Key != NULL             &&
-                            !strcmp(field_Sec_WebSocket_Version, "13")  &&
-                            field_Origin != NULL) {
-                        DEBUG_PRINT_LINE("(HTTP_SERVER) Communication Protocol will be Upgraded to Websocket! \r\n");
-                        /*
-                        if(ws.isValid()){
-                            ...//calcurate the key
-                            msger.resetHeader();
-                            msger.setStatusLine(101, "Switching Protocols");
-                            if(msger.msger.setHeaderField("Connection", "Upgrade"))             printf("(HTTP_SERVER)buffer over flow@ResposeMessenger");
-                            if(msger.msger.setHeaderField("Upgrade", "websocket"))              printf("(HTTP_SERVER)buffer over flow@ResposeMessenger");
-                            if(msger.msger.setHeaderField("Sec-WebSocket-Accept", ...))         printf("(HTTP_SERVER)buffer over flow@ResposeMessenger");
-                            if(msger.msger.setHeaderField("Access-Control-Allow-Origin", "*"))  printf("(HTTP_SERVER)buffer over flow@ResposeMessenger");
-                        }
-                        else goto AAA;
-                        */
-                    } else {
-                        //AAA:
-                        DEBUG_PRINT_LINE("(HTTP_SERVER) Communication Protocol won't be Upgraded.\r\n");
-                        msger.setStatusLine(426, "Upgrade Required");
-                        if(msger.setHeaderField("Connection", "Close"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger");
-                    }
-                }
-
-
-#ifdef DEBUG
-                //printf("status code :  %d\r\n", status_code);
-                //printf("content type:  %s\r\n", content_type);
-#endif
-
                 //  send response
                 msger.sendHTTPResponse(tcpcon, fhandl);
 
                 //file close
-#ifdef DEBUG
-                if(
-#endif
-                    fhandl.close()
-#ifndef DEBUG
-                    ;
-#endif
-#ifdef DEBUG
-                    == 0)
-                    printf("(HTTP_SERVER) file has closed\r\n");
+                if( fhandl.close()== 0)
+                    DEBUG_PRINT_LINE("(HTTP_SERVER) file has closed\r\n");
                 else if(EOF)
-                    printf("(HTTP_SERVER) failed to close the file\r\n");
-#endif
+                    DEBUG_PRINT_LINE("(HTTP_SERVER) failed to close the file\r\n");
+                    
+                msger.resetHeader();
+                printf("(HTTP_SERVER) echo back done.\r\n");
+            }
+            if (httpmethod == NULL) {
+                msger.sendHTTPResponse(tcpcon);
                 msger.resetHeader();
                 printf("(HTTP_SERVER) echo back done.\r\n");
             }
-            if (strcmp(httpmethod, NULL) == 0 ) {
-                msger.sendHTTPResponse(tcpcon);
-            }
             printf("(HTTP_SERVER) Response to Request has done\r\n");
+            //
+            //
+            //
         }
         printf("(HTTP_SERVER) close connection.\r\ntcp server is listening...\r\n");
         tcpcon.close();
-        //tcpsvr.close();
         led2 = false;
     }
+    tcpsvr.close();
+    listening_flag = false;
     led1 = false;
     return 0;
 }
\ No newline at end of file