this will take a image from C328 serial camera and store those images in mbed flash as html and this html page is uploaded into the server at ip:192.168.1.2

Dependencies:   mbed

Committer:
mitesh2patel
Date:
Wed Dec 15 15:01:56 2010 +0000
Revision:
0:e1a0471e5ffb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitesh2patel 0:e1a0471e5ffb 1 #ifndef HTTPDYNAMICPAGE_H
mitesh2patel 0:e1a0471e5ffb 2 #define HTTPDYNAMICPAGE_H
mitesh2patel 0:e1a0471e5ffb 3
mitesh2patel 0:e1a0471e5ffb 4 #include "HTTPServer.h"
mitesh2patel 0:e1a0471e5ffb 5
mitesh2patel 0:e1a0471e5ffb 6 /**
mitesh2patel 0:e1a0471e5ffb 7 * A datastorage helper for the HTTPDynamicPage class.
mitesh2patel 0:e1a0471e5ffb 8 * Stores dynamically created page data and the size left to upload.
mitesh2patel 0:e1a0471e5ffb 9 */
mitesh2patel 0:e1a0471e5ffb 10 class HTTPDynamicPageData : public HTTPData {
mitesh2patel 0:e1a0471e5ffb 11 public:
mitesh2patel 0:e1a0471e5ffb 12 HTTPDynamicPageData()
mitesh2patel 0:e1a0471e5ffb 13 : page_free(NULL), page(NULL), size(0), _left(0) {}
mitesh2patel 0:e1a0471e5ffb 14 virtual ~HTTPDynamicPageData() {
mitesh2patel 0:e1a0471e5ffb 15 if (page_free && page) {
mitesh2patel 0:e1a0471e5ffb 16 page_free(page);
mitesh2patel 0:e1a0471e5ffb 17 }
mitesh2patel 0:e1a0471e5ffb 18 }
mitesh2patel 0:e1a0471e5ffb 19
mitesh2patel 0:e1a0471e5ffb 20 /** Pointer to garbage collection function (for freeing page data)
mitesh2patel 0:e1a0471e5ffb 21 * Should be set by callback function to a suitable free() if page is dynamically allocated
mitesh2patel 0:e1a0471e5ffb 22 */
mitesh2patel 0:e1a0471e5ffb 23 void (*page_free)(void *page);
mitesh2patel 0:e1a0471e5ffb 24
mitesh2patel 0:e1a0471e5ffb 25 /** Pointer to the page data */
mitesh2patel 0:e1a0471e5ffb 26 char *page;
mitesh2patel 0:e1a0471e5ffb 27
mitesh2patel 0:e1a0471e5ffb 28 /** Page data size
mitesh2patel 0:e1a0471e5ffb 29 * As long as the page is NULL terminated you don't have to tell the data length.
mitesh2patel 0:e1a0471e5ffb 30 * But if you want to send binary data you should tell us the size.
mitesh2patel 0:e1a0471e5ffb 31 */
mitesh2patel 0:e1a0471e5ffb 32 int size;
mitesh2patel 0:e1a0471e5ffb 33
mitesh2patel 0:e1a0471e5ffb 34 /** Page data size left for the upload */
mitesh2patel 0:e1a0471e5ffb 35 int _left;
mitesh2patel 0:e1a0471e5ffb 36 };
mitesh2patel 0:e1a0471e5ffb 37
mitesh2patel 0:e1a0471e5ffb 38 /**
mitesh2patel 0:e1a0471e5ffb 39 * This class Provide a Handler to send Static HTTP Pages from the bin file.
mitesh2patel 0:e1a0471e5ffb 40 */
mitesh2patel 0:e1a0471e5ffb 41 class HTTPDynamicPage : public HTTPHandler {
mitesh2patel 0:e1a0471e5ffb 42 public:
mitesh2patel 0:e1a0471e5ffb 43 /**
mitesh2patel 0:e1a0471e5ffb 44 * Constructor takes the page path and the page callback function.
mitesh2patel 0:e1a0471e5ffb 45 */
mitesh2patel 0:e1a0471e5ffb 46 HTTPDynamicPage(const char *path, HTTPStatus page_fnc(HTTPConnection *con, HTTPDynamicPageData *pd))
mitesh2patel 0:e1a0471e5ffb 47 : HTTPHandler(path), _page_fnc(page_fnc) {
mitesh2patel 0:e1a0471e5ffb 48 }
mitesh2patel 0:e1a0471e5ffb 49
mitesh2patel 0:e1a0471e5ffb 50 HTTPDynamicPage(HTTPServer *server, const char *path, HTTPStatus page_fnc(HTTPConnection *con, HTTPDynamicPageData *pd))
mitesh2patel 0:e1a0471e5ffb 51 : HTTPHandler(path), _page_fnc(page_fnc) {
mitesh2patel 0:e1a0471e5ffb 52 server->addHandler(this);
mitesh2patel 0:e1a0471e5ffb 53 }
mitesh2patel 0:e1a0471e5ffb 54
mitesh2patel 0:e1a0471e5ffb 55 private:
mitesh2patel 0:e1a0471e5ffb 56 /**
mitesh2patel 0:e1a0471e5ffb 57 * This Dynamic Page is requested!
mitesh2patel 0:e1a0471e5ffb 58 * Prepare a datastorage helper "HTTPDynamicPageData", create the page and store in helper, then initialize remaining data size.
mitesh2patel 0:e1a0471e5ffb 59 * And return HTTP_OK if all ok.
mitesh2patel 0:e1a0471e5ffb 60 */
mitesh2patel 0:e1a0471e5ffb 61 virtual HTTPStatus init(HTTPConnection *con) const {
mitesh2patel 0:e1a0471e5ffb 62 HTTPDynamicPageData *data = new HTTPDynamicPageData();
mitesh2patel 0:e1a0471e5ffb 63 con->data = data;
mitesh2patel 0:e1a0471e5ffb 64 HTTPStatus status = _page_fnc(con, data); // assigns data->page, data->size and optionally data->page_free
mitesh2patel 0:e1a0471e5ffb 65 if (!data->size && data->page)
mitesh2patel 0:e1a0471e5ffb 66 data->size = strlen(data->page);
mitesh2patel 0:e1a0471e5ffb 67 data->_left = data->size;
mitesh2patel 0:e1a0471e5ffb 68 con->setLength(data->size);
mitesh2patel 0:e1a0471e5ffb 69 return status;
mitesh2patel 0:e1a0471e5ffb 70 }
mitesh2patel 0:e1a0471e5ffb 71
mitesh2patel 0:e1a0471e5ffb 72 /**
mitesh2patel 0:e1a0471e5ffb 73 * Send the maximum data out to the client.
mitesh2patel 0:e1a0471e5ffb 74 * If the file is complete transmitted close connection by returning HTTP_SuccessEnded
mitesh2patel 0:e1a0471e5ffb 75 */
mitesh2patel 0:e1a0471e5ffb 76 virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
mitesh2patel 0:e1a0471e5ffb 77 HTTPDynamicPageData *data = static_cast<HTTPDynamicPageData *>(con->data);
mitesh2patel 0:e1a0471e5ffb 78 int len = min(maximum, data->_left);
mitesh2patel 0:e1a0471e5ffb 79 err_t err;
mitesh2patel 0:e1a0471e5ffb 80
mitesh2patel 0:e1a0471e5ffb 81 do {
mitesh2patel 0:e1a0471e5ffb 82 err = con->write((void*)&data->page[data->size - data->_left], len, 1);
mitesh2patel 0:e1a0471e5ffb 83 if (err == ERR_MEM) {
mitesh2patel 0:e1a0471e5ffb 84 len >>= 1;
mitesh2patel 0:e1a0471e5ffb 85 }
mitesh2patel 0:e1a0471e5ffb 86 } while(err == ERR_MEM && len > 1);
mitesh2patel 0:e1a0471e5ffb 87 if (err == ERR_OK) {
mitesh2patel 0:e1a0471e5ffb 88 data->_left -= len;
mitesh2patel 0:e1a0471e5ffb 89 }
mitesh2patel 0:e1a0471e5ffb 90 return (data->_left) ? HTTP_Success : HTTP_SuccessEnded;
mitesh2patel 0:e1a0471e5ffb 91 }
mitesh2patel 0:e1a0471e5ffb 92
mitesh2patel 0:e1a0471e5ffb 93 HTTPStatus (*_page_fnc)(HTTPConnection *con, HTTPDynamicPageData *pd);
mitesh2patel 0:e1a0471e5ffb 94 };
mitesh2patel 0:e1a0471e5ffb 95
mitesh2patel 0:e1a0471e5ffb 96 #endif