• Skip to content
  • Skip to link menu
KDE 4.2 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 
00263   bool isOffline(const KUrl &url); // Check network status
00264 
00265 protected Q_SLOTS:
00266   void slotData(const QByteArray &);
00267   void error(int errid, const QString &text);
00268   void proxyAuthenticationForSocket(const QNetworkProxy &, QAuthenticator *);
00269   void saveProxyAuthenticationForSocket();
00270 
00271 protected:
00272   int readChunked();    // Read a chunk
00273   int readLimited();    // Read maximum m_iSize bytes.
00274   int readUnlimited();  // Read as much as possible.
00275 
00280   ssize_t write(const void *buf, size_t nbytes);
00281 
00287   void addEncoding(const QString &, QStringList &);
00288 
00289   //void processAuthenticationRequest();
00290 
00291   // The methods between here and sendQuery() are helpers for sendQuery().
00292 
00293   // Return true if the request is already "done", false otherwise.
00294   // *sucesss will be set to true if the page was found, false otherwise.
00295   bool satisfyRequestFromCache(bool *success);
00296   QString formatRequestUri() const;
00297   // create HTTP authentications response(s), if any
00298   QString authenticationHeader();
00299   bool sendQuery();
00300   
00301   void httpClose(bool keepAlive);  // Close transfer
00302   bool httpOpenConnection();   // Open connection
00303   void httpCloseConnection();  // Close connection
00304   bool httpShouldCloseConnection();  // Check whether to keep or close the connection.
00305 
00306   void forwardHttpResponseHeader();
00307 
00308   // Helper for readResponseHeader - fix common mimetype errors by webservers.
00309   void fixupResponseMimetype();
00310   bool readResponseHeader();
00311   bool readHeaderFromCache();
00312   void parseContentDisposition(const QString &disposition);
00313 
00314   bool sendBody();
00315 
00316   // where dataInternal == true, the content is to be made available
00317   // to an internal function.
00318   bool readBody( bool dataInternal = false );
00319 
00323   void davSetRequest( const QByteArray& requestXML );
00324   void davStatList( const KUrl& url, bool stat = true );
00325   void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
00326   void davParseActiveLocks( const QDomNodeList& activeLocks,
00327                             uint& lockCount );
00328 
00332   long parseDateTime( const QString& input, const QString& type );
00333 
00337   int codeFromResponse( const QString& response );
00338 
00343   QString davProcessLocks();
00344 
00348   void addCookies( const QString &url, const QByteArray &cookieHeader);
00349 
00353   QString findCookies( const QString &url);
00354 
00366   gzFile checkCacheEntry(bool readWrite = false);
00367 
00373   void createCacheEntry(const QString &mimetype, time_t expireDate);
00374 
00380   void writeCacheEntry( const char *buffer, int nbytes);
00381 
00385   void closeCacheEntry();
00386 
00390   void updateExpireDate(time_t expireDate, bool updateCreationDate=false);
00391 
00395   void cleanCache();
00396 
00402   // where dataInternal == true, the content is to be made available
00403   // to an internal function.
00404   void proceedUntilResponseContent( bool dataInternal = false );
00405 
00409   bool proceedUntilResponseHeader();
00410 
00414   void resetSessionSettings();
00415 
00419   void resetResponseParsing();
00420 
00427   void resetConnectionSettings();
00428 
00432   QString createNegotiateAuth();
00433 
00437   QByteArray gssError( int major_status, int minor_status );
00438 
00442   void fillPromptInfo(KIO::AuthInfo *info);
00443 
00444 protected:
00445   HTTPServerState m_server;
00446   HTTPRequest m_request;
00447   QList<HTTPRequest> m_requestQueue;
00448   quint16 m_defaultPort;
00449 
00450   // Processing related
00451   KIO::filesize_t m_iSize; // Expected size of message
00452   KIO::filesize_t m_iBytesLeft; // # of bytes left to receive in this message.
00453   KIO::filesize_t m_iContentLeft; // # of content bytes left
00454   QByteArray m_receiveBuf; // Receive buffer
00455   bool m_dataInternal; // Data is for internal consumption
00456   bool m_isChunked; // Chunked transfer encoding
00457 
00458   bool m_isBusy; // Busy handling request queue.
00459   bool m_isEOF;
00460   bool m_isEOD;
00461 
00462   // First request on a connection
00463   bool m_isFirstRequest;
00464 
00465 //--- Settings related to a single response only
00466   bool m_isRedirection; // Indicates current request is a redirection
00467   QStringList m_responseHeaders; // All headers
00468 
00469 
00470   // Language/Encoding related
00471   QStringList m_transferEncodings;
00472   QStringList m_contentEncodings;
00473   QString m_contentMD5;
00474   QString m_mimeType;
00475 
00476 
00477 //--- WebDAV
00478   // Data structure to hold data which will be passed to an internal func.
00479   QByteArray m_webDavDataBuf;
00480   QStringList m_davCapabilities;
00481 
00482   bool m_davHostOk;
00483   bool m_davHostUnsupported;
00484 //----------
00485 
00486   // Mimetype determination
00487   bool m_cpMimeBuffer;
00488   QByteArray m_mimeTypeBuffer;
00489 
00490 
00491   // Holds the POST data so it won't get lost on if we
00492   // happend to get a 401/407 response when submitting
00493   // a form.
00494   QByteArray m_POSTbuf;
00495 
00496   // Cache related
00497   int m_maxCacheAge; // Maximum age of a cache entry.
00498   long m_maxCacheSize; // Maximum cache size in Kb.
00499   QString m_strCacheDir; // Location of the cache.
00500 
00501 
00502 //--- Proxy related members
00503 
00504   // Operation mode
00505   QByteArray m_protocol;
00506 
00507   //TODO use PrevRequest fully
00508   //TODO isAuthValid/m_isUnauthorized -> auth.scheme != AUTH_None ?
00509 #if 0
00510   struct AuthState
00511   {
00512     AUTH_SCHEME scheme;
00513     QString realm;
00514     QString authorization;
00515     int authCount;
00516   };
00517 #endif
00518 
00519   KAbstractHttpAuthentication *m_wwwAuth;
00520   KAbstractHttpAuthentication *m_proxyAuth;
00521   // For proxy auth when it's handled by the Qt/KDE socket classes
00522   QAuthenticator *m_socketProxyAuth;
00523 
00524   // Indicates whether there was some connection error.
00525   bool m_isError;
00526 
00527   // Values that determine the remote connection timeouts.
00528   int m_remoteRespTimeout;
00529 
00530 
00531   QByteArray m_unreadBuf;
00532   void clearUnreadBuffer();
00533   void unread(char *buf, size_t size);
00534   size_t readBuffered(char *buf, size_t size);
00535   bool readDelimitedText(char *buf, int *idx, int end, int numNewlines);
00536 };
00537 #endif

KIOSlave

Skip menu "KIOSlave"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • 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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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