A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPStaticPage.h Source File

HTTPStaticPage.h

00001 #ifndef HTTPSTATICPAGE_H
00002 #define HTTPSTATICPAGE_H
00003 
00004 #include "HTTPServer.h"
00005 
00006 /**
00007  * A datastorage helper for the HTTPStaticPage class.
00008  * Stores only the left upload size.
00009  */
00010 class HTTPStaticPageData : public HTTPData {
00011   public: int left;
00012 };
00013 
00014 /**
00015  * This class Provide a Handler to send Static HTTP Pages from the bin file.
00016  */
00017 class HTTPStaticPage : public HTTPHandler {
00018   public:
00019     /**
00020      * Constructer takes the pagename and the pagedata.
00021      * As long as the pagedata is NULL terminated you have not to tell the data length.
00022      * But if you want to send binary data you should tell us the size.
00023      */
00024     HTTPStaticPage(const char *path, const char *page, int length = 0)
00025      : HTTPHandler(path), _page(page) {
00026       _size = (length)?length:strlen(_page);
00027     }
00028 
00029     HTTPStaticPage(HTTPServer *server, const char *path, const char *page, int length = 0)
00030      : HTTPHandler(path), _page(page) {
00031       _size = (length)?length:strlen(_page);
00032       server->addHandler(this);
00033     }
00034 
00035   private:
00036     /**
00037      * A this Static Page is requested!
00038      * Prepare a datastorage helper "HTTPStaticPageHelper" to store the left data size.
00039      * And return HTTP_OK
00040      */
00041     virtual HTTPStatus init(HTTPConnection *con) const {
00042       HTTPStaticPageData *data = new HTTPStaticPageData();
00043       con->data = data;
00044       data->left = _size;
00045       con->setLength(_size);
00046       return HTTP_OK;
00047     }
00048 
00049     /**
00050      * Send the maximum data out to the client. 
00051      * If the file is complete transmitted close connection by returning HTTP_SuccessEnded
00052      */
00053     virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
00054       HTTPStaticPageData *data = static_cast<HTTPStaticPageData *>(con->data);
00055       int len = min(maximum, data->left);
00056       err_t err;
00057 
00058       do {
00059         err = con->write((void*)&_page[_size - data->left], len, 1);
00060         if(err == ERR_MEM) {
00061           len >>= 1;
00062         }
00063       } while(err == ERR_MEM && len > 1);
00064       if(err == ERR_OK) {
00065         data->left -= len;
00066       }
00067 
00068       return (data->left)? HTTP_Success : HTTP_SuccessEnded;
00069     }
00070 
00071     /** Pointer to the page data */
00072     const char *_page;
00073     
00074     /** page data size*/
00075     int   _size;
00076 };
00077 
00078 #endif