Lab2_web / Mbed 2 deprecated webserverBlinky

Dependencies:   mbed

Fork of webserverBlinky by RealTimeCompLab2

Revision:
89:2c8dd0c2a426
Parent:
88:b4a71242837c
Child:
90:55e0f243a7ce
diff -r b4a71242837c -r 2c8dd0c2a426 main.cpp
--- a/main.cpp	Fri Jul 21 01:02:04 2017 +0000
+++ b/main.cpp	Sat Jul 22 23:22:05 2017 +0000
@@ -548,33 +548,44 @@
     int n=0; // number of bytes we have printed so far
     int nHeader; // byte size of HTTP header
     int contentLengthStart; // index where HTML starts
+    int xFetch, rootFetch; // temporary storage of strncmp results
 
     ppp.httpFrameCount++; // increment the number of frames we have made
 
-    int rootFetch = strncmp(dataStart, "GET / HTTP/1.1", 14);
-
-    if( rootFetch == 0 ) {
-        n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: PPP-Blinky\r\n"); // http header
+    rootFetch = strncmp(dataStart, "GET / HTTP/1.1", 14); // found GET /
+    xFetch = strncmp(dataStart, "GET /x HTTP/1.1", 15); // found GET /x
+    if( (rootFetch==0) || (xFetch==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: PPP-Blinky\r\n"); // http header
+        n=n+sprintf(n+dataStart,"HTTP/1.1 404 Not Found\r\nServer: mbed-PPP-Blinky\r\n"); // 404 header
     }
-
     n=n+sprintf(n+dataStart,"Content-Length: "); // http header
     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,"Connection: close\r\n"); // close connection immediately
     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)
     nHeader=n; // size of HTTP header
-
     if( rootFetch == 0 ) {
         // this is where we insert our web page into the buffer
         n=n+sprintf(n+dataStart,"%s", ourWebPage);
     } 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</title>"); // html start
-        n=n+sprintf(n+dataStart,"<body>%06d</body>",ppp.httpFrameCount); // body = the http frame count
+        if (xFetch == 0) {
+
+#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 element)
+            n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpFrameCount); // body = the http frame count
+#else
+            n=n+sprintf(n+dataStart,"%d",ppp.httpFrameCount); // not valid html but fast, most browsers and curl are ok with it
+#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,"<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