Device mode class driver callbacks are now fired before the control request status...
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / Lib / Webserver.c
index 8055d91..7f25020 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -31,7 +31,7 @@
 /** \file\r
  *\r
  *  Simple webserver application for demonstrating the RNDIS demo and TCP/IP stack. This\r
 /** \file\r
  *\r
  *  Simple webserver application for demonstrating the RNDIS demo and TCP/IP stack. This\r
- *  application will serve up a static HTTP webpage when requested by the host.\r
+ *  application will serve up a static HTTP web page when requested by the host.\r
  */\r
 \r
 #include "Webserver.h"\r
  */\r
 \r
 #include "Webserver.h"\r
 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the\r
  *  given location, and gives extra connection information.\r
  */\r
 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the\r
  *  given location, and gives extra connection information.\r
  */\r
-char PROGMEM HTTPHeader[] = "HTTP/1.1 200 OK\r\n"\r
-                            "Server: LUFA RNDIS\r\n"\r
-                            "Content-type: text/html\r\n"\r
-                            "Connection: close\r\n\r\n";\r
+char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"\r
+                               "Server: LUFA RNDIS\r\n"\r
+                               "Content-type: text/html\r\n"\r
+                               "Connection: close\r\n\r\n";\r
+\r
+/** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given\r
+ *  given URL is invalid, and gives extra error information.\r
+ */\r
+char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"\r
+                               "Server: LUFA RNDIS\r\n"\r
+                               "Connection: close\r\n\r\n";\r
 \r
 /** HTTP page to serve to the host when a HTTP request is made. This page is too long for a single response, thus it is automatically\r
  *  broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.\r
 \r
 /** HTTP page to serve to the host when a HTTP request is made. This page is too long for a single response, thus it is automatically\r
  *  broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.\r
@@ -105,29 +112,60 @@ void Webserver_ApplicationCallback(TCP_ConnectionState_t* ConnectionState, TCP_C
        {\r
                if (IsHTTPCommand(Buffer->Data, "GET"))\r
                {\r
        {\r
                if (IsHTTPCommand(Buffer->Data, "GET"))\r
                {\r
-                       PageBlock = 0;\r
-\r
-                       /* Copy the HTTP response header into the packet buffer */\r
-                       strcpy_P(BufferDataStr, HTTPHeader);\r
-                       \r
-                       /* Send the buffer contents to the host */\r
-                       TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));\r
-\r
-                       /* Lock the buffer to Device->Host transmissions only while we send the page contents */\r
-                       TCP_APP_CAPTURE_BUFFER(Buffer);\r
+                       if (IsHTTPCommand(Buffer->Data, "GET / "))\r
+                       {\r
+                               PageBlock = 0;\r
+\r
+                               /* Copy the HTTP 200 response header into the packet buffer */\r
+                               strcpy_P(BufferDataStr, HTTP200Header);\r
+                               \r
+                               /* Send the buffer contents to the host */\r
+                               TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));\r
+\r
+                               /* Lock the buffer to Device->Host transmissions only while we send the page contents */\r
+                               TCP_APP_CAPTURE_BUFFER(Buffer);\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Copy the HTTP 404 response header into the packet buffer */\r
+                               strcpy_P(BufferDataStr, HTTP404Header);\r
+                               \r
+                               /* Send the buffer contents to the host */\r
+                               TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));\r
+                               \r
+                               /* All data sent, close the connection */\r
+                               TCP_APP_CLOSECONNECTION(ConnectionState);\r
+                       }\r
                }\r
                else if (IsHTTPCommand(Buffer->Data, "HEAD"))\r
                {\r
                }\r
                else if (IsHTTPCommand(Buffer->Data, "HEAD"))\r
                {\r
-                       /* Copy the HTTP response header into the packet buffer */\r
-                       strcpy_P(BufferDataStr, HTTPHeader);\r
-\r
-                       /* Send the buffer contents to the host */\r
-                       TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));\r
+                       if (IsHTTPCommand(Buffer->Data, "HEAD / "))\r
+                       {\r
+                               /* Copy the HTTP response header into the packet buffer */\r
+                               strcpy_P(BufferDataStr, HTTP200Header);\r
+\r
+                               /* Send the buffer contents to the host */\r
+                               TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));\r
+                       }\r
+                       else\r
+                       {\r
+                               /* Copy the HTTP response header into the packet buffer */\r
+                               strcpy_P(BufferDataStr, HTTP404Header);\r
+\r
+                               /* Send the buffer contents to the host */\r
+                               TCP_APP_SEND_BUFFER(Buffer, strlen(BufferDataStr));                     \r
+                       }\r
+                       \r
+                       /* All data sent, close the connection */\r
+                       TCP_APP_CLOSECONNECTION(ConnectionState);\r
                }\r
                else if (IsHTTPCommand(Buffer->Data, "TRACE"))\r
                {\r
                        /* Echo the host's query back to the host */\r
                        TCP_APP_SEND_BUFFER(Buffer, Buffer->Length);\r
                }\r
                else if (IsHTTPCommand(Buffer->Data, "TRACE"))\r
                {\r
                        /* Echo the host's query back to the host */\r
                        TCP_APP_SEND_BUFFER(Buffer, Buffer->Length);\r
+                       \r
+                       /* All data sent, close the connection */\r
+                       TCP_APP_CLOSECONNECTION(ConnectionState);\r
                }\r
                else\r
                {\r
                }\r
                else\r
                {\r