MythTV  master
mythhttpencoding.cpp
Go to the documentation of this file.
1 // MythTV
2 #include "mythlogging.h"
3 #include "unziputil.h"
5 #include "http/mythhttpdata.h"
6 #include "http/mythhttpfile.h"
9 
10 // Qt
11 #include <QDomDocument>
12 #include <QJsonDocument>
13 #include <QJsonObject>
14 #include <QJsonValue>
15 
16 #define LOC QString("HTTPEnc: ")
17 
29 using MimePair = std::pair<float,QString>;
30 QStringList MythHTTPEncoding::GetMimeTypes(const QString &Accept)
31 {
32  // Split out mime types
33  auto types = Accept.split(",", Qt::SkipEmptyParts);
34 
35  std::vector<MimePair> weightings;
36  for (const auto & type : std::as_const(types))
37  {
38  QString mime = type.trimmed();
39  auto quality = 1.0F;
40  // Find any quality value (defaults to 1)
41  if (auto index = type.lastIndexOf(";"); index > -1)
42  {
43  mime = type.mid(0, index).trimmed().toLower();
44  auto qual = type.mid(index + 1).trimmed();
45  if (auto index2 = qual.lastIndexOf("="); index2 > -1)
46  {
47  bool ok = false;
48  auto newquality = qual.mid(index2 + 1).toFloat(&ok);
49  if (ok)
50  quality = newquality;
51  }
52  }
53  weightings.emplace_back(quality, mime);
54  }
55 
56  // Sort the list
57  auto comp = [](const MimePair& First, const MimePair& Second) { return First.first > Second.first; };
58  std::sort(weightings.begin(), weightings.end(), comp);
59 
60  // Build the final result. This will pass through invalid types - which should
61  // be handled by the consumer (e.g. wildcard specifiers are not handled).
62  QStringList result;
63  for (const auto & weight : weightings)
64  result.append(weight.second);
65 
66  // Default to xml
67  if (result.empty())
68  result.append("application/xml");
69  return result;
70 }
71 
75 {
76  if (!Request || !Request->m_content.get())
77  return;
78 
79  auto contenttype = MythHTTP::GetHeader(Request->m_headers, "content-type");
80 
81  // type is e.g. text/html; charset=UTF-8 or multipart/form-data; boundary=something
82  auto types = contenttype.split(";", Qt::SkipEmptyParts);
83  if (types.isEmpty())
84  return;
85 
86  // Note: This can produce an invalid mime type but there is no sensible fallback
87  if (auto mime = MythMimeDatabase::MimeTypeForName(types[0].trimmed().toLower()); mime.IsValid())
88  {
89  Request->m_content->m_mimeType = mime;
90  if (mime.Name() == "application/x-www-form-urlencoded")
92  else if (mime.Name() == "text/xml" || mime.Name() == "application/xml" ||
93  mime.Name() == "application/soap+xml")
95  else if (mime.Name() == "application/json")
97  else
98  LOG(VB_HTTP, LOG_ERR, QString("Don't know how to get the parameters for MIME type: '%1'").arg(mime.Name()));
99  }
100  else
101  LOG(VB_HTTP, LOG_ERR, QString("Unknown MIME type: '%1'").arg(types[0]));
102 
103 }
104 
106 {
107  if (!Request || !Request->m_content.get())
108  return;
109 
110  auto payload = QString::fromUtf8(Request->m_content->constData(), Request->m_content->size());
111 
112  // This looks odd, but it is here to cope with stupid UPnP clients that
113  // forget to de-escape the URLs. We can't map %26 here as well, as that
114  // breaks anything that is trying to pass & as part of a name or value.
115  payload.replace("&amp;", "&");
116  if (!payload.isEmpty())
117  {
118  QStringList params = payload.split('&', Qt::SkipEmptyParts);
119  for (const auto & param : std::as_const(params))
120  {
121  QString name = param.section('=', 0, 0);
122  QString value = param.section('=', 1);
123  value.replace("+", " ");
124  if (!name.isEmpty())
125  {
126  name = QUrl::fromPercentEncoding(name.toUtf8());
127  value = QUrl::fromPercentEncoding(value.toUtf8());
128  Request->m_queries.insert(name.trimmed().toLower(), value);
129  }
130  }
131  }
132 }
133 
135 {
136  LOG(VB_HTTP, LOG_DEBUG, "Inspecting XML payload");
137 
138  if (!Request || !Request->m_content.get())
139  return;
140 
141  // soapaction is formatted like "\"http://mythtv.org/Dvr/GetRecordedList\""
142  QString soapaction = Request->m_headers->value("soapaction");
143  soapaction.remove('"');
144  int lastSlashPos= soapaction.lastIndexOf('/');
145  if (lastSlashPos < 0)
146  return;
147  if (Request->m_path == "/")
148  Request->m_path.append(Request->m_fileName).append("/");
149  Request->m_fileName = soapaction.right(soapaction.size()-lastSlashPos-1);
150  LOG(VB_HTTP, LOG_DEBUG, QString("Found method call (%1)").arg(Request->m_fileName));
151 
152  auto payload = QDomDocument();
153  QString err_msg;
154  int err_line {-1};
155  int err_col {-1};
156  if (!payload.setContent(static_cast<QByteArray>(Request->m_content->constData()),
157  true, &err_msg, &err_line, &err_col))
158  {
159  LOG(VB_HTTP, LOG_WARNING, "Unable to parse XML request body");
160  LOG(VB_HTTP, LOG_WARNING, QString("- Error at line %1, column %2, msg: %3")
161  .arg(err_line).arg(err_col).arg(err_msg));
162  return;
163  }
164  QString doc_name = payload.documentElement().localName();
165  if (doc_name.compare("envelope", Qt::CaseInsensitive) == 0)
166  {
167  LOG(VB_HTTP, LOG_DEBUG, "Found SOAP XML message envelope");
168  auto doc_body = payload.documentElement().namedItem("Body");
169  if (doc_body.isNull() || !doc_body.hasChildNodes()) // None or empty body
170  {
171  LOG(VB_HTTP, LOG_DEBUG, "Missing or empty SOAP body");
172  return;
173  }
174  auto body_contents = doc_body.firstChild();
175  if (body_contents.hasChildNodes()) // params for the method
176  {
177  for (QDomNode node = body_contents.firstChild(); !node.isNull(); node = node.nextSibling())
178  {
179  QString name = node.localName();
180  QString value = node.toElement().text();
181  if (!name.isEmpty())
182  {
183  // TODO: html decode entities if required
184  Request->m_queries.insert(name.trimmed().toLower(), value);
185  LOG(VB_HTTP, LOG_DEBUG, QString("Found URL param (%1=%2)").arg(name, value));
186  }
187  }
188  }
189  }
190 }
191 
193 {
194  LOG(VB_HTTP, LOG_DEBUG, "Inspecting JSON payload");
195 
196  if (!Request || !Request->m_content.get())
197  return;
198 
199  QByteArray jstr = static_cast<QByteArray>(Request->m_content->constData());
200  QJsonParseError parseError {};
201  QJsonDocument doc = QJsonDocument::fromJson(jstr, &parseError);
202  if (parseError.error != QJsonParseError::NoError)
203  {
204  LOG(VB_HTTP, LOG_WARNING,
205  QString("Unable to parse JSON request body - Error at position %1, msg: %2")
206  .arg(parseError.offset).arg(parseError.errorString()));
207  return;
208  }
209 
210  QJsonObject json = doc.object();
211  foreach(const QString& key, json.keys())
212  {
213  if (!key.isEmpty())
214  {
215  QString value;
216  if (json.value(key).isObject())
217  {
218  QJsonDocument vd(json.value(key).toObject());
219  value = vd.toJson(QJsonDocument::Compact);
220  }
221  else
222  {
223  value = json.value(key).toVariant().toString();
224 
225  if (value.isEmpty())
226  {
227  LOG(VB_HTTP, LOG_WARNING,
228  QString("Failed to parse value for key '%1' from %2")
229  .arg(key, QString(jstr)));
230  }
231  }
232 
233  Request->m_queries.insert(key.trimmed().toLower(), value);
234  LOG(VB_HTTP, LOG_DEBUG,
235  QString("Found URL param (%1=%2)").arg(key, value));
236  }
237  }
238 }
239 
243 {
244  auto * data = std::get_if<HTTPData>(&Content);
245  auto * file = std::get_if<HTTPFile>(&Content);
246  if (!(data || file))
247  return {};
248 
249  QString filename;
250  if (data)
251  filename = (*data)->m_fileName;
252  else if (file)
253  filename = (*file)->m_fileName;
254 
255  // Look for unambiguous mime type
257  if (types.size() == 1)
258  return types.front();
259 
260  // Look for an override. QMimeDatabase gets it wrong sometimes when the result
261  // is ambiguous and it resorts to probing. Add to this list as necessary
262  static const std::map<QString,QString> s_mimeOverrides =
263  {
264  { "ts", "video/mp2t"}
265  };
266 
268  for (const auto & type : s_mimeOverrides)
269  if (suffix.compare(type.first, Qt::CaseInsensitive) == 0)
271 
272  // Try interrogating content as well
273  if (data)
274  if (auto mime = MythMimeDatabase::MimeTypeForFileNameAndData(filename, **data); mime.IsValid())
275  return mime;
276  if (file)
277  if (auto mime = MythMimeDatabase::MimeTypeForFileNameAndData(filename, (*file).get()); mime.IsValid())
278  return mime;
279 
280  // Default to text/plain (possibly use application/octet-stream as well?)
281  return MythMimeDatabase::MimeTypeForName("text/plain");
282 }
283 
292 {
293  auto result = HTTPNoEncode;
294  if (!Response || !Response->m_requestHeaders)
295  return result;
296 
297  // We need something to compress/chunk
298  auto * data = std::get_if<HTTPData>(&Response->m_response);
299  auto * file = std::get_if<HTTPFile>(&Response->m_response);
300  if (!(data || file))
301  return result;
302 
303  // Don't touch range requests. They do not work with compression and there
304  // is no point in chunking gzipped content as the client still has to wait
305  // for the entire payload before unzipping
306  // Note: It is permissible to chunk a range request - but ignore for the
307  // timebeing to keep the code simple.
308  if ((data && !(*data)->m_ranges.empty()) || (file && !(*file)->m_ranges.empty()))
309  return result;
310 
311  // Has the client actually requested compression
312  bool wantgzip = MythHTTP::GetHeader(Response->m_requestHeaders, "accept-encoding").toLower().contains("gzip");
313 
314  // Chunking is HTTP/1.1 only - and must be supported
315  bool chunkable = Response->m_version == HTTPOneDotOne;
316 
317  // Has the client requested no chunking by specifying identity?
318  bool allowchunk = ! MythHTTP::GetHeader(Response->m_requestHeaders, "accept-encoding").toLower().contains("identity");
319 
320  // and restrict to 'chunky' files
321  bool chunky = Size > 102400; // 100KB
322 
323  // Don't compress anything that is too large. Under normal circumstances this
324  // should not be a problem as we only compress text based data - but avoid
325  // potentially memory hungry compression operations.
326  // On the flip side, don't compress trivial amounts of data
327  bool gzipsize = Size > 512 && !chunky; // 0.5KB <-> 100KB
328 
329  // Only consider compressing text based content. No point in compressing audio,
330  // video and images.
331  bool compressable = (data ? (*data)->m_mimeType : (*file)->m_mimeType).Inherits("text/plain");
332 
333  // Decision time
334  bool gzip = wantgzip && gzipsize && compressable;
335  bool chunk = chunkable && chunky && allowchunk;
336 
337  if (!gzip)
338  {
339  // Chunking happens as we write to the socket, so flag it as required
340  if (chunk)
341  {
342  result = HTTPChunked;
343  if (data) (*data)->m_encoding = result;
344  if (file) (*file)->m_encoding = result;
345  }
346  return result;
347  }
348 
349  // As far as I can tell, Qt's implicit sharing of data should ensure we aren't
350  // copying data unnecessarily here - but I can't be sure. We could definitely
351  // improve compressing files by avoiding the copy into a temporary buffer.
352  HTTPData buffer = MythHTTPData::Create(data ? gzipCompress(**data) : gzipCompress((*file)->readAll()));
353 
354  // Add the required header
355  Response->AddHeader("Content-Encoding", "gzip");
356 
357  LOG(VB_HTTP, LOG_INFO, LOC + QString("'%1' compressed from %2 to %3 bytes")
358  .arg(data ? (*data)->m_fileName : (*file)->fileName())
359  .arg(Size).arg(buffer->size()));
360 
361  // Copy the filename and last modified, set the new buffer and set the content size
362  buffer->m_lastModified = data ? (*data)->m_lastModified : (*file)->m_lastModified;
363  buffer->m_etag = data ? (*data)->m_etag : (*file)->m_etag;
364  buffer->m_fileName = data ? (*data)->m_fileName : (*file)->m_fileName;
365  buffer->m_cacheType = data ? (*data)->m_cacheType : (*file)->m_cacheType;
366  buffer->m_encoding = HTTPGzip;
367  Response->m_response = buffer;
368  Size = buffer->size();
369  return HTTPGzip;
370 }
HTTPChunked
@ HTTPChunked
Definition: mythhttptypes.h:138
MythHTTPRequest
Limited parsing of HTTP method and some headers to determine validity of request.
Definition: mythhttprequest.h:12
unziputil.h
HTTPVariant
std::variant< std::monostate, HTTPData, HTTPFile > HTTPVariant
Definition: mythhttptypes.h:41
MythHTTP::GetHeader
static QString GetHeader(const HTTPHeaders &Headers, const QString &Value, const QString &Default="")
Definition: mythhttptypes.h:317
MythMimeDatabase::MimeTypesForFileName
static MythMimeTypes MimeTypesForFileName(const QString &FileName)
Return a vector of mime types that match the given filename.
Definition: mythmimedatabase.cpp:175
MythHTTPResponse
Definition: mythhttpresponse.h:16
types
static const struct wl_interface * types[]
Definition: idle_inhibit_unstable_v1.c:39
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
build_compdb.file
file
Definition: build_compdb.py:55
MythHTTPEncoding::GetContentType
static void GetContentType(MythHTTPRequest *Request)
Parse the incoming Content-Type header for POST/PUT content.
Definition: mythhttpencoding.cpp:74
MythHTTPResponse::m_version
MythHTTPVersion m_version
Definition: mythhttpresponse.h:49
mythhttpdata.h
HTTPData
std::shared_ptr< MythHTTPData > HTTPData
Definition: mythhttptypes.h:36
mythhttpfile.h
MythHTTPEncode
MythHTTPEncode
Definition: mythhttptypes.h:134
mythlogging.h
MythHTTPEncoding::GetMimeType
static MythMimeType GetMimeType(HTTPVariant Content)
Return a QMimeType that represents Content.
Definition: mythhttpencoding.cpp:242
MythHTTPData::Create
static HTTPData Create()
Definition: mythhttpdata.cpp:4
MythHTTPResponse::m_response
HTTPVariant m_response
Definition: mythhttpresponse.h:57
MythHTTPEncoding::GetURLEncodedParameters
static void GetURLEncodedParameters(MythHTTPRequest *Request)
Definition: mythhttpencoding.cpp:105
gzipCompress
QByteArray gzipCompress(const QByteArray &data)
Definition: unziputil.cpp:93
mythhttpencoding.h
MythMimeDatabase::SuffixForFileName
static QString SuffixForFileName(const QString &FileName)
Return the preferred suffix for the given filename.
Definition: mythmimedatabase.cpp:187
mythhttpresponse.h
MimePair
std::pair< float, QString > MimePair
Parse the incoming HTTP 'Accept' header and return an ordered list of preferences.
Definition: mythhttpencoding.cpp:29
mythmimedatabase.h
LOC
#define LOC
Definition: mythhttpencoding.cpp:16
MythMimeDatabase::MimeTypeForName
static MythMimeType MimeTypeForName(const QString &Name)
Return a mime type that matches the given name.
Definition: mythmimedatabase.cpp:201
HTTPOneDotOne
@ HTTPOneDotOne
Definition: mythhttptypes.h:86
hardwareprofile.distros.mythtv_data.request.Request
def Request(url=None)
Definition: request.py:62
MythHTTPEncoding::GetJSONEncodedParameters
static void GetJSONEncodedParameters(MythHTTPRequest *Request)
Definition: mythhttpencoding.cpp:192
MythHTTPEncoding::GetXMLEncodedParameters
static void GetXMLEncodedParameters(MythHTTPRequest *Request)
Definition: mythhttpencoding.cpp:134
MythMimeType::IsValid
bool IsValid() const
Definition: mythmimetype.cpp:26
MythMimeType
Definition: mythmimetype.h:16
HTTPNoEncode
@ HTTPNoEncode
Definition: mythhttptypes.h:136
MythHTTPEncoding::Compress
static MythHTTPEncode Compress(MythHTTPResponse *Response, int64_t &Size)
Compress the response content under certain circumstances or mark the content as 'chunkable'.
Definition: mythhttpencoding.cpp:291
MythHTTPResponse::m_requestHeaders
HTTPHeaders m_requestHeaders
Definition: mythhttpresponse.h:55
HTTPGzip
@ HTTPGzip
Definition: mythhttptypes.h:137
MythHTTPResponse::AddHeader
std::enable_if_t< std::is_convertible_v< T, QString >, void > AddHeader(const QString &key, const T &val)
Definition: mythhttpresponse.h:35
MythHTTPEncoding::GetMimeTypes
static QStringList GetMimeTypes(const QString &Accept)
Definition: mythhttpencoding.cpp:30
build_compdb.filename
filename
Definition: build_compdb.py:21
MythMimeDatabase::MimeTypeForFileNameAndData
static MythMimeType MimeTypeForFileNameAndData(const QString &FileName, const QByteArray &Data)
Return a mime type for the given FileName and data.
Definition: mythmimedatabase.cpp:217
Content
Definition: content.h:36