MythTV  0.26-pre
htmlserver.cpp
Go to the documentation of this file.
00001 
00002 // Program Name: htmlserver.cpp
00003 // Created     : Mar. 9, 2011
00004 //
00005 // Purpose     : Http server extension to serve up static html content
00006 //                                                                            
00007 // Copyright (c) 2011 David Blain <dblain@mythtv.org>
00008 //                                          
00009 // Licensed under the GPL v2 or later, see COPYING for details                    
00010 //
00012 
00013 #include "mythlogging.h"
00014 #include "htmlserver.h"
00015 #include "storagegroup.h"
00016 
00017 #include <QFileInfo>
00018 #include <QDir>
00019 #include <QTextStream>
00020 
00022 //
00024 
00025 HtmlServerExtension::HtmlServerExtension( const QString sSharePath,
00026                                           const QString sApplicationPrefix)
00027   : HttpServerExtension( "Html" , sSharePath),
00028     m_IndexFilename(sApplicationPrefix + "index")
00029 {
00030     // Cache the absolute path for the share directory.
00031 
00032     QDir dir( sSharePath + "/html" );
00033 
00034     dir.makeAbsolute();
00035 
00036     m_sAbsoluteSharePath =  dir.absolutePath();
00037 
00038     if (getenv("MYTHHTMLDIR"))
00039     {
00040         QString sTempSharePath = getenv("MYTHHTMLDIR");
00041         if (!sTempSharePath.isEmpty())
00042         {
00043             dir.setPath( sTempSharePath );
00044             m_sAbsoluteSharePath = dir.absolutePath();
00045         }
00046     }
00047 }
00048 
00050 //
00052 
00053 HtmlServerExtension::~HtmlServerExtension( )
00054 {
00055 }
00056 
00058 //
00060 
00061 bool HtmlServerExtension::ProcessRequest( HTTPRequest *pRequest )
00062 {
00063     if (pRequest)
00064     {
00065         if ( pRequest->m_sBaseUrl.startsWith("/") == false)
00066             return( false );
00067 
00068         bool      bStorageGroupFile = false;
00069         QFileInfo oInfo( m_sAbsoluteSharePath + pRequest->m_sResourceUrl );
00070 
00071         if (oInfo.isDir())
00072         {
00073             QString sIndexFileName = oInfo.filePath() + m_IndexFilename + ".qsp";
00074 
00075             if (QFile::exists( sIndexFileName ))
00076                 oInfo.setFile( sIndexFileName );
00077             else 
00078                 oInfo.setFile( oInfo.filePath() + m_IndexFilename + ".html" );
00079         }
00080 
00081         if (pRequest->m_sResourceUrl.startsWith("/StorageGroup/"))
00082         {
00083             StorageGroup oGroup(pRequest->m_sResourceUrl.section('/', 2, 2));
00084             QString      sFile =
00085                 oGroup.FindFile(pRequest->m_sResourceUrl.section('/', 3));
00086             if (!sFile.isEmpty())
00087             {
00088                 oInfo.setFile(sFile);
00089                 bStorageGroupFile = true;
00090             }
00091         }
00092 
00093         if (bStorageGroupFile || oInfo.exists() == true )
00094         {
00095             oInfo.makeAbsolute();
00096 
00097             QString sResName = oInfo.canonicalFilePath();
00098 
00099             // --------------------------------------------------------------
00100             // Checking for url's that contain ../ or similar.
00101             // --------------------------------------------------------------
00102 
00103             if (( bStorageGroupFile ) ||
00104                 (sResName.startsWith( m_sAbsoluteSharePath, Qt::CaseInsensitive )))
00105             {
00106                 if (oInfo.exists())
00107                 {
00108                     if (oInfo.isSymLink())
00109                         sResName = oInfo.symLinkTarget();
00110 
00111                     // ------------------------------------------------------
00112                     // Is this a Qt Server Page (File contains script)...
00113                     // ------------------------------------------------------
00114 
00115                     QString sSuffix = oInfo.suffix();
00116 
00117                     if ((sSuffix.compare( "qsp", Qt::CaseInsensitive ) == 0) ||
00118                         (sSuffix.compare( "qjs", Qt::CaseInsensitive ) == 0)) 
00119                     {
00120                         pRequest->m_eResponseType = ResponseTypeHTML;
00121 
00122                         QTextStream stream( &pRequest->m_response );
00123                         
00124                         m_Scripting.EvaluatePage( &stream, sResName );
00125 
00126                         return true;
00127 
00128                     }
00129 
00130                     // ------------------------------------------------------
00131                     // Return the file.
00132                     // ------------------------------------------------------
00133 
00134                     pRequest->FormatFileResponse( sResName );
00135 
00136                     return true;
00137                 }
00138             }
00139         }
00140 
00141         // force return as a 404...
00142         pRequest->FormatFileResponse( "" );
00143     }
00144 
00145     return( true );
00146 }
00147 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends