Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HTTPDynamicPage.h
00001 #ifndef HTTPDYNAMICPAGE_H 00002 #define HTTPDYNAMICPAGE_H 00003 00004 #include "HTTPServer.h" 00005 00006 /** 00007 * A datastorage helper for the HTTPDynamicPage class. 00008 * Stores dynamically created page data and the size left to upload. 00009 */ 00010 class HTTPDynamicPageData : public HTTPData { 00011 public: 00012 HTTPDynamicPageData() 00013 : page_free(NULL), page(NULL), size(0), _left(0) {} 00014 virtual ~HTTPDynamicPageData() { 00015 if (page_free && page) { 00016 page_free(page); 00017 } 00018 } 00019 00020 /** Pointer to garbage collection function (for freeing page data) 00021 * Should be set by callback function to a suitable free() if page is dynamically allocated 00022 */ 00023 void (*page_free)(void *page); 00024 00025 /** Pointer to the page data */ 00026 char *page; 00027 00028 /** Page data size 00029 * As long as the page is NULL terminated you don't have to tell the data length. 00030 * But if you want to send binary data you should tell us the size. 00031 */ 00032 int size; 00033 00034 /** Page data size left for the upload */ 00035 int _left; 00036 }; 00037 00038 /** 00039 * This class Provide a Handler to send Static HTTP Pages from the bin file. 00040 */ 00041 class HTTPDynamicPage : public HTTPHandler { 00042 public: 00043 /** 00044 * Constructor takes the page path and the page callback function. 00045 */ 00046 HTTPDynamicPage(const char *path, HTTPStatus page_fnc(HTTPConnection *con, HTTPDynamicPageData *pd)) 00047 : HTTPHandler(path), _page_fnc(page_fnc) { 00048 } 00049 00050 HTTPDynamicPage(HTTPServer *server, const char *path, HTTPStatus page_fnc(HTTPConnection *con, HTTPDynamicPageData *pd)) 00051 : HTTPHandler(path), _page_fnc(page_fnc) { 00052 server->addHandler(this); 00053 } 00054 00055 private: 00056 /** 00057 * This Dynamic Page is requested! 00058 * Prepare a datastorage helper "HTTPDynamicPageData", create the page and store in helper, then initialize remaining data size. 00059 * And return HTTP_OK if all ok. 00060 */ 00061 virtual HTTPStatus init(HTTPConnection *con) const { 00062 HTTPDynamicPageData *data = new HTTPDynamicPageData(); 00063 con->data = data; 00064 HTTPStatus status = _page_fnc(con, data); // assigns data->page, data->size and optionally data->page_free 00065 if (!data->size && data->page) 00066 data->size = strlen(data->page); 00067 data->_left = data->size; 00068 con->setLength(data->size); 00069 return status; 00070 } 00071 00072 /** 00073 * Send the maximum data out to the client. 00074 * If the file is complete transmitted close connection by returning HTTP_SuccessEnded 00075 */ 00076 virtual HTTPHandle send(HTTPConnection *con, int maximum) const { 00077 HTTPDynamicPageData *data = static_cast<HTTPDynamicPageData *>(con->data); 00078 int len = min(maximum, data->_left); 00079 err_t err; 00080 00081 do { 00082 err = con->write((void*)&data->page[data->size - data->_left], len, 1); 00083 if (err == ERR_MEM) { 00084 len >>= 1; 00085 } 00086 } while(err == ERR_MEM && len > 1); 00087 if (err == ERR_OK) { 00088 data->_left -= len; 00089 } 00090 return (data->_left) ? HTTP_Success : HTTP_SuccessEnded; 00091 } 00092 00093 HTTPStatus (*_page_fnc)(HTTPConnection *con, HTTPDynamicPageData *pd); 00094 }; 00095 00096 #endif
Generated on Tue Jul 12 2022 20:39:37 by
1.7.2