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