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"
10#include "http/mythhttpfile.h"
11
12#define LOC QString("HTTPFile: ")
13
14HTTPFile MythHTTPFile::Create(const QString &ShortName, const QString& FullName)
15{
16 return std::shared_ptr<MythHTTPFile>(new MythHTTPFile(ShortName, FullName));
17}
18
29MythHTTPFile::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
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}
static HTTPFile Create(const QString &ShortName, const QString &FullName)
MythHTTPFile(const QString &ShortName, const QString &FullName)
Default constructor.
static HTTPResponse ProcessFile(const HTTPRequest2 &Request)
static HTTPResponse FileResponse(const HTTPRequest2 &Request, const HTTPFile &File)
static HTTPResponse HandleOptions(const HTTPRequest2 &Request)
static HTTPResponse ErrorResponse(MythHTTPStatus Status, const QString &ServerName)
#define LOC
@ HTTPNotFound
std::shared_ptr< MythHTTPFile > HTTPFile
Definition: mythhttptypes.h:41
std::shared_ptr< MythHTTPRequest > HTTPRequest2
Definition: mythhttptypes.h:39
std::shared_ptr< MythHTTPResponse > HTTPResponse
Definition: mythhttptypes.h:40
@ HTTPNoCache
@ HTTPLastModified
@ HTTPLongLife
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:93
@ kOverrideUTC
Present date/time in UTC.
Definition: mythdate.h:31
@ kRFC822
HTTP Date format.
Definition: mythdate.h:30
bool exists(str path)
Definition: xbmcvfs.py:51