MythTV  master
serverSideScripting.h
Go to the documentation of this file.
1 // Program Name: serverSideScripting.h
3 // Created : Mar. 22, 2011
4 //
5 // Purpose : Server Side Scripting support for Html Server
6 //
7 // Copyright (c) 2011 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef SERVERSIDESCRIPTING_H_
14 #define SERVERSIDESCRIPTING_H_
15 
16 #include <utility>
17 
18 // Qt headers
19 #include <QDateTime>
20 #include <QMap>
21 #include <QMutex>
22 #include <QScriptEngine>
23 #include <QScriptable>
24 #include <QString>
25 #include <QTextStream>
26 
27 // MythTV headers
28 #include "upnpexp.h"
29 #include "upnputil.h"
30 #include "httprequest.h"
31 
32 #ifdef _WIN32
33 #include <QScriptEngineDebugger>
34 #endif
35 
36 class ScriptInfo
37 {
38  public:
39  QScriptValue m_oFunc;
40  QDateTime m_dtTimeStamp;
41 
42  ScriptInfo() = default;
43 
44  ScriptInfo( const QScriptValue& func, QDateTime dt )
45  : m_oFunc( func ), m_dtTimeStamp(std::move( dt ))
46  {}
47 };
48 
50 {
51  public:
52 
53  QScriptEngine m_engine;
54 
55 #ifdef _WIN32
56  QScriptEngineDebugger m_debugger;
57 #endif
58 
61 
62  QString SetResourceRootPath( const QString &path );
63 
64  void RegisterMetaObjectType( const QString &sName,
65  const QMetaObject *pMetaObject,
66  QScriptEngine::FunctionSignature pFunction);
67 
68  bool EvaluatePage( QTextStream *pOutStream, const QString &sFileName,
69  HTTPRequest *pRequest, const QByteArray &cspToken);
70 
71  protected:
72 
73  QMutex m_mutex;
74  QMap< QString, ScriptInfo* > m_mapScripts;
75  QString m_sResRootPath;
76 
77  void Lock () { m_mutex.lock(); }
78  void Unlock () { m_mutex.unlock(); }
79 
80  ScriptInfo *GetLoadedScript ( const QString &sFileName );
81 
82  static QString ReadFileContents ( const QString &sFileName ) ;
83 
84  QString CreateMethodFromFile( const QString &sFileName ) const;
85 
86  bool ProcessLine ( QTextStream &sCode,
87  QString &sLine,
88  bool bInCode,
89  QString &sTransBuffer ) const;
90 };
91 
92 
93 class OutputStream : public QObject, public QScriptable
94 {
95  Q_OBJECT
96 
97  public:
98 
99  explicit OutputStream( QTextStream *pStream, QObject *parent = nullptr )
100  : QObject( parent ), m_pTextStream( pStream ) {}
101 
102  ~OutputStream() override = default;
103 
104  public slots:
105 
106  void write( const QString &sValue )
107  {
108  *m_pTextStream << sValue;
109  }
110 
111  void writeln( const QString &sValue )
112  {
113  *m_pTextStream << sValue << "\n";
114  }
115 
116  private:
117  QTextStream *m_pTextStream {nullptr};
118 };
119 
120 #endif
ScriptInfo::m_dtTimeStamp
QDateTime m_dtTimeStamp
Definition: serverSideScripting.h:40
HTTPRequest
Definition: httprequest.h:109
ScriptInfo::ScriptInfo
ScriptInfo(const QScriptValue &func, QDateTime dt)
Definition: serverSideScripting.h:44
OutputStream::~OutputStream
~OutputStream() override=default
OutputStream::write
void write(const QString &sValue)
Definition: serverSideScripting.h:106
ScriptInfo::m_oFunc
QScriptValue m_oFunc
Definition: serverSideScripting.h:39
ServerSideScripting::m_sResRootPath
QString m_sResRootPath
Definition: serverSideScripting.h:75
ScriptInfo::ScriptInfo
ScriptInfo()=default
ServerSideScripting::m_engine
QScriptEngine m_engine
Definition: serverSideScripting.h:53
OutputStream::writeln
void writeln(const QString &sValue)
Definition: serverSideScripting.h:111
OutputStream::m_pTextStream
QTextStream * m_pTextStream
Definition: serverSideScripting.h:117
ServerSideScripting::m_mutex
QMutex m_mutex
Definition: serverSideScripting.h:73
OutputStream::OutputStream
OutputStream(QTextStream *pStream, QObject *parent=nullptr)
Definition: serverSideScripting.h:99
ServerSideScripting::m_debugger
QScriptEngineDebugger m_debugger
Definition: serverSideScripting.h:56
OutputStream
Definition: serverSideScripting.h:93
ServerSideScripting::Lock
void Lock()
Definition: serverSideScripting.h:77
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
ServerSideScripting::Unlock
void Unlock()
Definition: serverSideScripting.h:78
ServerSideScripting
Definition: serverSideScripting.h:49
ScriptInfo
Definition: serverSideScripting.h:36
upnputil.h
httprequest.h
ServerSideScripting::m_mapScripts
QMap< QString, ScriptInfo * > m_mapScripts
Definition: serverSideScripting.h:74
upnpexp.h