Qt 4.8
Functions
qdataurl.cpp File Reference
#include "qplatformdefs.h"
#include "qurl.h"
#include "private/qdataurl_p.h"

Go to the source code of this file.

Functions

Q_CORE_EXPORT QPair< QString, QByteArrayqDecodeDataUrl (const QUrl &uri)
 Decode a data: URL into its mimetype and payload. More...
 

Function Documentation

◆ qDecodeDataUrl()

Q_CORE_EXPORT QPair<QString, QByteArray> qDecodeDataUrl ( const QUrl uri)

Decode a data: URL into its mimetype and payload.

Warning
This function is not part of the public interface.

Returns a null string if the URL could not be decoded.

Definition at line 57 of file qdataurl.cpp.

Referenced by QTextDocument::loadResource(), and QNetworkReplyDataImpl::QNetworkReplyDataImpl().

58 {
59  QString mimeType;
60  QByteArray payload;
61 
62  if (uri.scheme() == QLatin1String("data") && uri.host().isEmpty()) {
63  mimeType = QLatin1String("text/plain;charset=US-ASCII");
64 
65  // the following would have been the correct thing, but
66  // reality often differs from the specification. People have
67  // data: URIs with ? and #
68  //QByteArray data = QByteArray::fromPercentEncoding(uri.encodedPath());
70 
71  // remove the data: scheme
72  data.remove(0, 5);
73 
74  // parse it:
75  int pos = data.indexOf(',');
76  if (pos != -1) {
77  payload = data.mid(pos + 1);
78  data.truncate(pos);
79  data = data.trimmed();
80 
81  // find out if the payload is encoded in Base64
82  if (data.endsWith(";base64")) {
83  payload = QByteArray::fromBase64(payload);
84  data.chop(7);
85  }
86 
87  if (data.toLower().startsWith("charset")) {
88  int i = 7; // strlen("charset")
89  while (data.at(i) == ' ')
90  ++i;
91  if (data.at(i) == '=')
92  data.prepend("text/plain;");
93  }
94 
95  if (!data.isEmpty())
96  mimeType = QLatin1String(data.trimmed());
97 
98  }
99  }
100 
101  return QPair<QString,QByteArray>(mimeType,payload);
102 }
void chop(int n)
Removes n bytes from the end of the byte array.
void truncate(int pos)
Truncates the byte array at index position pos.
static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent='%')
Returns a decoded copy of the URI/URL-style percent-encoded input.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QByteArray toLower() const
Returns a lowercase copy of the byte array.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QByteArray & prepend(char c)
Prepends the character ch to this byte array.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString host() const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4837
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QByteArray trimmed() const
Returns a byte array that has whitespace removed from the start and the end.
static const char * data(const QByteArray &arr)
QByteArray mid(int index, int len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos...
int indexOf(char c, int from=0) const
Returns the index position of the first occurrence of the character ch in the byte array...
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
static QByteArray fromBase64(const QByteArray &base64)
Returns a decoded copy of the Base64 array base64.
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
QByteArray & remove(int index, int len)
Removes len bytes from the array, starting at index position pos, and returns a reference to the arra...
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...