Wexus2 0.20
wexus2.src/wexus/FileHTTPHandler.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_FILEHTTPHANDLER_H__
00009 #define __INCLUDED_WEXUS_FILEHTTPHANDLER_H__
00010 
00011 #include <wexus/HTTPHandler.h>
00012 #include <wexus/HTTPServer.h>
00013 
00014 namespace wexus
00015 {
00016   class FileHTTPHandler;
00017 }
00018 
00019 /**
00020  * A HTTP handler that servces up statis files.
00021  *
00022  * @author Aleksander Demko
00023  */ 
00024 class wexus::FileHTTPHandler : public wexus::HTTPHandler
00025 {
00026   public:
00027     enum FileHTTPHandlerFlags {
00028       /// shows the index.html or index.htm file if given a directory
00029       /// takes precedance over FileHTTPHandlerFlags::AutoDirIndex
00030       IndexHtml = 0x1,
00031       /// auto generates an index if given a directory
00032       AutoDirIndex = 0x2,
00033       /// only handle files for which there is a mime type (you usually don't want this)
00034       /// otherwise unkown types will be sent as application/octet-stream
00035       AllowAllMimeTypes = 0x4,
00036     };
00037   public:
00038     class FileException : public Exception
00039     {
00040       public:
00041         FileException(const QString &usermsg);
00042     };
00043   public:
00044     /**
00045      * Constructor.
00046      * dir is the dir to serve files from. Subdirectories will be included.
00047      *
00048      * @author Aleksander Demko
00049      */ 
00050     FileHTTPHandler(const QString &docdir, int flags = 0);
00051 
00052     /// handler
00053     virtual void handleRequest(wexus::HTTPRequest &req, wexus::HTTPReply &reply);
00054 
00055     /**
00056      * The core handler itself, reusable in other places
00057      * (like FileApp) and without an FileHTTPHandler instance.
00058      *
00059      * @author Aleksander Demko
00060      */ 
00061     static void processRequest(int flags, const QString &docdir, const QString &relpath, wexus::HTTPReply &reply);
00062 
00063     /**
00064      * Sends the given file to the given device (often reply.output().device().
00065      *
00066      * Returns true on success, false on cant-open-file,
00067      * and throws on critical errors.
00068      *
00069      * @author Aleksander Demko
00070      */ 
00071     static bool sendFile(const QString &filename, QIODevice * outputdev);
00072 
00073   protected:
00074     static void generateDirIndex(const QString &fullpath, const QString &relpath, wexus::HTTPReply &reply);
00075 
00076   protected:
00077     QString dm_docdir;
00078     int dm_flags;
00079 };
00080 
00081 #endif
00082 
 All Classes Namespaces Functions Variables Enumerations Enumerator