Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / Projects / Incomplete / Webserver / Lib / WebserverApp.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * Simple HTTP Webserver Application. When connected to the uIP stack,
34 * this will serve out files to HTTP clients.
35 */
36
37 #include "WebserverApp.h"
38
39 /** HTTP server response header, for transmission before the page contents. This indicates to the host that a page exists at the
40 * given location, and gives extra connection information.
41 */
42 char PROGMEM HTTP200Header[] = "HTTP/1.1 200 OK\r\n"
43 "Server: LUFA RNDIS\r\n"
44 "Content-type: text/html\r\n"
45 "Connection: close\r\n\r\n";
46
47 /** HTTP server response header, for transmission before a resource not found error. This indicates to the host that the given
48 * given URL is invalid, and gives extra error information.
49 */
50 char PROGMEM HTTP404Header[] = "HTTP/1.1 404 Not Found\r\n"
51 "Server: LUFA RNDIS\r\n"
52 "Connection: close\r\n\r\n";
53
54 /****************************************************************************************/
55 /** 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
56 * broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.
57 */
58 char PROGMEM HTTPPage[] =
59 "<html>"
60 " <head>"
61 " <title>"
62 " LUFA Webserver Demo"
63 " </title>"
64 " </head>"
65 " <body>"
66 " <h1>Hello from your USB AVR!</h1>"
67 " <p>"
68 " Hello! Welcome to the LUFA RNDIS Demo Webserver test page, running on your USB AVR via the LUFA library. This demonstrates the HTTP webserver, TCP/IP stack and RNDIS demo all running atop the LUFA USB stack."
69 " <br /><br />"
70 " <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>"
71 " <hr />"
72 " <i>LUFA Version: </i>" LUFA_VERSION_STRING
73 " </p>"
74 " </body>"
75 "</html>";
76
77 void WebserverAppCallback(void)
78 {
79 char* AppDataPtr = (char*)uip_appdata;
80 uint16_t AppDataSize = 0;
81
82 if (uip_closed() || uip_aborted() || uip_timedout())
83 {
84 /* Terminated or completed connection - don't send any new data */
85 return;
86 }
87 else if (uip_connected())
88 {
89 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
90 uip_conn->appstate.SendPos = HTTP200Header;
91 uip_conn->appstate.CurrentState = WEBSERVER_STATE_SendHeaders;
92 }
93
94 /* Calculate the maximum segment size and remaining data size */
95 uint16_t BytesRemaining = strlen_P(uip_conn->appstate.SendPos);
96 uint16_t MaxSegSize = uip_mss();
97
98 /* No more bytes remaining in the current data being sent - progress to next data chunk or
99 * terminate the connection once all chunks are sent */
100 if (!(BytesRemaining))
101 {
102 /* Check which data chunk we are currently sending (header or data) */
103 if (uip_conn->appstate.CurrentState == WEBSERVER_STATE_SendHeaders)
104 {
105 uip_conn->appstate.SendPos = HTTPPage;
106 uip_conn->appstate.CurrentState = WEBSERVER_STATE_SendData;
107 }
108 else if (uip_conn->appstate.CurrentState == WEBSERVER_STATE_SendData)
109 {
110 uip_close();
111 uip_conn->appstate.CurrentState = WEBSERVER_STATE_Closed;
112 }
113
114 return;
115 }
116 else if (BytesRemaining > MaxSegSize)
117 {
118 AppDataSize = MaxSegSize;
119 }
120 else
121 {
122 AppDataSize = BytesRemaining;
123 }
124
125 /* Copy over the next data segment to the application buffer, advance send position pointer */
126 strncpy_P(uip_appdata, uip_conn->appstate.SendPos, AppDataSize);
127 uip_conn->appstate.SendPos += AppDataSize;
128
129 /* Send the data to the requesting host */
130 uip_send(AppDataPtr, AppDataSize);
131 }