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 <QRegExp>
14 #include <QBuffer>
15 #include <QEventLoop>
16 #include <QImage>
17 
18 #include "internetContent.h"
19 
20 #include "mythcorecontext.h"
21 #include "mythdate.h"
22 #include "mythsystemlegacy.h"
23 #include "mythdirs.h"
24 
25 #include "rssparse.h"
26 #include "netgrabbermanager.h"
27 
29 //
31 
32 InternetContent::InternetContent( const QString &sSharePath)
33  : HttpServerExtension( "InternetContent", sSharePath)
34 {
35 }
36 
38 //
40 
42 {
43  return QStringList( "/InternetContent" );
44 }
45 
47 //
49 
51 {
52  try
53  {
54  if (pRequest)
55  {
56  if (pRequest->m_sBaseUrl != "/InternetContent")
57  return false;
58 
59  LOG(VB_UPNP, LOG_INFO,
60  QString("InternetContent::ProcessRequest: %1 : %2")
61  .arg(pRequest->m_sMethod)
62  .arg(pRequest->m_sRawRequest));
63 
64  // --------------------------------------------------------------
65 
66  if (pRequest->m_sMethod == "GetInternetSearch")
67  {
68  GetInternetSearch( pRequest );
69  return true;
70  }
71 
72  // --------------------------------------------------------------
73 
74  if (pRequest->m_sMethod == "GetInternetSources")
75  {
76  GetInternetSources( pRequest );
77  return true;
78  }
79 
80  // --------------------------------------------------------------
81 
82  if (pRequest->m_sMethod == "GetInternetContent")
83  {
84  GetInternetContent( pRequest );
85  return true;
86  }
87  }
88  }
89  catch( ... )
90  {
91  LOG(VB_GENERAL, LOG_ERR,
92  "InternetContent::ProcessRequest() - Unexpected Exception" );
93  }
94 
95  return false;
96 }
97 
98 // ==========================================================================
99 // Request handler Methods
100 // ==========================================================================
101 
103 //
105 
107 {
108  pRequest->m_eResponseType = ResponseTypeHTML;
109 
110  QString grabber = pRequest->m_mapParams[ "Grabber" ];
111  QString query = pRequest->m_mapParams[ "Query" ];
112  QString page = pRequest->m_mapParams[ "Page" ];
113 
114  if (grabber.isEmpty() || query.isEmpty())
115  return;
116 
117  QString command = QString("%1internetcontent/%2").arg(GetShareDir())
118  .arg(grabber);
119 
120  if (!QFile::exists(command))
121  {
122  pRequest->FormatRawResponse( QString("<HTML>Grabber %1 does "
123  "not exist!</HTML>").arg(command) );
124  return;
125  }
126 
127  Search *search = new Search();
128  QEventLoop loop;
129 
130  QObject::connect(search, SIGNAL(finishedSearch(Search *)),
131  &loop, SLOT(quit(void)));
132  QObject::connect(search, SIGNAL(searchTimedOut(Search *)),
133  &loop, SLOT(quit(void)));
134 
135  search->executeSearch(command, query, page);
136  loop.exec();
137 
138  search->process();
139 
140  QDomDocument ret;
141  ret.setContent(search->GetData());
142 
143  delete search;
144 
145  if (ret.isNull())
146  return;
147 
148  pRequest->FormatRawResponse( ret.toString() );
149 }
150 
152 //
154 
156 {
157  pRequest->m_eResponseType = ResponseTypeHTML;
158 
159  QString ret;
160  QString GrabberDir = QString("%1/internetcontent/").arg(GetShareDir());
161  QDir GrabberPath(GrabberDir);
162  QStringList Grabbers = GrabberPath.entryList(QDir::Files | QDir::Executable);
163 
164  for (QStringList::const_iterator i = Grabbers.begin();
165  i != Grabbers.end(); ++i)
166  {
167  QString commandline = GrabberDir + (*i);
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:
void Run(time_t timeout=0)
Runs a command inside the /bin/sh shell. Returns immediately.
QString m_sBaseUrl
Definition: httprequest.h:123
allow access to stdout
Definition: mythsystem.h:39
void FormatRawResponse(const QString &sXML)
QString m_sMethod
Definition: httprequest.h:125
long m_nResponseStatus
Definition: httprequest.h:148
bool ProcessRequest(HTTPRequest *pRequest) override
void GetInternetSearch(HTTPRequest *pRequest)
InternetContent(const QString &sSharePath)
QString m_sRawRequest
Definition: httprequest.h:119
QByteArray GetData()
run process through shell
Definition: mythsystem.h:41
QByteArray & ReadAll()
void GetInternetContent(HTTPRequest *pRequest)
QString GetShareDir(void)
Definition: mythdirs.cpp:222
void FormatActionResponse(Serializer *ser)
QString m_sFileName
Definition: httprequest.h:151
void process(void)
QStringMap m_mapParams
Definition: httprequest.h:127
void GetInternetSources(HTTPRequest *pRequest)
uint Wait(time_t timeout=0)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void executeSearch(const QString &script, const QString &query, const QString &pagenum="")
QStringList GetBasePaths() override
HttpResponseType m_eResponseType
Definition: httprequest.h:145