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