MythTV  0.26-pre
httpserver.h
Go to the documentation of this file.
00001 
00002 // Program Name: httpserver.h
00003 // Created     : Oct. 1, 2005
00004 //
00005 // Purpose     : HTTP 1.1 Mini Server Implmenetation
00006 //               Used for UPnp/AV implementation & status information
00007 //                                                                            
00008 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
00009 //                                          
00010 // Licensed under the GPL v2 or later, see COPYING for details                    
00011 //
00013 
00014 #ifndef __HTTPSERVER_H__
00015 #define __HTTPSERVER_H__
00016 
00017 // POSIX headers
00018 #include <sys/types.h>
00019 #ifndef USING_MINGW
00020 #include <netinet/in.h>
00021 #include <arpa/inet.h>
00022 #endif
00023 
00024 // Qt headers
00025 #include <QReadWriteLock>
00026 #include <QMultiMap>
00027 #include <QRunnable>
00028 #include <QPointer>
00029 #include <QMutex>
00030 #include <QList>
00031 
00032 // MythTV headers
00033 #include "serverpool.h"
00034 #include "httprequest.h"
00035 #include "mthreadpool.h"
00036 #include "refcounted.h"
00037 #include "upnputil.h"
00038 #include "compat.h"
00039 
00040 typedef struct timeval  TaskTime;
00041 
00042 class HttpWorkerThread;
00043 class QScriptEngine;
00044 class HttpServer;
00045 
00048 //
00049 // HttpServerExtension Class Definition
00050 //
00053 
00054 class UPNP_PUBLIC HttpServerExtension : public QObject
00055 {
00056     Q_OBJECT
00057 
00058     public:
00059 
00060         QString     m_sName;
00061         QString     m_sSharePath;
00062         
00063     public:
00064 
00065         HttpServerExtension( const QString &sName, const  QString &sSharePath )
00066            :m_sName( sName ), m_sSharePath( sSharePath ) {};
00067 
00068         virtual ~HttpServerExtension() {};
00069 
00070         virtual bool ProcessRequest(HTTPRequest *pRequest) = 0;
00071 
00072         virtual QStringList GetBasePaths() = 0;
00073 };
00074 
00075 typedef QList<QPointer<HttpServerExtension> > HttpServerExtensionList;
00076 
00079 //
00080 // HttpServer Class Definition
00081 //
00084 
00085 class UPNP_PUBLIC HttpServer : public ServerPool
00086 {
00087   protected:
00088     mutable QReadWriteLock  m_rwlock;
00089     HttpServerExtensionList m_extensions;
00090     // This multimap does NOT take ownership of the HttpServerExtension*
00091     QMultiMap< QString, HttpServerExtension* >  m_basePaths;
00092     QString                 m_sSharePath;
00093     HttpServerExtension    *m_pHtmlServer;
00094     MThreadPool             m_threadPool;
00095     bool                    m_running; // protected by m_rwlock
00096 
00097     static QMutex           s_platformLock;
00098     static QString          s_platform;
00099 
00100   public:
00101     HttpServer(const QString sApplicationPrefix = QString(""));
00102     virtual ~HttpServer();
00103 
00104     void RegisterExtension(HttpServerExtension*);
00105     void UnregisterExtension(HttpServerExtension*);
00106     void DelegateRequest(HTTPRequest*);
00107 
00108     QScriptEngine *ScriptEngine(void);
00109 
00110     virtual void newTcpConnection(int socket); // QTcpServer
00111 
00112     QString GetSharePath(void) const
00113     { // never modified after creation, so no need to lock
00114         return m_sSharePath;
00115     }
00116 
00117     bool IsRunning(void) const
00118     {
00119         m_rwlock.lockForRead();
00120         bool tmp = m_running;
00121         m_rwlock.unlock();
00122         return tmp;
00123     }
00124 
00125     static QString GetPlatform(void);
00126 };
00127 
00130 //
00131 // HttpWorkerThread Class Definition
00132 //
00135 
00136 class HttpWorker : public QRunnable
00137 {
00138   public:
00139     HttpWorker(HttpServer &httpServer, int sock);
00140 
00141     virtual void run(void);
00142 
00143   protected:
00144     HttpServer &m_httpServer; 
00145     int         m_socket;
00146     int         m_socketTimeout;
00147 };
00148 
00149 
00150 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends