MythTV  master
mythhttpfile.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QFile>
3 #include <QFileInfo>
4 
5 // MythTV
6 #include "mythlogging.h"
7 #include "mythdate.h"
9 #include "http/mythhttprequest.h"
10 #include "http/mythhttpfile.h"
11 
12 #define LOC QString("HTTPFile: ")
13 
14 HTTPFile MythHTTPFile::Create(const QString &ShortName, const QString& FullName)
15 {
16  return std::shared_ptr<MythHTTPFile>(new MythHTTPFile(ShortName, FullName));
17 }
18 
29 MythHTTPFile::MythHTTPFile(const QString& ShortName, const QString& FullName)
30  : QFile(FullName),
31  MythHTTPContent(ShortName)
32 {
33 }
34 
36 {
37  // Build full path
38  QString file = Request->m_root + Request->m_path + Request->m_fileName;
39 
40  // Process options requests
42  if (response)
43  return response;
44 
45  // Ensure the file exists
46  LOG(VB_HTTP, LOG_INFO, LOC + QString("Looking for '%1'").arg(file));
47 
48  if (!QFileInfo::exists(file))
49  {
50  LOG(VB_GENERAL, LOG_WARNING, LOC + QString("Failed to find '%1'").arg(file));
51  Request->m_status = HTTPNotFound;
53  }
54 
55  // Try and open
56  auto httpfile = MythHTTPFile::Create(Request->m_fileName, file);
57  if (!httpfile->open(QIODevice::ReadOnly))
58  {
59  LOG(VB_GENERAL, LOG_WARNING, LOC + QString("Failed to open '%1'").arg(file));
60  Request->m_status = HTTPNotFound;
62  }
63 
64  // Extensions that should not be cached
65  static const std::vector<const char *> s_exts = { ".json", ".js", ".html", ".css" };
66 
67  if (std::any_of(s_exts.cbegin(), s_exts.cend(),
68  [&](const char * value) { return file.endsWith(value); }))
69  httpfile->m_cacheType = HTTPNoCache;
70  else
71  httpfile->m_cacheType = HTTPLastModified | HTTPLongLife;
72 
73  httpfile->m_lastModified = QFileInfo(file).lastModified();
74 
75  LOG(VB_HTTP, LOG_DEBUG, LOC + QString("Last modified: %2")
76  .arg(MythDate::toString(httpfile->m_lastModified, MythDate::kOverrideUTC | MythDate::kRFC822)));
77 
78  // Create our response
79  response = MythHTTPResponse::FileResponse(Request, httpfile);
80  QString mime = httpfile->m_mimeType.Name();
81  LOG(VB_HTTP, LOG_INFO, LOC + QString("mimetype '%1'").arg(mime));
82  // Assume static content
83  return response;
84 }
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:84
MythHTTPResponse::FileResponse
static HTTPResponse FileResponse(const HTTPRequest2 &Request, const HTTPFile &File)
Definition: mythhttpresponse.cpp:273
MythHTTPFile::Create
static HTTPFile Create(const QString &ShortName, const QString &FullName)
Definition: mythhttpfile.cpp:14
xbmcvfs.exists
bool exists(str path)
Definition: xbmcvfs.py:51
MythDate::kOverrideUTC
@ kOverrideUTC
Present date/time in UTC.
Definition: mythdate.h:31
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
HTTPLongLife
@ HTTPLongLife
Definition: mythhttptypes.h:149
build_compdb.file
file
Definition: build_compdb.py:55
HTTPNotFound
@ HTTPNotFound
Definition: mythhttptypes.h:115
mythhttpfile.h
MythHTTPResponse::HandleOptions
static HTTPResponse HandleOptions(const HTTPRequest2 &Request)
Definition: mythhttpresponse.cpp:192
mythdate.h
MythHTTPFile::ProcessFile
static HTTPResponse ProcessFile(const HTTPRequest2 &Request)
Definition: mythhttpfile.cpp:35
mythlogging.h
MythHTTPFile::MythHTTPFile
MythHTTPFile(const QString &ShortName, const QString &FullName)
Default constructor.
Definition: mythhttpfile.cpp:29
HTTPNoCache
@ HTTPNoCache
Definition: mythhttptypes.h:144
MythHTTPResponse::ErrorResponse
static HTTPResponse ErrorResponse(MythHTTPStatus Status, const QString &ServerName)
Definition: mythhttpresponse.cpp:216
HTTPLastModified
@ HTTPLastModified
Definition: mythhttptypes.h:146
mythhttpresponse.h
HTTPResponse
std::shared_ptr< MythHTTPResponse > HTTPResponse
Definition: mythhttptypes.h:39
hardwareprofile.distros.mythtv_data.request.Request
def Request(url=None)
Definition: request.py:62
MythDate::kRFC822
@ kRFC822
HTTP Date format.
Definition: mythdate.h:30
HTTPRequest2
std::shared_ptr< MythHTTPRequest > HTTPRequest2
Definition: mythhttptypes.h:38
mythhttprequest.h
LOC
#define LOC
Definition: mythhttpfile.cpp:12
HTTPFile
std::shared_ptr< MythHTTPFile > HTTPFile
Definition: mythhttptypes.h:40
MythHTTPContent
Definition: mythhttptypes.h:163