A simple web server that can be bound to either the EthernetInterface or the WiflyInterface.
Dependents: Smart-WiFly-WebServer WattEye X10Svr SSDP_Server
Diff: SW_HTTPServer.h
- Revision:
- 12:109bf1558300
- Parent:
- 10:9c8d2c6a3469
- Child:
- 13:8975d7928678
--- a/SW_HTTPServer.h Sun Aug 04 21:34:44 2013 +0000 +++ b/SW_HTTPServer.h Sun Aug 11 15:49:51 2013 +0000 @@ -19,20 +19,20 @@ #define FILESEND_BUF_SIZE 1460 -/// MAX_HEADER_SIZE is the default size to contain the largest header. +/// MAX_HEADER_SIZE is the default size to contain the largest header. /// This is the size of the URL and query string, and also all the /// other header information about the client. This can be -/// a couple of K, larger if you have big forms as it includes the +/// a couple of K, larger if you have big forms as it includes the /// form data that is submitted. #define MAX_HEADER_SIZE 1000 /// HTTPServer is a simple web server using the WiFly module. -/// +/// /// While simple, it is a capable, web server. The basic mode /// of operation is for it to serve static web pages from an available /// file system. -/// +/// /// The default page is index.htm (compile time defined) /// standard support to serve a number of standard file types; /// gif, jpg, jpeg, ico, png, zip, gz, tar, txt, pdf, htm, html @@ -104,8 +104,6 @@ /// @li 20130623 Make it non-blocking. "Poll" takes a variable amount /// of time, based on whether it is idle, or how much it /// has to do. -/// @li It now survives overnight and still responds, so the problem with -/// it going offline after long inactivity appears resolved. /// /// @note Copyright © 2013 by Smartware Computing, all rights reserved. /// Individuals may use this application for evaluation or non-commercial @@ -128,18 +126,18 @@ char * name; char * value; } namevalue; - + /** * Indicates the purpose of the Handler callback * - * Application code in a dynamic page uses this to determine the state + * Application code in a dynamic page uses this to determine the state * and therefore the needed operation to be performed. * * @code * bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount) { * char buf[100]; * bool ret = false; - * + * * switch (type) { * case HTTPServer::SEND_PAGE: * svr->header(200, "OK", "Content-Type: text/html\r\n"); @@ -176,26 +174,26 @@ SEND_PAGE, ///< the activated method should now send the page } CallBackType; - /** + /** * This is the prototype for custom handlers that are activated via a callback * * This callback gets overloaded for a few purposes, which can be identified by the \see CallBackType parameter - * @li SEND_PAGE - the callback should now send the html page, using as many svr->send() as needed. + * @li SEND_PAGE - the callback should now send the html page, using as many svr->send() as needed. * When the callback returns, it should always indicate true. * @li CONTENT_LENGTH_REQUEST - the server is asking the callback if it wants to receive the message, * which may require significant memory. If the request is accepted, true should be returned. * If the request is denied, false should be returned. - * + * * @param svr is a handle to this class, so the callback has access to member functions * @param params is a pointer to an array of name value pairs * @paramcount is the number of parameters. * @return true if command was accepted */ typedef bool (* Handler)(HTTPServer * svr, CallBackType type, const char *path, const namevalue *params, int paramcount); - + /** * Create the HTTPServer object. - * + * * @param wifly is the serial port with the wifly interface. * @param port is the optional parameter for the port number to use, default is 80. * @param webroot is a file system path to the root folder for the web space. @@ -203,17 +201,17 @@ * @param maxdynamicpages defines the maximum number of dynamic pages that can be registered. * @param pc is the serial port for debug information (I should transform this to a log interface) * @param allocforheader is the memory allocation to support the largest expected header from a client - * @param allocforfile is the memory allocation to support sending a file to the client. This is typically sized to fit + * @param allocforfile is the memory allocation to support sending a file to the client. This is typically sized to fit * an ethernet frame. */ - HTTPServer(Wifly * wifly, int port = 80, const char * webroot = "/", int maxparams = 30, int maxdynamicpages = 10, - PC * pc = NULL, int _allocforheader = MAX_HEADER_SIZE, int _allocforfile = FILESEND_BUF_SIZE); - + HTTPServer(Wifly * wifly, int port = 80, const char * webroot = "/", int maxparams = 30, int maxdynamicpages = 10, + PC * pc = NULL, int _allocforheader = MAX_HEADER_SIZE, int _allocforfile = FILESEND_BUF_SIZE); + /** * Destructor, which can clean up memory. */ ~HTTPServer(); - + /** * The process to call whenever there is free time, as this basically does * all the work to monitor for connections and handle replies. @@ -221,9 +219,9 @@ * 20130601 Renamed from ip_process to Poll */ void Poll(); - + /** - * Send typical header data, and some optional data back to the client. + * Send typical header data, and some optional data back to the client. * * This forms and sends the typical header back to the client. It may also send * optional data (which must end with "\r\n"). It then sends the second newline @@ -248,7 +246,7 @@ * @param bytes is the number of bytes to send. If not set, then strlen is calculated. */ void send(const char * msg, int bytes = -1); - + /** * Send a referenced file to the client, including the header * @@ -260,8 +258,8 @@ * @return true if it thinks it sent ok, false otherwise. */ bool SendFile(const char * filename, const char * filetype); - - /** + + /** * register a handler for a specific URL. * * This api lets you register a dynamic handler in the web server. This is @@ -269,7 +267,7 @@ * pages. * * @code - * + * * ... * svr.RegisterHandler("/dyn1", SimpleDynamicPage);svr.RegisterHandler("/dyn1", SimpleDynamicPage); * ... @@ -277,7 +275,7 @@ * bool SimpleDynamicPage(HTTPServer *svr, HTTPServer::CallBackType type, const char * path, const HTTPServer::namevalue *params, int paramcount) { * char buf[100]; * bool ret = false; - * + * * switch (type) { * case HTTPServer::SEND_PAGE: * svr->header(200, "OK", "Content-Type: text/html\r\n"); @@ -318,7 +316,7 @@ * @return true if successfully registered */ bool RegisterHandler(const char * path, Handler callback); - + /** * determine if the named file is a supported type (e.g. .htm, .jpg, ...) * @@ -331,7 +329,7 @@ * if (fType) { * ... * @endcode - * + * * @param filename is the filename to test, based on the extension * @return pointer to a Content-Type string if supported, or NULL if not. */ @@ -340,7 +338,7 @@ /** * search the available parameters for 'name' and if found, return the 'value' * - * After the querystring is parsed, the server maintains an array of + * After the querystring is parsed, the server maintains an array of * name=value pairs. This Get function will search for the passed in name * and provide access to the value. * @@ -356,26 +354,26 @@ const char * GetParameter(const char * name); /** - * Parse the text string into name=value parameters. + * Parse the text string into name=value parameters. * - * This will directly modify the referenced string. If there is a + * This will directly modify the referenced string. If there is a * #fragment_id on the end of the string, it will be removed. * * @param pString is a pointer to the string. */ void ParseParameters(char * pString); - + /** * Unescape string converts a coded string "in place" into a normal string * * A query string will have a number of characters replaced for communication * which includes spaces, quotes, question marks and more. Most of them - * will be replaced with a %xx format, where xx is the hex code for the + * will be replaced with a %xx format, where xx is the hex code for the * character. Since the string will only get shorter when this happens * the operation is performed in place. * * this "This%20is%20a%20question%3F%20and%20an%20answer." - * + * * becomes "This is a question? and an answer." * * @note '+' is another form of space, so is converted to a space before the %xx @@ -383,17 +381,17 @@ * @param encoded string to be converted */ void UnescapeString(char * encoded); - + /** * Get the IP address of the remote node to which we are connected. * * This will get the IP address of the remote node to which we are - * currently connected. This is written into the buffer in + * currently connected. This is written into the buffer in * "192.168.100.234" format. If the buffer size is note >= 16 bytes, * it will set the buffer to null. - * + * * @note This switches the module into, and out of, command mode - * which has quite a time penalty. + * which has quite a time penalty. * * @param str is the string to write the address into, which should be at * least as large as "192.168.100.203" (16-bytes). @@ -402,7 +400,7 @@ */ bool GetRemoteAddr(char * str, int strSize); - /** + /** * This is used to force a connection to close * * This switches the module into command mode, performs the close, @@ -412,19 +410,19 @@ * @returns true if successful */ bool close_connection(); - + /** - * Get the size of the largest header. + * Get the size of the largest header. * - * This is a diagnostic function, so you can resize the allocated - * buffer for your application. With proper sizing, more of the + * This is a diagnostic function, so you can resize the allocated + * buffer for your application. With proper sizing, more of the * system memory is available for your application. * * @code * sprintf(buf,"Max Header size: %d<br/>\r\n", svr->GetMaxHeaderSize()); * svr->send(buf); * @endcode - * + * * @returns size in bytes of the larger header measured. */ int GetMaxHeaderSize(); @@ -438,7 +436,7 @@ unsigned long Samples; unsigned long MaxTime_us; } SW_PerformanceParam; - + /** * Performance metrics */ @@ -447,7 +445,7 @@ SW_PerformanceParam SendData; //SW_PerformanceParam SendFile; } SW_PerformanceData; - + /** * Get performance metrics from the web server. * @@ -457,12 +455,12 @@ * @param p is a pointer to a SW_PerformanceData structure to be populated */ void GetPerformanceData(SW_PerformanceData * p); - + /** * Reset performance metrics. */ void ResetPerformanceData(); - + /** * Get the underlying wifly object. * @@ -477,7 +475,9 @@ * * returns the wifly option. */ - Wifly * GetWifly() { return wifly; }; + Wifly * GetWifly() { + return wifly; + }; private: Wifly * wifly; @@ -493,11 +493,11 @@ int maxheaderbytes; char * headerbuffer; int headerbuffersize; - + Timer PerformanceTimer; /** * Records performance data - * + * * This will take a pointer to a SW_PerformanceParam, and it will * take the time when the performance measurement started. It locally * accesses the current time to measure the elapsed. @@ -509,7 +509,7 @@ */ int RecordPerformanceData(SW_PerformanceParam * param, int value); SW_PerformanceData perfData; - + typedef struct HANDLER { char * path; Handler callback; @@ -523,8 +523,9 @@ char * hostString; char * contentLength; char * contentType; + char * authorization; char * postQueryString; - + /** * Extract the parameter from the record, by searching for the needle in the haystack. *