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:
- 2:33714d7c0f45
- Parent:
- 1:3a1fe94c6e42
- Child:
- 3:59884bc0a238
diff -r 3a1fe94c6e42 -r 33714d7c0f45 HTTP_SERVER.cpp --- a/HTTP_SERVER.cpp Sat Nov 26 16:49:04 2016 +0000 +++ b/HTTP_SERVER.cpp Sat Nov 26 18:06:53 2016 +0000 @@ -153,24 +153,72 @@ if(msger.setHeaderField("Keep-Alive", "timeouit=15"))printf("buffer over flow"); // Define behaviour of server according to Request Header lines + // Apply request header field to response header field + char* field_Connection = NULL, + field_Upgrade = NULL, + field_Sec_WebSocket_Key = NULL, + field_Sec-WebSocket_Version = NULL, + field_Origin = NULL; do { - //strtok(NULL, "\r\n"); + //Analyze the header feilds header_field_name = strtok(NULL, ":"); header_field_name++; - //strtok(NULL, " "); header_field_val = strtok(NULL, "\r\n"); header_field_val++; + + if(header_field_name != NULL) { + switch(header_field_name) { + case "Connection": + filed_Connection = header_field_val; + break; + case "Upgrade": + field_Upgrade = header_field_val; + break; + case "Sec-WebSocket-Key": + field_Sec_WebSocket_Key = header_field_val; + break; + case "Sec-WebSocket-Version": + field_Sec_WebSocket_Version = header_field_val; + break; + case "Origin": + field_Origin = header_field_val; + break; + default: + break; + } + } 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 - // Apply request header field to response header field - if(header_field_name - 1 != NULL) { - if(msger.setHeaderField(header_field_name, header_field_val))printf("buffer over flow"); - } else { - break; + } while(1); + + // if the request is to switching to the WebSocket Server + if( !strcmp(filed_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()){ + 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"); + ... } - } while(1); + */ + } else { + 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"); + } + #ifdef DEBUG //printf("status code : %d\r\n", status_code);