copy of TCPEchoServer, with changes for a make-shift garage door opener

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPEchoServer by mbed official

Files at this revision

API Documentation at this revision

Comitter:
elevatorguy
Date:
Mon Jan 21 07:40:08 2013 +0000
Parent:
5:89cca64797f3
Child:
7:e2ffd7edf9c9
Commit message:
proper HTTP responses

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Jan 21 03:15:51 2013 +0000
+++ b/main.cpp	Mon Jan 21 07:40:08 2013 +0000
@@ -31,10 +31,17 @@
         printf("Connection from: %s\r\n", client.get_address());
         char buffer[256];
         
-        char firstpage[] = {"<html><head><title>Garage Door</title></head><body><h4>Garage Door Opener</h4><form action=action.htm method=link><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"};
-        char secondpage[] = {"<html><head><title>Pressing Button</title></head><body><h4>Garage Door Opener</h4><p>Door should be moving...</p><form action=action.htm method=link><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"};
-        char error[] = {"<html><head><title>Error</title></head><body>Sorry, I don't understand your request :(</body></html>"};
-        char error404[] = {"<html><head><title>Error 404</title></head><body><h1>Page not found.</h1> <h2>404</h2><br /><p>(this is not a real webserver FYI)</p></body></html>"};
+        // HTTP is statusline + headers + \r\n + body
+        
+        //body of different pages
+        char firstpage[] = {"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 213\r\n\r\n<html><head><title>Garage Door</title></head><body><h4>Garage Door Opener</h4><form action=index.html method=post><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"};
+        char secondpage[] = {"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 248\r\n\r\n<html><head><title>Pressing Button</title></head><body><h4>Garage Door Opener</h4><p>Door should be moving...</p><form action=index.html method=post><input style='border : 1px solid #000000;' value='Open/Close' type='submit' /></form></body></html>"};
+        char error[] = {"HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 100\r\n\r\n<html><head><title>Error</title></head><body>Sorry, I don't understand your request :(</body></html>"};
+        char error404[] = {"HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 147\r\n\r\n<html><head><title>Error 404</title></head><body><h1>Page not found.</h1> <h2>404</h2><br /><p>(this is not a real webserver FYI)</p></body></html>"};
+
+        //I used this website to get string length
+        //http://www.string-functions.com/length.aspx
+        
         while (true) { //accept data
             int n = client.receive(buffer, sizeof(buffer));
             if (n <= 0) break;
@@ -56,8 +63,8 @@
                 && buffer[6] == 'c' && buffer[7] == 't' && buffer[8] == 'i' && buffer[9] == 'o' && buffer[10] == 'n'
                 && buffer[11] == '.' && buffer[12] == 'h' && buffer[13] == 't' && buffer[14] == 'm') // action.htm
                 {
-                    client.send_all(secondpage, sizeof(secondpage));
-                    doorcontrol();
+                    //backwards compatibility with the first way I did it
+                    client.send_all(firstpage, sizeof(firstpage));
                     if (n <= 0) break;
                 }
                 else
@@ -66,9 +73,16 @@
                     if (n <= 0) break;
                 }
             }
+            else if (buffer[0]=='P' && buffer[1]=='O' && buffer[2]=='S' && buffer[3]=='T' && buffer[4]==' ' && buffer[5]=='/'
+             && buffer[6]=='i' && buffer[7]=='n' && buffer[8]=='d' && buffer[9]=='e' && buffer[10]=='x' && buffer[11]=='.')
+            {
+                    client.send_all(secondpage, sizeof(secondpage));
+                    doorcontrol();
+                    if (n <= 0) break;
+            }
             else
             {
-                //client.send_all(error, sizeof(error));
+                client.send_all(error, sizeof(error));
                 if (n <= 0) break;
             }
         }