Skip to content

Commit

Permalink
mythbackend webserver server side scripting enhancements
Browse files Browse the repository at this point in the history
- Add LOG_DEBUG level logging of all requests coming into the webserver

- Don't trim whitespace and newlines from server side script files as they
  are processed.  This was sometimes messing up formatting and obfuscating
  the output where we didn't need to be.

- Add .qxml as a valid server side scripting extension.

  .qxml files are processed like .qsp & .qjs, but return a mime type text/xml

- Add ability to pass arguments to server side scripts via the URL parameters

  http://BACKENDIP:PORT/samples/somescript.qsp?arg1=value1&arg2=value2
  http://BACKENDIP:PORT/samples/otherscript.qsp?sort=title&limit=10&start=30

  Arguments are accessed within the script via the 'ARGS' array:

  if (ARGS["sort"] == "title")
  • Loading branch information
cpinkham committed Oct 10, 2012
1 parent 79d3aa2 commit 19e49f9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
14 changes: 9 additions & 5 deletions mythtv/libs/libmythupnp/htmlserver.cpp
Expand Up @@ -105,16 +105,20 @@ bool HtmlServerExtension::ProcessRequest( HTTPRequest *pRequest )
// Is this a Qt Server Page (File contains script)...
// ------------------------------------------------------

QString sSuffix = oInfo.suffix();
QString sSuffix = oInfo.suffix().toLower();

if ((sSuffix.compare( "qsp", Qt::CaseInsensitive ) == 0) ||
(sSuffix.compare( "qjs", Qt::CaseInsensitive ) == 0))
if ((sSuffix == "qsp") ||
(sSuffix == "qxml") ||
(sSuffix == "qjs" ))
{
pRequest->m_eResponseType = ResponseTypeHTML;
if (sSuffix == "qxml")
pRequest->m_eResponseType = ResponseTypeXML;
else
pRequest->m_eResponseType = ResponseTypeHTML;

QTextStream stream( &pRequest->m_response );

m_Scripting.EvaluatePage( &stream, sResName );
m_Scripting.EvaluatePage( &stream, sResName, pRequest->m_mapParams );

return true;

Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythupnp/httprequest.cpp
Expand Up @@ -63,6 +63,7 @@ static MIMETypes g_MIMETypes[] =
{ "qjs" , "application/javascript" },
{ "txt" , "text/plain" },
{ "xml" , "text/xml" },
{ "qxml", "text/xml" },
{ "xslt", "text/xml" },
{ "pdf" , "application/pdf" },
{ "avi" , "video/avi" },
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythupnp/httpserver.cpp
Expand Up @@ -182,6 +182,7 @@ void HttpServer::DelegateRequest(HTTPRequest *pRequest)
{
bool bProcessed = false;

LOG(VB_UPNP, LOG_DEBUG, QString("m_sBaseUrl: %1").arg( pRequest->m_sBaseUrl ));
m_rwlock.lockForRead();

QList< HttpServerExtension* > list = m_basePaths.values( pRequest->m_sBaseUrl );
Expand Down
28 changes: 22 additions & 6 deletions mythtv/libs/libmythupnp/serverSideScripting.cpp
Expand Up @@ -106,7 +106,8 @@ void ServerSideScripting::RegisterMetaObjectType( const QString &sName,
//
//////////////////////////////////////////////////////////////////////////////

bool ServerSideScripting::EvaluatePage( QTextStream *pOutStream, const QString &sFileName )
bool ServerSideScripting::EvaluatePage( QTextStream *pOutStream, const QString &sFileName,
const QStringMap &mapParams )
{
try
{
Expand Down Expand Up @@ -162,6 +163,24 @@ bool ServerSideScripting::EvaluatePage( QTextStream *pOutStream, const QString &
}
}

// ------------------------------------------------------------------
// Build array of arguments passed to script
// ------------------------------------------------------------------

QString params = "ARGS = { ";
if (mapParams.size())
{
QMap<QString, QString>::const_iterator it = mapParams.begin();

for (; it != mapParams.end(); ++it)
{
params += QString("%1: '%2', ").arg(it.key()).arg(it.value());
}
}

params += " }";
m_engine.evaluate(params);

// ------------------------------------------------------------------
// Execute function to render output
// ------------------------------------------------------------------
Expand All @@ -170,6 +189,7 @@ bool ServerSideScripting::EvaluatePage( QTextStream *pOutStream, const QString &

QScriptValueList args;
args << m_engine.newQObject( &outStream );
args << m_engine.globalObject().property("ARGS");

pInfo->m_oFunc.call( QScriptValue(), args );

Expand Down Expand Up @@ -213,7 +233,7 @@ QString ServerSideScripting::CreateMethodFromFile( const QString &sFileName )
QTextStream stream( &scriptFile );
QString sTransBuffer;

sCode << "(function( os ) {\n";
sCode << "(function( os, ARGS ) {\n";

while( !stream.atEnd() )
{
Expand Down Expand Up @@ -246,8 +266,6 @@ bool ServerSideScripting::ProcessLine( QTextStream &sCode,
bool bInCode,
QString &sTransBuffer )
{
sLine = sLine.trimmed();

QString sLowerLine = sLine.toLower();

if (!sTransBuffer.isEmpty())
Expand Down Expand Up @@ -355,8 +373,6 @@ bool ServerSideScripting::ProcessLine( QTextStream &sCode,

if (bMatchFound)
bInCode = true;
else
bNewLine = false;
}
}
else
Expand Down
4 changes: 3 additions & 1 deletion mythtv/libs/libmythupnp/serverSideScripting.h
Expand Up @@ -14,6 +14,7 @@
#define SERVERSIDESCRIPTING_H_

#include "upnpexp.h"
#include "upnputil.h"

#include <QString>
#include <QMap>
Expand Down Expand Up @@ -59,7 +60,8 @@ class UPNP_PUBLIC ServerSideScripting
const QMetaObject *pMetaObject,
QScriptEngine::FunctionSignature pFunction);

bool EvaluatePage( QTextStream *pOutStream, const QString &sFileName );
bool EvaluatePage( QTextStream *pOutStream, const QString &sFileName,
const QStringMap &mapParams );

protected:

Expand Down

0 comments on commit 19e49f9

Please sign in to comment.