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 /** 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
55 * broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run.
57 char PROGMEM HTTPPage
[] =
61 " LUFA Webserver Demo"
65 " <h1>Hello from your USB AVR!</h1>"
67 " 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"
68 " demonstrates a simple HTTP webserver serving out pages to HTTP clients."
70 " <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>"
72 " <i>LUFA Version: </i>" LUFA_VERSION_STRING
77 /** Initialization function for the simple HTTP webserver. */
78 void WebserverApp_Init(void)
80 /* Listen on port 80 for HTTP connections from hosts */
81 uip_listen(HTONS(80));
84 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
85 * TCP/IP stack needs a TCP packet to be processed.
87 void WebserverApp_Callback(void)
89 char* AppDataPtr
= (char*)uip_appdata
;
90 uint16_t AppDataSize
= 0;
92 if (uip_closed() || uip_aborted() || uip_timedout())
94 /* Terminated or completed connection - don't send any new data */
97 else if (uip_connected())
99 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
100 uip_conn
->appstate
.SendPos
= HTTP200Header
;
101 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_SendHeaders
;
104 /* Calculate the maximum segment size and remaining data size */
105 uint16_t BytesRemaining
= strlen_P(uip_conn
->appstate
.SendPos
);
106 uint16_t MaxSegSize
= uip_mss();
108 /* No more bytes remaining in the current data being sent - progress to next data chunk or
109 * terminate the connection once all chunks are sent */
110 if (!(BytesRemaining
))
112 /* Check which data chunk we are currently sending (header or data) */
113 if (uip_conn
->appstate
.CurrentState
== WEBSERVER_STATE_SendHeaders
)
115 uip_conn
->appstate
.SendPos
= HTTPPage
;
116 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_SendData
;
118 else if (uip_conn
->appstate
.CurrentState
== WEBSERVER_STATE_SendData
)
121 uip_conn
->appstate
.CurrentState
= WEBSERVER_STATE_Closed
;
126 else if (BytesRemaining
> MaxSegSize
)
128 /* More bytes remaining to send than the maximum segment size, send next chunk */
129 AppDataSize
= MaxSegSize
;
133 /* Less bytes than the segment size remaining, send all remaining bytes in the one packet */
134 AppDataSize
= BytesRemaining
;
137 /* Copy over the next data segment to the application buffer, advance send position pointer */
138 strncpy_P(uip_appdata
, uip_conn
->appstate
.SendPos
, AppDataSize
);
139 uip_conn
->appstate
.SendPos
+= AppDataSize
;
141 /* Send the data to the requesting host */
142 uip_send(AppDataPtr
, AppDataSize
);