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