13using namespace Qt::Literals::StringLiterals;
17inline QByteArray decodeBasicAuth(
const QByteArray &auth)
20 int pos = auth.indexOf(
"Basic ");
23 ret = auth.mid(pos, auth.indexOf(
',', pos) - pos);
24 ret = QByteArray::fromBase64(ret);
32 const QByteArray authorization = decodeBasicAuth(auth);
33 if (!authorization.isEmpty()) {
34 int pos = authorization.indexOf(
':');
36 ret.user = QString::fromLatin1(authorization);
38 ret.user = QString::fromLatin1(authorization.left(pos));
39 ret.password = QString::fromLatin1(authorization.mid(pos + 1));
45QVector<Headers::HeaderKeyValue>::const_iterator
46 findHeaderConst(
const QVector<Headers::HeaderKeyValue> &headers, QAnyStringView key)
noexcept
49 return QAnyStringView::compare(key, entry.key, Qt::CaseInsensitive) == 0;
56 : m_data(other.m_data)
62 return header(
"Content-Disposition");
77 if (filename.isEmpty()) {
86 return header(
"Content-Encoding");
91 setHeader(
"Content-Encoding"_ba, encoding);
96 QByteArray ret =
header(
"Content-Type");
98 ret = ret.mid(0, ret.indexOf(
';')).toLower();
111 const QByteArray _contentType =
header(
"Content-Type");
112 if (!_contentType.isEmpty()) {
113 int pos = _contentType.indexOf(
"charset=", 0);
115 int endPos = _contentType.indexOf(u
';', pos);
116 ret = _contentType.mid(pos + 8, endPos).trimmed().toUpper();
125 auto result = findHeaderConst(m_data,
"Content-Type");
126 if (result == m_data.end() || (result->value.isEmpty() && !charset.isEmpty())) {
131 QByteArray _contentType = result->value;
132 int pos = _contentType.indexOf(
"charset=", 0);
134 int endPos = _contentType.indexOf(
';', pos);
136 if (charset.isEmpty()) {
137 int lastPos = _contentType.lastIndexOf(
';', pos);
142 _contentType.remove(lastPos, _contentType.length() - lastPos);
145 _contentType.replace(pos + 8, _contentType.length() - pos + 8, charset);
148 _contentType.replace(pos + 8, endPos, charset);
150 }
else if (!charset.isEmpty()) {
151 _contentType.append(
"; charset=" + charset);
158 return header(
"Content-Type").startsWith(
"text/");
164 return ct.compare(
"text/html") == 0 || ct.compare(
"application/xhtml+xml") == 0 ||
165 ct.compare(
"application/vnd.wap.xhtml+xml") == 0;
171 return ct.compare(
"application/xhtml+xml") == 0 ||
172 ct.compare(
"application/vnd.wap.xhtml+xml") == 0;
178 return ct.compare(
"text/xml") == 0 || ct.compare(
"application/xml") == 0 || ct.endsWith(
"xml");
183 auto value =
header(
"Content-Type");
184 if (!value.isEmpty()) {
185 return value.compare(
"application/json") == 0;
192 auto value =
header(
"Content-Length");
193 if (!value.isEmpty()) {
194 return value.toLongLong();
201 setHeader(
"Content-Length"_ba, QByteArray::number(value));
209 QLocale::c().toString(
date.toUTC(), u
"ddd, dd MMM yyyy hh:mm:ss 'GMT").toLatin1();
217 auto value =
header(
"Date");
218 if (!value.isEmpty()) {
219 if (value.endsWith(
" GMT")) {
220 ret = QLocale::c().toDateTime(QString::fromLatin1(value.left(value.size() - 4)),
221 u
"ddd, dd MMM yyyy hh:mm:ss"_s);
224 QLocale::c().toDateTime(QString::fromLatin1(value), u
"ddd, dd MMM yyyy hh:mm:ss"_s);
226 ret.setTimeSpec(Qt::UTC);
234 return header(
"If-Modified-Since");
240 auto value =
header(
"If-Modified-Since");
241 if (!value.isEmpty()) {
242 if (value.endsWith(
" GMT")) {
243 ret = QLocale::c().toDateTime(QString::fromLatin1(value.left(value.size() - 4)),
244 u
"ddd, dd MMM yyyy hh:mm:ss"_s);
247 QLocale::c().toDateTime(QString::fromLatin1(value), u
"ddd, dd MMM yyyy hh:mm:ss"_s);
249 ret.setTimeSpec(Qt::UTC);
257 auto value =
header(
"If-Modified-Since");
258 if (!value.isEmpty()) {
259 return value != QLocale::c()
260 .toString(
lastModified.toUTC(), u
"ddd, dd MMM yyyy hh:mm:ss 'GMT")
268 auto value =
header(
"If-Match");
269 if (!value.isEmpty()) {
270 const auto clientETag = QByteArrayView(value);
271 return clientETag.sliced(1, clientETag.size() - 2) == etag ||
272 clientETag.sliced(3, clientETag.size() - 4) == etag;
279 auto value =
header(
"If-None-Match");
280 if (!value.isEmpty()) {
281 const auto clientETag = QByteArrayView(value);
282 return clientETag.sliced(1, clientETag.size() - 2) == etag ||
283 clientETag.sliced(3, clientETag.size() - 4) == etag;
295 return header(
"Last-Modified");
307 auto dt = QLocale::c().toString(
lastModified.toUTC(), u
"ddd, dd MMM yyyy hh:mm:ss 'GMT");
324 return header(
"Connection");
334 return header(
"User-Agent");
344 int fragmentPos = uri.indexOf(
'#');
345 if (fragmentPos != -1) {
347 setHeader(
"Referer"_ba, uri.mid(0, fragmentPos));
360 setHeader(
"Proxy-Authenticate"_ba, value);
365 return header(
"Authorization");
372 int pos = auth.indexOf(
"Bearer ");
375 ret = auth.mid(pos, auth.indexOf(
',', pos) - pos);
393 if (username.contains(u
':')) {
394 qCWarning(CUTELYST_CORE) <<
"Headers::Basic authorization user name can't contain ':'";
398 const QString result = username + u
':' + password;
399 ret =
"Basic " + result.toLatin1().toBase64();
406 return header(
"Proxy-Authorization");
421 if (
auto result = findHeaderConst(m_data, key); result != m_data.end()) {
422 return result->value;
429 return QString::fromLatin1(
header(key));
432QByteArray
Headers::header(QAnyStringView key,
const QByteArray &defaultValue)
const noexcept
434 if (
auto result = findHeaderConst(m_data, key); result != m_data.end()) {
435 return result->value;
442 if (
auto result = findHeaderConst(m_data, key); result != m_data.end()) {
443 return QString::fromLatin1(result->value);
451 for (
auto result = findHeaderConst(m_data, key); result != m_data.end(); ++result) {
452 ret.append(result->value);
460 for (
auto result = findHeaderConst(m_data, key); result != m_data.end(); ++result) {
461 ret.append(QString::fromLatin1(result->value));
469 return key.compare(entry.key, Qt::CaseInsensitive) == 0;
472 if (
auto result = std::ranges::find_if(m_data, matchKey); result != m_data.end()) {
473 result->value = value;
476 QVector<HeaderKeyValue>::ConstIterator begin =
477 std::remove_if(result, m_data.end(), matchKey);
478 m_data.erase(begin, m_data.cend());
502 return QAnyStringView::compare(key, entry.key, Qt::CaseInsensitive) == 0;
508 auto result = findHeaderConst(m_data, key);
509 return result != m_data.end();
512QByteArrayList Headers::keys()
const
516 for (
const auto &_header : m_data) {
517 const bool exists = std::ranges::any_of(ret, [&](
const QByteArray &key) {
518 return _header.key.compare(key, Qt::CaseInsensitive) == 0;
522 ret.append(_header.key);
536 const auto otherData = other.data();
537 if (m_data.size() != otherData.size()) {
541 return std::ranges::all_of(
542 m_data, [otherData](
const auto &myValue) {
return otherData.contains(myValue); });
545QDebug operator<<(QDebug debug,
const Headers &headers)
547 const auto data = headers.
data();
548 const bool oldSetting = debug.autoInsertSpaces();
549 debug.nospace() <<
"Headers[";
550 for (
auto it = data.begin(); it != data.end(); ++it) {
551 debug <<
'(' << it->key +
'=' + it->value <<
')';
554 debug.setAutoInsertSpaces(oldSetting);
555 return debug.maybeSpace();
The Cutelyst namespace holds all public Cutelyst API.