Qt 4.8
qdataurl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qplatformdefs.h"
43 #include "qurl.h"
44 #include "private/qdataurl_p.h"
45 
47 
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 }
103 
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.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
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 QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
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.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
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
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
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...
Q_CORE_EXPORT QPair< QString, QByteArray > qDecodeDataUrl(const QUrl &uri)
Decode a data: URL into its mimetype and payload.
Definition: qdataurl.cpp:57
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...