Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement XEP-0363: HTTP File Upload: Request IQs
This implements the IQs for requesting and receiving upload slots as defined by XEP-0363: HTTP File Upload [v0.9.0].
- Loading branch information
Showing
8 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright (C) 2008-2019 The QXmpp developers | ||
* | ||
* Authors: | ||
* Linus Jahn <lnj@kaidan.im> | ||
* | ||
* Source: | ||
* https://github.com/qxmpp-project/qxmpp | ||
* | ||
* This file is a part of QXmpp library. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
*/ | ||
|
||
#include <QDomElement> | ||
#include <QMimeDatabase> | ||
|
||
#include "QXmppHttpUploadIq.h" | ||
#include "QXmppConstants_p.h" | ||
|
||
QString QXmppHttpUploadRequestIq::fileName() const | ||
{ | ||
return m_fileName; | ||
} | ||
|
||
void QXmppHttpUploadRequestIq::setFileName(const QString &fileName) | ||
{ | ||
m_fileName = fileName; | ||
} | ||
|
||
qint64 QXmppHttpUploadRequestIq::size() const | ||
{ | ||
return m_size; | ||
} | ||
|
||
void QXmppHttpUploadRequestIq::setSize(const qint64 &size) | ||
{ | ||
m_size = size; | ||
} | ||
|
||
QMimeType QXmppHttpUploadRequestIq::contentType() const | ||
{ | ||
return m_contentType; | ||
} | ||
|
||
void QXmppHttpUploadRequestIq::setContentType(const QMimeType &type) | ||
{ | ||
m_contentType = type; | ||
} | ||
|
||
bool QXmppHttpUploadRequestIq::isHttpUploadRequestIq(const QDomElement &element) | ||
{ | ||
if (element.tagName() == "iq") { | ||
QDomElement request = element.firstChildElement("request"); | ||
return !request.isNull() && request.namespaceURI() == ns_http_upload; | ||
} | ||
return false; | ||
} | ||
|
||
/// \cond | ||
void QXmppHttpUploadRequestIq::parseElementFromChild(const QDomElement &element) | ||
{ | ||
QDomElement request = element.firstChildElement("request"); | ||
m_fileName = request.attribute("filename"); | ||
m_size = request.attribute("size").toLongLong(); | ||
if (request.hasAttribute("content-type")) { | ||
QMimeDatabase mimeDb; | ||
QMimeType type = mimeDb.mimeTypeForName(request.attribute("content-type")); | ||
if (!type.isDefault() && type.isValid()) | ||
m_contentType = type; | ||
} | ||
} | ||
|
||
void QXmppHttpUploadRequestIq::toXmlElementFromChild(QXmlStreamWriter *writer) const | ||
{ | ||
writer->writeStartElement("request"); | ||
writer->writeAttribute("xmlns", ns_http_upload); | ||
// filename and size are required | ||
writer->writeAttribute("filename", m_fileName); | ||
writer->writeAttribute("size", QString::number(m_size)); | ||
// content-type is optional | ||
if (!m_contentType.isDefault() && m_contentType.isValid()) | ||
writer->writeAttribute("content-type", m_contentType.name()); | ||
writer->writeEndElement(); | ||
} | ||
/// \endcond | ||
|
||
QUrl QXmppHttpUploadSlotIq::putUrl() const | ||
{ | ||
return m_putUrl; | ||
} | ||
|
||
void QXmppHttpUploadSlotIq::setPutUrl(const QUrl &putUrl) | ||
{ | ||
m_putUrl = putUrl; | ||
} | ||
|
||
QUrl QXmppHttpUploadSlotIq::getUrl() const | ||
{ | ||
return m_getUrl; | ||
} | ||
|
||
void QXmppHttpUploadSlotIq::setGetUrl(const QUrl &getUrl) | ||
{ | ||
m_getUrl = getUrl; | ||
} | ||
|
||
QMap<QString, QString> QXmppHttpUploadSlotIq::headerFields() const | ||
{ | ||
return m_headerFields; | ||
} | ||
|
||
void QXmppHttpUploadSlotIq::setHeaderFields(const QMap<QString, QString> &headerFields) | ||
{ | ||
m_headerFields.clear(); | ||
for (QString &name : headerFields.keys()) { | ||
if (name == "Authorization" || name == "Cookie" || name == "Expires") { | ||
QString value = headerFields[name]; | ||
m_headerFields[name] = value.replace("\n", ""); | ||
} | ||
} | ||
} | ||
|
||
/// Returns whether the URLs (put and get) are both HTTPS URLs. It will return | ||
/// false, if the URLs are non-HTTPS URLs or not set. HTTP URLs are not | ||
/// XEP-compliant, but are used in some cases. | ||
|
||
bool QXmppHttpUploadSlotIq::hasHttpsUrls() const | ||
{ | ||
return m_getUrl.toString().startsWith("https://") && | ||
m_putUrl.toString().startsWith("https://"); | ||
} | ||
|
||
bool QXmppHttpUploadSlotIq::isHttpUploadSlotIq(const QDomElement &element) | ||
{ | ||
if (element.tagName() == "iq") { | ||
QDomElement slot = element.firstChildElement("slot"); | ||
return !slot.isNull() && slot.namespaceURI() == ns_http_upload; | ||
} | ||
return false; | ||
} | ||
|
||
/// \cond | ||
void QXmppHttpUploadSlotIq::parseElementFromChild(const QDomElement &element) | ||
{ | ||
QDomElement slot = element.firstChildElement("slot"); | ||
QDomElement put = slot.firstChildElement("put"); | ||
m_getUrl = QUrl::fromEncoded(slot.firstChildElement("get").attribute("url").toUtf8()); | ||
m_putUrl = QUrl::fromEncoded(put.attribute("url").toUtf8()); | ||
if (put.hasChildNodes()) { | ||
QMap<QString, QString> headers; | ||
QDomElement header = put.firstChildElement("header"); | ||
while (!header.isNull()) { | ||
headers[header.attribute("name")] = header.text(); | ||
|
||
header = header.nextSiblingElement("header"); | ||
} | ||
|
||
setHeaderFields(headers); | ||
} | ||
} | ||
|
||
void QXmppHttpUploadSlotIq::toXmlElementFromChild(QXmlStreamWriter *writer) const | ||
{ | ||
writer->writeStartElement("slot"); | ||
writer->writeAttribute("xmlns", ns_http_upload); | ||
|
||
writer->writeStartElement("put"); | ||
writer->writeAttribute("url", m_putUrl.toEncoded()); | ||
if (!m_headerFields.isEmpty()) { | ||
for (const QString &name : m_headerFields.keys()) { | ||
writer->writeStartElement("header"); | ||
writer->writeAttribute("name", name); | ||
writer->writeCharacters(m_headerFields[name]); | ||
writer->writeEndElement(); | ||
} | ||
} | ||
writer->writeEndElement(); | ||
|
||
writer->writeStartElement("get"); | ||
writer->writeAttribute("url", m_getUrl.toEncoded()); | ||
writer->writeEndElement(); | ||
|
||
writer->writeEndElement(); | ||
} | ||
/// \endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2008-2019 The QXmpp developers | ||
* | ||
* Authors: | ||
* Linus Jahn <lnj@kaidan.im> | ||
* | ||
* Source: | ||
* https://github.com/qxmpp-project/qxmpp | ||
* | ||
* This file is a part of QXmpp library. | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
*/ | ||
|
||
#ifndef QXMPPHTTPUPLOADIQ_H | ||
#define QXMPPHTTPUPLOADIQ_H | ||
|
||
#include <QMap> | ||
#include <QMimeType> | ||
#include <QUrl> | ||
|
||
#include "QXmppIq.h" | ||
|
||
/// \brief Represents an HTTP File Upload IQ for requesting an upload slot as | ||
/// defined by XEP-0363: HTTP File Upload [v0.9.0]. | ||
/// | ||
/// \ingroup Stanzas | ||
|
||
class QXMPP_EXPORT QXmppHttpUploadRequestIq : public QXmppIq | ||
{ | ||
public: | ||
QString fileName() const; | ||
void setFileName(const QString &filename); | ||
|
||
qint64 size() const; | ||
void setSize(const qint64 &size); | ||
|
||
QMimeType contentType() const; | ||
void setContentType(const QMimeType &type); | ||
|
||
static bool isHttpUploadRequestIq(const QDomElement &element); | ||
|
||
protected: | ||
/// \cond | ||
void parseElementFromChild(const QDomElement &element); | ||
void toXmlElementFromChild(QXmlStreamWriter *writer) const; | ||
/// \endcond | ||
|
||
private: | ||
QString m_fileName; | ||
qint64 m_size; | ||
QMimeType m_contentType; | ||
}; | ||
|
||
/// \brief Represents an HTTP File Upload IQ result for receiving an upload slot as | ||
/// defined by XEP-0363: HTTP File Upload [v0.9.0]. | ||
/// | ||
/// \ingroup Stanzas | ||
|
||
class QXMPP_EXPORT QXmppHttpUploadSlotIq : public QXmppIq | ||
{ | ||
public: | ||
QUrl putUrl() const; | ||
void setPutUrl(const QUrl &putUrl); | ||
|
||
QUrl getUrl() const; | ||
void setGetUrl(const QUrl &getUrl); | ||
|
||
QMap<QString, QString> headerFields() const; | ||
void setHeaderFields(const QMap<QString, QString> &headerFields); | ||
|
||
bool hasHttpsUrls() const; | ||
|
||
static bool isHttpUploadSlotIq(const QDomElement &element); | ||
|
||
protected: | ||
/// \cond | ||
void parseElementFromChild(const QDomElement &element); | ||
void toXmlElementFromChild(QXmlStreamWriter *writer) const; | ||
/// \endcond | ||
|
||
private: | ||
QUrl m_putUrl; | ||
QUrl m_getUrl; | ||
QMap<QString, QString> m_headerFields; | ||
}; | ||
|
||
#endif // QXMPPHTTPUPLOADIQ_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.