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 "HTTPServerApp.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"
53 "The requested file was not found.";
55 /** FAT Fs structure to hold the internal state of the FAT driver for the dataflash contents. */
58 /** Initialization function for the simple HTTP webserver. */
59 void WebserverApp_Init(void)
61 /* Listen on port 80 for HTTP connections from hosts */
62 uip_listen(HTONS(HTTP_SERVER_PORT
));
64 /* Mount the dataflash disk via FatFS */
65 f_mount(0, &DiskFATState
);
68 /** uIP stack application callback for the simple HTTP webserver. This function must be called each time the
69 * TCP/IP stack needs a TCP packet to be processed.
71 void WebserverApp_Callback(void)
73 uip_tcp_appstate_t
* const AppState
= &uip_conn
->appstate
;
74 char* AppData
= (char*)uip_appdata
;
75 uint16_t AppDataSize
= 0;
77 if (uip_aborted() || uip_timedout())
79 /* Close the file before terminating, if it is open */
80 f_close(&AppState
->FileToSend
);
82 AppState
->CurrentState
= WEBSERVER_STATE_Closed
;
86 else if (uip_closed())
88 /* Completed connection, just return */
91 else if (uip_connected())
93 /* New connection - initialize connection state and data pointer to the appropriate HTTP header */
94 AppState
->CurrentState
= WEBSERVER_STATE_OpenRequestedFile
;
97 switch (AppState
->CurrentState
)
99 case WEBSERVER_STATE_OpenRequestedFile
:
100 /* Wait for the packet containing the request header */
103 /* Must be a GET request, abort otherwise */
104 if (strncmp(AppData
, "GET ", (sizeof("GET ") - 1)) != 0)
112 /* Copy over the requested filename from the GET request */
113 for (uint8_t i
= 0; i
< (sizeof(FileName
) - 1); i
++)
115 FileName
[i
] = AppData
[sizeof("GET ") + i
];
117 if (FileName
[i
] == ' ')
124 /* Ensure requested filename is null-terminated */
125 FileName
[(sizeof(FileName
) - 1)] = 0x00;
127 /* If no filename specified, assume the default of INDEX.HTM */
128 if (FileName
[0] == 0x00)
129 strcpy(FileName
, "INDEX.HTM");
131 /* Try to open the file from the Dataflash disk */
132 AppState
->FileOpen
= (f_open(&AppState
->FileToSend
, FileName
, FA_OPEN_EXISTING
| FA_READ
) == FR_OK
);
134 AppState
->CurrentState
= WEBSERVER_STATE_SendHeaders
;
138 case WEBSERVER_STATE_SendHeaders
:
139 /* Determine what HTTP header should be sent to the client */
140 if (AppState
->FileOpen
)
142 AppDataSize
= strlen_P(HTTP200Header
);
143 strncpy_P(AppData
, HTTP200Header
, AppDataSize
);
147 AppDataSize
= strlen_P(HTTP404Header
);
148 strncpy_P(AppData
, HTTP404Header
, AppDataSize
);
151 uip_send(AppData
, AppDataSize
);
153 AppState
->CurrentState
= WEBSERVER_STATE_SendData
;
155 case WEBSERVER_STATE_SendData
:
156 /* If end of file/file not open, progress to the close state */
157 if (!(AppState
->FileOpen
))
159 f_close(&AppState
->FileToSend
);
161 AppState
->CurrentState
= WEBSERVER_STATE_Closed
;
165 uint16_t MaxSegSize
= uip_mss();
167 /* Read the next chunk of data from the open file */
168 f_read(&AppState
->FileToSend
, AppData
, MaxSegSize
, &AppDataSize
);
169 AppState
->FileOpen
= (MaxSegSize
== AppDataSize
);
171 /* If data was read, send it to the client */
173 uip_send(AppData
, AppDataSize
);