LRSD stephane / Mbed 2 deprecated WEBserverv4

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPFS.h Source File

HTTPFS.h

00001 #ifndef HTTPFILESYSTEM_H
00002 #define HTTPFILESYSTEM_H
00003 
00004 #include "mbed.h"
00005 extern char *lieu;
00006 extern int par1;
00007 #include "HTTPServer.h"
00008 
00009 #define HTTP_BUFFER_SIZE 700
00010 #define FILENAMELANGTH 100
00011 
00012 /**
00013  * This class will store the data which are required for an request.
00014  * We are not in every case able to return all data at once, that means we have to store
00015  * the actual level of transmission.
00016  */
00017 class HTTPFileSystemData : public HTTPData {
00018   public:
00019     int fleft;
00020     int bleft;
00021     int offset;
00022     FILE *file;
00023     char buffer[HTTP_BUFFER_SIZE];
00024     
00025     virtual ~HTTPFileSystemData() {
00026       if(file) {
00027         fclose(file);
00028         file = 0;
00029       }
00030     }
00031 };
00032 
00033 /**
00034  * This class will deliver files form the virtual file system.
00035  * Furthermore it is a simple example how to implement an HTTPHandler for big data requests.
00036  */
00037 class HTTPFileSystemHandler : public HTTPHandler {
00038   public:
00039     /**
00040      * Create a new HTTPFileSzstemHandler.
00041      * @param prefix The Prefix is the URL Proefix in witch the Handler will work.
00042      * @param dir The Prefix will be directly mappt on the dir.
00043      */
00044     HTTPFileSystemHandler(const char *path, const char *dir) : HTTPHandler(path), _dir(dir) {}
00045     HTTPFileSystemHandler(HTTPServer *server, const char *path, const char *dir) : HTTPHandler(path), _dir(dir) { server->addHandler(this); }
00046 
00047   private:
00048     /**
00049      * Check if a requested file exists.
00050      * If it exists open it and store the data back in the HTTPConnection.
00051      * We would not store connection specific data into the Handler.
00052      * If the file exists and we can serve a page return HTTP_OK else HTTP_NotFound.
00053      */
00054     virtual HTTPStatus init(HTTPConnection *con) const {
00055       char filename[FILENAMELANGTH];
00056       HTTPFileSystemData *data = new HTTPFileSystemData();
00057       snprintf(filename, FILENAMELANGTH, "%s%s\0", _dir, con->getURL() + strlen(_prefix));
00058                printf("fichier:  %s \r\n",  filename );  // verif
00059       lieu = filename;
00060       char ou[3] = "00";
00061       if ( filename[15] == '?' )         // si il y a ? for metho get en HTML il y aura ?
00062             { lieu = "/sd/testbed.htm";  //on force une reponse avec testbed.htm
00063             ou[0] = filename[20];        // extraction parametre derriere ?varxx
00064             ou[1] = filename[21];
00065             ou[2] = 0;
00066             par1 = atoi(ou);  //ici  on peut en creer d'autres ou tester var 
00067             }
00068       data->file = fopen(lieu, "r");
00069       if(!data->file) {
00070         delete data;
00071         return HTTP_NotFound;
00072       }
00073       data->fleft  = fleft(data->file);
00074       data->bleft  = 0;
00075       data->offset = 0;
00076       
00077       con->data = data;
00078       con->setLength(data->fleft);
00079       loadFromFile(con);
00080       return HTTP_OK;
00081     }
00082 
00083     /**
00084      * Send the maximum available data chunk to the Client.
00085      * If it is the last chunk close connection by returning HTTP_SuccessEnded
00086      * @param con The connection to handle
00087      * @param maximum The maximal available sendbuffer size.
00088      * @return HTTP_Success when mor data is available or HTTP_SuccessEnded when the file is complete.
00089      */
00090     virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
00091       HTTPFileSystemData *data = static_cast<HTTPFileSystemData *>(con->data);
00092       err_t err;
00093       u16_t len = min(data->bleft, maximum);
00094 //      printf("Send File\n");
00095       if(len) {
00096         do {
00097           err = con->write(
00098             &data->buffer[data->offset], len, (((!data->fleft)&&(data->bleft==len))? 
00099             (TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE) : (TCP_WRITE_FLAG_COPY)));
00100           if(err == ERR_MEM) {
00101             len /= 2;
00102           }
00103         } while (err == ERR_MEM && len > 1);  
00104   
00105         if(err == ERR_OK) {
00106           data->offset += len;
00107           data->bleft  -= len;
00108         }
00109       }
00110       return loadFromFile(con);
00111     }
00112 
00113     /**
00114      * Returns the left size of a file.
00115      * @param fd The filehandler of which we want to know the filesize.
00116      * @return The filesize of fd.
00117      */
00118     long fleft(FILE *fd) const {
00119       long len, cur;
00120       cur = ftell(fd);
00121       fseek(fd, 0, SEEK_END);
00122       len = ftell(fd);
00123       fseek(fd, cur, SEEK_SET);
00124       return len;
00125     }
00126     
00127     /**
00128      * Fill the buffer if the buffer is empty.
00129      * If the file is complete close the filehandler and return HTTP_SuccessEnded.
00130      */
00131     HTTPHandle loadFromFile(HTTPConnection *con) const {
00132       HTTPFileSystemData *data = static_cast<HTTPFileSystemData *>(con->data);
00133       if(!data->bleft) {
00134         if(data->fleft) {
00135           int len = fread(&data->buffer[0], sizeof(char), HTTP_BUFFER_SIZE, data->file);
00136           data->fleft -= len;
00137           data->bleft  = len;
00138           data->offset = 0;
00139         } else {
00140           if(data->file) {
00141             fclose(data->file);
00142             data->file = 0;
00143           }
00144           return HTTP_SuccessEnded;
00145         }
00146       }
00147       return HTTP_Success;
00148     }
00149     
00150     /** The Directory which will replace the prefix of the URL */
00151     const char *_dir;
00152 };
00153 
00154 #endif