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
20 #include "libmythbase/mythdate.h"
21 #include "libmythbase/mythdirs.h"
24 #include "libmythbase/rssparse.h"
25 
26 // MythBackend
27 #include "internetContent.h"
28 
30 //
32 
33 InternetContent::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 {
109  pRequest->m_eResponseType = ResponseTypeHTML;
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 {
158  pRequest->m_eResponseType = ResponseTypeHTML;
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 {
191  pRequest->m_eResponseType = ResponseTypeHTML;
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  {
206  pRequest->m_eResponseType = ResponseTypeFile;
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:
NameValue
Definition: upnputil.h:40
HTTPRequest::m_sBaseUrl
QString m_sBaseUrl
Definition: httprequest.h:127
Search::finishedSearch
void finishedSearch(Search *item)
HTTPRequest::FormatRawResponse
void FormatRawResponse(const QString &sXML)
Definition: httprequest.cpp:773
HTTPRequest
Definition: httprequest.h:109
InternetContent::GetInternetSearch
static void GetInternetSearch(HTTPRequest *pRequest)
Definition: internetContent.cpp:107
InternetContent::GetInternetSources
static void GetInternetSources(HTTPRequest *pRequest)
Definition: internetContent.cpp:156
MythSystemLegacy
Definition: mythsystemlegacy.h:67
InternetContent::InternetContent
InternetContent(const QString &sSharePath)
Definition: internetContent.cpp:33
HTTPRequest::m_sMethod
QString m_sMethod
Definition: httprequest.h:129
rssparse.h
InternetContent::GetBasePaths
QStringList GetBasePaths() override
Definition: internetContent.cpp:42
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythSystemLegacy::ReadAll
QByteArray & ReadAll()
Definition: mythsystemlegacy.cpp:402
mythdirs.h
mythsystemlegacy.h
ResponseTypeFile
@ ResponseTypeFile
Definition: httprequest.h:84
quit
@ quit
Definition: lirc_client.h:30
NameValues
Definition: upnputil.h:77
HTTPRequest::FormatActionResponse
void FormatActionResponse(Serializer *ser)
Definition: httprequest.cpp:700
mythdate.h
HTTPRequest::m_nResponseStatus
long m_nResponseStatus
Definition: httprequest.h:152
InternetContent::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: internetContent.cpp:51
netgrabbermanager.h
HTTPRequest::m_mapParams
QStringMap m_mapParams
Definition: httprequest.h:131
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:254
MythSystemLegacy::Wait
uint Wait(std::chrono::seconds timeout=0s)
Definition: mythsystemlegacy.cpp:243
HTTPRequest::m_sRawRequest
QString m_sRawRequest
Definition: httprequest.h:123
Search
Definition: netgrabbermanager.h:143
HTTPRequest::m_sFileName
QString m_sFileName
Definition: httprequest.h:155
kMSRunShell
@ kMSRunShell
run process through shell
Definition: mythsystem.h:43
ResponseTypeHTML
@ ResponseTypeHTML
Definition: httprequest.h:79
mythcorecontext.h
Search::searchTimedOut
void searchTimedOut(Search *item)
HTTPRequest::m_eResponseType
HttpResponseType m_eResponseType
Definition: httprequest.h:149
internetContent.h
InternetContent::GetInternetContent
static void GetInternetContent(HTTPRequest *pRequest)
Definition: internetContent.cpp:189
MythSystemLegacy::Run
void Run(std::chrono::seconds timeout=0s)
Runs a command inside the /bin/sh shell. Returns immediately.
Definition: mythsystemlegacy.cpp:213
HttpServerExtension
Definition: httpserver.h:71
kMSStdOut
@ kMSStdOut
allow access to stdout
Definition: mythsystem.h:41