Example of HTTPServer with additional features: * SNTPClient, DST rules * Link status indication * Local or SDCard-based WebServer * RPC-able class * Static and Dynamic HTML page

Dependencies:   mbed

Committer:
iva2k
Date:
Tue Jan 12 07:41:55 2010 +0000
Revision:
2:360fda42fefd
Parent:
0:886e4b3119ad

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iva2k 0:886e4b3119ad 1 #ifndef HTTPSTATICPAGE_H
iva2k 0:886e4b3119ad 2 #define HTTPSTATICPAGE_H
iva2k 0:886e4b3119ad 3
iva2k 0:886e4b3119ad 4 #include "HTTPServer.h"
iva2k 0:886e4b3119ad 5
iva2k 0:886e4b3119ad 6 /**
iva2k 0:886e4b3119ad 7 * A datastorage helper for the HTTPStaticPage class.
iva2k 0:886e4b3119ad 8 * Stores only the left upload size.
iva2k 0:886e4b3119ad 9 */
iva2k 0:886e4b3119ad 10 class HTTPStaticPageData : public HTTPData {
iva2k 0:886e4b3119ad 11 public: int left;
iva2k 0:886e4b3119ad 12 };
iva2k 0:886e4b3119ad 13
iva2k 0:886e4b3119ad 14 /**
iva2k 0:886e4b3119ad 15 * This class Provide a Handler to send Static HTTP Pages from the bin file.
iva2k 0:886e4b3119ad 16 */
iva2k 0:886e4b3119ad 17 class HTTPStaticPage : public HTTPHandler {
iva2k 0:886e4b3119ad 18 public:
iva2k 0:886e4b3119ad 19 /**
iva2k 0:886e4b3119ad 20 * Constructer takes the pagename and the pagedata.
iva2k 0:886e4b3119ad 21 * As long as the pagedata is NULL terminated you have not to tell the data length.
iva2k 0:886e4b3119ad 22 * But if you want to send binary data you should tell us the size.
iva2k 0:886e4b3119ad 23 */
iva2k 0:886e4b3119ad 24 HTTPStaticPage(const char *path, const char *page, int length = 0)
iva2k 0:886e4b3119ad 25 : HTTPHandler(path), _page(page) {
iva2k 0:886e4b3119ad 26 _size = (length)?length:strlen(_page);
iva2k 0:886e4b3119ad 27 }
iva2k 0:886e4b3119ad 28
iva2k 0:886e4b3119ad 29 HTTPStaticPage(HTTPServer *server, const char *path, const char *page, int length = 0)
iva2k 0:886e4b3119ad 30 : HTTPHandler(path), _page(page) {
iva2k 0:886e4b3119ad 31 _size = (length)?length:strlen(_page);
iva2k 0:886e4b3119ad 32 server->addHandler(this);
iva2k 0:886e4b3119ad 33 }
iva2k 0:886e4b3119ad 34
iva2k 0:886e4b3119ad 35 private:
iva2k 0:886e4b3119ad 36 /**
iva2k 0:886e4b3119ad 37 * A this Static Page is requested!
iva2k 0:886e4b3119ad 38 * Prepare a datastorage helper "HTTPStaticPageHelper" to store the left data size.
iva2k 0:886e4b3119ad 39 * And return HTTP_OK
iva2k 0:886e4b3119ad 40 */
iva2k 0:886e4b3119ad 41 virtual HTTPStatus init(HTTPConnection *con) const {
iva2k 0:886e4b3119ad 42 HTTPStaticPageData *data = new HTTPStaticPageData();
iva2k 0:886e4b3119ad 43 con->data = data;
iva2k 0:886e4b3119ad 44 data->left = _size;
iva2k 0:886e4b3119ad 45 con->setLength(_size);
iva2k 0:886e4b3119ad 46 return HTTP_OK;
iva2k 0:886e4b3119ad 47 }
iva2k 0:886e4b3119ad 48
iva2k 0:886e4b3119ad 49 /**
iva2k 0:886e4b3119ad 50 * Send the maximum data out to the client.
iva2k 0:886e4b3119ad 51 * If the file is complete transmitted close connection by returning HTTP_SuccessEnded
iva2k 0:886e4b3119ad 52 */
iva2k 0:886e4b3119ad 53 virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
iva2k 0:886e4b3119ad 54 HTTPStaticPageData *data = static_cast<HTTPStaticPageData *>(con->data);
iva2k 0:886e4b3119ad 55 int len = min(maximum, data->left);
iva2k 0:886e4b3119ad 56 err_t err;
iva2k 0:886e4b3119ad 57
iva2k 0:886e4b3119ad 58 do {
iva2k 0:886e4b3119ad 59 err = con->write((void*)&_page[_size - data->left], len, 1);
iva2k 0:886e4b3119ad 60 if(err == ERR_MEM) {
iva2k 0:886e4b3119ad 61 len >>= 1;
iva2k 0:886e4b3119ad 62 }
iva2k 0:886e4b3119ad 63 } while(err == ERR_MEM && len > 1);
iva2k 0:886e4b3119ad 64 if(err == ERR_OK) {
iva2k 0:886e4b3119ad 65 data->left -= len;
iva2k 0:886e4b3119ad 66 }
iva2k 0:886e4b3119ad 67
iva2k 0:886e4b3119ad 68 return (data->left)? HTTP_Success : HTTP_SuccessEnded;
iva2k 0:886e4b3119ad 69 }
iva2k 0:886e4b3119ad 70
iva2k 0:886e4b3119ad 71 /** Pointer to the page data */
iva2k 0:886e4b3119ad 72 const char *_page;
iva2k 0:886e4b3119ad 73
iva2k 0:886e4b3119ad 74 /** page data size*/
iva2k 0:886e4b3119ad 75 int _size;
iva2k 0:886e4b3119ad 76 };
iva2k 0:886e4b3119ad 77
iva2k 0:886e4b3119ad 78 #endif