MythTV master
internetContent.cpp
Go to the documentation of this file.
1// Program Name: internetContent.cpp
2//
3// Purpose - Html & XML status HttpServerExtension
4//
5// Created By : David Blain Created On : Oct. 24, 2005
6// Modified By : Daniel Kristjansson Modified On: Oct. 31, 2007
7//
9
10// Qt
11#include <QBuffer>
12#include <QDir>
13#include <QEventLoop>
14#include <QFile>
15#include <QImage>
16#include <QTextStream>
17
18// MythTV
25
26// MythBackend
27#include "internetContent.h"
28
30//
32
33InternetContent::InternetContent( const QString &sSharePath)
34 : HttpServerExtension( "InternetContent", sSharePath)
35{
36}
37
39//
41
43{
44 return QStringList( "/InternetContent" );
45}
46
48//
50
52{
53 try
54 {
55 if (pRequest)
56 {
57 if (pRequest->m_sBaseUrl != "/InternetContent")
58 return false;
59
60 LOG(VB_UPNP, LOG_INFO,
61 QString("InternetContent::ProcessRequest: %1 : %2")
62 .arg(pRequest->m_sMethod,
63 pRequest->m_sRawRequest));
64
65 // --------------------------------------------------------------
66
67 if (pRequest->m_sMethod == "GetInternetSearch")
68 {
69 GetInternetSearch( pRequest );
70 return true;
71 }
72
73 // --------------------------------------------------------------
74
75 if (pRequest->m_sMethod == "GetInternetSources")
76 {
77 GetInternetSources( pRequest );
78 return true;
79 }
80
81 // --------------------------------------------------------------
82
83 if (pRequest->m_sMethod == "GetInternetContent")
84 {
85 GetInternetContent( pRequest );
86 return true;
87 }
88 }
89 }
90 catch( ... )
91 {
92 LOG(VB_GENERAL, LOG_ERR,
93 "InternetContent::ProcessRequest() - Unexpected Exception" );
94 }
95
96 return false;
97}
98
99// ==========================================================================
100// Request handler Methods
101// ==========================================================================
102
104//
106
108{
110
111 QString grabber = pRequest->m_mapParams[ "Grabber" ];
112 QString query = pRequest->m_mapParams[ "Query" ];
113 QString page = pRequest->m_mapParams[ "Page" ];
114
115 if (grabber.isEmpty() || query.isEmpty())
116 return;
117
118 QString command = QString("%1internetcontent/%2")
119 .arg(GetShareDir(), grabber);
120
121 if (!QFile::exists(command))
122 {
123 pRequest->FormatRawResponse( QString("<HTML>Grabber %1 does "
124 "not exist!</HTML>").arg(command) );
125 return;
126 }
127
128 auto *search = new Search();
129 QEventLoop loop;
130
131 QObject::connect(search, &Search::finishedSearch,
132 &loop, &QEventLoop::quit);
133 QObject::connect(search, &Search::searchTimedOut,
134 &loop, &QEventLoop::quit);
135
136 search->executeSearch(command, query, page);
137 loop.exec();
138
139 search->process();
140
141 QDomDocument ret;
142 ret.setContent(search->GetData());
143
144 delete search;
145
146 if (ret.isNull())
147 return;
148
149 pRequest->FormatRawResponse( ret.toString() );
150}
151
153//
155
157{
159
160 QString ret;
161 QString GrabberDir = QString("%1/internetcontent/").arg(GetShareDir());
162 QDir GrabberPath(GrabberDir);
163 QStringList Grabbers = GrabberPath.entryList(QDir::Files | QDir::Executable);
164
165 for (const auto & name : std::as_const(Grabbers))
166 {
167 QString commandline = GrabberDir + name;
168 MythSystemLegacy scriptcheck(commandline, QStringList("-v"),
170 scriptcheck.Run();
171 scriptcheck.Wait();
172 QByteArray result = scriptcheck.ReadAll();
173
174 if (!result.isEmpty() && result.toLower().startsWith("<grabber>"))
175 ret += result;
176 }
177
178 NameValues list;
179
180 list.push_back( NameValue( "InternetContent", ret ));
181
182 pRequest->FormatActionResponse( list );
183}
184
186//
188
190{
192
193 QString grabber = pRequest->m_mapParams[ "Grabber" ];
194
195 if (grabber.isEmpty())
196 return;
197
198 QString contentDir = QString("%1internetcontent/").arg(GetShareDir());
199 QString htmlFile(contentDir + grabber);
200
201 // Try to prevent directory traversal
202 QFileInfo fileInfo(htmlFile);
203 if (fileInfo.canonicalFilePath().startsWith(contentDir) &&
204 QFile::exists( htmlFile ))
205 {
207 pRequest->m_nResponseStatus = 200;
208 pRequest->m_sFileName = htmlFile;
209 }
210 else
211 {
212 pRequest->FormatRawResponse( QString("<HTML>File %1 does "
213 "not exist!</HTML>").arg(htmlFile) );
214 }
215}
216
217// vim:set shiftwidth=4 tabstop=4 expandtab:
HttpResponseType m_eResponseType
Definition: httprequest.h:150
QString m_sFileName
Definition: httprequest.h:156
long m_nResponseStatus
Definition: httprequest.h:153
void FormatRawResponse(const QString &sXML)
QString m_sMethod
Definition: httprequest.h:130
void FormatActionResponse(Serializer *ser)
QString m_sBaseUrl
Definition: httprequest.h:128
QString m_sRawRequest
Definition: httprequest.h:124
QStringMap m_mapParams
Definition: httprequest.h:132
InternetContent(const QString &sSharePath)
QStringList GetBasePaths() override
static void GetInternetContent(HTTPRequest *pRequest)
static void GetInternetSearch(HTTPRequest *pRequest)
bool ProcessRequest(HTTPRequest *pRequest) override
static void GetInternetSources(HTTPRequest *pRequest)
uint Wait(std::chrono::seconds timeout=0s)
void Run(std::chrono::seconds timeout=0s)
Runs a command inside the /bin/sh shell. Returns immediately.
QByteArray & ReadAll()
void finishedSearch(Search *item)
void searchTimedOut(Search *item)
@ ResponseTypeFile
Definition: httprequest.h:85
@ ResponseTypeHTML
Definition: httprequest.h:80
@ quit
Definition: lirc_client.h:30
QString GetShareDir(void)
Definition: mythdirs.cpp:261
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
@ kMSStdOut
allow access to stdout
Definition: mythsystem.h:41
@ kMSRunShell
run process through shell
Definition: mythsystem.h:43
bool exists(str path)
Definition: xbmcvfs.py:51