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
lwip/HTTPServer/HTTPFS.h@0:e1a0471e5ffb, 2010-12-15 (annotated)
- Committer:
- mitesh2patel
- Date:
- Wed Dec 15 15:01:56 2010 +0000
- Revision:
- 0:e1a0471e5ffb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mitesh2patel | 0:e1a0471e5ffb | 1 | #ifndef HTTPFILESYSTEM_H |
mitesh2patel | 0:e1a0471e5ffb | 2 | #define HTTPFILESYSTEM_H |
mitesh2patel | 0:e1a0471e5ffb | 3 | |
mitesh2patel | 0:e1a0471e5ffb | 4 | #include "mbed.h" |
mitesh2patel | 0:e1a0471e5ffb | 5 | |
mitesh2patel | 0:e1a0471e5ffb | 6 | #include "HTTPServer.h" |
mitesh2patel | 0:e1a0471e5ffb | 7 | |
mitesh2patel | 0:e1a0471e5ffb | 8 | #define HTTP_BUFFER_SIZE 700 |
mitesh2patel | 0:e1a0471e5ffb | 9 | #define FILENAMELANGTH 100 |
mitesh2patel | 0:e1a0471e5ffb | 10 | |
mitesh2patel | 0:e1a0471e5ffb | 11 | /** |
mitesh2patel | 0:e1a0471e5ffb | 12 | * This class will store the data which are required for an request. |
mitesh2patel | 0:e1a0471e5ffb | 13 | * We are not in every case able to return all data at once, that means we have to store |
mitesh2patel | 0:e1a0471e5ffb | 14 | * the actual level of transmission. |
mitesh2patel | 0:e1a0471e5ffb | 15 | */ |
mitesh2patel | 0:e1a0471e5ffb | 16 | class HTTPFileSystemData : public HTTPData { |
mitesh2patel | 0:e1a0471e5ffb | 17 | public: |
mitesh2patel | 0:e1a0471e5ffb | 18 | int fleft; |
mitesh2patel | 0:e1a0471e5ffb | 19 | int bleft; |
mitesh2patel | 0:e1a0471e5ffb | 20 | int offset; |
mitesh2patel | 0:e1a0471e5ffb | 21 | FILE *file; |
mitesh2patel | 0:e1a0471e5ffb | 22 | char buffer[HTTP_BUFFER_SIZE]; |
mitesh2patel | 0:e1a0471e5ffb | 23 | |
mitesh2patel | 0:e1a0471e5ffb | 24 | virtual ~HTTPFileSystemData() { |
mitesh2patel | 0:e1a0471e5ffb | 25 | if(file) { |
mitesh2patel | 0:e1a0471e5ffb | 26 | fclose(file); |
mitesh2patel | 0:e1a0471e5ffb | 27 | file = 0; |
mitesh2patel | 0:e1a0471e5ffb | 28 | } |
mitesh2patel | 0:e1a0471e5ffb | 29 | } |
mitesh2patel | 0:e1a0471e5ffb | 30 | }; |
mitesh2patel | 0:e1a0471e5ffb | 31 | |
mitesh2patel | 0:e1a0471e5ffb | 32 | /** |
mitesh2patel | 0:e1a0471e5ffb | 33 | * This class will deliver files form the virtual file system. |
mitesh2patel | 0:e1a0471e5ffb | 34 | * Furthermore it is a simple example how to implement an HTTPHandler for big data requests. |
mitesh2patel | 0:e1a0471e5ffb | 35 | */ |
mitesh2patel | 0:e1a0471e5ffb | 36 | class HTTPFileSystemHandler : public HTTPHandler { |
mitesh2patel | 0:e1a0471e5ffb | 37 | public: |
mitesh2patel | 0:e1a0471e5ffb | 38 | /** |
mitesh2patel | 0:e1a0471e5ffb | 39 | * Create a new HTTPFileSzstemHandler. |
mitesh2patel | 0:e1a0471e5ffb | 40 | * @param prefix The Prefix is the URL Proefix in witch the Handler will work. |
mitesh2patel | 0:e1a0471e5ffb | 41 | * @param dir The Prefix will be directly mappt on the dir. |
mitesh2patel | 0:e1a0471e5ffb | 42 | */ |
mitesh2patel | 0:e1a0471e5ffb | 43 | HTTPFileSystemHandler(const char *path, const char *dir) : HTTPHandler(path), _dir(dir) {} |
mitesh2patel | 0:e1a0471e5ffb | 44 | HTTPFileSystemHandler(HTTPServer *server, const char *path, const char *dir) : HTTPHandler(path), _dir(dir) { server->addHandler(this); } |
mitesh2patel | 0:e1a0471e5ffb | 45 | |
mitesh2patel | 0:e1a0471e5ffb | 46 | private: |
mitesh2patel | 0:e1a0471e5ffb | 47 | /** |
mitesh2patel | 0:e1a0471e5ffb | 48 | * Check if a requested file exists. |
mitesh2patel | 0:e1a0471e5ffb | 49 | * If it exists open it and store the data back in the HTTPConnection. |
mitesh2patel | 0:e1a0471e5ffb | 50 | * We would not store connection specific data into the Handler. |
mitesh2patel | 0:e1a0471e5ffb | 51 | * If the file exists and we cann serve a page return HTTP_OK else HTTP_NotFound. |
mitesh2patel | 0:e1a0471e5ffb | 52 | * @param con The Connection which will be handled. |
mitesh2patel | 0:e1a0471e5ffb | 53 | */ |
mitesh2patel | 0:e1a0471e5ffb | 54 | virtual HTTPStatus init(HTTPConnection *con) const { |
mitesh2patel | 0:e1a0471e5ffb | 55 | char filename[FILENAMELANGTH]; |
mitesh2patel | 0:e1a0471e5ffb | 56 | HTTPFileSystemData *data = new HTTPFileSystemData(); |
mitesh2patel | 0:e1a0471e5ffb | 57 | snprintf(filename, FILENAMELANGTH, "%s%s\0", _dir, con->getURL() + strlen(_prefix)); |
mitesh2patel | 0:e1a0471e5ffb | 58 | data->file = fopen(filename, "r"); |
mitesh2patel | 0:e1a0471e5ffb | 59 | if(!data->file) { |
mitesh2patel | 0:e1a0471e5ffb | 60 | delete data; |
mitesh2patel | 0:e1a0471e5ffb | 61 | return HTTP_NotFound; |
mitesh2patel | 0:e1a0471e5ffb | 62 | } |
mitesh2patel | 0:e1a0471e5ffb | 63 | data->fleft = fleft(data->file); |
mitesh2patel | 0:e1a0471e5ffb | 64 | data->bleft = 0; |
mitesh2patel | 0:e1a0471e5ffb | 65 | data->offset = 0; |
mitesh2patel | 0:e1a0471e5ffb | 66 | |
mitesh2patel | 0:e1a0471e5ffb | 67 | con->data = data; |
mitesh2patel | 0:e1a0471e5ffb | 68 | con->setLength(data->fleft); |
mitesh2patel | 0:e1a0471e5ffb | 69 | loadFromFile(con); |
mitesh2patel | 0:e1a0471e5ffb | 70 | return HTTP_OK; |
mitesh2patel | 0:e1a0471e5ffb | 71 | } |
mitesh2patel | 0:e1a0471e5ffb | 72 | |
mitesh2patel | 0:e1a0471e5ffb | 73 | /** |
mitesh2patel | 0:e1a0471e5ffb | 74 | * Send the maximum available data chunk to the Client. |
mitesh2patel | 0:e1a0471e5ffb | 75 | * If it is the last chunk close connection by returning HTTP_SuccessEnded |
mitesh2patel | 0:e1a0471e5ffb | 76 | * @param con The connection to handle |
mitesh2patel | 0:e1a0471e5ffb | 77 | * @param maximum The maximal available sendbuffer size. |
mitesh2patel | 0:e1a0471e5ffb | 78 | * @return HTTP_Success when mor data is available or HTTP_SuccessEnded when the file is complete. |
mitesh2patel | 0:e1a0471e5ffb | 79 | */ |
mitesh2patel | 0:e1a0471e5ffb | 80 | virtual HTTPHandle send(HTTPConnection *con, int maximum) const { |
mitesh2patel | 0:e1a0471e5ffb | 81 | HTTPFileSystemData *data = static_cast<HTTPFileSystemData *>(con->data); |
mitesh2patel | 0:e1a0471e5ffb | 82 | err_t err; |
mitesh2patel | 0:e1a0471e5ffb | 83 | u16_t len = min(data->bleft, maximum); |
mitesh2patel | 0:e1a0471e5ffb | 84 | // printf("Send File\n"); |
mitesh2patel | 0:e1a0471e5ffb | 85 | if(len) { |
mitesh2patel | 0:e1a0471e5ffb | 86 | do { |
mitesh2patel | 0:e1a0471e5ffb | 87 | err = con->write( |
mitesh2patel | 0:e1a0471e5ffb | 88 | &data->buffer[data->offset], len, (((!data->fleft)&&(data->bleft==len))? |
mitesh2patel | 0:e1a0471e5ffb | 89 | (TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE) : (TCP_WRITE_FLAG_COPY))); |
mitesh2patel | 0:e1a0471e5ffb | 90 | if(err == ERR_MEM) { |
mitesh2patel | 0:e1a0471e5ffb | 91 | len /= 2; |
mitesh2patel | 0:e1a0471e5ffb | 92 | } |
mitesh2patel | 0:e1a0471e5ffb | 93 | } while (err == ERR_MEM && len > 1); |
mitesh2patel | 0:e1a0471e5ffb | 94 | |
mitesh2patel | 0:e1a0471e5ffb | 95 | if(err == ERR_OK) { |
mitesh2patel | 0:e1a0471e5ffb | 96 | data->offset += len; |
mitesh2patel | 0:e1a0471e5ffb | 97 | data->bleft -= len; |
mitesh2patel | 0:e1a0471e5ffb | 98 | } |
mitesh2patel | 0:e1a0471e5ffb | 99 | } |
mitesh2patel | 0:e1a0471e5ffb | 100 | return loadFromFile(con); |
mitesh2patel | 0:e1a0471e5ffb | 101 | } |
mitesh2patel | 0:e1a0471e5ffb | 102 | |
mitesh2patel | 0:e1a0471e5ffb | 103 | /** |
mitesh2patel | 0:e1a0471e5ffb | 104 | * Returns the left size of a file. |
mitesh2patel | 0:e1a0471e5ffb | 105 | * @param fd The filehandler of which we want to know the filesize. |
mitesh2patel | 0:e1a0471e5ffb | 106 | * @return The filesize of fd. |
mitesh2patel | 0:e1a0471e5ffb | 107 | */ |
mitesh2patel | 0:e1a0471e5ffb | 108 | long fleft(FILE *fd) const { |
mitesh2patel | 0:e1a0471e5ffb | 109 | long len, cur; |
mitesh2patel | 0:e1a0471e5ffb | 110 | cur = ftell(fd); |
mitesh2patel | 0:e1a0471e5ffb | 111 | fseek(fd, 0, SEEK_END); |
mitesh2patel | 0:e1a0471e5ffb | 112 | len = ftell(fd); |
mitesh2patel | 0:e1a0471e5ffb | 113 | fseek(fd, cur, SEEK_SET); |
mitesh2patel | 0:e1a0471e5ffb | 114 | return len; |
mitesh2patel | 0:e1a0471e5ffb | 115 | } |
mitesh2patel | 0:e1a0471e5ffb | 116 | |
mitesh2patel | 0:e1a0471e5ffb | 117 | /** |
mitesh2patel | 0:e1a0471e5ffb | 118 | * Fill the buffer if the buffer is empty. |
mitesh2patel | 0:e1a0471e5ffb | 119 | * If the file is complete close the filehandler and return HTTP_SuccessEnded. |
mitesh2patel | 0:e1a0471e5ffb | 120 | */ |
mitesh2patel | 0:e1a0471e5ffb | 121 | HTTPHandle loadFromFile(HTTPConnection *con) const { |
mitesh2patel | 0:e1a0471e5ffb | 122 | HTTPFileSystemData *data = static_cast<HTTPFileSystemData *>(con->data); |
mitesh2patel | 0:e1a0471e5ffb | 123 | if(!data->bleft) { |
mitesh2patel | 0:e1a0471e5ffb | 124 | if(data->fleft) { |
mitesh2patel | 0:e1a0471e5ffb | 125 | int len = fread(&data->buffer[0], sizeof(char), HTTP_BUFFER_SIZE, data->file); |
mitesh2patel | 0:e1a0471e5ffb | 126 | data->fleft -= len; |
mitesh2patel | 0:e1a0471e5ffb | 127 | data->bleft = len; |
mitesh2patel | 0:e1a0471e5ffb | 128 | data->offset = 0; |
mitesh2patel | 0:e1a0471e5ffb | 129 | } else { |
mitesh2patel | 0:e1a0471e5ffb | 130 | if(data->file) { |
mitesh2patel | 0:e1a0471e5ffb | 131 | fclose(data->file); |
mitesh2patel | 0:e1a0471e5ffb | 132 | data->file = 0; |
mitesh2patel | 0:e1a0471e5ffb | 133 | } |
mitesh2patel | 0:e1a0471e5ffb | 134 | return HTTP_SuccessEnded; |
mitesh2patel | 0:e1a0471e5ffb | 135 | } |
mitesh2patel | 0:e1a0471e5ffb | 136 | } |
mitesh2patel | 0:e1a0471e5ffb | 137 | return HTTP_Success; |
mitesh2patel | 0:e1a0471e5ffb | 138 | } |
mitesh2patel | 0:e1a0471e5ffb | 139 | |
mitesh2patel | 0:e1a0471e5ffb | 140 | /** The Directory which will replace the prefix of the URL */ |
mitesh2patel | 0:e1a0471e5ffb | 141 | const char *_dir; |
mitesh2patel | 0:e1a0471e5ffb | 142 | }; |
mitesh2patel | 0:e1a0471e5ffb | 143 | |
mitesh2patel | 0:e1a0471e5ffb | 144 | #endif |