Added 404 errors to the RNDIS Webserver example.
[pub/USBasp.git] / Demos / Device / ClassDriver / RNDISEthernet / Lib / Webserver.c
index 8055d91..573ff33 100644 (file)
 /** 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
+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
@@ -105,29 +109,60 @@ void Webserver_ApplicationCallback(TCP_ConnectionState_t* ConnectionState, TCP_C
        {\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
-                       /* 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
+                       /* All data sent, close the connection */\r
+                       TCP_APP_CLOSECONNECTION(ConnectionState);\r
                }\r
                else\r
                {\r