Uri.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef UTILS__URI_H
00024 #define UTILS__URI_H
00025 
00026 namespace Utils {
00027 
00029 
00082 class Uri
00083 {
00084 public:
00085     VALUE_OBJECT (Uri)
00086 
00087 public:
00088     typedef QPair <QByteArray, QByteArray>  QueryItem;
00089     typedef QList <QueryItem>               QueryItemList;
00090 
00091     enum Field
00092     {
00093         Scheme          = 0x01,
00094         UserInfo        = 0x02,
00095         Host            = 0x04,
00096         Port            = 0x08,
00097         Authority       = UserInfo | Host | Port,
00098         Path            = 0x10,
00099         Query           = 0x20,
00100         Fragment        = 0x40,
00101         AllFields       = Scheme | Authority | Path | Query | Fragment
00102     };
00103     Q_DECLARE_FLAGS (Fields, Field);
00104 
00105                         Uri();
00106     QByteArray          scheme() const;
00107     QByteArray          userInfo() const;
00108     QByteArray          host() const;
00109     QByteArray          port() const;
00110     QByteArray          authority() const;
00111     QByteArray          path() const;
00112     QByteArray          query() const;
00113     QByteArray          queryItemValue (const QByteArray &key) const;
00114     QueryItemList       queryItems() const;
00115     QByteArray          fragment() const;
00116 
00117     QByteArray          unencoded (Fields fields = AllFields) const;
00118     QByteArray          encoded (Fields fields = AllFields) const;
00119 
00120     void                setScheme (const QByteArray &);
00121     void                setAuthority (const QByteArray &);
00122     void                setAuthority (const QByteArray &userInfo,
00123                                       const QByteArray &host,
00124                                       const QByteArray &port);
00125     void                setPath (const QByteArray &);
00126     void                setQuery (const QByteArray &);
00127     void                appendQuery (const QByteArray &bytes);
00128     void                appendQueryItem (const QByteArray &key,
00129                                          const QByteArray &value);
00130     void                appendQueryItem (const QueryItem &item);
00131     void                appendQueryItems (const QueryItemList &itemList);
00132     void                setFragment (const QByteArray &);
00133 
00134     static Uri          fromUnencoded (const QByteArray &);
00135     static Uri          fromEncoded (const QByteArray &);
00136 
00137 private:
00138     QByteArray          toQByteArray (Fields fields, bool encode) const;
00139     static QByteArray   doEncode (const QByteArray &bytes, bool encode,
00140                                   const QByteArray &charsToExclude);
00141     static bool         shouldEncode (char ch,
00142                                       const QByteArray &charsToExclude);
00143     static char         hex (char);
00144     static QByteArray   quoted (char);
00145     bool                parse (const QByteArray &bytes, bool decode);
00146     bool                parseAuthority (const QByteArray &authority,
00147                                         bool decode);
00148     static bool         doDecode (QByteArray &bytes, bool decode);
00149     static bool         fromHex (char &);
00150     static char         unquoted (char hex1, char hex2);
00151     static void         ensureNotNull (QByteArray &bytes);
00152 
00153     static const QByteArray     UserInfoEncodeExcludeChars;
00154     static const QByteArray     HostEncodeExcludeChars;
00155     static const QByteArray     PathEncodeExcludeChars;
00156     static const QByteArray     QueryEncodeExcludeChars;
00157     static const QByteArray     QueryItemEncodeExcludeChars;
00158     static const QByteArray     FragmentEncodeExcludeChars;
00159 
00161     struct PrivateData : public QSharedData
00162     {
00163         QByteArray      scheme;
00164         QByteArray      userInfo;
00165         QByteArray      host;
00166         QByteArray      port;
00167         QByteArray      path;
00168         QByteArray      query;
00169         QByteArray      fragment;
00170     };
00171 
00172     QSharedDataPointer <PrivateData> d; 
00173 };
00174 
00175 uint qHash (const Uri &);
00176 
00177 Q_DECLARE_OPERATORS_FOR_FLAGS (Uri::Fields)
00178 
00179 
00180 inline QByteArray Uri::scheme() const
00181 { return d->scheme; }
00182 
00184 inline QByteArray Uri::userInfo() const
00185 { return d->userInfo; }
00186 
00188 inline QByteArray Uri::host() const
00189 { return d->host; }
00190 
00192 inline QByteArray Uri::port() const
00193 { return d->port; }
00194 
00196 inline QByteArray Uri::path() const
00197 { return d->path; }
00198 
00200 inline QByteArray Uri::fragment() const
00201 { return d->fragment; }
00202 
00204 inline void Uri::setScheme (const QByteArray &scheme)
00205 { d->scheme = scheme; }
00206 
00208 inline void Uri::setPath (const QByteArray &path)
00209 { d->path = path; }
00210 
00212 inline void Uri::setFragment (const QByteArray &fragment)
00213 { d->fragment = fragment; }
00214 
00215 } // namespace Utils
00216 
00217 #endif // UTILS__URI_H