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