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 webserver application for demonstrating the RNDIS demo and TCP/IP stack. This 
  34  *  application will serve up a static HTTP web page when requested by the host. 
  37 #include "Webserver.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 char PROGMEM HTTP404Header
[] = "HTTP/1.1 404 Not Found\r\n" 
  48                                "Server: LUFA RNDIS\r\n" 
  49                                "Connection: close\r\n\r\n"; 
  51 /** 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 
  52  *  broken up into smaller blocks and sent as a series of packets each time the webserver application callback is run. 
  54 char PROGMEM HTTPPage
[]   =  
  58                 "                       LUFA Webserver Demo" 
  62                 "               <h1>Hello from your USB AVR!</h1>" 
  64                 "                       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." 
  66                 "                       <small>Project Information: <a href=\"http://www.fourwalledcubicle.com/LUFA.php\">http://www.fourwalledcubicle.com/LUFA.php</a>.</small>" 
  68                 "                       <i>LUFA Version: </i>" LUFA_VERSION_STRING
 
  74 /** Initializes the Webserver application, opening the appropriate HTTP port in the TCP handler and registering the application 
  75  *  callback routine for packets sent to the HTTP protocol port. 
  77 void Webserver_Init(void) 
  79         /* Open the HTTP port in the TCP protocol so that HTTP connections to the device can be established */ 
  80         TCP_SetPortState(TCP_PORT_HTTP
, TCP_Port_Open
, Webserver_ApplicationCallback
); 
  83 /** Indicates if a given request equals the given HTTP command. 
  85  *  \param[in] RequestHeader  HTTP request made by the host 
  86  *  \param[in] Command        HTTP command to compare the request to 
  88  *  \return Boolean true if the command matches the request, false otherwise 
  90 static bool IsHTTPCommand(uint8_t* RequestHeader
, char* Command
) 
  92         /* Returns true if the non null terminated string in RequestHeader matches the null terminated string Command */ 
  93         return (strncmp((char*)RequestHeader
, Command
, strlen(Command
)) == 0); 
  96 /** Application callback routine, executed each time the TCP processing task runs. This callback determines what request 
  97  *  has been made (if any), and serves up appropriate responses. 
  99  *  \param[in] ConnectionState  Pointer to a TCP Connection State structure giving connection information 
 100  *  \param[in,out] Buffer       Pointer to the application's send/receive packet buffer 
 102 void Webserver_ApplicationCallback(TCP_ConnectionState_t
* ConnectionState
, TCP_ConnectionBuffer_t
* Buffer
) 
 104         char*          BufferDataStr 
= (char*)Buffer
->Data
; 
 105         static uint8_t PageBlock     
= 0; 
 107         /* Check to see if a packet has been received on the HTTP port from a remote host */ 
 108         if (TCP_APP_HAS_RECEIVED_PACKET(Buffer
)) 
 110                 if (IsHTTPCommand(Buffer
->Data
, "GET")) 
 112                         if (IsHTTPCommand(Buffer
->Data
, "GET / ")) 
 116                                 /* Copy the HTTP 200 response header into the packet buffer */ 
 117                                 strcpy_P(BufferDataStr
, HTTP200Header
); 
 119                                 /* Send the buffer contents to the host */ 
 120                                 TCP_APP_SEND_BUFFER(Buffer
, strlen(BufferDataStr
)); 
 122                                 /* Lock the buffer to Device->Host transmissions only while we send the page contents */ 
 123                                 TCP_APP_CAPTURE_BUFFER(Buffer
); 
 127                                 /* Copy the HTTP 404 response header into the packet buffer */ 
 128                                 strcpy_P(BufferDataStr
, HTTP404Header
); 
 130                                 /* Send the buffer contents to the host */ 
 131                                 TCP_APP_SEND_BUFFER(Buffer
, strlen(BufferDataStr
)); 
 133                                 /* All data sent, close the connection */ 
 134                                 TCP_APP_CLOSECONNECTION(ConnectionState
); 
 137                 else if (IsHTTPCommand(Buffer
->Data
, "HEAD")) 
 139                         if (IsHTTPCommand(Buffer
->Data
, "HEAD / ")) 
 141                                 /* Copy the HTTP response header into the packet buffer */ 
 142                                 strcpy_P(BufferDataStr
, HTTP200Header
); 
 144                                 /* Send the buffer contents to the host */ 
 145                                 TCP_APP_SEND_BUFFER(Buffer
, strlen(BufferDataStr
)); 
 149                                 /* Copy the HTTP response header into the packet buffer */ 
 150                                 strcpy_P(BufferDataStr
, HTTP404Header
); 
 152                                 /* Send the buffer contents to the host */ 
 153                                 TCP_APP_SEND_BUFFER(Buffer
, strlen(BufferDataStr
));                      
 156                         /* All data sent, close the connection */ 
 157                         TCP_APP_CLOSECONNECTION(ConnectionState
); 
 159                 else if (IsHTTPCommand(Buffer
->Data
, "TRACE")) 
 161                         /* Echo the host's query back to the host */ 
 162                         TCP_APP_SEND_BUFFER(Buffer
, Buffer
->Length
); 
 164                         /* All data sent, close the connection */ 
 165                         TCP_APP_CLOSECONNECTION(ConnectionState
); 
 169                         /* Unknown request, just clear the buffer (drop the packet) */ 
 170                         TCP_APP_CLEAR_BUFFER(Buffer
); 
 173         else if (TCP_APP_HAVE_CAPTURED_BUFFER(Buffer
)) 
 175                 uint16_t RemLength 
= strlen_P(&HTTPPage
[PageBlock 
* HTTP_REPLY_BLOCK_SIZE
]); 
 178                 /* Determine the length of the loaded block */ 
 179                 Length 
= ((RemLength 
> HTTP_REPLY_BLOCK_SIZE
) ? HTTP_REPLY_BLOCK_SIZE 
: RemLength
); 
 181                 /* Copy the next buffer sized block of the page to the packet buffer */ 
 182                 strncpy_P(BufferDataStr
, &HTTPPage
[PageBlock 
* HTTP_REPLY_BLOCK_SIZE
], Length
); 
 184                 /* Send the buffer contents to the host */ 
 185                 TCP_APP_SEND_BUFFER(Buffer
, Length
); 
 187                 /* Check to see if the entire page has been sent */ 
 188                 if (PageBlock
++ == (sizeof(HTTPPage
) / HTTP_REPLY_BLOCK_SIZE
)) 
 190                         /* Unlock the buffer so that the host can fill it with future packets */ 
 191                         TCP_APP_RELEASE_BUFFER(Buffer
); 
 193                         /* Close the connection to the host */ 
 194                         TCP_APP_CLOSECONNECTION(ConnectionState
);