A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Dependencies:   SW_HTTPServer WiflyInterface mbed C12832 IniManager

Here's the code

But you also might want to check out the SmartBoard-WiFly project page.

Basic Web Server

  • Serves static files from the selected file system. This is a compile-time setting, and a typical configuration supports gif, jpg, jpeg, ico, png, zip, gz, tar, txt, pdf, htm, and html.
  • It is designed to be small, thereby better supporting the limited resources of an embedded environment.

Advanced Web Services

  • Serves dynamically generated pages, where your software registers for a path, and then everything to that path activates your handler. Your handler then defines the header and body response.
  • Dynamic handlers can process GET query parameters (e.g. /dyn1?sky=blue&grass=green).
  • Dynamic handlers can process POST query parameters, as delivered from submission of a form.
  • Dynamic handlers can protect a resource with user:password access.

Run-Time Configurations

  • File System Support - using either the "local" file system supported by the magic chip, or from either an SD-Card or a USB flash drive.
  • Configurable to the maximum number of dynamic handlers (minimize memory requirements).
  • Configurable to the maximum number of name=value pairs for dynamic handlers (minimize memory requirements).

Compile-Time Configurations

  • Default filename for URL ending in '/' - default is 'index.htm'.
  • Configurable buffer sizes permit minimizing RAM requirements.
  • Configurable header response information.
  • Configurable for which serial port is used to communicate to the WiFly module.
  • Improved security option - to disable telnet access.

Diagnostics

  • API to determine the largest header (to more efficiently size the buffers).
  • API to gather the run-time characteristics - header processing time and content delivery time.

Limitations / Constraints

Known Issues

These are known issues, not yet resolved.

  1. Occasionally fails to serve a page - one test will constantly reload a web page every 30 seconds. It may run for hours, or minutes, then fail to load. Behaviors then are:
    • Hit the reload button in the browser and away it goes.
    • Hit the reload and you'll see the Wifly LEDs energize from the request, but no response by the web server. It appears that the embedded code does not "accept()" the connection in the TCP Socket Server.
      • In this case, the Wifly module has gone through an internal watchdog reset and the configuration parameters are such that it does not gracefully recover. Microchip is aware of this issue, but has not solved it.

Wifly Limitations

  • Single thread - it doesn't respond to overlapping requests (e.g. an embedded image may be requested before the main page completes transfer - the request is lost and the image not shown).
  • Single client - goes along with the single thread, but it doesn't support more than one client at a time.

Smart-Wifly-WebServer

  • Dynamic memory allocation - it does use dynamic memory allocation, which would be discouraged/avoided in many embedded systems. Here it uses it in parsing a request and it releases those resources upon completion of that request. If there is no other dynamic allocation that persists beyond a transaction, it should not cause memory fragmentation. Note that with multi-threading (if this is implemented with an OS), you then have race conditions that could cause fragmentation.

Web Server

Here's the web server in action. A combination of static pages served from the file system and dynamically generated pages.

/media/uploads/WiredHome/swsvr_1.pngPart of the main demo page,
which basically has all the
specifications, configurations, and limitations.
/media/uploads/WiredHome/swsvr_2.pngA zoomed out view of the same page.
/media/uploads/WiredHome/swsvr_3.pngIt would be possible to configure
the server via the web.
/media/uploads/WiredHome/swsvr_4.pngOne of the dynamically generated pages.
This one has parsed the query parameters.
/media/uploads/WiredHome/swsvr_5.pngA simple form which has a dynamic handler on the back end.
Here it takes the value associated with "leds"
and uses that to set the 4 LEDs on the mbed module.
/media/uploads/WiredHome/swsvr_6.pngA dynamic handler can require authentication.
/media/uploads/WiredHome/swsvr_7.pngSuccess!

But I've now gone so far beyond that in the current version. Here's what this one can do:

  1. It serves static web pages from a file system. I've only tested with the local file system and an SD card, but should work for any, so long as you remember that the local file system can't read subdirectories.
  2. It can serve dynamically generated web pages. This lets you accept name=value pairs using the URL (using either a GET or POST method). It can also accept them from forms. The demo lets you control the 4 LEDs from a form.
  3. As safely as possible it retrieves your credentials to the Wi-Fi Access Point. After using them, it overwrites that ram so they can't be as easily extracted.
  4. I made a large number of changes to the Wifly driver. It had too short of a timeout and I found quite a number of optimizations for performance and robustness.
  5. I have the start on a security feature - you can configure a resource to require user credentials to access it. The browser typically provides a username and password dialog. Take care however, as it does not support a secure (https) connection, so the credentials are not as securely transferred as I would like.

Optimizations I'd like to do:

  1. speed it up - I'm running the mbed to wifly module interface at 230K, which is about the top speed w/o flow control. There are other places where some time delays remain - I have eliminated a number of them.
  2. make it non-blocking, so other work can happen.
  3. integrate it with the rtos
  4. When a web page has referenced resources (e.g. an image tag), it rarely loads the image on the first try. I think the request for the resource comes in while it is still in the WiflyInterface cleaning up from the last connection. The Wifly module supports only a single connection at a time. I worked around this with a small bit of javascript to load the images after the web page.

But all in all I think it is a good start.

Program prerequisite

Here's the link to the program, but when you open it up, note a few very important items.

  1. Port Numbers listed in the constructor match the SmartBoard Baseboard.
  2. I sped up the communication baud rate to the mbed from the default 9600. Match your terminal program accordingly.
  3. Download this zip. Place it and an unzipped copy into the mbed local file system. These are the demo files.
  4. The typical ssid and password are not shown. See below to set yours.

ssid and password

You need to create a simple text file on your mbed root folder named "config.ini". The easiest way perhaps is to create "config.txt", add the information shown below and then rename it. This will be read at startup to connect you to the network. Something quite simple, like this:

[Wifi]
ssid=your_ssid
pass=your_pass_code

The program

And the program.

Import programSmart-WiFly-WebServer

A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Committer:
WiredHome
Date:
Mon Aug 12 23:06:52 2013 +0000
Revision:
14:85c805890454
Parent:
12:479ff89c190b
Added demo page for a simple password protected page.; Added demo page for how a server configuration may be created.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 7:72b5df2fac93 1 /// Demonstration of dynamic page creation using the Smartware Web Server
WiredHome 11:183b3893eb7d 2 ///
WiredHome 11:183b3893eb7d 3 /// There are a few samples here of the dynamically generated web pages.
WiredHome 11:183b3893eb7d 4 /// Read the header above each one for more details.
WiredHome 11:183b3893eb7d 5 ///
WiredHome 3:b3e21f3306a1 6 #include "mbed.h"
WiredHome 3:b3e21f3306a1 7
WiredHome 3:b3e21f3306a1 8 #include "SW_HTTPServer.h"
WiredHome 3:b3e21f3306a1 9 #include "DynamicPages.h"
WiredHome 3:b3e21f3306a1 10 #include "Utility.h"
WiredHome 14:85c805890454 11 #include "Base64.h"
WiredHome 3:b3e21f3306a1 12
WiredHome 11:183b3893eb7d 13 BusOut leds(LED4,LED3,LED2,LED1); // dynamic page "/dyn2" lets you control these
WiredHome 3:b3e21f3306a1 14
WiredHome 14:85c805890454 15 Base64 bc64;
WiredHome 14:85c805890454 16 char * converted = NULL;
WiredHome 14:85c805890454 17 size_t convertedLen = 0;
WiredHome 14:85c805890454 18 bool accessGranted = false;
WiredHome 14:85c805890454 19
WiredHome 14:85c805890454 20 bool CredentialCheck(char * user, char * pass)
WiredHome 14:85c805890454 21 {
WiredHome 14:85c805890454 22 printf("CredentialCheck(%s,%s)\r\n", user, pass);
WiredHome 14:85c805890454 23 if (strcmp(user, "user1") == 0 && strcmp(pass, "pass1") == 0)
WiredHome 14:85c805890454 24 return true;
WiredHome 14:85c805890454 25 else
WiredHome 14:85c805890454 26 return false;
WiredHome 14:85c805890454 27 }
WiredHome 14:85c805890454 28
WiredHome 14:85c805890454 29
WiredHome 14:85c805890454 30 /// SimpleSecurityCheck
WiredHome 14:85c805890454 31 bool SimpleSecurityCheck(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
WiredHome 14:85c805890454 32 {
WiredHome 14:85c805890454 33 bool ret = false;
WiredHome 14:85c805890454 34 char buf[150];
WiredHome 14:85c805890454 35
WiredHome 14:85c805890454 36 switch (type) {
WiredHome 14:85c805890454 37 case HTTPServer::SEND_PAGE:
WiredHome 14:85c805890454 38 accessGranted = false;
WiredHome 14:85c805890454 39 if (svr->GetHeaderValue("Authorization") != NULL) {
WiredHome 14:85c805890454 40 const char * p = svr->GetHeaderValue("Authorization");
WiredHome 14:85c805890454 41 if (p && strncmp(p, "Basic ", 6) == 0) {
WiredHome 14:85c805890454 42 p += 6;
WiredHome 14:85c805890454 43 if (converted) // this should never be true, but is a nice safeguard
WiredHome 14:85c805890454 44 free(converted);
WiredHome 14:85c805890454 45 converted = bc64.Decode(p, strlen(p), &convertedLen);
WiredHome 14:85c805890454 46 if (converted) {
WiredHome 14:85c805890454 47 // Now check the actual credentials...
WiredHome 14:85c805890454 48 char *colon;
WiredHome 14:85c805890454 49 converted[convertedLen] = '\0';
WiredHome 14:85c805890454 50 colon = strchr(converted, ':');
WiredHome 14:85c805890454 51 if (colon) {
WiredHome 14:85c805890454 52 *colon++ = '\0';
WiredHome 14:85c805890454 53 if (CredentialCheck(converted, colon))
WiredHome 14:85c805890454 54 accessGranted = true;
WiredHome 14:85c805890454 55 }
WiredHome 14:85c805890454 56 }
WiredHome 14:85c805890454 57 }
WiredHome 14:85c805890454 58 }
WiredHome 14:85c805890454 59 if (!accessGranted)
WiredHome 14:85c805890454 60 svr->header(401, "Access Denied", "WWW-Authenticate: Basic realm='Smart HTTPServer insecure logon'\r\n");
WiredHome 14:85c805890454 61 else {
WiredHome 14:85c805890454 62 svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 14:85c805890454 63 svr->send("<html><head><title>Security Check</title></head>\r\n");
WiredHome 14:85c805890454 64 svr->send("<body>\r\n");
WiredHome 14:85c805890454 65 sprintf(buf, "<h1>Welcome %s</h1>\r\n", converted);
WiredHome 14:85c805890454 66 svr->send(buf);
WiredHome 14:85c805890454 67 sprintf(buf, "Authorization was approved.<br/>\r\n");
WiredHome 14:85c805890454 68 svr->send(buf);
WiredHome 14:85c805890454 69 svr->send("Remember to close your browser for maximum security (and don't cache your credentials).<br/>\r\n");
WiredHome 14:85c805890454 70 svr->send("<a href='/'>back to main</a></body></html>\r\n");
WiredHome 14:85c805890454 71 }
WiredHome 14:85c805890454 72 if (converted)
WiredHome 14:85c805890454 73 free(converted); // don't leak memory
WiredHome 14:85c805890454 74 ret = true;
WiredHome 14:85c805890454 75 break;
WiredHome 14:85c805890454 76 case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 14:85c805890454 77 ret = true;
WiredHome 14:85c805890454 78 break;
WiredHome 14:85c805890454 79 case HTTPServer::DATA_TRANSFER:
WiredHome 14:85c805890454 80 ret = true;
WiredHome 14:85c805890454 81 break;
WiredHome 14:85c805890454 82 default:
WiredHome 14:85c805890454 83 ret = false;
WiredHome 14:85c805890454 84 break;
WiredHome 14:85c805890454 85 }
WiredHome 14:85c805890454 86 return ret;
WiredHome 14:85c805890454 87 }
WiredHome 3:b3e21f3306a1 88
WiredHome 4:178df829d62b 89 /// SimplyDynamicPage1
WiredHome 4:178df829d62b 90 ///
WiredHome 14:85c805890454 91 /// This web page is generated dynamically as a kind of "bare minimum".
WiredHome 11:183b3893eb7d 92 /// It doesn't do much.
WiredHome 11:183b3893eb7d 93 ///
WiredHome 11:183b3893eb7d 94 /// You can see in main how this page was registered.
WiredHome 4:178df829d62b 95 ///
WiredHome 14:85c805890454 96 bool SuperSimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
WiredHome 14:85c805890454 97 {
WiredHome 4:178df829d62b 98 bool ret = false;
WiredHome 14:85c805890454 99
WiredHome 4:178df829d62b 100 switch (type) {
WiredHome 4:178df829d62b 101 case HTTPServer::SEND_PAGE:
WiredHome 4:178df829d62b 102 svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 4:178df829d62b 103 svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
WiredHome 4:178df829d62b 104 svr->send("<body>\r\n");
WiredHome 11:183b3893eb7d 105 svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
WiredHome 7:72b5df2fac93 106 svr->send("This page was generated dynamically.<br/>\r\n");
WiredHome 7:72b5df2fac93 107 svr->send("<a href='/'>back to main</a></body></html>\r\n");
WiredHome 4:178df829d62b 108 ret = true;
WiredHome 4:178df829d62b 109 break;
WiredHome 4:178df829d62b 110 case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 4:178df829d62b 111 ret = true;
WiredHome 4:178df829d62b 112 break;
WiredHome 4:178df829d62b 113 case HTTPServer::DATA_TRANSFER:
WiredHome 4:178df829d62b 114 ret = true;
WiredHome 4:178df829d62b 115 break;
WiredHome 4:178df829d62b 116 default:
WiredHome 4:178df829d62b 117 ret = false;
WiredHome 4:178df829d62b 118 break;
WiredHome 4:178df829d62b 119 }
WiredHome 4:178df829d62b 120 return ret;
WiredHome 4:178df829d62b 121 }
WiredHome 4:178df829d62b 122
WiredHome 4:178df829d62b 123
WiredHome 3:b3e21f3306a1 124 /// SimplyDynamicPage
WiredHome 3:b3e21f3306a1 125 ///
WiredHome 14:85c805890454 126 /// This web page is generated dynamically and fills in part of the information from
WiredHome 11:183b3893eb7d 127 /// data extracted from the URL as a query string. It also shows some additional
WiredHome 11:183b3893eb7d 128 /// information - memory stats, server stats and so forth.
WiredHome 3:b3e21f3306a1 129 ///
WiredHome 3:b3e21f3306a1 130 /// You can see in main how this page was registered.
WiredHome 3:b3e21f3306a1 131 ///
WiredHome 14:85c805890454 132 bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
WiredHome 14:85c805890454 133 {
WiredHome 3:b3e21f3306a1 134 char buf[100];
WiredHome 11:183b3893eb7d 135 char remoteIP[36];
WiredHome 3:b3e21f3306a1 136 bool ret = false;
WiredHome 4:178df829d62b 137 HTTPServer::SW_PerformanceData perfData;
WiredHome 14:85c805890454 138
WiredHome 3:b3e21f3306a1 139 switch (type) {
WiredHome 3:b3e21f3306a1 140 case HTTPServer::SEND_PAGE:
WiredHome 4:178df829d62b 141 if (strcmp("true", svr->GetParameter("Reset")) == 0)
WiredHome 4:178df829d62b 142 svr->ResetPerformanceData();
WiredHome 4:178df829d62b 143 // Send the header
WiredHome 3:b3e21f3306a1 144 svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 14:85c805890454 145
WiredHome 4:178df829d62b 146 // Send top of the page
WiredHome 3:b3e21f3306a1 147 svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
WiredHome 3:b3e21f3306a1 148 svr->send("<body>\r\n");
WiredHome 11:183b3893eb7d 149 svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
WiredHome 3:b3e21f3306a1 150 svr->send("This page was generated dynamically. Create your own name=value pairs on the URL "
WiredHome 3:b3e21f3306a1 151 "which uses the GET method.<br/>\r\n");
WiredHome 4:178df829d62b 152
WiredHome 4:178df829d62b 153 // Show Passed-in parameters
WiredHome 4:178df829d62b 154 svr->send("<h2>Query Parameters:</h2><blockquote>\r\n");
WiredHome 3:b3e21f3306a1 155 sprintf(buf, "%d parameters passed to {%s}:<br/>\r\n", paramcount, path);
WiredHome 3:b3e21f3306a1 156 svr->send(buf);
WiredHome 3:b3e21f3306a1 157 // show each of the parameters passed on the URL
WiredHome 3:b3e21f3306a1 158 for (int i=0; i<paramcount; i++) {
WiredHome 3:b3e21f3306a1 159 sprintf(buf, "%d: %s = %s<br/>\r\n", i, params[i].name, params[i].value);
WiredHome 3:b3e21f3306a1 160 svr->send(buf);
WiredHome 3:b3e21f3306a1 161 }
WiredHome 4:178df829d62b 162 svr->send("</blockquote>\r\n");
WiredHome 4:178df829d62b 163
WiredHome 4:178df829d62b 164 // Show Memory Stats
WiredHome 7:72b5df2fac93 165 svr->send("<h2>System Statistics:</h2><blockquote>\r\n");
WiredHome 7:72b5df2fac93 166 svr->send("<table border='1'><tr><td>Parameter</td><td>Description</td></tr>\r\n");
WiredHome 7:72b5df2fac93 167 sprintf(buf,"<tr><td align='right'>%d</td><td>Free memory</td></tr>\r\n", Free());
WiredHome 7:72b5df2fac93 168 svr->send(buf);
WiredHome 7:72b5df2fac93 169 sprintf(buf,"<tr><td align='right'>%d</td><td>Max Header size</td></tr>\r\n", svr->GetMaxHeaderSize());
WiredHome 4:178df829d62b 170 svr->send(buf);
WiredHome 14:85c805890454 171 //TODO sprintf(buf,"<tr><td align='right'>%3.2f</td><td>Wifly SW version</td></tr>\r\n", svr->GetWifly()->getWiflyVersion());
WiredHome 14:85c805890454 172 // svr->send(buf);
WiredHome 14:85c805890454 173 // sprintf(buf,"<tr><td align='right'>%s</td><td>Wifly Version Information</td></tr>\r\n", svr->GetWifly()->getWiflyVersionString());
WiredHome 14:85c805890454 174 // svr->send(buf);
WiredHome 7:72b5df2fac93 175 svr->GetRemoteAddr(remoteIP, sizeof(remoteIP));
WiredHome 7:72b5df2fac93 176 sprintf(buf,"<tr><td align='right'>%s</td><td>Client IP Address</td></tr>\r\n", remoteIP);
WiredHome 7:72b5df2fac93 177 svr->send(buf);
WiredHome 7:72b5df2fac93 178 svr->send("</table>\r\n");
WiredHome 4:178df829d62b 179 svr->send("</blockquote>\r\n");
WiredHome 4:178df829d62b 180
WiredHome 7:72b5df2fac93 181 // Show Web Server Performance metrics
WiredHome 4:178df829d62b 182 svr->GetPerformanceData(&perfData);
WiredHome 7:72b5df2fac93 183 svr->send("<h2>Web Server Performance Metrics</h2><blockquote>\r\n");
WiredHome 4:178df829d62b 184 svr->send("<table border='1'><tr><td>avg time (uS)</td><td>samples</td><td>max time (uS)</td><td>description</td></tr>\r\n");
WiredHome 4:178df829d62b 185 sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
WiredHome 14:85c805890454 186 (unsigned long)(perfData.Header.TotalTime_us / perfData.Header.Samples),
WiredHome 14:85c805890454 187 perfData.Header.Samples,
WiredHome 14:85c805890454 188 (unsigned long)(perfData.Header.MaxTime_us),
WiredHome 14:85c805890454 189 "Header Response");
WiredHome 4:178df829d62b 190 svr->send(buf);
WiredHome 4:178df829d62b 191 sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
WiredHome 14:85c805890454 192 (unsigned long)(perfData.SendData.TotalTime_us / perfData.SendData.Samples),
WiredHome 14:85c805890454 193 perfData.SendData.Samples,
WiredHome 14:85c805890454 194 (unsigned long)(perfData.SendData.MaxTime_us),
WiredHome 14:85c805890454 195 "SendData Response");
WiredHome 4:178df829d62b 196 svr->send(buf);
WiredHome 7:72b5df2fac93 197 svr->send("<tr><td colspan='3'>&nbsp;</td><td><a href='?Reset=false'>Reload</a> <a href='?Reset=true'>Reset Metrics</a></td></tr>\r\n");
WiredHome 4:178df829d62b 198 svr->send("</table>\r\n");
WiredHome 4:178df829d62b 199 svr->send("</blockquote>\r\n");
WiredHome 4:178df829d62b 200
WiredHome 4:178df829d62b 201 // Send bottom of the page
WiredHome 4:178df829d62b 202 svr->send("<br/><a href='/'>back to main</a></body></html>\r\n");
WiredHome 3:b3e21f3306a1 203 ret = true;
WiredHome 3:b3e21f3306a1 204 break;
WiredHome 3:b3e21f3306a1 205 case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 3:b3e21f3306a1 206 ret = true;
WiredHome 3:b3e21f3306a1 207 break;
WiredHome 3:b3e21f3306a1 208 case HTTPServer::DATA_TRANSFER:
WiredHome 3:b3e21f3306a1 209 ret = true;
WiredHome 3:b3e21f3306a1 210 break;
WiredHome 3:b3e21f3306a1 211 default:
WiredHome 3:b3e21f3306a1 212 ret = false;
WiredHome 3:b3e21f3306a1 213 break;
WiredHome 3:b3e21f3306a1 214 }
WiredHome 3:b3e21f3306a1 215 return ret;
WiredHome 3:b3e21f3306a1 216 }
WiredHome 3:b3e21f3306a1 217
WiredHome 3:b3e21f3306a1 218 /// SimplyDynamicForm
WiredHome 3:b3e21f3306a1 219 ///
WiredHome 14:85c805890454 220 /// This web page is generated dynamically and fills in part of the information from
WiredHome 3:b3e21f3306a1 221 /// data extracted from the URL. It also generates a web form, and allows you to
WiredHome 14:85c805890454 222 /// update the data in the form using the POST method. This one also updates the
WiredHome 11:183b3893eb7d 223 /// leds on the mbed module based on the form submission, so demonstrates a very
WiredHome 11:183b3893eb7d 224 /// simple interaction with the hardware.
WiredHome 3:b3e21f3306a1 225 ///
WiredHome 3:b3e21f3306a1 226 /// You can see in main how this page was registered.
WiredHome 3:b3e21f3306a1 227 ///
WiredHome 14:85c805890454 228 bool SimpleDynamicForm(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
WiredHome 14:85c805890454 229 {
WiredHome 3:b3e21f3306a1 230 char buf[100];
WiredHome 3:b3e21f3306a1 231 bool ret = false;
WiredHome 14:85c805890454 232
WiredHome 3:b3e21f3306a1 233 switch (type) {
WiredHome 3:b3e21f3306a1 234 case HTTPServer::SEND_PAGE:
WiredHome 3:b3e21f3306a1 235 // set the LEDs based on a passed in parameter.
WiredHome 3:b3e21f3306a1 236 leds = atoi(svr->GetParameter("leds"));
WiredHome 3:b3e21f3306a1 237 // send the header
WiredHome 3:b3e21f3306a1 238 svr->header(200, "OK", svr->GetSupportedType(".htm")); //svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 3:b3e21f3306a1 239 // send some data
WiredHome 3:b3e21f3306a1 240 svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
WiredHome 3:b3e21f3306a1 241 svr->send("<body>\r\n");
WiredHome 11:183b3893eb7d 242 svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
WiredHome 3:b3e21f3306a1 243 svr->send("This form was generated dynamically. You can add name=value pairs on the URL "
WiredHome 3:b3e21f3306a1 244 "which will show up in the form, but the form is submitted using POST method.<br/>\r\n");
WiredHome 3:b3e21f3306a1 245 svr->send("Hint: leds=7 turns on 3 of the 4 blue leds on the mbed<br/>\r\n");
WiredHome 3:b3e21f3306a1 246 sprintf(buf, "%d parameters passed to {%s}:<br/>\r\n", paramcount, path);
WiredHome 3:b3e21f3306a1 247 svr->send(buf);
WiredHome 3:b3e21f3306a1 248 // Create a user form for which they can post changes
WiredHome 3:b3e21f3306a1 249 sprintf(buf, "<form method='post' action='%s'>\r\n", path);
WiredHome 3:b3e21f3306a1 250 //sprintf(buf, "<form method='post' enctype='multipart/form-data' action='%s'>\r\n", path); // not supported
WiredHome 3:b3e21f3306a1 251 svr->send(buf);
WiredHome 3:b3e21f3306a1 252 // show the parameters in a nice format
WiredHome 3:b3e21f3306a1 253 svr->send("<table>\r\n");
WiredHome 3:b3e21f3306a1 254 for (int i=0; i<paramcount; i++) {
WiredHome 3:b3e21f3306a1 255 if (strcmp(params[i].name, "InFile") == 0)
WiredHome 3:b3e21f3306a1 256 continue;
WiredHome 3:b3e21f3306a1 257 sprintf(buf, "<tr><td>%d</td><td>%s</td><td><input type='text' name='%s' value='%s'></td></tr>\r\n", i, params[i].name, params[i].name, params[i].value);
WiredHome 3:b3e21f3306a1 258 svr->send(buf);
WiredHome 3:b3e21f3306a1 259 }
WiredHome 3:b3e21f3306a1 260 //svr->send("<tr><td>&nbsp;</td><td>File</td><td><input type='file' name='InFile' size='40'></td></tr>\r\n");
WiredHome 3:b3e21f3306a1 261 svr->send("<tr><td>&nbsp;</td><td colspan='2'><input type='submit' value='submit'><input type='reset' value='clear'></td></tr>\r\n");
WiredHome 3:b3e21f3306a1 262 svr->send("</table>\r\n");
WiredHome 3:b3e21f3306a1 263 svr->send("</form>\r\n");
WiredHome 3:b3e21f3306a1 264 // see how we're doing with free memory
WiredHome 4:178df829d62b 265 svr->send("<br/><a href='/'>Back to main</a></body></html>\r\n");
WiredHome 3:b3e21f3306a1 266 ret = true;
WiredHome 3:b3e21f3306a1 267 break;
WiredHome 3:b3e21f3306a1 268 case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 3:b3e21f3306a1 269 ret = true;
WiredHome 3:b3e21f3306a1 270 break;
WiredHome 3:b3e21f3306a1 271 case HTTPServer::DATA_TRANSFER:
WiredHome 3:b3e21f3306a1 272 ret = true;
WiredHome 3:b3e21f3306a1 273 break;
WiredHome 3:b3e21f3306a1 274 default:
WiredHome 3:b3e21f3306a1 275 ret = false;
WiredHome 3:b3e21f3306a1 276 break;
WiredHome 3:b3e21f3306a1 277 }
WiredHome 3:b3e21f3306a1 278 return ret;
WiredHome 3:b3e21f3306a1 279 }
WiredHome 7:72b5df2fac93 280