• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIOSlave

http.h

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2000,2001 Dawit Alemayehu <adawit@kde.org>
00003    Copyright (C) 2000,2001 Waldo Bastian <bastian@kde.org>
00004    Copyright (C) 2000,2001 George Staikos <staikos@kde.org>
00005    Copyright (C) 2001,2002 Hamish Rodda <rodda@kde.org>
00006    Copyright (C) 2007      Daniel Nicoletti <mirttex@users.sourceforge.net>
00007    Copyright (C) 2008,2009 Andreas Hartmetz <ahartmetz@gmail.com>
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Library General Public
00011    License as published by the Free Software Foundation; either
00012    version 2 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Library General Public License for more details.
00018 
00019    You should have received a copy of the GNU Library General Public License
00020    along with this library; see the file COPYING.LIB.  If not, write to
00021    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022    Boston, MA 02110-1301, USA.
00023 */
00024 
00025 #ifndef HTTP_H_
00026 #define HTTP_H_
00027 
00028 
00029 #include <sys/types.h>
00030 #include <netinet/in.h>
00031 #include <arpa/inet.h>
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <zlib.h>
00035 #include <time.h>
00036 
00037 #include <QtCore/QByteRef>
00038 #include <QtCore/QList>
00039 #include <QtCore/QStringList>
00040 
00041 #include <kurl.h>
00042 #include "kio/tcpslavebase.h"
00043 #include "kio/http.h"
00044 
00045 // HeaderTokenizer declarations
00046 #include "parsinghelpers.h"
00047 // KHttpAuthentication & KHttpAuthenticationOutcome declarations
00048 #include "httpauthentication.h"
00049 
00050 class QDomNodeList;
00051 
00052 namespace KIO {
00053     class AuthInfo;
00054 }
00055 
00056 class HTTPProtocol : public QObject, public KIO::TCPSlaveBase
00057 {
00058   Q_OBJECT
00059 public:
00060   HTTPProtocol( const QByteArray &protocol, const QByteArray &pool,
00061                 const QByteArray &app );
00062   virtual ~HTTPProtocol();
00063 
00065   enum HTTP_REV    {HTTP_None, HTTP_Unknown, HTTP_10, HTTP_11, SHOUTCAST};
00066 
00068   enum AUTH_SCHEME   {AUTH_None, AUTH_Basic, AUTH_NTLM, AUTH_Digest, AUTH_Negotiate};
00069 
00071   struct DAVRequest
00072   {
00073     DAVRequest ()
00074     {
00075       overwrite = false;
00076       depth = 0;
00077     }
00078 
00079     QString desturl;
00080     bool overwrite;
00081     int depth;
00082   };
00083 
00084   struct CacheTag
00085   {
00086     CacheTag()
00087     {
00088       useCache = false;
00089       readFromCache = false;
00090       writeToCache = false;
00091       isExpired = false;
00092       bytesCached = 0;
00093       gzs = 0;
00094       expireDateOffset = 0;
00095       expireDate = 0;
00096       creationDate = 0;
00097     }
00098 
00099     KIO::CacheControl policy;    // ### initialize in the constructor?
00100     bool useCache; // Whether the cache is active
00101     bool readFromCache; // Whether the request is to be read from file.
00102     bool writeToCache; // Whether the request is to be written to file.
00103     bool isExpired;
00104     long bytesCached;
00105     QString etag; // entity tag header as described in the HTTP standard.
00106     QString file; // cache entry file belonging to this URL.
00107     gzFile gzs; // gzip stream of the cache entry
00108     QString lastModified; // Last modified.
00109     long expireDateOffset; // Position in the cache entry where the
00110                            // 16 byte expire date is stored.
00111     time_t expireDate; // Date when the cache entry will expire
00112     time_t creationDate; // Date when the cache entry was created
00113     QString charset;
00114   };
00115 
00117   struct HTTPRequest
00118   {
00119     HTTPRequest ()
00120     {
00121       method = KIO::HTTP_UNKNOWN;
00122       offset = 0;
00123       endoffset = 0;
00124       allowTransferCompression = false;
00125       disablePassDialog = false;
00126       doNotAuthenticate = false;
00127       preferErrorPage = false;
00128       useCookieJar = false;
00129     }
00130 
00131     KUrl url;
00132     QString encoded_hostname; //### can be calculated on-the-fly
00133     // Persistent connections
00134     bool isKeepAlive;
00135     int keepAliveTimeout;   // Timeout in seconds.
00136 
00137     KIO::HTTP_METHOD method;
00138     KIO::filesize_t offset;
00139     KIO::filesize_t endoffset;
00140     QString windowId;                 // Window Id this request is related to.
00141     // Header fields
00142     QString referrer;
00143     QString charsets;
00144     QString languages;
00145     QString userAgent;
00146     // Previous and current response codes
00147     unsigned int responseCode;
00148     unsigned int prevResponseCode;
00149     // Miscellaneous
00150     QString id;
00151     DAVRequest davData;
00152     KUrl proxyUrl;
00153     bool isPersistentProxyConnection;
00154     bool allowTransferCompression;
00155     bool disablePassDialog;
00156     bool doNotAuthenticate;
00157     // Indicates whether an error page or error message is preferred.
00158     bool preferErrorPage;
00159 
00160     // Use the cookie jar (or pass cookies to the application as metadata instead)
00161     bool useCookieJar;
00162     // Cookie flags
00163     enum { CookiesAuto, CookiesManual, CookiesNone } cookieMode;
00164 
00165     CacheTag cacheTag;
00166   };
00167 
00169   struct HTTPServerState
00170   {
00171     HTTPServerState()
00172     {
00173       isKeepAlive = false;
00174       isPersistentProxyConnection = false;
00175     }
00176 
00177     void initFrom(const HTTPRequest &request)
00178     {
00179       url = request.url;
00180       encoded_hostname = request.encoded_hostname;
00181       isKeepAlive = request.isKeepAlive;
00182       proxyUrl = request.proxyUrl;
00183       isPersistentProxyConnection = request.isPersistentProxyConnection;
00184     }
00185 
00186     void clear()
00187     {
00188       url.clear();
00189       encoded_hostname.clear();
00190       proxyUrl.clear();
00191       isKeepAlive = false;
00192       isPersistentProxyConnection = false;
00193     }
00194 
00195     KUrl url;
00196     QString encoded_hostname;
00197     KUrl proxyUrl;
00198     bool isKeepAlive;
00199     bool isPersistentProxyConnection;
00200   };
00201 
00202 //---------------------- Re-implemented methods ----------------
00203   virtual void setHost(const QString& host, quint16 port, const QString& user,
00204                        const QString& pass);
00205 
00206   virtual void slave_status();
00207 
00208   virtual void get( const KUrl& url );
00209   virtual void put( const KUrl& url, int _mode, KIO::JobFlags flags );
00210 
00211 //----------------- Re-implemented methods for WebDAV -----------
00212   virtual void listDir( const KUrl& url );
00213   virtual void mkdir( const KUrl& url, int _permissions );
00214 
00215   virtual void rename( const KUrl& src, const KUrl& dest, KIO::JobFlags flags );
00216   virtual void copy( const KUrl& src, const KUrl& dest, int _permissions, KIO::JobFlags flags );
00217   virtual void del( const KUrl& url, bool _isfile );
00218 
00219   // ask the host whether it supports WebDAV & cache this info
00220   bool davHostOk();
00221 
00222   // send generic DAV request
00223   void davGeneric( const KUrl& url, KIO::HTTP_METHOD method );
00224 
00225   // Send requests to lock and unlock resources
00226   void davLock( const KUrl& url, const QString& scope,
00227                 const QString& type, const QString& owner );
00228   void davUnlock( const KUrl& url );
00229 
00230   // Calls httpClose() and finished()
00231   void davFinished();
00232 
00233   // Handle error conditions
00234   QString davError( int code = -1, const QString &url = QString() );
00235 //---------------------------- End WebDAV -----------------------
00236 
00246   virtual void special( const QByteArray &data );
00247 
00248   virtual void mimetype( const KUrl& url);
00249 
00250   virtual void stat( const KUrl& url );
00251 
00252   virtual void reparseConfiguration();
00253 
00254   virtual void closeConnection(); // Forced close of connection
00255 
00256   void post( const KUrl& url );
00257   void multiGet(const QByteArray &data);
00258   bool maybeSetRequestUrl(const KUrl &);
00259   void cacheUpdate( const KUrl &url, bool nocache, time_t expireDate);
00260 
00261   void httpError(); // Generate error message based on response code
00262   void setLoadingErrorPage(); // Call SlaveBase::errorPage() and remember that we've called it
00263 
00264   bool isOffline(const KUrl &url); // Check network status
00265 
00266 protected Q_SLOTS:
00267   void slotData(const QByteArray &);
00268   void slotFilterError(const QString &text);
00269   void error(int errid, const QString &text);
00270   void proxyAuthenticationForSocket(const QNetworkProxy &, QAuthenticator *);
00271   void saveProxyAuthenticationForSocket();
00272 
00273 protected:
00274   int readChunked();    // Read a chunk
00275   int readLimited();    // Read maximum m_iSize bytes.
00276   int readUnlimited();  // Read as much as possible.
00277 
00282   ssize_t write(const void *buf, size_t nbytes);
00283 
00289   void addEncoding(const QString &, QStringList &);
00290 
00291   quint16 defaultPort() const;
00292 
00293   // The methods between here and sendQuery() are helpers for sendQuery().
00294 
00295   // Return true if the request is already "done", false otherwise.
00296   // *sucesss will be set to true if the page was found, false otherwise.
00297   bool satisfyRequestFromCache(bool *success);
00298   QString formatRequestUri() const;
00299   // create HTTP authentications response(s), if any
00300   QString authenticationHeader();
00301   bool sendQuery();
00302 
00303   void httpClose(bool keepAlive);  // Close transfer
00304   bool httpOpenConnection();   // Open connection
00305   void httpCloseConnection();  // Close connection
00306   bool httpShouldCloseConnection();  // Check whether to keep or close the connection.
00307 
00308   void forwardHttpResponseHeader();
00309 
00310   // Helper for readResponseHeader - fix common mimetype errors by webservers.
00311   void fixupResponseMimetype();
00312   bool readResponseHeader();
00313   bool readHeaderFromCache();
00314   void parseContentDisposition(const QString &disposition);
00315 
00316   bool sendBody();
00317 
00318   // where dataInternal == true, the content is to be made available
00319   // to an internal function.
00320   bool readBody( bool dataInternal = false );
00321 
00325   void davSetRequest( const QByteArray& requestXML );
00326   void davStatList( const KUrl& url, bool stat = true );
00327   void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
00328   void davParseActiveLocks( const QDomNodeList& activeLocks,
00329                             uint& lockCount );
00330 
00334   long parseDateTime( const QString& input, const QString& type );
00335 
00339   int codeFromResponse( const QString& response );
00340 
00345   QString davProcessLocks();
00346 
00350   void addCookies( const QString &url, const QByteArray &cookieHeader);
00351 
00355   QString findCookies( const QString &url);
00356 
00368   gzFile checkCacheEntry(bool readWrite = false);
00369 
00375   void createCacheEntry(const QString &mimetype, time_t expireDate);
00376 
00382   void writeCacheEntry( const char *buffer, int nbytes);
00383 
00387   void closeCacheEntry();
00388 
00392   void updateExpireDate(time_t expireDate, bool updateCreationDate=false);
00393 
00397   void cleanCache();
00398 
00404   // where dataInternal == true, the content is to be made available
00405   // to an internal function.
00406   void proceedUntilResponseContent( bool dataInternal = false );
00407 
00411   bool proceedUntilResponseHeader();
00412 
00416   void resetSessionSettings();
00417 
00421   void resetResponseParsing();
00422 
00429   void resetConnectionSettings();
00430 
00434   QString createNegotiateAuth();
00435 
00439   QByteArray gssError( int major_status, int minor_status );
00440 
00444   void fillPromptInfo(KIO::AuthInfo *info);
00445 
00446 protected:
00447   HTTPServerState m_server;
00448   HTTPRequest m_request;
00449   QList<HTTPRequest> m_requestQueue;
00450 
00451   // Processing related
00452   KIO::filesize_t m_iSize; // Expected size of message
00453   KIO::filesize_t m_iBytesLeft; // # of bytes left to receive in this message.
00454   KIO::filesize_t m_iContentLeft; // # of content bytes left
00455   QByteArray m_receiveBuf; // Receive buffer
00456   bool m_dataInternal; // Data is for internal consumption
00457   bool m_isChunked; // Chunked transfer encoding
00458 
00459   bool m_isBusy; // Busy handling request queue.
00460   bool m_isEOF;
00461   bool m_isEOD;
00462 
00463   // First request on a connection
00464   bool m_isFirstRequest;
00465 
00466 //--- Settings related to a single response only
00467   bool m_isRedirection; // Indicates current request is a redirection
00468   QStringList m_responseHeaders; // All headers
00469 
00470 
00471   // Language/Encoding related
00472   QStringList m_transferEncodings;
00473   QStringList m_contentEncodings;
00474   QString m_contentMD5;
00475   QString m_mimeType;
00476 
00477 
00478 //--- WebDAV
00479   // Data structure to hold data which will be passed to an internal func.
00480   QByteArray m_webDavDataBuf;
00481   QStringList m_davCapabilities;
00482 
00483   bool m_davHostOk;
00484   bool m_davHostUnsupported;
00485 //----------
00486 
00487   // Mimetype determination
00488   bool m_cpMimeBuffer;
00489   QByteArray m_mimeTypeBuffer;
00490 
00491 
00492   // Holds the POST data so it won't get lost on if we
00493   // happend to get a 401/407 response when submitting
00494   // a form.
00495   QByteArray m_POSTbuf;
00496 
00497   // Cache related
00498   int m_maxCacheAge; // Maximum age of a cache entry.
00499   long m_maxCacheSize; // Maximum cache size in Kb.
00500   QString m_strCacheDir; // Location of the cache.
00501 
00502   // Operation mode
00503   QByteArray m_protocol;
00504 
00505   KAbstractHttpAuthentication *m_wwwAuth;
00506   KAbstractHttpAuthentication *m_proxyAuth;
00507   // For proxy auth when it's handled by the Qt/KDE socket classes
00508   QAuthenticator *m_socketProxyAuth;
00509 
00510   // Indicates whether there was some connection error.
00511   bool m_isError;
00512   // Whether we are loading an error page (we should close the connection afterwards)
00513   bool m_isLoadingErrorPage;
00514 
00515   // Values that determine the remote connection timeouts.
00516   int m_remoteRespTimeout;
00517 
00518   QByteArray m_unreadBuf;
00519   void clearUnreadBuffer();
00520   void unread(char *buf, size_t size);
00521   size_t readBuffered(char *buf, size_t size);
00522   bool readDelimitedText(char *buf, int *idx, int end, int numNewlines);
00523 };
00524 #endif

KIOSlave

Skip menu "KIOSlave"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal