RealtimeCompLab2

Dependencies:   mbed

Fork of PPP-Blinky by Nicolas Nackel

Revision:
44:d0c61ae49ea5
Parent:
43:aa57db08995d
Child:
45:ed9112b1734c
--- a/main.cpp	Tue Jan 17 21:08:03 2017 +0000
+++ b/main.cpp	Wed Jan 18 19:06:41 2017 +0000
@@ -17,8 +17,8 @@
 // http://atari.kensclassics.org/wcomlog.htm
 
 // Handy tools
-// https://ttssh2.osdn.jp/index.html.en - Tera Term, agood terminal program to monitor the debug output from the second serial port with!
-// Wireshark - can't monitor Dial-Up network packets on windows, but very hand - can import our dumpFrame routine's hex output
+// https://ttssh2.osdn.jp/index.html.en - Tera Term, a good terminal program to monitor the debug output from the second serial port with!
+// Wireshark - can't monitor Dial-Up network packets on windows, but useful - can import our dumpFrame routine's hex output
 // Microsoft network monitor - real-time monitoring of all our packets
 // http://pingtester.net/ - nice tool for high rate ping testing
 // http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - Correctly calculates the 16-bit FCS (crc) on our frames (Choose CRC16_CCITT_FALSE)
@@ -26,12 +26,13 @@
 // https://technet.microsoft.com/en-us/sysinternals/pstools.aspx - psping for fast testing of ICMP ping function
 // https://eternallybored.org/misc/netcat/ - use netcat -u 172.10.10.1 80 to send/receive UDP packets from PPP-Blinky
 
-// This #define enables/disables a second serial port that prints out interesting diagnostic messages
-#define SERIAL_PORT_MONITOR_YES
-// #define SERIAL_PORT_MONITOR_NO
+// The #define below enables/disables a second serial port that prints out interesting diagnostic messages.
+
+//#define SERIAL_PORT_MONITOR_YES
+#define SERIAL_PORT_MONITOR_NO
 
 #ifndef SERIAL_PORT_MONITOR_NO
-Serial xx(PC_10, PC_11); // See debug messages on this port. Not necessary to work, but VERY interesting output!
+Serial xx(PC_10, PC_11); // Not needed to run - if you want to see this change SERIAL_PORT_MONITOR_NO to SERIAL_PORT_MONITOR_YES
 #define debug(x...) xx.printf (x)
 #else
 // no debug monitoring
@@ -537,38 +538,59 @@
         fastResponse = 1; // we can respond fast to a push
         // It's a push, so let's check the incoming data for an HTTP GET request
         if ( strncmp(dataStart, "GET / HTTP/1.1", 14) == 0) {
-            dataLen = 17*32; // this block has to hold the web page below, but keep it under 1k
+            dataLen = 32*32; // this block has to hold the web page below, but keep it under 1024 or increase this
             memset(dataStart,'x', dataLen ); // initialize the data block
+            
             int n=0; // number of bytes we have printed so far
             n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header
-            n=n+sprintf(n+dataStart,"Content-Length: 441\r\n"); // http header
-            n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header
+            n=n+sprintf(n+dataStart,"Content-Length: "); // http header
+            int contentLengthStart = n; // remember where Content-Length is in buffer
+            n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later
+            n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header must end with empty line (\r\n)
             int nHeader=n; // byte total of all headers
-            n=n+sprintf(n+dataStart,"<!DOCTYPE html><html><head><title>mbed-PPP-Blinky</title>\n<script>window.onload=function()"); // html
-            n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};"); // html
-            n=n+sprintf(n+dataStart,"x().textContent = parseInt(x().textContent)+1;},100);};</script>\n</head><body style=\"font-size:30px; color:#807070\">"); // html
-            n=n+sprintf(n+dataStart,"<h1>mbed PPP-Blinky Up and Running</h1><h1 id=\"w\" style=\"text-align:"); // html
-            n=n+sprintf(n+dataStart," center;\">0</h1><h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1></body></html>"); // html
-            int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier
-            contentLength = contentLength+0; // get around unreferenced variable warning
+
+            n=n+sprintf(n+dataStart,"<!DOCTYPE html>\n<html><head><title>mbed-PPP-Blinky</title>\n<script>window.onload=function()"); // html start
+            n=n+sprintf(n+dataStart,"{setInterval(function(){function x(){return document.getElementById('w');};\n"); // html
+            n=n+sprintf(n+dataStart,"x().textContent = parseInt(x().textContent)+1;},100);};</script></head>\n<body style=\"font-size:30px; color:#807070\">"); // html
+            n=n+sprintf(n+dataStart,"<h1>mbed PPP-Blinky Up and Running</h1>\n<h1 id=\"w\" style=\"text-align:"); // html
+            n=n+sprintf(n+dataStart," center;\">0</h1><h1><a href=\"http://bit.ly/pppBlink2\">Source on mbed</a></h1>\n</body></html>\n"); // html end
+            
+            int contentLength = n-nHeader; // Content-Length is the count of every character after the header
+            int cSize = 5; // maximum number of digits we allow for Content-Length
+            char contentLengthString[cSize+1]; // temporary buffer to create Content-Length string
+            snprintf(contentLengthString,cSize+1,"%*d",cSize,contentLength); // print Content-Length with leading spaces and fixed width = csize
+            memcpy(dataStart+contentLengthStart, contentLengthString, cSize); // copy Content-Length to the send buffer
+            
             if (v0) {
-                debug("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n);
+                debug("HTTP GET BufferSize %d*32=%d Header %d Content-Length %d Total %d Available %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n);
             }
+            dataLen=n; // update the buffer size
         } else if ( strncmp(dataStart, "GET /", 4) == 0) { // all other HTTP GET requests get 404 Not Found response
-            dataLen = 5*32; // block size for File not found webpage
+            dataLen = 32*32; // maximum size for File not found webpage
             memset(dataStart,'x', dataLen ); // initialize the data block
+            
             int n=0; // number of bytes we have printed so far
             n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: PPP-Blinky\r\n"); // http header
-            n=n+sprintf(n+dataStart,"Content-Length: 58\r\n"); // http header
-            n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header
+            n=n+sprintf(n+dataStart,"Content-Length: "); // http header
+            int contentLengthStart = n; // remember where Content-Length is in buffer
+            n=n+sprintf(n+dataStart,"?????\r\n"); // leave five spaces for content length - will be updated later
+            n=n+sprintf(n+dataStart,"Content-Type: text/html; charset=us-ascii\r\n\r\n"); // http header must end with empty line (\r\n)
             int nHeader=n; // byte total of all headers
-            n=n+sprintf(n+dataStart,"<html><head>"); // html
-            n=n+sprintf(n+dataStart,"<body><h1>File Not Found</h1></body>\r\n</html>"); // html
-            int contentLength = dataLen-nHeader; // this is how to calculate Content-Length, but using curl -v is easier
-            contentLength = contentLength+0; // get around unreferenced variable warning
+
+            n=n+sprintf(n+dataStart,"<!DOCTYPE html><html><head></head>"); // html start
+            n=n+sprintf(n+dataStart,"<body><h1>File Not Found</h1></body>");
+            n=n+sprintf(n+dataStart,"</html>\n"); // html end
+
+            int contentLength = n-nHeader; // Content-Length is the count of every character after the header
+            int cSize = 5; // maximum number of digits we allow for Content-Length
+            char contentLengthString[cSize+1]; // temporary buffer to create Content-Length string
+            snprintf(contentLengthString,cSize+1,"%*d",cSize,contentLength); // print Content-Length with leading spaces and fixed width = csize
+            memcpy(dataStart+contentLengthStart, contentLengthString, cSize); // copy Content-Length to the send buffer
+
             if (v0) {
-                debug("HTTP GET dataLen %d*32=%d Header %d Content-Length %d Total %d Margin %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n);
+                debug("HTTP GET BufSize %d*32=%d Header %d Content-Length %d Total %d Available %d\n",dataLen/32,dataLen,nHeader,contentLength,n,dataLen-n);
             }
+            dataLen=n; // update the buffer size
         } else {
             dataLen=0; // we did not find a valid HTTP request, so just ACK with zero data
         }