MythTV master
mythhttpfile.cpp
Go to the documentation of this file.
1// C++ headers
2#include <algorithm>
3
4// Qt
5#include <QFile>
6#include <QFileInfo>
7
8// MythTV
9#include "mythlogging.h"
10#include "mythdate.h"
13#include "http/mythhttpfile.h"
14
15#define LOC QString("HTTPFile: ")
16
17HTTPFile MythHTTPFile::Create(const QString &ShortName, const QString& FullName)
18{
19 return std::shared_ptr<MythHTTPFile>(new MythHTTPFile(ShortName, FullName));
20}
21
32MythHTTPFile::MythHTTPFile(const QString& ShortName, const QString& FullName)
33 : QFile(FullName),
34 MythHTTPContent(ShortName)
35{
36}
37
39{
40 // Build full path
41 QString file = Request->m_root + Request->m_path + Request->m_fileName;
42
43 // Process options requests
45 if (response)
46 return response;
47
48 // Ensure the file exists
49 LOG(VB_HTTP, LOG_INFO, LOC + QString("Looking for '%1'").arg(file));
50
52 {
53 LOG(VB_HTTP, LOG_INFO, LOC + QString("Failed to find '%1'").arg(file));
54 Request->m_status = HTTPNotFound;
56 }
57
58 // Try and open
59 auto httpfile = MythHTTPFile::Create(Request->m_fileName, file);
60 if (!httpfile->open(QIODevice::ReadOnly))
61 {
62 LOG(VB_GENERAL, LOG_WARNING, LOC + QString("Failed to open '%1'").arg(file));
63 Request->m_status = HTTPNotFound;
65 }
66
67 // Extensions that should not be cached
68 static const std::vector<const char *> s_exts = { ".json", ".js", ".html", ".css" };
69
70 if (std::ranges::any_of(s_exts,
71 [&](const char * value) { return file.endsWith(value); }))
72 httpfile->m_cacheType = HTTPNoCache;
73 else
74 httpfile->m_cacheType = HTTPLastModified | HTTPLongLife;
75
76 httpfile->m_lastModified = QFileInfo(file).lastModified();
77
78 LOG(VB_HTTP, LOG_DEBUG, LOC + QString("Last modified: %2")
79 .arg(MythDate::toString(httpfile->m_lastModified, MythDate::kOverrideUTC | MythDate::kRFC822)));
80
81 // Create our response
82 response = MythHTTPResponse::FileResponse(Request, httpfile);
83 QString mime = httpfile->m_mimeType.Name();
84 LOG(VB_HTTP, LOG_INFO, LOC + QString("mimetype '%1'").arg(mime));
85 // Assume static content
86 return response;
87}
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