Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Files at this revision

API Documentation at this revision

Comitter:
nixnax
Date:
Sun Aug 13 02:18:18 2017 +0000
Parent:
127:3ea6f776e287
Child:
129:b8a0b0e8cff1
Commit message:
Added WebSocket Service.; TCP-header checksum bug on odd lfixed.; Comments.;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sha1.cpp Show annotated file Show diff for this revision Revisions of this file
sha1.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Aug 09 19:39:05 2017 +0000
+++ b/main.cpp	Sun Aug 13 02:18:18 2017 +0000
@@ -1,7 +1,8 @@
 // PPP-Blinky - "The Most Basic Internet Of Things"
 
-// A Tiny Webserver Using Windows XP/7/8/10/Linux Dial-Up Networking Over A Serial Port.
+// A Tiny HTTP Webserver Using Windows XP/7/8/10/Linux Dial-Up Networking Over A Serial Port.
 // Also receives UDP packets and responds to ping (ICMP Echo requests)
+// Also: WebSocket Service - see https://en.wikipedia.org/wiki/WebSocket
 
 // Copyright 2016/2017 Nicolas Nackel aka Nixnax. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
@@ -30,9 +31,13 @@
 // in the above command 172.10.10.1 is the adapter IP, and 172.10.10.2 is the IP of PPP-Blinky.
 // See also https://en.wikipedia.org/wiki/Point-to-Point_Protocol_daemon
 
+// To try the WebSocket, enter the following in your browser console:
+// var x = new WebSocket("ws://172.10.10.2"); x.onmessage=function(msg){console.log(msg.data);} ; x.send("Hello WebSocket World");
+
 // Ok, enough talking, time to check out some code!!
 
 #include "mbed.h"
+#include "sha1.h"
 
 // The #define below enables/disables a second (OPTIONAL) serial port that prints out interesting diagnostic messages.
 // Change to SERIAL_PORT_MONITOR_YES to enable diagnostics messages. You need to wire a second serial port to your mbed hardware to monitor this.
@@ -42,14 +47,14 @@
 // here we define the OPTIONAL, second debug serial port for the various target boards
 // insert your target board's port here if it's not in yet - if it works, please send it to me - thanks!!!
 #ifdef SERIAL_PORT_MONITOR_YES
-#if defined(TARGET_LPC1768) 
+#if defined(TARGET_LPC1768)
 Serial xx(p9, p10); // Second serial port on LPC1768 - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_L053R8) || defined(TARGET_NUCLEO_L476RG) || defined(TARGET_NUCLEO_F401RE)
 Serial xx(PC_10, PC_11); // Second serial port on NUCLEO boards - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
 #elif defined(TARGET_LPC11U24)
 #error The LPC11U24 does not have a second serial port to use for debugging - change SERIAL_PORT_MONITOR_YES back to SERIAL_PORT_MONITOR_NO
-#elif defined (TARGET_KL46Z)
-Serial xx(PTE0,PTE1); // Second serial port on NUCLEO boards - not required to run, if you get compile error here, change #define SERIAL_PORT_MONITOR_YES to #define SERIAL_PORT_MONITOR_NO
+#elif defined (TARGET_KL46Z) || (TARGET_KL25Z)
+Serial xx(PTE0,PTE1); // Second serial port on FRDM-KL46Z board
 #elif defined(YOUR_TARGET_BOARD_NAME_HERE)
 Serial xx(p9, p10); // insert your board's debug serial port pins here - and please send it to me if it works
 #else
@@ -78,11 +83,11 @@
 <!DOCTYPE html>\
 <html>\
 <head>\
-<title>mbed-PPP-Blinky</title>\
+<title>mbed PPP-Blinky</title>\
 <script>\
 window.onload=function(){\
 setInterval(function(){function x(){return document.getElementById('w');};\
-x().textContent = parseInt(x().textContent)+1;},100);};\
+x().textContent=parseInt(x().textContent)+1;},100);};\
 </script>\
 </head>\
 <body style=\"font-family: sans-serif; font-size:30px; color:#807070\">\
@@ -90,7 +95,7 @@
 <h1 id=\"w\" style=\"text-align:center;\">0</h1>\
 <h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\
 </body>\
-</html>"; // around 464 bytes long
+</html>"; // around 461 bytes long
 
 // The serial port on your mbed hardware. Your PC should be configured to view this port as a standard dial-up networking modem.
 // On Windows the model type of the modem should be selected as "Communications cable between two computers"
@@ -200,7 +205,7 @@
     while( *nextChar != 0 ) {
         putcWhileCheckingInput( *nextChar ); // write one character to debug port while checking input
         nextChar++;
-    } 
+    }
 #endif
 }
 
@@ -294,14 +299,14 @@
         void determinePacketType(); // declaration only
         determinePacketType();
     } else {
-#ifdef REPORT_FCS_ERROR_YES            
-            char pbuf[50]; // local print buffer
-            fillbuf();
-            sprintf(pbuf, "\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // print a debug line
-            fillbuf();
-            putsWhileCheckingInput( pbuf );
-            if(0) dumpPPPFrame(); // set to 1 to dump frames with errors in them
-#endif        
+#ifdef REPORT_FCS_ERROR_YES
+        char pbuf[50]; // local print buffer
+        fillbuf();
+        sprintf(pbuf, "\nPPP FCS(crc) Error CRC=%x Length = %d\n",ppp.pkt.crc,ppp.pkt.len); // print a debug line
+        fillbuf();
+        putsWhileCheckingInput( pbuf );
+        if(0) dumpPPPFrame(); // set to 1 to dump frames with errors in them
+#endif
     }
 }
 
@@ -344,7 +349,7 @@
     ppp.pkt.buf[4]=1; // change code to request
     ppp.pkt.buf[7]=4; // no options in this request
     ppp.pkt.len=10; // no options in this request shortest ipcp packet possible (4 ppp + 4 ipcp + 2 crc)
-    send_pppFrame(); // send our request 
+    send_pppFrame(); // send our request
 }
 
 void ipcpAckHandler()
@@ -423,23 +428,22 @@
 
 unsigned int dataCheckSum(unsigned char * ptr, int len)
 {
-    unsigned int sum=0;
+    unsigned int i,hi,lo,sum;
     unsigned char placeHolder;
     if (len&1) {
-        placeHolder = ptr[len];    // when length is odd stuff in a zero byte
-        ptr[len]=0;
+        placeHolder = ptr[len];
+        ptr[len]=0;  // if the byte count is odd, insert one extra zero byte is after the last real byte because we sum byte PAIRS
     }
-    for (int i=0; i<len/2; i++) {
+    sum=0;
+    i=0;
+    while ( i<len ) {
         fillbuf();
-        unsigned int hi = *ptr;
-        ptr++;
-        unsigned int lo = *ptr;
-        ptr++;
-        unsigned int val = ( (hi<<8) | lo );
-        sum = sum + val;
+        hi = ptr[i++];
+        lo = ptr[i++];
+        sum = sum + ( (hi<<8) | lo );
     }
     if (len&1) {
-        ptr[len] = placeHolder;    // restore the last byte for odd lengths
+        ptr[len] = placeHolder;    // restore the extra byte we made zero
     }
     sum = (sum & 0xffff) + (sum>>16);
     sum = (sum & 0xffff) + (sum>>16); // sum one more time to catch any carry from the carry
@@ -468,7 +472,7 @@
 }
 
 void ICMPpacket()   // internet control message protocol
-{ 
+{
     char * ipPkt = ppp.pkt.buf+4; // ip packet start
     char * pktLen = ipPkt+2;
     int packetLength = (pktLen[0]<<8) | pktLen[1]; // icmp packet length
@@ -494,7 +498,7 @@
             fillbuf();
             putsWhileCheckingInput( pbuf );
         }
-#endif        
+#endif
         char src[4];
         char dst[4];
         memcpy(src, srcAdr,4);
@@ -533,7 +537,7 @@
         if (v0) {
             debugPrintf("ICMP type=%d \n", icmpType[0]);
         }
-    } 
+    }
 }
 
 void IGMPpacket()   // internet group management protocol
@@ -599,7 +603,6 @@
     unsigned int ack = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
     if (seq && ack) {} // shut up the compiler about unused variables
     int flags = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1];
-
     char flagInfo[9]; // text string presenting the 8 most important TCP flags
 #define PRINT_ALL_TCP_FLAGS_YES
 #ifdef PRINT_ALL_TCP_FLAGS_YES
@@ -633,19 +636,85 @@
 #endif
 }
 
+void enc64(char * in, char * out, int len)
+{
+    const static char lut [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+    int i,j,a,b,c;
+    i=0;
+    j=0;
+    while(1) {
+        if (i<len) {
+            a = in[i++];
+            out[j++] = lut[ ( (a >> 2) & 0x3f) ];
+        } else break;
+        if (i<len) {
+            b = in[i++];
+            out[j++] = lut[ ( (a << 4) & 0x30) | ( (b >> 4) & 0x0f) ];
+            out[j++] = lut[ ( (b << 2) & 0x3c)  ];
+        } else out[j++] = '=';
+        if (i<len) {
+            c = in[i++];
+            j--;
+            out[j++] = lut[ ( (b << 2) & 0x3c) | ( (c >> 6) & 0x03) ];
+            out[j++] = lut[ ( (c >> 0) & 0x3f) ];
+        } else out[j++] = '=';
+    }
+    out[j]=0;
+}
+
+// we end up here if we enter the following javascript in a web browser console: x = new WebSocket("ws://172.10.10.2");
+int webSocketHandler(char * dataStart)
+{
+    if (v0) putsWhileCheckingInput("WebSocket Request\n");
+    int n=0; // byte counter
+    char * key = strstr(dataStart, "Sec-WebSocket-Key: "); // search for the key in the payload
+    if (key != NULL) {
+        char challenge [70];
+        strncpy(challenge,key+19,70); // a local buffer
+        *strchr(challenge,'\r')=0; // insert null so we can use sprintf
+        strncat(challenge,"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",70); // append websocket gui code
+        char shaOutput [20]; // sha1 output
+        sha1( shaOutput, challenge, strlen(challenge));
+        char encOut[50];
+        enc64( shaOutput, encOut, 20);
+        char * versionstring = strstr(dataStart, "Sec-WebSocket-Version:");
+        char * version = challenge;
+        strncpy(version, versionstring,70); // copy version string
+        *strchr(version,'\r')=0; // null terminate so we can sprintf it
+        memset(dataStart,0,500); // blank out old data befor send the websocket response header
+        n=n+sprintf(dataStart+n, "HTTP/1.1 101 Switching Protocols\r\n");
+        n=n+sprintf(dataStart+n, "Upgrade: websocket\r\n");
+        n=n+sprintf(dataStart+n, "Connection: Upgrade\r\n");
+        n=n+sprintf(dataStart+n, "Sec-WebSocket-Accept: %s\r\n",encOut);
+        n=n+sprintf(dataStart+n, "%s\r\n",version);
+        n=n+sprintf(dataStart+n, "mbed-Code:  PPP-Blinky\r\n");
+        n=n+sprintf(dataStart+n, "\r\n"); // websocket response header ending
+    }
+    return n; // this response should satisfy a web browser's websocket protocol request
+}
+
+#define TCP_FLAG_ACK (1<<4)
+#define TCP_FLAG_SYN (1<<1)
+#define TCP_FLAG_PSH (1<<3)
+#define TCP_FLAG_RST (1<<2)
+#define TCP_FLAG_FIN (1<<0)
+
 int httpResponse(char * dataStart)
 {
     int n=0; // number of bytes we have printed so far
+    n = webSocketHandler( dataStart ); // test for and handle WebSocket upgrade requests
+    if (n>0) return n; // if it's a WebSocket we already have the response, so return
+
     int nHeader; // byte size of HTTP header
     int contentLengthStart; // index where HTML starts
-    int xFetch, httpGetRoot; // temporary storage of strncmp results
+    int httpGetx, httpGetRoot; // temporary storage of strncmp results
 
     ppp.httpPageCount++; // increment the number of frames we have made
 
-    httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13); // found GET, respond to both HTTP/1.<anything>
-    xFetch = strncmp(dataStart, "GET /x", 6); // found GET /x , respond to both HTTP/1.<anything>
+    httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13);  // found a GET to the root directory
+    httpGetx    = strncmp(dataStart, "GET /x", 6);          // found a GET to /x which we will treat special (anything starting with /x, e.g. /x, /xyz, /xABC?pqr=123
     // for example, you could try this using netcat (nc):    echo "GET /x" | nc 172.10.10.2
-    if( (httpGetRoot==0) || (xFetch==0) ) {
+    if( (httpGetRoot==0) || (httpGetx==0) ) {
         n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: mbed-PPP-Blinky\r\n"); // 200 OK header
     } else {
         n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: mbed-PPP-Blinky\r\n"); // 404 header
@@ -661,12 +730,12 @@
         memcpy(n+dataStart,rootWebPage,sizeof(rootWebPage));
         n = n + sizeof(rootWebPage);
     } else {
-        if (xFetch == 0) { // the page request started with "GET /x"
+        if (httpGetx == 0) { // the page request started with "GET /x" - here we treat anything starting with /x special:
 
 #define W3C_COMPLIANT_RESPONSE_NO
 // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response
 #ifdef W3C_COMPLIANT_RESPONSE_YES
-            n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required elements)
+            n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (W3C.org required elements)
             n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpPageCount); // body = the http frame count
 #else
 #define BENCHMARK_USING_BROWSER_NO /* set to _YES if you want to use your browser as a benchmark tool */
@@ -674,47 +743,54 @@
             // semd a small browser script that will reload the page after 10 ms - handy for benchmarking with your web browser, use http://172.10.10.2/x
             n=n+sprintf(n+dataStart, "<script>setTimeout(function(){location.reload();},10);</script><body>%d</body>",ppp.httpPageCount);
 #else
-            n=n+sprintf(n+dataStart,"%d",ppp.httpPageCount); // not valid html but fast, most browsers and curl are ok with it
+            // /x is a very short page, in fact, it is only a decimal number showing the http Page count
+            n=n+sprintf(n+dataStart,"%d",ppp.httpPageCount); // not really valid html but fast - most browsers and curl are ok with it
 #endif
 #endif
-
         } else {
             // all other requests get 404 Not Found response with a http frame count - nice for debugging
-            n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>ppp-blinky-mbed</title>"); // html title (required element)
+            n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed PPP-Blinky</title>"); // html title (required element)
             n=n+sprintf(n+dataStart,"<body>Not Found</body>"); // not found message
         }
     }
-    while( (n%4)!= 2) n=n+sprintf(n+dataStart," "); // insert spaces until n is exactly two away from a multiple of four
-    n=n+sprintf(n+dataStart,"\r\n");  // add the last two characters (\r\n) - n is now an exact multiple of four
-
 #define CONTENTLENGTHSIZE 5
     char contentLengthString[CONTENTLENGTHSIZE+1];
     snprintf(contentLengthString,CONTENTLENGTHSIZE+1,"%*d",CONTENTLENGTHSIZE,n-nHeader); // print Content-Length with leading spaces and fixed width equal to csize
     memcpy(dataStart+contentLengthStart, contentLengthString, CONTENTLENGTHSIZE); // copy Content-Length to it's place in the send buffer
-
-    if (v2) {
-        // debugPrintf("HTTP Response: HTTP-header %d HTTP-content %d HTTP-total %d\n",nHeader,n-nHeader,n);
-    }
     return n; // total byte size of our response
 }
 
-// if not an http response we just report the number of bytes received
+// this is the response if we have TCP data but it's not an HTTP GET
 // this is handy when you for example want to use netcat (nc.exe) to talk to PPP-Blinky
-int tcpResponse(char * dataStart, int len)
+// this could also be a websocket receive event - especially if the first byte is 0x81 (websocket data push)
+int tcpResponse(char * dataStart, int len, int * outFlags)
 {
     int n=0; // number of bytes we have printed so far
-    n=n+sprintf(n+dataStart,"Got %04d bytes.\n",len); // report the number of bytes received
-    while( (n%4)!= 0) n=n+sprintf(n+dataStart,"*"); // insert spaces until n is exactly two away from a multiple of four
-    if (v2) {
-        // debugPrintf("TCP response %d bytes\n",n);
-    }
+    if (dataStart[0] == 0x81) { // check if this is a websocket push message
+        // this is most likely a websocket push message. you get this when you enter this in your browser console: x.send("my message");
+        if (0) putsWhileCheckingInput( "Got data from websocket send()\n" );
+
+        // for now we simply echo the websocket data back to the client - the client should therefore see an onmessage event
+        // to display the echoed data in your browser, enter the following into the browser console: x.onmessage = function(msg){ console.log( msg.data ); }
+        if (1) {
+            char mask [4];
+            memcpy ( mask, dataStart+2, 4); // websocket messages are "masked", so first we obtain the 4-byte mask
+            int websocketMessageSize = len - 6;  // 1 byte prefix (0x81), 1 byte, 4 bytes mask = 6 bytes
+            if((dataStart[1]&0x80)==0x80) // test if the mask bit is set, which means all data is xor'ed with the mask
+                for (int i=0; i<websocketMessageSize; i++) dataStart[i+6]^= mask[i%4]; // unmask each byte with one of the mask bytes
+            dataStart[1] = len-2; // add four extra bytes to the message length because we don't use mask bytes for the send
+            memcpy(dataStart+2, "Got:",4); // insert our own text into the four mask bytes
+            n = len; // our response size remains exactly the same length as what we received
+        }
+    } else if ( (dataStart[0]==0x88) && (dataStart[1]==0x80) && (len == 6) ) { // test for a websocket close request
+        *outFlags |= TCP_FLAG_FIN; // set the fin flag to close the websocket request
+    } else if (v1) putsWhileCheckingInput("TCP data received\n");
     return n; // total byte size of our response
 }
 
 void tcpHandler()
 {
     // IP header
-
     char * ipPkt = ppp.pkt.buf+4; // ip packet start
     char * ihl =        ipPkt;    // bottom 4 bits
     char * pktLen =     ipPkt+2;  // 2 bytes
@@ -727,7 +803,6 @@
     int packetLength = (pktLen[0]<<8)|pktLen[1]; // ip total packet length
 
     // TCP header
-
     char * tcp             = ppp.pkt.buf+4+headerSizeIP; // start of tcp packet
     char * srctcp        = tcp + 0;  // 2 bytes
     char * dsttcp        = tcp + 2;  // 2 bytes
@@ -737,32 +812,20 @@
     char * flagbitstcp   = tcp + 12; // 9 bits
     char * windowsizetcp = tcp + 14; // 2 bytes
     char * checksumtcp   = tcp + 16; // 2 bytes
-    
+
     if(ident) {}; // shut up unused variable reference warning
-
     int tcpSize = packetLength - headerSizeIP;
     int headerSizeTCP = ((offset[0]>>4)&0x0f)*4; // size of tcp header only
     int protocolIP = protocol[0];
-
     char * tcpDataIn = tcp + headerSizeTCP; // start of data block after TCP header
     int tcpDataSize = tcpSize - headerSizeTCP; // size of data block after TCP header
     char * tcpDataOut = tcp + 20; // start of outgoing data
-
     unsigned int seq_in = (seqtcp[0]<<24)|(seqtcp[1]<<16)|(seqtcp[2]<<8)|(seqtcp[3]);
     unsigned int ack_in = (acktcp[0]<<24)|(acktcp[1]<<16)|(acktcp[2]<<8)|(acktcp[3]);
-
     unsigned int ack_out = seq_in + tcpDataSize;
     unsigned int seq_out = ack_in; // use their version of our current sequence number
 
-#define TCP_FLAG_ACK (1<<4)
-#define TCP_FLAG_SYN (1<<1)
-#define TCP_FLAG_PSH (1<<3)
-#define TCP_FLAG_RST (1<<2)
-#define TCP_FLAG_FIN (1<<0)
-
-    // first we shorten the TCP response header to only 20 bytes.
-    // this means we ignore all TCP option requests
-
+    // first we shorten the TCP response header to only 20 bytes. This means we ignore all TCP option requests
     tcpSize = 20; // shorten total TCP packet size to 20 bytes (no data)
     headerSizeTCP = 20; // shorten outgoing TCP header size 20 bytes
     offset[0] =  (headerSizeTCP/4)<<4; // shorten tcp header size to 20 bytes
@@ -773,31 +836,32 @@
     int dataLen = 0; // most of our responses will have zero TCP data, only a header
     int flagsOut = TCP_FLAG_ACK; // the default case is an ACK packet
     int flagsTCP = ((flagbitstcp[0]&1)<<8)|flagbitstcp[1]; // the tcp flags we received
-
     windowsizetcp[0] = (700 >> 8  );   // tcp window size hi byte
     windowsizetcp[1] = (700 & 0xff);   // tcp window size lo byte
 
     // A sparse TCP flag interpreter that implements stateless TCP connections
 
     switch ( flagsTCP ) {
+        case TCP_FLAG_ACK:
+            return;
         case TCP_FLAG_SYN:
             flagsOut = TCP_FLAG_SYN | TCP_FLAG_ACK; // something wants to connect - acknowledge it
             seq_out = seq_in+0x10000000U; // create a new sequence number using their sequence as a starting point, increase the highest digit
             ack_out++; // for SYN flag we have to increase the sequence by 1
             break;
         case TCP_FLAG_ACK | TCP_FLAG_PSH:
-            if ( strncmp(tcpDataIn, "GET /", 5) == 0) { // check for an http GET command
-                flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN | TCP_FLAG_PSH; // set outgoing FIN flag to ask them to close from their side
+            if ( (strncmp(tcpDataIn, "GET /", 5) == 0) ) { // check for an http GET command
+                flagsOut = TCP_FLAG_ACK | TCP_FLAG_PSH; // set outgoing FIN flag to ask them to close from their side
                 dataLen = httpResponse(tcpDataOut); // send an http response
             } else {
-                dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, send a packet reporting number of received bytes
+                dataLen = tcpResponse(tcpDataOut,tcpDataSize, &flagsOut); // not a web request, send a packet reporting number of received bytes
             }
             break;
         case TCP_FLAG_FIN:
         case TCP_FLAG_FIN | TCP_FLAG_ACK:
         case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK:
             flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set outgoing FIN flag to ask them to close from their side
-            ack_out++; // for FIN flag we always have to increase sequence by 1
+            ack_out++; // for FIN flag we have to increase the sequence by 1
             break;
         default:
             return; // ignore remaining packets
@@ -805,12 +869,10 @@
 
     // The TCP flag handling is now done
     // first we swap source and destination TCP addresses and insert the new ack and seq numbers
-
     char tempHold[12]; // it's 12 long because we later reuse it when building the TCP pseudo-header
     memcpy(tempHold, srcAdr,4);
     memcpy(srcAdr, dstAdr,4);
     memcpy(dstAdr, tempHold,4); // swap ip address source/dest
-
     memcpy(tempHold, srctcp,2);
     memcpy(srctcp, dsttcp,2);
     memcpy(dsttcp, tempHold,2); // swap ip port source/dest
@@ -819,7 +881,6 @@
     acktcp[1]=ack_out>>16;
     acktcp[2]=ack_out>>8;
     acktcp[3]=ack_out>>0; // save ack 32-bit integer
-
     seqtcp[0]=seq_out>>24;
     seqtcp[1]=seq_out>>16;
     seqtcp[2]=seq_out>>8;
@@ -829,8 +890,6 @@
 
     // increment our outgoing ip packet counter
     ppp.ip.ident++; // get next ident number for our packet
-    //ident[0] = ppp.ip.ident>>8;
-    //ident[1] = ppp.ip.ident>>0; // insert OUR ident
 
     // Now we recalculate all the header sizes
     int newPacketSize = headerSizeIP + headerSizeTCP + dataLen; // calculate size of the outgoing packet
@@ -846,7 +905,6 @@
 
     // now we have to build the so-called 12-byte TCP "pseudo-header" in front of the TCP header (containing some IP header values) in order to correctly calculate the TCP checksum
     // this header  contains the most important parts of the IP header, i.e. source and destination address, protocol number and data length.
-
     char * pseudoHeader = tcp-12; // mark the start of the TCP pseudo-header
     memcpy(tempHold, pseudoHeader, 12); // preserve the 12 bytes of the IP header where the TCP pseudo-Header will be built
     memcpy( pseudoHeader+0, srcAdr, 8); // IP source and destination addresses from IP header
@@ -869,6 +927,7 @@
         wait_us(10); // wait less than 1 character duration at 115200
     }
     send_pppFrame(); // All preparation complete - send the TCP response
+    memset(ppp.pkt.buf+44,0,500); // flush out traces of previous data that we may scan for
 }
 
 void dumpDataTCP()
@@ -892,12 +951,10 @@
 }
 
 void TCPpacket()
-{ 
+{
     dumpHeaderIP(0);     // dump incoming packet header
     dumpHeaderTCP(0);   // dump incoming packet header
-    if (v2) {
-        dumpDataTCP();
-    } 
+    if (v2) dumpDataTCP();
     tcpHandler();
 }
 
@@ -984,9 +1041,7 @@
 
 void discardedFrame()
 {
-    if (v0) {
-        debugPrintf("Frame is not IP, IPCP or LCP: %02x %02x %02x %02x\n", ppp.pkt.buf[0],ppp.pkt.buf[1],ppp.pkt.buf[2],ppp.pkt.buf[3]);
-    }
+    if (v0) debugPrintf("Frame is not IP, IPCP or LCP: %02x %02x %02x %02x\n", ppp.pkt.buf[0],ppp.pkt.buf[1],ppp.pkt.buf[2],ppp.pkt.buf[3]);
 }
 
 void determinePacketType()
@@ -1019,7 +1074,7 @@
     }
 }
 
-void wait_for_PPP_frame()
+void wait_for_PPP_frame() // scan the PPP serial input stream for frame start markers
 {
     while(1) {
         fillbuf(); // handle received characters
@@ -1029,13 +1084,13 @@
             if (rx==FRAME_7E) {
                 if (ppp.firstFrame) { // is this the start of the first frame start
                     ppp.firstFrame=0;
-                    ppp.rx.rtail = ppp.rx.tail; // update real-time tail with virtual tail
+                    ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
                     ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where first frame started
                 }  else {
                     ppp.hdlc.frameEndIndex=oldTail; // mark the frame end character
                     processPPPFrame(ppp.hdlc.frameStartIndex, ppp.hdlc.frameEndIndex); // process the frame
-                    ppp.rx.rtail = ppp.rx.tail; // update real-time tail with virtual tail
-                    ppp.hdlc.frameStartIndex = ppp.rx.tail; // where next frame will start
+                    ppp.rx.rtail = ppp.rx.tail; // update real-time tail with the virtual tail
+                    ppp.hdlc.frameStartIndex = ppp.rx.tail; // remember where next frame started
                     break;
                 }
             }
@@ -1061,12 +1116,12 @@
 
 int main()
 {
-    pc.baud(115200); // USB serial port
-    debugBaudRate(115200); // baud rate for our debug port - if we have one
-    debugPrintf("\x1b[2J\x1b[H\x1b[30mPPP-Blinky Ready.\n"); // VT100 codes for clear_screen, home, black_text - Tera Term is a handy VT100 terminal
+    pc.baud(115200); // USB serial port to pc
+    debugBaudRate(115200); // baud rate for our (optional) debug port
+    debugPrintf("\x1b[2J\x1b[H\x1b[30mmbed PPP-Blinky HTTP & WebSocket server ready :)\n"); // VT100 codes for clear_screen, home, black_text - Tera Term is a handy VT100 terminal
     pppInitStruct(); // initialize all the PPP properties
     while(1) {
-        scanForConnectString(); // wait for connect
+        scanForConnectString(); // wait for connect from PC dial-up networking
         while(ppp.online) {
             wait_for_PPP_frame(); // wait for a PPP frame
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sha1.cpp	Sun Aug 13 02:18:18 2017 +0000
@@ -0,0 +1,285 @@
+/*
+SHA-1 in C
+By Steve Reid <steve@edmweb.com>
+100% Public Domain
+*/
+
+#define SHA1HANDSOFF
+
+#include <stdio.h>
+#include <string.h>
+
+/* for uint32_t */
+#include <stdint.h>
+
+#include "sha1.h"
+
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+/* blk0() and blk() perform the initial expand. */
+/* I got the idea of expanding during the round function from SSLeay */
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
+    |(rol(block->l[i],8)&0x00FF00FF))
+#elif BYTE_ORDER == BIG_ENDIAN
+#define blk0(i) block->l[i]
+#else
+#error "Endianness not defined!"
+#endif
+#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
+    ^block->l[(i+2)&15]^block->l[i&15],1))
+
+/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
+#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
+#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
+#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
+
+
+/* Hash a single 512-bit block. This is the core of the algorithm. */
+
+void SHA1Transform(
+    uint32_t state[5],
+    const unsigned char buffer[64]
+)
+{
+    uint32_t a, b, c, d, e;
+
+    typedef union
+    {
+        unsigned char c[64];
+        uint32_t l[16];
+    } CHAR64LONG16;
+
+#ifdef SHA1HANDSOFF
+    CHAR64LONG16 block[1];      /* use array to appear as a pointer */
+
+    memcpy(block, buffer, 64);
+#else
+    /* The following had better never be used because it causes the
+     * pointer-to-const buffer to be cast into a pointer to non-const.
+     * And the result is written through.  I threw a "const" in, hoping
+     * this will cause a diagnostic.
+     */
+    CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
+#endif
+    /* Copy context->state[] to working vars */
+    a = state[0];
+    b = state[1];
+    c = state[2];
+    d = state[3];
+    e = state[4];
+    /* 4 rounds of 20 operations each. Loop unrolled. */
+    R0(a, b, c, d, e, 0);
+    R0(e, a, b, c, d, 1);
+    R0(d, e, a, b, c, 2);
+    R0(c, d, e, a, b, 3);
+    R0(b, c, d, e, a, 4);
+    R0(a, b, c, d, e, 5);
+    R0(e, a, b, c, d, 6);
+    R0(d, e, a, b, c, 7);
+    R0(c, d, e, a, b, 8);
+    R0(b, c, d, e, a, 9);
+    R0(a, b, c, d, e, 10);
+    R0(e, a, b, c, d, 11);
+    R0(d, e, a, b, c, 12);
+    R0(c, d, e, a, b, 13);
+    R0(b, c, d, e, a, 14);
+    R0(a, b, c, d, e, 15);
+    R1(e, a, b, c, d, 16);
+    R1(d, e, a, b, c, 17);
+    R1(c, d, e, a, b, 18);
+    R1(b, c, d, e, a, 19);
+    R2(a, b, c, d, e, 20);
+    R2(e, a, b, c, d, 21);
+    R2(d, e, a, b, c, 22);
+    R2(c, d, e, a, b, 23);
+    R2(b, c, d, e, a, 24);
+    R2(a, b, c, d, e, 25);
+    R2(e, a, b, c, d, 26);
+    R2(d, e, a, b, c, 27);
+    R2(c, d, e, a, b, 28);
+    R2(b, c, d, e, a, 29);
+    R2(a, b, c, d, e, 30);
+    R2(e, a, b, c, d, 31);
+    R2(d, e, a, b, c, 32);
+    R2(c, d, e, a, b, 33);
+    R2(b, c, d, e, a, 34);
+    R2(a, b, c, d, e, 35);
+    R2(e, a, b, c, d, 36);
+    R2(d, e, a, b, c, 37);
+    R2(c, d, e, a, b, 38);
+    R2(b, c, d, e, a, 39);
+    R3(a, b, c, d, e, 40);
+    R3(e, a, b, c, d, 41);
+    R3(d, e, a, b, c, 42);
+    R3(c, d, e, a, b, 43);
+    R3(b, c, d, e, a, 44);
+    R3(a, b, c, d, e, 45);
+    R3(e, a, b, c, d, 46);
+    R3(d, e, a, b, c, 47);
+    R3(c, d, e, a, b, 48);
+    R3(b, c, d, e, a, 49);
+    R3(a, b, c, d, e, 50);
+    R3(e, a, b, c, d, 51);
+    R3(d, e, a, b, c, 52);
+    R3(c, d, e, a, b, 53);
+    R3(b, c, d, e, a, 54);
+    R3(a, b, c, d, e, 55);
+    R3(e, a, b, c, d, 56);
+    R3(d, e, a, b, c, 57);
+    R3(c, d, e, a, b, 58);
+    R3(b, c, d, e, a, 59);
+    R4(a, b, c, d, e, 60);
+    R4(e, a, b, c, d, 61);
+    R4(d, e, a, b, c, 62);
+    R4(c, d, e, a, b, 63);
+    R4(b, c, d, e, a, 64);
+    R4(a, b, c, d, e, 65);
+    R4(e, a, b, c, d, 66);
+    R4(d, e, a, b, c, 67);
+    R4(c, d, e, a, b, 68);
+    R4(b, c, d, e, a, 69);
+    R4(a, b, c, d, e, 70);
+    R4(e, a, b, c, d, 71);
+    R4(d, e, a, b, c, 72);
+    R4(c, d, e, a, b, 73);
+    R4(b, c, d, e, a, 74);
+    R4(a, b, c, d, e, 75);
+    R4(e, a, b, c, d, 76);
+    R4(d, e, a, b, c, 77);
+    R4(c, d, e, a, b, 78);
+    R4(b, c, d, e, a, 79);
+    /* Add the working vars back into context.state[] */
+    state[0] += a;
+    state[1] += b;
+    state[2] += c;
+    state[3] += d;
+    state[4] += e;
+    /* Wipe variables */
+    a = b = c = d = e = 0;
+#ifdef SHA1HANDSOFF
+    memset(block, '\0', sizeof(block));
+#endif
+}
+
+
+/* SHA1Init - Initialize new context */
+
+void SHA1Init(
+    SHA1_CTX * context
+)
+{
+    /* SHA1 initialization constants */
+    context->state[0] = 0x67452301;
+    context->state[1] = 0xEFCDAB89;
+    context->state[2] = 0x98BADCFE;
+    context->state[3] = 0x10325476;
+    context->state[4] = 0xC3D2E1F0;
+    context->count[0] = context->count[1] = 0;
+}
+
+
+/* Run your data through this. */
+
+void SHA1Update(
+    SHA1_CTX * context,
+    const unsigned char *data,
+    uint32_t len
+)
+{
+    uint32_t i;
+
+    uint32_t j;
+
+    j = context->count[0];
+    if ((context->count[0] += len << 3) < j)
+        context->count[1]++;
+    context->count[1] += (len >> 29);
+    j = (j >> 3) & 63;
+    if ((j + len) > 63)
+    {
+        memcpy(&context->buffer[j], data, (i = 64 - j));
+        SHA1Transform(context->state, context->buffer);
+        for (; i + 63 < len; i += 64)
+        {
+            SHA1Transform(context->state, &data[i]);
+        }
+        j = 0;
+    }
+    else
+        i = 0;
+    memcpy(&context->buffer[j], &data[i], len - i);
+}
+
+
+/* Add padding and return the message digest. */
+
+void SHA1Final(
+    unsigned char digest[20],
+    SHA1_CTX * context
+)
+{
+    unsigned i;
+
+    unsigned char finalcount[8];
+
+    unsigned char c;
+
+#if 0    /* untested "improvement" by DHR */
+    /* Convert context->count to a sequence of bytes
+     * in finalcount.  Second element first, but
+     * big-endian order within element.
+     * But we do it all backwards.
+     */
+    unsigned char *fcp = &finalcount[8];
+
+    for (i = 0; i < 2; i++)
+    {
+        uint32_t t = context->count[i];
+
+        int j;
+
+        for (j = 0; j < 4; t >>= 8, j++)
+            *--fcp = (unsigned char) t}
+#else
+    for (i = 0; i < 8; i++)
+    {
+        finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255);      /* Endian independent */
+    }
+#endif
+    c = 0200;
+    SHA1Update(context, &c, 1);
+    while ((context->count[0] & 504) != 448)
+    {
+        c = 0000;
+        SHA1Update(context, &c, 1);
+    }
+    SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
+    for (i = 0; i < 20; i++)
+    {
+        digest[i] = (unsigned char)
+            ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
+    }
+    /* Wipe variables */
+    memset(context, '\0', sizeof(*context));
+    memset(&finalcount, '\0', sizeof(finalcount));
+}
+
+void sha1(
+    char *hash_out,
+    const char *str,
+    int len)
+{
+    SHA1_CTX ctx;
+    unsigned int ii;
+
+    SHA1Init(&ctx);
+    for (ii=0; ii<len; ii+=1)
+        SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
+    SHA1Final((unsigned char *)hash_out, &ctx);
+    // hash_out[20] = '\0';
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sha1.h	Sun Aug 13 02:18:18 2017 +0000
@@ -0,0 +1,44 @@
+#ifndef SHA1_H
+#define SHA1_H
+
+/*
+   SHA-1 in C
+   By Steve Reid <steve@edmweb.com>
+   100% Public Domain
+ */
+
+#include "stdint.h"
+
+typedef struct
+{
+    uint32_t state[5];
+    uint32_t count[2];
+    unsigned char buffer[64];
+} SHA1_CTX;
+
+void SHA1Transform(
+    uint32_t state[5],
+    const unsigned char buffer[64]
+    );
+
+void SHA1Init(
+    SHA1_CTX * context
+    );
+
+void SHA1Update(
+    SHA1_CTX * context,
+    const unsigned char *data,
+    uint32_t len
+    );
+
+void SHA1Final(
+    unsigned char digest[20],
+    SHA1_CTX * context
+    );
+
+void sha1(
+    char *hash_out,
+    const char *str,
+    int len);
+
+#endif /* SHA1_H */