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