A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.

Dependents:   Smart-WiFly-WebServer WattEye X10Svr SSDP_Server

Committer:
WiredHome
Date:
Sun May 11 21:16:42 2014 +0000
Revision:
37:0cb2774e2410
Parent:
36:1bb5fa6b109c
Child:
38:c8fa31e6fe02
Enhanced to support POST as well as GET methods.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 1:54353af0d20a 1
WiredHome 1:54353af0d20a 2 #ifndef SW_HTTPSERVER_H
WiredHome 1:54353af0d20a 3 #define SW_HTTPSERVER_H
WiredHome 1:54353af0d20a 4 #include "mbed.h"
WiredHome 36:1bb5fa6b109c 5 //#include "MODSERIAL.h" // would like to hook in mod serial for higher performance, less blocking
WiredHome 36:1bb5fa6b109c 6 #include "RawSerial.h"
WiredHome 1:54353af0d20a 7 #include "Wifly.h"
WiredHome 1:54353af0d20a 8 #include "TCPSocketServer.h"
WiredHome 1:54353af0d20a 9 #include "TCPSocketConnection.h"
WiredHome 1:54353af0d20a 10
WiredHome 1:54353af0d20a 11 #ifdef MODSERIAL_H
WiredHome 1:54353af0d20a 12 #define PC MODSERIAL
WiredHome 1:54353af0d20a 13 #else
WiredHome 36:1bb5fa6b109c 14 #define PC RawSerial
WiredHome 1:54353af0d20a 15 #endif
WiredHome 1:54353af0d20a 16
WiredHome 9:2ea342765c9d 17 /// This is the default buffer size used to send files. You might size
WiredHome 9:2ea342765c9d 18 /// this to be equal or less than the payload size of 1460 bytes.
WiredHome 9:2ea342765c9d 19 /// See User Manual 3.6.1.
WiredHome 9:2ea342765c9d 20 #define FILESEND_BUF_SIZE 1460
WiredHome 3:17928786bdb5 21
WiredHome 3:17928786bdb5 22
WiredHome 12:109bf1558300 23 /// MAX_HEADER_SIZE is the default size to contain the largest header.
WiredHome 9:2ea342765c9d 24 /// This is the size of the URL and query string, and also all the
WiredHome 3:17928786bdb5 25 /// other header information about the client. This can be
WiredHome 12:109bf1558300 26 /// a couple of K, larger if you have big forms as it includes the
WiredHome 3:17928786bdb5 27 /// form data that is submitted.
WiredHome 3:17928786bdb5 28 #define MAX_HEADER_SIZE 1000
WiredHome 3:17928786bdb5 29
WiredHome 9:2ea342765c9d 30
WiredHome 21:660143f20b04 31 /// INCLUDE_GETREMOTE is used for backward compatibility, however
WiredHome 21:660143f20b04 32 /// this makes the server dependent on the WiFly module. By not
WiredHome 21:660143f20b04 33 /// defining this, the web server can be more readily adapted to
WiredHome 21:660143f20b04 34 /// a wired interface using EthernetInterface.
WiredHome 21:660143f20b04 35 //#define INCLUDE_GETREMOTE
WiredHome 21:660143f20b04 36
WiredHome 21:660143f20b04 37
WiredHome 1:54353af0d20a 38 /// HTTPServer is a simple web server using the WiFly module.
WiredHome 12:109bf1558300 39 ///
WiredHome 3:17928786bdb5 40 /// While simple, it is a capable, web server. The basic mode
WiredHome 3:17928786bdb5 41 /// of operation is for it to serve static web pages from an available
WiredHome 3:17928786bdb5 42 /// file system.
WiredHome 12:109bf1558300 43 ///
WiredHome 3:17928786bdb5 44 /// The default page is index.htm (compile time defined)
WiredHome 3:17928786bdb5 45 /// standard support to serve a number of standard file types;
WiredHome 3:17928786bdb5 46 /// gif, jpg, jpeg, ico, png, zip, gz, tar, txt, pdf, htm, html
WiredHome 3:17928786bdb5 47 /// (this list is also compile time defined)
WiredHome 3:17928786bdb5 48 ///
WiredHome 3:17928786bdb5 49 /// It can also serve dynamically generated pages, and therefore
WiredHome 3:17928786bdb5 50 /// respond to form submission. Through the dynamic interface it is
WiredHome 3:17928786bdb5 51 /// then quite easy to interact with the hardware, reading the inputs
WiredHome 3:17928786bdb5 52 /// or signaling outputs.
WiredHome 3:17928786bdb5 53 ///
WiredHome 3:17928786bdb5 54 /// @code
WiredHome 27:90a1f5a5392f 55 /// HTTPServer svr(&wifly, HTTP_SERVER_PORT, "/local", 15, 30, 10, &pc);
WiredHome 3:17928786bdb5 56 /// svr.RegisterHandler("/dyn1", SimpleDynamicPage);
WiredHome 3:17928786bdb5 57 /// while (true)
WiredHome 3:17928786bdb5 58 /// {
WiredHome 3:17928786bdb5 59 /// svr.Poll(); // this is non blocking process, but variable execution
WiredHome 3:17928786bdb5 60 /// }
WiredHome 3:17928786bdb5 61 /// @endcode
WiredHome 3:17928786bdb5 62 ///
WiredHome 3:17928786bdb5 63 /// This web server used nweb as a starting point, but expanded well beyond there.
WiredHome 0:729320f63c5c 64 /// http://www.ibm.com/developerworks/systems/library/es-nweb/sidefile1.html
WiredHome 0:729320f63c5c 65 ///
WiredHome 3:17928786bdb5 66 /// @note This server uses a modified version of the mbed WiflyInterface - there
WiredHome 3:17928786bdb5 67 /// were a number of performance issues identified and resolved in the local version.
WiredHome 0:729320f63c5c 68 ///
WiredHome 3:17928786bdb5 69 /// Given: scheme://server:port/path?query_string#fragment_id
WiredHome 0:729320f63c5c 70 /// @li scheme is "http"
WiredHome 3:17928786bdb5 71 /// @li server is whatever IP the server has
WiredHome 0:729320f63c5c 72 /// @li port is the registered port
WiredHome 0:729320f63c5c 73 /// @li /path is the reference to the file (actual or logical) on the server
WiredHome 0:729320f63c5c 74 /// @li query_string is any combination of name=value pairs
WiredHome 0:729320f63c5c 75 /// @li fragment_id is a reference to an anchor on the page
WiredHome 0:729320f63c5c 76 ///
WiredHome 3:17928786bdb5 77 /// Features:
WiredHome 3:17928786bdb5 78 /// @li Serves static pages from a file system. Many normal filetypes are
WiredHome 3:17928786bdb5 79 /// supported.
WiredHome 3:17928786bdb5 80 /// @li Compile time configurable for the "default" file, typically index.htm.
WiredHome 3:17928786bdb5 81 /// @li Provides a registration interface for dynamically generated pages that
WiredHome 3:17928786bdb5 82 /// can then interact with other hardware.
WiredHome 3:17928786bdb5 83 /// @li Revised to be Non-blocking, however the execution time is variable
WiredHome 7:99ad7a67f05e 84 /// depending on the actions being performed and can span hundreds of msec.
WiredHome 3:17928786bdb5 85 ///
WiredHome 3:17928786bdb5 86 /// Limitations:
WiredHome 3:17928786bdb5 87 /// @li Supports only a single connection at a time.
WiredHome 18:6199558632c0 88 /// A web page with served objects (img src=...) is rarely served properly. It
WiredHome 18:6199558632c0 89 /// might trace to forcing the connection to close, but not yet sure.
WiredHome 18:6199558632c0 90 /// Explore "Set Uart Rx Data Buffer" in WiFly manual 2.3.65.
WiredHome 18:6199558632c0 91 /// This is a limitation of the Wifly module. No solution is forthcoming,
WiredHome 18:6199558632c0 92 /// so a simple workaround is to use javascript to load the images after
WiredHome 18:6199558632c0 93 /// the page loads.
WiredHome 3:17928786bdb5 94 /// @li Rapid requests for page objects (e.g. embedded images) are lost. Still
WiredHome 3:17928786bdb5 95 /// working to understand this issue.
WiredHome 3:17928786bdb5 96 ///
WiredHome 18:6199558632c0 97 /// Improvements:
WiredHome 18:6199558632c0 98 /// @li hunted down several lengthy operations - the speed of the file system
WiredHome 18:6199558632c0 99 /// and the "close" operation which requires <delay 0.25s>$$$<delay>close\r.
WiredHome 18:6199558632c0 100 /// @li parses the header similar to the query string, and then makes
WiredHome 18:6199558632c0 101 /// those parameters accessible.
WiredHome 18:6199558632c0 102 /// @li Added basic password capability to dynamic web pages.
WiredHome 18:6199558632c0 103 ///
WiredHome 3:17928786bdb5 104 /// ToDo:
WiredHome 3:17928786bdb5 105 /// @li move part of the POST method handler to the registered handler, so
WiredHome 0:729320f63c5c 106 /// it can decide if it should allocate the needed memory.
WiredHome 3:17928786bdb5 107 /// @li transform the pc serial interface to a log interface, which might
WiredHome 0:729320f63c5c 108 /// be more useful.
WiredHome 3:17928786bdb5 109 /// @li Add ability to put WiFly in AP mode and then configuration pages
WiredHome 2:a29c32190037 110 /// to find and join a network.
WiredHome 3:17928786bdb5 111 /// @li Add ability to change/update SW in the WiFly module
WiredHome 3:17928786bdb5 112 /// @li Add ability to upload a new application to the mbed
WiredHome 2:a29c32190037 113 ///
WiredHome 2:a29c32190037 114 /// History:
WiredHome 2:a29c32190037 115 /// @li 20130530 Initial version
WiredHome 2:a29c32190037 116 /// @li 20130601 Renamed ip_process to Poll
WiredHome 3:17928786bdb5 117 /// @li 20130617 Cleaned up some of the documentation changes
WiredHome 3:17928786bdb5 118 /// @li 20130623 Make it non-blocking. "Poll" takes a variable amount
WiredHome 3:17928786bdb5 119 /// of time, based on whether it is idle, or how much it
WiredHome 3:17928786bdb5 120 /// has to do.
WiredHome 18:6199558632c0 121 /// @li 20130911 Lots of incremental changes along this way, this update
WiredHome 18:6199558632c0 122 /// refreshes the documentation.
WiredHome 0:729320f63c5c 123 ///
WiredHome 0:729320f63c5c 124 /// @note Copyright &copy; 2013 by Smartware Computing, all rights reserved.
WiredHome 0:729320f63c5c 125 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:729320f63c5c 126 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:729320f63c5c 127 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:729320f63c5c 128 /// clear that their work is a derived work, and not the original.
WiredHome 0:729320f63c5c 129 /// Users of this application and sources accept this application "as is" and
WiredHome 0:729320f63c5c 130 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:729320f63c5c 131 /// using this application - whether real or imagined.
WiredHome 0:729320f63c5c 132 ///
WiredHome 0:729320f63c5c 133 /// @author David Smart, Smartware Computing
WiredHome 0:729320f63c5c 134 ///
WiredHome 0:729320f63c5c 135 class HTTPServer
WiredHome 0:729320f63c5c 136 {
WiredHome 0:729320f63c5c 137 public:
WiredHome 0:729320f63c5c 138 /**
WiredHome 3:17928786bdb5 139 * name-value pairs for parameters
WiredHome 0:729320f63c5c 140 */
WiredHome 3:17928786bdb5 141 typedef struct NAMEVALUE {
WiredHome 0:729320f63c5c 142 char * name;
WiredHome 0:729320f63c5c 143 char * value;
WiredHome 0:729320f63c5c 144 } namevalue;
WiredHome 12:109bf1558300 145
WiredHome 2:a29c32190037 146 /**
WiredHome 3:17928786bdb5 147 * Indicates the purpose of the Handler callback
WiredHome 3:17928786bdb5 148 *
WiredHome 12:109bf1558300 149 * Application code in a dynamic page uses this to determine the state
WiredHome 3:17928786bdb5 150 * and therefore the needed operation to be performed.
WiredHome 3:17928786bdb5 151 *
WiredHome 3:17928786bdb5 152 * @code
WiredHome 13:8975d7928678 153 * bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type,
WiredHome 13:8975d7928678 154 * const char * path, const HTTPServer::namevalue *queryParams,
WiredHome 13:8975d7928678 155 * int queryParamCount) {
WiredHome 3:17928786bdb5 156 * char buf[100];
WiredHome 3:17928786bdb5 157 * bool ret = false;
WiredHome 12:109bf1558300 158 *
WiredHome 3:17928786bdb5 159 * switch (type) {
WiredHome 3:17928786bdb5 160 * case HTTPServer::SEND_PAGE:
WiredHome 3:17928786bdb5 161 * svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 3:17928786bdb5 162 * svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
WiredHome 3:17928786bdb5 163 * svr->send("<body>\r\n");
WiredHome 3:17928786bdb5 164 * svr->send("This page was generated dynamically. Create your own name=value pairs on the URL "
WiredHome 3:17928786bdb5 165 * "which uses the GET method.<br/>\r\n");
WiredHome 13:8975d7928678 166 * sprintf(buf, "%d parameters passed to {%s}:<br/>\r\n", queryParamCount, path);
WiredHome 3:17928786bdb5 167 * svr->send(buf);
WiredHome 13:8975d7928678 168 * for (int i=0; i<queryParamCount; i++) {
WiredHome 13:8975d7928678 169 * sprintf(buf, "%d: %s = %s<br/>\r\n", i, queryParams[i].name, queryParams[i].value);
WiredHome 3:17928786bdb5 170 * svr->send(buf);
WiredHome 3:17928786bdb5 171 * }
WiredHome 3:17928786bdb5 172 * svr->send("<br/><a href='/'>back to main</a></body></html>\r\n");
WiredHome 3:17928786bdb5 173 * ret = true;
WiredHome 3:17928786bdb5 174 * break;
WiredHome 3:17928786bdb5 175 * case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 3:17928786bdb5 176 * ret = true;
WiredHome 3:17928786bdb5 177 * break;
WiredHome 3:17928786bdb5 178 * case HTTPServer::DATA_TRANSFER:
WiredHome 3:17928786bdb5 179 * ret = true;
WiredHome 3:17928786bdb5 180 * break;
WiredHome 3:17928786bdb5 181 * default:
WiredHome 3:17928786bdb5 182 * ret = false;
WiredHome 3:17928786bdb5 183 * break;
WiredHome 3:17928786bdb5 184 * }
WiredHome 3:17928786bdb5 185 * return ret;
WiredHome 3:17928786bdb5 186 * }
WiredHome 3:17928786bdb5 187 * @endcode
WiredHome 2:a29c32190037 188 */
WiredHome 3:17928786bdb5 189 typedef enum CALLBACKTYPE {
WiredHome 14:19c5f6151319 190 CONTENT_LENGTH_REQUEST, ///< ask the client if they wish to accept the data, typically from a POST event
WiredHome 27:90a1f5a5392f 191 DATA_TRANSFER, ///< used when submitting a file via a form
WiredHome 33:ef165a67ab22 192 DATA_TRANSFER_END, ///< used when all data has been given to the client (may be used to close the file)
WiredHome 3:17928786bdb5 193 SEND_PAGE, ///< the activated method should now send the page
WiredHome 2:a29c32190037 194 } CallBackType;
WiredHome 0:729320f63c5c 195
WiredHome 28:f93ef41b78e1 196 typedef enum CALLBACKRESULTS {
WiredHome 28:f93ef41b78e1 197 ACCEPT_ERROR, ///< client not accepting the request.
WiredHome 28:f93ef41b78e1 198 ACCEPT_COMPLETE, ///< client accepted the request, the work is done.
WiredHome 28:f93ef41b78e1 199 ACCEPT_CONTINUE, ///< client accepted the request, additional transactions to complete.
WiredHome 28:f93ef41b78e1 200 } CallBackResults;
WiredHome 28:f93ef41b78e1 201
WiredHome 12:109bf1558300 202 /**
WiredHome 3:17928786bdb5 203 * This is the prototype for custom handlers that are activated via a callback
WiredHome 0:729320f63c5c 204 *
WiredHome 37:0cb2774e2410 205 * This callback gets overloaded for a few purposes, which can be identified by the
WiredHome 37:0cb2774e2410 206 * \see CallBackType parameter.
WiredHome 37:0cb2774e2410 207 *
WiredHome 27:90a1f5a5392f 208 * @li CONTENT_LENGTH_REQUEST - the server is asking the callback if it wants to receive the message,
WiredHome 27:90a1f5a5392f 209 * which may require significant memory. If the request is accepted, true should be returned.
WiredHome 27:90a1f5a5392f 210 * If the request is denied, false should be returned.
WiredHome 27:90a1f5a5392f 211 * @li DATA_TRANSFER - the server is handing off a large body of data, which was accepted based
WiredHome 27:90a1f5a5392f 212 * on the CONTENT_LENGTH_REQUEST callback. The data is now available for processing.
WiredHome 27:90a1f5a5392f 213 * The callback should return true to continue the processing.
WiredHome 12:109bf1558300 214 * @li SEND_PAGE - the callback should now send the html page, using as many svr->send() as needed.
WiredHome 29:00116fc9da74 215 * When the callback returns, it should always indicate true that it has sent the page.
WiredHome 27:90a1f5a5392f 216 *
WiredHome 27:90a1f5a5392f 217 * @note The queryParams pointer purpose depends on the callback type.
WiredHome 33:ef165a67ab22 218 * For CONTENT_LENGTH_REQUEST, the pointer points to the name=value pairs from the
WiredHome 27:90a1f5a5392f 219 * header.
WiredHome 33:ef165a67ab22 220 * For DATA_TRANSFER, the pointer points to the start of the actual data.
WiredHome 29:00116fc9da74 221 * For SEND_PAGE, ... <to be determined>
WiredHome 12:109bf1558300 222 *
WiredHome 0:729320f63c5c 223 * @param svr is a handle to this class, so the callback has access to member functions
WiredHome 33:ef165a67ab22 224 * @param type is the callback type @see CallBackType
WiredHome 37:0cb2774e2410 225 * @param path is the pointer to a large block of information being transferred. This pointer
WiredHome 37:0cb2774e2410 226 * references a dynamically managed resource, so any information of value must be
WiredHome 37:0cb2774e2410 227 * extracted from here, and not referenced into this memory space.
WiredHome 27:90a1f5a5392f 228 * @param queryParams is a pointer based on the callback type.
WiredHome 33:ef165a67ab22 229 * @param count is the number of items - for type = CONTENT_LENGTH_REQUEST this is the number of
WiredHome 33:ef165a67ab22 230 * name=value pars in the queryParams parameter, and for the DATA_TRANSFER this is the
WiredHome 33:ef165a67ab22 231 * number of bytes being passed in the path parameters.
WiredHome 33:ef165a67ab22 232 * @return one of the @see CallBackResults signals indicating error or successes
WiredHome 0:729320f63c5c 233 */
WiredHome 37:0cb2774e2410 234 typedef CallBackResults (* Handler)(HTTPServer * svr, CallBackType type, const char *path,
WiredHome 37:0cb2774e2410 235 const namevalue *queryParams, int queryParamCount);
WiredHome 12:109bf1558300 236
WiredHome 0:729320f63c5c 237 /**
WiredHome 0:729320f63c5c 238 * Create the HTTPServer object.
WiredHome 12:109bf1558300 239 *
WiredHome 0:729320f63c5c 240 * @param wifly is the serial port with the wifly interface.
WiredHome 0:729320f63c5c 241 * @param port is the optional parameter for the port number to use, default is 80.
WiredHome 27:90a1f5a5392f 242 * @param webroot is a file system path to the root folder for the web space. If any trailing '/'
WiredHome 27:90a1f5a5392f 243 * is included (e.g. "/web/path/") it will be removed (to "/web/path").
WiredHome 37:0cb2774e2410 244 * @param maxheaderParams defines the maximum number of parameters to extract from a header
WiredHome 37:0cb2774e2410 245 * (Host: 192..\r\nConnection: keep-alive\r\n...)
WiredHome 37:0cb2774e2410 246 * @param maxqueryParams defines the maximum number of query parameters to a dynamic function
WiredHome 37:0cb2774e2410 247 * (and the memory to support them).
WiredHome 0:729320f63c5c 248 * @param maxdynamicpages defines the maximum number of dynamic pages that can be registered.
WiredHome 0:729320f63c5c 249 * @param pc is the serial port for debug information (I should transform this to a log interface)
WiredHome 3:17928786bdb5 250 * @param allocforheader is the memory allocation to support the largest expected header from a client
WiredHome 37:0cb2774e2410 251 * @param allocforfile is the memory allocation to support sending a file to the client. This is
WiredHome 37:0cb2774e2410 252 * typically sized to fit an ethernet frame.
WiredHome 0:729320f63c5c 253 */
WiredHome 37:0cb2774e2410 254 HTTPServer(Wifly * wifly, int port = 80, const char * webroot = "/", int maxheaderParams = 15,
WiredHome 37:0cb2774e2410 255 int maxqueryParams = 30, int maxdynamicpages = 10,
WiredHome 37:0cb2774e2410 256 PC * pc = NULL, int _allocforheader = MAX_HEADER_SIZE, int _allocforfile = FILESEND_BUF_SIZE);
WiredHome 12:109bf1558300 257
WiredHome 0:729320f63c5c 258 /**
WiredHome 3:17928786bdb5 259 * Destructor, which can clean up memory.
WiredHome 0:729320f63c5c 260 */
WiredHome 0:729320f63c5c 261 ~HTTPServer();
WiredHome 12:109bf1558300 262
WiredHome 0:729320f63c5c 263 /**
WiredHome 27:90a1f5a5392f 264 * Get the path to the webroot, for applications that need to
WiredHome 27:90a1f5a5392f 265 * reference the file system relative to that point.
WiredHome 24:062431453abb 266 *
WiredHome 27:90a1f5a5392f 267 * @note The returned value may not be exactly as set at instantiation
WiredHome 27:90a1f5a5392f 268 * as trailing '/' were removed (unless the web root == "/").
WiredHome 27:90a1f5a5392f 269 * e.g. "/msc/web/" becomes "/msc/web"
WiredHome 27:90a1f5a5392f 270 *
WiredHome 27:90a1f5a5392f 271 * @returns pointer to the webroot string.
WiredHome 24:062431453abb 272 */
WiredHome 24:062431453abb 273 const char * GetWebRoot() {
WiredHome 27:90a1f5a5392f 274 return (const char *)webroot;
WiredHome 27:90a1f5a5392f 275 };
WiredHome 24:062431453abb 276
WiredHome 24:062431453abb 277 /**
WiredHome 3:17928786bdb5 278 * The process to call whenever there is free time, as this basically does
WiredHome 0:729320f63c5c 279 * all the work to monitor for connections and handle replies.
WiredHome 2:a29c32190037 280 *
WiredHome 2:a29c32190037 281 * 20130601 Renamed from ip_process to Poll
WiredHome 0:729320f63c5c 282 */
WiredHome 2:a29c32190037 283 void Poll();
WiredHome 12:109bf1558300 284
WiredHome 0:729320f63c5c 285 /**
WiredHome 12:109bf1558300 286 * Send typical header data, and some optional data back to the client.
WiredHome 3:17928786bdb5 287 *
WiredHome 3:17928786bdb5 288 * This forms and sends the typical header back to the client. It may also send
WiredHome 3:17928786bdb5 289 * optional data (which must end with "\r\n"). It then sends the second newline
WiredHome 3:17928786bdb5 290 * sequence that signals the end of the header.
WiredHome 0:729320f63c5c 291 *
WiredHome 0:729320f63c5c 292 * @param code is the optional return code; 200 = OK, if not provided then 404 = Not found is returned
WiredHome 0:729320f63c5c 293 * @param code_text is the text to align with the code (e.g. 404, "Not Found")
WiredHome 0:729320f63c5c 294 * @param content_type is a pointer to "Content-Type: text/html\r\n" (for example)
WiredHome 3:17928786bdb5 295 * @param optional_text is a pointer to any other text that is part of the header, which must
WiredHome 3:17928786bdb5 296 * have \r\n termination.
WiredHome 0:729320f63c5c 297 */
WiredHome 37:0cb2774e2410 298 void header(int code = 404, const char * code_text = "Not Found", const char * content_type = NULL,
WiredHome 37:0cb2774e2410 299 const char * optional_text = NULL);
WiredHome 0:729320f63c5c 300
WiredHome 0:729320f63c5c 301 /**
WiredHome 0:729320f63c5c 302 * Send text to the client
WiredHome 0:729320f63c5c 303 *
WiredHome 3:17928786bdb5 304 * This sends the specified text to the client. If the number of bytes is not set,
WiredHome 3:17928786bdb5 305 * then it calculates the number of bytes as a string. For binary transfers, the
WiredHome 3:17928786bdb5 306 * number of bytes to send is required for proper operation.
WiredHome 3:17928786bdb5 307 *
WiredHome 0:729320f63c5c 308 * @param msg is the text string to send
WiredHome 0:729320f63c5c 309 * @param bytes is the number of bytes to send. If not set, then strlen is calculated.
WiredHome 0:729320f63c5c 310 */
WiredHome 0:729320f63c5c 311 void send(const char * msg, int bytes = -1);
WiredHome 12:109bf1558300 312
WiredHome 0:729320f63c5c 313 /**
WiredHome 3:17928786bdb5 314 * Send a referenced file to the client, including the header
WiredHome 3:17928786bdb5 315 *
WiredHome 3:17928786bdb5 316 * This sends a file from the filesystem to the client. It must be of a supported type
WiredHome 3:17928786bdb5 317 * in order to properly create the header.
WiredHome 0:729320f63c5c 318 *
WiredHome 0:729320f63c5c 319 * @param filename is the fully qualified path and filename
WiredHome 3:17928786bdb5 320 * @param filetype is the header information (e.g. "Content-Type: application/pdf")
WiredHome 0:729320f63c5c 321 * @return true if it thinks it sent ok, false otherwise.
WiredHome 0:729320f63c5c 322 */
WiredHome 0:729320f63c5c 323 bool SendFile(const char * filename, const char * filetype);
WiredHome 12:109bf1558300 324
WiredHome 12:109bf1558300 325 /**
WiredHome 0:729320f63c5c 326 * register a handler for a specific URL.
WiredHome 0:729320f63c5c 327 *
WiredHome 3:17928786bdb5 328 * This api lets you register a dynamic handler in the web server. This is
WiredHome 3:17928786bdb5 329 * most useful for interactive web pages, rather than simply serving static
WiredHome 3:17928786bdb5 330 * pages.
WiredHome 3:17928786bdb5 331 *
WiredHome 3:17928786bdb5 332 * @code
WiredHome 12:109bf1558300 333 *
WiredHome 3:17928786bdb5 334 * ...
WiredHome 3:17928786bdb5 335 * svr.RegisterHandler("/dyn1", SimpleDynamicPage);svr.RegisterHandler("/dyn1", SimpleDynamicPage);
WiredHome 3:17928786bdb5 336 * ...
WiredHome 3:17928786bdb5 337 *
WiredHome 37:0cb2774e2410 338 * bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path,
WiredHome 37:0cb2774e2410 339 * const HTTPServer::namevalue *queryParams, int queryParamCount) {
WiredHome 3:17928786bdb5 340 * char buf[100];
WiredHome 3:17928786bdb5 341 * bool ret = false;
WiredHome 12:109bf1558300 342 *
WiredHome 3:17928786bdb5 343 * switch (type) {
WiredHome 3:17928786bdb5 344 * case HTTPServer::SEND_PAGE:
WiredHome 3:17928786bdb5 345 * svr->header(200, "OK", "Content-Type: text/html\r\n");
WiredHome 3:17928786bdb5 346 * svr->send("<html><head><title>Dynamic Page</title></head>\r\n");
WiredHome 3:17928786bdb5 347 * svr->send("<body>\r\n");
WiredHome 3:17928786bdb5 348 * svr->send("This page was generated dynamically. Create your own name=value pairs on the URL "
WiredHome 3:17928786bdb5 349 * "which uses the GET method.<br/>\r\n");
WiredHome 13:8975d7928678 350 * sprintf(buf, "%d parameters passed to {%s}:<br/>\r\n", queryParamCount, path);
WiredHome 3:17928786bdb5 351 * svr->send(buf);
WiredHome 13:8975d7928678 352 * for (int i=0; i<queryParamCount; i++) {
WiredHome 13:8975d7928678 353 * sprintf(buf, "%d: %s = %s<br/>\r\n", i, queryParams[i].name, queryParams[i].value);
WiredHome 3:17928786bdb5 354 * svr->send(buf);
WiredHome 3:17928786bdb5 355 * }
WiredHome 3:17928786bdb5 356 * svr->send("Stats:<br/>\r\n");
WiredHome 3:17928786bdb5 357 * sprintf(buf,"Free memory space: %d<br/>\r\n", Free());
WiredHome 3:17928786bdb5 358 * svr->send(buf);
WiredHome 3:17928786bdb5 359 * sprintf(buf,"Max Header size: %d<br/>\r\n", svr->GetMaxHeaderSize());
WiredHome 3:17928786bdb5 360 * svr->send(buf);
WiredHome 3:17928786bdb5 361 * svr->send("<br/><a href='/'>back to main</a></body></html>\r\n");
WiredHome 3:17928786bdb5 362 * ret = true;
WiredHome 3:17928786bdb5 363 * break;
WiredHome 3:17928786bdb5 364 * case HTTPServer::CONTENT_LENGTH_REQUEST:
WiredHome 3:17928786bdb5 365 * ret = true;
WiredHome 3:17928786bdb5 366 * break;
WiredHome 3:17928786bdb5 367 * case HTTPServer::DATA_TRANSFER:
WiredHome 3:17928786bdb5 368 * ret = true;
WiredHome 3:17928786bdb5 369 * break;
WiredHome 3:17928786bdb5 370 * default:
WiredHome 3:17928786bdb5 371 * ret = false;
WiredHome 3:17928786bdb5 372 * break;
WiredHome 3:17928786bdb5 373 * }
WiredHome 3:17928786bdb5 374 * return ret;
WiredHome 3:17928786bdb5 375 * }
WiredHome 3:17928786bdb5 376 * @endcode
WiredHome 3:17928786bdb5 377 *
WiredHome 0:729320f63c5c 378 * @param path to register
WiredHome 0:729320f63c5c 379 * @param callback of type Handler
WiredHome 0:729320f63c5c 380 * @return true if successfully registered
WiredHome 0:729320f63c5c 381 */
WiredHome 0:729320f63c5c 382 bool RegisterHandler(const char * path, Handler callback);
WiredHome 12:109bf1558300 383
WiredHome 0:729320f63c5c 384 /**
WiredHome 16:6ebacf2946d8 385 * determine if the named file is a supported type (htm, html, jpg, etc)
WiredHome 0:729320f63c5c 386 *
WiredHome 3:17928786bdb5 387 * if you pass in a filename, it will attempt to extract the extension
WiredHome 3:17928786bdb5 388 * and compare that to the list of supported file types. If it finds a
WiredHome 3:17928786bdb5 389 * match, then it will return a pointer to the content-type string.
WiredHome 3:17928786bdb5 390 *
WiredHome 3:17928786bdb5 391 * @code
WiredHome 3:17928786bdb5 392 * fType = GetSupportedType("mypix.jpg");
WiredHome 3:17928786bdb5 393 * if (fType) {
WiredHome 3:17928786bdb5 394 * ...
WiredHome 3:17928786bdb5 395 * @endcode
WiredHome 12:109bf1558300 396 *
WiredHome 0:729320f63c5c 397 * @param filename is the filename to test, based on the extension
WiredHome 0:729320f63c5c 398 * @return pointer to a Content-Type string if supported, or NULL if not.
WiredHome 0:729320f63c5c 399 */
WiredHome 0:729320f63c5c 400 const char * GetSupportedType(const char * filename);
WiredHome 0:729320f63c5c 401
WiredHome 0:729320f63c5c 402 /**
WiredHome 0:729320f63c5c 403 * search the available parameters for 'name' and if found, return the 'value'
WiredHome 0:729320f63c5c 404 *
WiredHome 12:109bf1558300 405 * After the querystring is parsed, the server maintains an array of
WiredHome 3:17928786bdb5 406 * name=value pairs. This Get function will search for the passed in name
WiredHome 3:17928786bdb5 407 * and provide access to the value.
WiredHome 3:17928786bdb5 408 *
WiredHome 3:17928786bdb5 409 * @code
WiredHome 3:17928786bdb5 410 * BusOut leds(LED1,LED2,LED3,LED4);
WiredHome 3:17928786bdb5 411 * ...
WiredHome 3:17928786bdb5 412 * leds = atoi(svr->GetParameter("leds"));
WiredHome 3:17928786bdb5 413 * @endcode
WiredHome 3:17928786bdb5 414 *
WiredHome 0:729320f63c5c 415 * @param name is the name to search for
WiredHome 0:729320f63c5c 416 * @return pointer to the value, or NULL
WiredHome 0:729320f63c5c 417 */
WiredHome 0:729320f63c5c 418 const char * GetParameter(const char * name);
WiredHome 0:729320f63c5c 419
WiredHome 0:729320f63c5c 420 /**
WiredHome 12:109bf1558300 421 * Parse the text string into name=value parameters.
WiredHome 3:17928786bdb5 422 *
WiredHome 12:109bf1558300 423 * This will directly modify the referenced string. If there is a
WiredHome 3:17928786bdb5 424 * #fragment_id on the end of the string, it will be removed.
WiredHome 0:729320f63c5c 425 *
WiredHome 0:729320f63c5c 426 * @param pString is a pointer to the string.
WiredHome 37:0cb2774e2410 427 * @returns The total number of items that have been parsed,
WiredHome 37:0cb2774e2410 428 * which can include a count from a url query string.
WiredHome 0:729320f63c5c 429 */
WiredHome 37:0cb2774e2410 430 int ParseParameters(char * pString);
WiredHome 12:109bf1558300 431
WiredHome 0:729320f63c5c 432 /**
WiredHome 16:6ebacf2946d8 433 * Unescape string converts a coded string "in place" into a normal string.
WiredHome 3:17928786bdb5 434 *
WiredHome 3:17928786bdb5 435 * A query string will have a number of characters replaced for communication
WiredHome 3:17928786bdb5 436 * which includes spaces, quotes, question marks and more. Most of them
WiredHome 12:109bf1558300 437 * will be replaced with a %xx format, where xx is the hex code for the
WiredHome 3:17928786bdb5 438 * character. Since the string will only get shorter when this happens
WiredHome 3:17928786bdb5 439 * the operation is performed in place.
WiredHome 3:17928786bdb5 440 *
WiredHome 0:729320f63c5c 441 * this "This%20is%20a%20question%3F%20and%20an%20answer."
WiredHome 12:109bf1558300 442 *
WiredHome 0:729320f63c5c 443 * becomes "This is a question? and an answer."
WiredHome 3:17928786bdb5 444 *
WiredHome 0:729320f63c5c 445 * @note '+' is another form of space, so is converted to a space before the %xx
WiredHome 0:729320f63c5c 446 *
WiredHome 0:729320f63c5c 447 * @param encoded string to be converted
WiredHome 0:729320f63c5c 448 */
WiredHome 0:729320f63c5c 449 void UnescapeString(char * encoded);
WiredHome 12:109bf1558300 450
WiredHome 21:660143f20b04 451
WiredHome 21:660143f20b04 452 #ifdef INCLUDE_GETREMOTE
WiredHome 0:729320f63c5c 453 /**
WiredHome 0:729320f63c5c 454 * Get the IP address of the remote node to which we are connected.
WiredHome 3:17928786bdb5 455 *
WiredHome 3:17928786bdb5 456 * This will get the IP address of the remote node to which we are
WiredHome 12:109bf1558300 457 * currently connected. This is written into the buffer in
WiredHome 36:1bb5fa6b109c 458 * "192.168.100.234" format. If the buffer size is not >= 16 bytes,
WiredHome 3:17928786bdb5 459 * it will set the buffer to null.
WiredHome 12:109bf1558300 460 *
WiredHome 21:660143f20b04 461 * @deprecated This binds the web server to only network interfaces
WiredHome 21:660143f20b04 462 * that support this API (WiFly), so can limit its use.
WiredHome 21:660143f20b04 463 * This API will be removed.
WiredHome 21:660143f20b04 464 *
WiredHome 3:17928786bdb5 465 * @note This switches the module into, and out of, command mode
WiredHome 12:109bf1558300 466 * which has quite a time penalty.
WiredHome 3:17928786bdb5 467 *
WiredHome 3:17928786bdb5 468 * @param str is the string to write the address into, which should be at
WiredHome 3:17928786bdb5 469 * least as large as "192.168.100.203" (16-bytes).
WiredHome 4:f34642902056 470 * @param strSize of the str buffer must be >=16, so it will not buffer overrun.
WiredHome 5:c9b27e718054 471 * @returns true if it succeeded, false otherwise
WiredHome 0:729320f63c5c 472 */
WiredHome 5:c9b27e718054 473 bool GetRemoteAddr(char * str, int strSize);
WiredHome 21:660143f20b04 474 #endif
WiredHome 0:729320f63c5c 475
WiredHome 12:109bf1558300 476 /**
WiredHome 16:6ebacf2946d8 477 * This is used to force a connection to close.
WiredHome 3:17928786bdb5 478 *
WiredHome 3:17928786bdb5 479 * This switches the module into command mode, performs the close,
WiredHome 3:17928786bdb5 480 * then switches it back to data mode. So, this is a time-expensive
WiredHome 3:17928786bdb5 481 * command.
WiredHome 7:99ad7a67f05e 482 *
WiredHome 7:99ad7a67f05e 483 * @returns true if successful
WiredHome 0:729320f63c5c 484 */
WiredHome 7:99ad7a67f05e 485 bool close_connection();
WiredHome 12:109bf1558300 486
WiredHome 3:17928786bdb5 487 /**
WiredHome 16:6ebacf2946d8 488 * Diagnostic to get the size of the largest header.
WiredHome 3:17928786bdb5 489 *
WiredHome 12:109bf1558300 490 * This is a diagnostic function, so you can resize the allocated
WiredHome 12:109bf1558300 491 * buffer for your application. With proper sizing, more of the
WiredHome 3:17928786bdb5 492 * system memory is available for your application.
WiredHome 3:17928786bdb5 493 *
WiredHome 3:17928786bdb5 494 * @code
WiredHome 3:17928786bdb5 495 * sprintf(buf,"Max Header size: %d<br/>\r\n", svr->GetMaxHeaderSize());
WiredHome 3:17928786bdb5 496 * svr->send(buf);
WiredHome 3:17928786bdb5 497 * @endcode
WiredHome 12:109bf1558300 498 *
WiredHome 3:17928786bdb5 499 * @returns size in bytes of the larger header measured.
WiredHome 3:17928786bdb5 500 */
WiredHome 3:17928786bdb5 501 int GetMaxHeaderSize();
WiredHome 3:17928786bdb5 502
WiredHome 13:8975d7928678 503 /**
WiredHome 16:6ebacf2946d8 504 * Get a value from the http header, if it exists.
WiredHome 13:8975d7928678 505 *
WiredHome 13:8975d7928678 506 * @param hdr is the string to search for (e.g. "Content-Length")
WiredHome 13:8975d7928678 507 *
WiredHome 13:8975d7928678 508 * @returns pointer to the value associated with that header.
WiredHome 13:8975d7928678 509 * @returns NULL if the header is not found.
WiredHome 13:8975d7928678 510 */
WiredHome 13:8975d7928678 511 const char * GetHeaderValue(const char * hdr);
WiredHome 3:17928786bdb5 512
WiredHome 3:17928786bdb5 513 /**
WiredHome 3:17928786bdb5 514 * Performance parameter
WiredHome 3:17928786bdb5 515 */
WiredHome 3:17928786bdb5 516 typedef struct SW_PERFPARAM {
WiredHome 3:17928786bdb5 517 unsigned long long TotalTime_us;
WiredHome 3:17928786bdb5 518 unsigned long Samples;
WiredHome 3:17928786bdb5 519 unsigned long MaxTime_us;
WiredHome 3:17928786bdb5 520 } SW_PerformanceParam;
WiredHome 12:109bf1558300 521
WiredHome 3:17928786bdb5 522 /**
WiredHome 3:17928786bdb5 523 * Performance metrics
WiredHome 3:17928786bdb5 524 */
WiredHome 3:17928786bdb5 525 typedef struct SW_PERFDATA {
WiredHome 17:69ff00ce39f4 526 SW_PerformanceParam ConnectionAccepted;
WiredHome 17:69ff00ce39f4 527 SW_PerformanceParam HeaderParsed;
WiredHome 17:69ff00ce39f4 528 SW_PerformanceParam ResponseSent;
WiredHome 17:69ff00ce39f4 529 SW_PerformanceParam ConnectionClosed;
WiredHome 3:17928786bdb5 530 //SW_PerformanceParam SendFile;
WiredHome 3:17928786bdb5 531 } SW_PerformanceData;
WiredHome 12:109bf1558300 532
WiredHome 3:17928786bdb5 533 /**
WiredHome 3:17928786bdb5 534 * Get performance metrics from the web server.
WiredHome 3:17928786bdb5 535 *
WiredHome 3:17928786bdb5 536 * This is a diagnostic function, and gathers data on the internal
WiredHome 3:17928786bdb5 537 * performance of the server, as it works various actions.
WiredHome 3:17928786bdb5 538 *
WiredHome 3:17928786bdb5 539 * @param p is a pointer to a SW_PerformanceData structure to be populated
WiredHome 3:17928786bdb5 540 */
WiredHome 3:17928786bdb5 541 void GetPerformanceData(SW_PerformanceData * p);
WiredHome 12:109bf1558300 542
WiredHome 3:17928786bdb5 543 /**
WiredHome 3:17928786bdb5 544 * Reset performance metrics.
WiredHome 3:17928786bdb5 545 */
WiredHome 3:17928786bdb5 546 void ResetPerformanceData();
WiredHome 17:69ff00ce39f4 547
WiredHome 17:69ff00ce39f4 548 /**
WiredHome 17:69ff00ce39f4 549 * Get performance clock
WiredHome 17:69ff00ce39f4 550 */
WiredHome 17:69ff00ce39f4 551 unsigned int GetPerformanceClock();
WiredHome 12:109bf1558300 552
WiredHome 5:c9b27e718054 553 /**
WiredHome 5:c9b27e718054 554 * Get the underlying wifly object.
WiredHome 6:fdce4464d92b 555 *
WiredHome 6:fdce4464d92b 556 * This lets you get to the underlying wifly object in order to
WiredHome 6:fdce4464d92b 557 * interact with it.
WiredHome 6:fdce4464d92b 558 *
WiredHome 6:fdce4464d92b 559 * @code
WiredHome 13:8975d7928678 560 * HTTPServer svr(&wifly, HTTP_SERVER_PORT, "/local/", 15, 30, 10, &pc);
WiredHome 6:fdce4464d92b 561 * ...
WiredHome 6:fdce4464d92b 562 * svr->GetWifly()->getWiflyVerString()
WiredHome 6:fdce4464d92b 563 * @endcode
WiredHome 6:fdce4464d92b 564 *
WiredHome 6:fdce4464d92b 565 * returns the wifly option.
WiredHome 5:c9b27e718054 566 */
WiredHome 12:109bf1558300 567 Wifly * GetWifly() {
WiredHome 12:109bf1558300 568 return wifly;
WiredHome 12:109bf1558300 569 };
WiredHome 0:729320f63c5c 570
WiredHome 0:729320f63c5c 571 private:
WiredHome 0:729320f63c5c 572 Wifly * wifly;
WiredHome 0:729320f63c5c 573 char * webroot;
WiredHome 0:729320f63c5c 574 PC * pc;
WiredHome 0:729320f63c5c 575 TCPSocketServer * server;
WiredHome 0:729320f63c5c 576 TCPSocketConnection client;
WiredHome 0:729320f63c5c 577 char * rewriteWithDefaultFile(char * queryString);
WiredHome 0:729320f63c5c 578 char * rewritePrependWebroot(char * queryString);
WiredHome 13:8975d7928678 579
WiredHome 13:8975d7928678 580 namevalue *queryParams; // Query Parameters from the URL this=that&sky=blue&...
WiredHome 13:8975d7928678 581 int maxqueryParams;
WiredHome 13:8975d7928678 582 int queryParamCount;
WiredHome 13:8975d7928678 583
WiredHome 13:8975d7928678 584 namevalue *headerParams; // Header params Host: 192.168...\r\nConnection: keep-alive\r\n...
WiredHome 13:8975d7928678 585 int maxheaderParams;
WiredHome 13:8975d7928678 586 int headerParamCount;
WiredHome 13:8975d7928678 587
WiredHome 3:17928786bdb5 588 int maxheaderbytes;
WiredHome 3:17928786bdb5 589 char * headerbuffer;
WiredHome 3:17928786bdb5 590 int headerbuffersize;
WiredHome 12:109bf1558300 591
WiredHome 10:9c8d2c6a3469 592 Timer PerformanceTimer;
WiredHome 3:17928786bdb5 593 /**
WiredHome 3:17928786bdb5 594 * Records performance data
WiredHome 12:109bf1558300 595 *
WiredHome 3:17928786bdb5 596 * This will take a pointer to a SW_PerformanceParam, and it will
WiredHome 3:17928786bdb5 597 * take the time when the performance measurement started. It locally
WiredHome 3:17928786bdb5 598 * accesses the current time to measure the elapsed.
WiredHome 3:17928786bdb5 599 *
WiredHome 3:17928786bdb5 600 * @param param is the performance parameter to update
WiredHome 3:17928786bdb5 601 * @param value is the reference time.
WiredHome 3:17928786bdb5 602 * @returns the current time which may be used as the reference time
WiredHome 3:17928786bdb5 603 * for further measurements.
WiredHome 3:17928786bdb5 604 */
WiredHome 16:6ebacf2946d8 605 unsigned int RecordPerformanceData(SW_PerformanceParam * param, unsigned int value);
WiredHome 3:17928786bdb5 606 SW_PerformanceData perfData;
WiredHome 12:109bf1558300 607
WiredHome 3:17928786bdb5 608 typedef struct HANDLER {
WiredHome 0:729320f63c5c 609 char * path;
WiredHome 0:729320f63c5c 610 Handler callback;
WiredHome 0:729320f63c5c 611 } handler;
WiredHome 0:729320f63c5c 612 int maxdynamicpages;
WiredHome 0:729320f63c5c 613 handler *handlers;
WiredHome 0:729320f63c5c 614 int handlercount;
WiredHome 3:17928786bdb5 615
WiredHome 3:17928786bdb5 616 char * queryType;
WiredHome 3:17928786bdb5 617 char * queryString;
WiredHome 3:17928786bdb5 618 char * postQueryString;
WiredHome 12:109bf1558300 619
WiredHome 0:729320f63c5c 620 /**
WiredHome 8:262583f054f6 621 * Extract the parameter from the record, by searching for the needle in the haystack.
WiredHome 8:262583f054f6 622 *
WiredHome 8:262583f054f6 623 * The parameter of interest follows the needle, and may be ' ' delimited
WiredHome 0:729320f63c5c 624 * Can damage haystack while processing it.
WiredHome 0:729320f63c5c 625 *
WiredHome 0:729320f63c5c 626 * @param haystack is the record to search
WiredHome 0:729320f63c5c 627 * @param needle is the text to search for, which precedes the text to return
WiredHome 0:729320f63c5c 628 * @param string is the text following the needle
WiredHome 0:729320f63c5c 629 * @return true if it extracted something successfully
WiredHome 0:729320f63c5c 630 */
WiredHome 0:729320f63c5c 631 bool Extract(char * rec, char * needle, char ** string);
WiredHome 0:729320f63c5c 632
WiredHome 3:17928786bdb5 633 void SendResponse();
WiredHome 29:00116fc9da74 634 HTTPServer::CallBackResults ParseHeader(char * bPtr);
WiredHome 3:17928786bdb5 635 bool CheckDynamicHandlers();
WiredHome 3:17928786bdb5 636
WiredHome 0:729320f63c5c 637 int HexCharToInt(char c);
WiredHome 0:729320f63c5c 638 char HexPairToChar(char * p);
WiredHome 29:00116fc9da74 639
WiredHome 29:00116fc9da74 640 #ifdef DEBUG
WiredHome 29:00116fc9da74 641 void * MyMalloc(int x, int y);
WiredHome 29:00116fc9da74 642 char toP(void * x);
WiredHome 29:00116fc9da74 643 #endif
WiredHome 0:729320f63c5c 644 };
WiredHome 0:729320f63c5c 645 #endif //SW_HTTPSERVER_H
WiredHome 4:f34642902056 646
WiredHome 7:99ad7a67f05e 647