3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Simple HTTP Webserver Application. When connected to the uIP stack,
34 * this will serve out files to HTTP clients.
37 #include "WebserverApp.h"
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.
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";
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.
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";
54 /** Static HTTP page to serve to the host when a HTTP request is made from a host. */
55 char PROGMEM HTTPPage
[] =
59 " LUFA Webserver Demo"
63 " <h1>Hello from your USB AVR!</h1>"
65 " Hello! Welcome to the LUFA RNDIS Demo Webserver test page, running on your USB AVR via the LUFA library and uIP TCP/IP network stack. This"
66 " demonstrates a simple HTTP webserver serving out pages to HTTP clients."
68 " <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>"
70 " <i>LUFA Version: </i>" LUFA_VERSION_STRING
75 /** Initialization function for the simple HTTP webserver. */
76 void WebserverApp_Init(void)
78 /* Listen on port 80 for HTTP connections from hosts */
79 uip_listen(HTONS(80));
82 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
83 * TCP/IP stack needs a TCP packet to be processed.
85 void WebserverApp_Callback(void)
87 char* AppDataPtr
= (char*)uip_appdata
;
88 uint16_t AppDataSize
= 0;
90 if (uip_closed() || uip_aborted() || uip_timedout())
92 /* Terminated or completed connection - don't send any new data */
95 else if (uip_connected())
97 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
98 uip_conn
->appstate
.SendPos
= HTTP200Header
;
99 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_SendHeaders
;
102 /* Calculate the maximum segment size and remaining data size */
103 uint16_t BytesRemaining
= strlen_P(uip_conn
->appstate
.SendPos
);
104 uint16_t MaxSegSize
= uip_mss();
106 /* No more bytes remaining in the current data being sent - progress to next data chunk or
107 * terminate the connection once all chunks are sent */
108 if (!(BytesRemaining
))
110 /* Check which data chunk we are currently sending (header or data) */
111 if (uip_conn
->appstate
.CurrentState
== WEBSERVER_STATE_SendHeaders
)
113 uip_conn
->appstate
.SendPos
= HTTPPage
;
114 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_SendData
;
116 else if (uip_conn
->appstate
.CurrentState
== WEBSERVER_STATE_SendData
)
119 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_Closed
;
124 else if (BytesRemaining
> MaxSegSize
)
126 /* More bytes remaining to send than the maximum segment size, send next chunk */
127 AppDataSize
= MaxSegSize
;
131 /* Less bytes than the segment size remaining, send all remaining bytes in the one packet */
132 AppDataSize
= BytesRemaining
;
135 /* Copy over the next data segment to the application buffer, advance send position pointer */
136 strncpy_P(uip_appdata
, uip_conn
->appstate
.SendPos
, AppDataSize
);
137 uip_conn
->appstate
.SendPos
+= AppDataSize
;
139 /* Send the data to the requesting host */
140 uip_send(AppDataPtr
, AppDataSize
);