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