Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SW_HTTPServer WiflyInterface mbed C12832 IniManager
Diff: Examples/DynamicPages.cpp
- Revision:
- 16:ab1867ffcf94
- Parent:
- 15:1f2b62130ffb
- Child:
- 20:0ae416ce9eaf
--- a/Examples/DynamicPages.cpp Sun Sep 01 19:54:42 2013 +0000
+++ b/Examples/DynamicPages.cpp Thu Sep 05 23:00:21 2013 +0000
@@ -23,15 +23,28 @@
bool SuperSimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
{
bool ret = false;
+ char contentlen[30];
+ char remoteIP[36];
+ char buf[250];
switch (type) {
case HTTPServer::SEND_PAGE:
- svr->header(200, "OK", "Content-Type: text/html\r\n");
- svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
- svr->send("<body>\r\n");
- svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
- svr->send("This page was generated dynamically.<br/>\r\n");
- svr->send("<a href='/'>back to main</a></body></html>\r\n");
+ // This sample drops it all into a local buffer, computes the length,
+ // and passes that along as well. This can help the other end with efficiency.
+ svr->GetRemoteAddr(remoteIP, sizeof(remoteIP));
+ strcpy(buf, "<html><head><title>Dynamic Page</title></head>\r\n");
+ strcat(buf, "<body>\r\n");
+ strcat(buf, "<h1>Smart WiFly Web Server</h1>\r\n");
+ strcat(buf, "This page was generated dynamically.<br/>\r\n");
+ strcat(buf, "Client IP Address: ");
+ strcat(buf, remoteIP);
+ strcat(buf, "<br/>\r\n");
+ strcat(buf, "<a href='/'>back to main</a></body></html>\r\n");
+ sprintf(contentlen, "Content-Length: %d\r\n", strlen(buf));
+ // Now the actual header response
+ svr->header(200, "OK", "Content-Type: text/html\r\n", contentlen);
+ // and data are sent
+ svr->send(buf);
ret = true;
break;
case HTTPServer::CONTENT_LENGTH_REQUEST:
@@ -59,7 +72,6 @@
bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount)
{
char buf[100];
- char remoteIP[36];
bool ret = false;
HTTPServer::SW_PerformanceData perfData;
@@ -71,14 +83,15 @@
svr->header(200, "OK", "Content-Type: text/html\r\n");
// Send top of the page
- svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
- svr->send("<body>\r\n");
- svr->send("<h1>Smart WiFly Web Server</h1>\r\n");
- svr->send("This page was generated dynamically. Create your own name=value pairs on the URL "
- "which uses the GET method.<br/>\r\n");
+ svr->send(
+ "<html><head><title>Dynamic Page</title></head>\r\n"
+ "<body>\r\n"
+ "<h1>Smart WiFly Web Server</h1>\r\n"
+ "This page was generated dynamically. Create your own name=value pairs on the URL "
+ "which uses the GET method.<br/>\r\n"
// Show Passed-in parameters
- svr->send("<h2>Query Parameters:</h2><blockquote>\r\n");
+ "<h2>Query Parameters:</h2><blockquote>\r\n");
sprintf(buf, "%d parameters passed to {%s}:<br/>\r\n", paramcount, path);
svr->send(buf);
// show each of the parameters passed on the URL
@@ -89,18 +102,16 @@
svr->send("</blockquote>\r\n");
// Show Memory Stats
- svr->send("<h2>System Statistics:</h2><blockquote>\r\n");
- svr->send("<table border='1'><tr><td>Parameter</td><td>Description</td></tr>\r\n");
+ svr->send(
+ "<h2>System Statistics:</h2><blockquote>\r\n"
+ "<table border='1'><tr><td>Parameter</td><td>Description</td></tr>\r\n");
sprintf(buf,"<tr><td align='right'>%d</td><td>Free memory</td></tr>\r\n", Free());
svr->send(buf);
sprintf(buf,"<tr><td align='right'>%d</td><td>Max Header size</td></tr>\r\n", svr->GetMaxHeaderSize());
svr->send(buf);
-//TODO sprintf(buf,"<tr><td align='right'>%3.2f</td><td>Wifly SW version</td></tr>\r\n", svr->GetWifly()->getWiflyVersion());
-// svr->send(buf);
-// sprintf(buf,"<tr><td align='right'>%s</td><td>Wifly Version Information</td></tr>\r\n", svr->GetWifly()->getWiflyVersionString());
-// svr->send(buf);
- svr->GetRemoteAddr(remoteIP, sizeof(remoteIP));
- sprintf(buf,"<tr><td align='right'>%s</td><td>Client IP Address</td></tr>\r\n", remoteIP);
+ sprintf(buf,"<tr><td align='right'>%3.2f</td><td>Wifly SW version</td></tr>\r\n", svr->GetWifly()->getWiflyVersion());
+ svr->send(buf);
+ sprintf(buf,"<tr><td align='right'>%s</td><td>Wifly Version Information</td></tr>\r\n", svr->GetWifly()->getWiflyVersionString());
svr->send(buf);
svr->send("</table>\r\n");
svr->send("</blockquote>\r\n");
@@ -109,21 +120,40 @@
svr->GetPerformanceData(&perfData);
svr->send("<h2>Web Server Performance Metrics</h2><blockquote>\r\n");
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");
+
sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
- (unsigned long)(perfData.Header.TotalTime_us / perfData.Header.Samples),
- perfData.Header.Samples,
- (unsigned long)(perfData.Header.MaxTime_us),
- "Header Response");
+ (unsigned long)(perfData.ConnectionAccepted.TotalTime_us / perfData.ConnectionAccepted.Samples),
+ perfData.ConnectionAccepted.Samples,
+ (unsigned long)(perfData.ConnectionAccepted.MaxTime_us),
+ "Connection Accepted");
svr->send(buf);
+
+ sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
+ (unsigned long)(perfData.HeaderParsed.TotalTime_us / perfData.HeaderParsed.Samples),
+ perfData.HeaderParsed.Samples,
+ (unsigned long)(perfData.HeaderParsed.MaxTime_us),
+ "Header Parsed");
+ svr->send(buf);
+
sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
- (unsigned long)(perfData.SendData.TotalTime_us / perfData.SendData.Samples),
- perfData.SendData.Samples,
- (unsigned long)(perfData.SendData.MaxTime_us),
- "SendData Response");
+ (unsigned long)(perfData.ResponseSent.TotalTime_us / perfData.ResponseSent.Samples),
+ perfData.ResponseSent.Samples,
+ (unsigned long)(perfData.ResponseSent.MaxTime_us),
+ "Response Sent");
svr->send(buf);
- svr->send("<tr><td colspan='3'> </td><td><a href='?Reset=false'>Reload</a> <a href='?Reset=true'>Reset Metrics</a></td></tr>\r\n");
- svr->send("</table>\r\n");
- svr->send("</blockquote>\r\n");
+
+ sprintf(buf, "<tr><td align='right'>%d</td><td align='right'>%d</td><td align='right'>%d</td><td>%s</td></tr>\r\n",
+ (unsigned long)(perfData.ConnectionClosed.TotalTime_us / perfData.ConnectionClosed.Samples),
+ perfData.ConnectionClosed.Samples,
+ (unsigned long)(perfData.ConnectionClosed.MaxTime_us),
+ "Connection Closed");
+ svr->send(buf);
+
+ svr->send(
+ "<tr><td colspan='3'> </td><td><a href='?Reset=false'>Reload</a>"
+ " <a href='?Reset=true'>Reset Metrics</a></td></tr>\r\n"
+ "</table>\r\n"
+ "</blockquote>\r\n");
// Send bottom of the page
svr->send("<br/><a href='/'>back to main</a></body></html>\r\n");