Simple HTTP Server with one page index.html stored inside MBED as char vector and javascript to update a table content
Fork of HTTP_SERVER by
Diff: HTTP_SERVER.cpp
- Revision:
- 5:dedbaa9c633b
- Parent:
- 4:b551799053c6
- Child:
- 6:4eb469f51570
diff -r b551799053c6 -r dedbaa9c633b HTTP_SERVER.cpp --- a/HTTP_SERVER.cpp Sat Nov 26 18:30:33 2016 +0000 +++ b/HTTP_SERVER.cpp Sat Nov 26 18:54:10 2016 +0000 @@ -91,6 +91,7 @@ case 0: printf("(HTTP_SERVER) recieved buffer is empty.\r\n"); msger.setStatusLine(400, "No Request"); + if(msger.setHeaderField("Connection", "close"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); httpmethod = NULL; filepath = NULL; http_ver = NULL; @@ -98,6 +99,7 @@ case -1: printf("(HTTP_SERVER) failed to read data from client.\r\n"); msger.setStatusLine(500, "Internal Server Error"); + if(msger.setHeaderField("Connection", "close"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); httpmethod = NULL; filepath = NULL; http_ver = NULL; @@ -131,26 +133,26 @@ fhandl.open(filepath,"rb"); if(fhandl.arrival()) { msger.setStatusLine(200, "OK"); - if(msger.setHeaderField("Content-Length", fhandl.getFileSize()))printf("buffer over flow"); - if(msger.setHeaderField("Connection", "keep-alive"))printf("buffer over flow"); + if(msger.setHeaderField("Content-Length", fhandl.getFileSize()))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); + if(msger.setHeaderField("Connection", "keep-alive"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); } else { - if(msger.setStatusLine(404, "NOT FOUND"))printf("buffer over flow"); - if(msger.setHeaderField("Connection", "close"))printf("buffer over flow"); + 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"); 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(msger.setHeaderField("Content-Type", "text/html"))printf("buffer over flow"); + if(msger.setHeaderField("Content-Type", "text/html"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); } else if (strcmp(fhandl.getSuffix(), "ico" ) ) { - if(msger.setHeaderField("Content-Type", "image/png"))printf("buffer over flow"); + if(msger.setHeaderField("Content-Type", "image/png"))printf("(HTTP_SERVER)buffer over flow @ ResponseMessenger"); } else { msger.setStatusLine(406, "not acceptable"); } // Connection timeout field - if(msger.setHeaderField("Keep-Alive", "timeouit=15"))printf("buffer over flow"); + 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 @@ -188,29 +190,30 @@ } while(1); // if the request is to switching to the WebSocket Server - if( !strcmp(field_Connection, "Upgrade") && - !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"); + 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"); } - 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("buffer over flow"); }