00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 class QAuthenticator;
00046 class QDomNodeList;
00047 class QNetworkProxy;
00048
00049 namespace KIO {
00050 class AuthInfo;
00051 }
00052
00053 class HTTPProtocol : public QObject, public KIO::TCPSlaveBase
00054 {
00055 Q_OBJECT
00056 public:
00057 HTTPProtocol( const QByteArray &protocol, const QByteArray &pool,
00058 const QByteArray &app );
00059 virtual ~HTTPProtocol();
00060
00062 enum HTTP_REV {HTTP_None, HTTP_Unknown, HTTP_10, HTTP_11, SHOUTCAST};
00063
00065 enum HTTP_AUTH {AUTH_None, AUTH_Basic, AUTH_NTLM, AUTH_Digest, AUTH_Negotiate};
00066
00068
00069
00070
00071
00072
00074 struct HTTPState
00075 {
00076 HTTPState ()
00077 {
00078 port = 0;
00079 doProxy = false;
00080 }
00081
00082 QString hostname;
00083 QString encoded_hostname;
00084 short unsigned int port;
00085 QString user;
00086 QString passwd;
00087 bool doProxy;
00088 KUrl proxyUrl;
00089 };
00090
00092 struct DAVRequest
00093 {
00094 DAVRequest ()
00095 {
00096 overwrite = false;
00097 depth = 0;
00098 }
00099
00100 QString desturl;
00101 bool overwrite;
00102 int depth;
00103 };
00104
00106 struct HTTPRequest
00107 {
00108 HTTPRequest ()
00109 {
00110 port = 0;
00111 method = KIO::HTTP_UNKNOWN;
00112 offset = 0;
00113 endoffset = 0;
00114 doProxy = false;
00115 allowCompressedPage = false;
00116 disablePassDlg = false;
00117 bNoAuth = false;
00118 bUseCache = false;
00119 bCachedRead = false;
00120 bCachedWrite = false;
00121 fcache = 0;
00122 bMustRevalidate = false;
00123 cacheExpireDateOffset = 0;
00124 bErrorPage = false;
00125 bUseCookiejar = false;
00126 expireDate = 0;
00127 creationDate = 0;
00128 bytesCached=0;
00129 }
00130
00131 QString hostname;
00132 QString encoded_hostname;
00133 short unsigned int port;
00134 QString user;
00135 QString passwd;
00136 QString path;
00137 QString query;
00138 KIO::HTTP_METHOD method;
00139 KIO::CacheControl cache;
00140 KIO::filesize_t offset;
00141 KIO::filesize_t endoffset;
00142 KUrl url;
00143 QString window;
00144 QString referrer;
00145 QString charsets;
00146 QString languages;
00147 QString userAgent;
00148 QString id;
00149 DAVRequest davData;
00150 bool doProxy;
00151 KUrl proxyUrl;
00152 bool allowCompressedPage;
00153 bool disablePassDlg;
00154 bool bNoAuth;
00155
00156
00157 bool bErrorPage;
00158
00159
00160 bool bUseCookiejar;
00161
00162
00163 bool bUseCache;
00164 bool bCachedRead;
00165 bool bCachedWrite;
00166 bool bMustRevalidate;
00167 QString cef;
00168 gzFile fcache;
00169 QString etag;
00170 QString lastModified;
00171 long cacheExpireDateOffset;
00172
00173 long bytesCached;
00174 time_t expireDate;
00175 time_t creationDate;
00176 QString strCharset;
00177
00178
00179 enum { CookiesAuto, CookiesManual, CookiesNone } cookieMode;
00180 };
00181
00182 struct DigestAuthInfo
00183 {
00184 QByteArray nc;
00185 QByteArray qop;
00186 QByteArray realm;
00187 QByteArray nonce;
00188 QByteArray method;
00189 QByteArray cnonce;
00190 QByteArray username;
00191 QByteArray password;
00192 KUrl::List digestURI;
00193 QByteArray algorithm;
00194 QByteArray entityBody;
00195 };
00196
00197
00198 virtual void setHost(const QString& host, quint16 port, const QString& user,
00199 const QString& pass);
00200
00201 virtual void slave_status();
00202
00203 virtual void get( const KUrl& url );
00204 virtual void put( const KUrl& url, int _mode, KIO::JobFlags flags );
00205
00206
00207 virtual void listDir( const KUrl& url );
00208 virtual void mkdir( const KUrl& url, int _permissions );
00209
00210 virtual void rename( const KUrl& src, const KUrl& dest, KIO::JobFlags flags );
00211 virtual void copy( const KUrl& src, const KUrl& dest, int _permissions, KIO::JobFlags flags );
00212 virtual void del( const KUrl& url, bool _isfile );
00213
00214
00215 bool davHostOk();
00216
00217
00218 void davGeneric( const KUrl& url, KIO::HTTP_METHOD method );
00219
00220
00221 void davLock( const KUrl& url, const QString& scope,
00222 const QString& type, const QString& owner );
00223 void davUnlock( const KUrl& url );
00224
00225
00226 void davFinished();
00227
00228
00229 QString davError( int code = -1, const QString &url = QString() );
00230
00231
00241 virtual void special( const QByteArray &data );
00242
00243 virtual void mimetype( const KUrl& url);
00244
00245 virtual void stat( const KUrl& url );
00246
00247 virtual void reparseConfiguration();
00248
00249 virtual void closeConnection();
00250
00251 void post( const KUrl& url );
00252 void multiGet(const QByteArray &data);
00253 bool checkRequestUrl( const KUrl& );
00254 void cacheUpdate( const KUrl &url, bool nocache, time_t expireDate);
00255
00256 void httpError();
00257
00258 bool isOffline(const KUrl &url);
00259
00260 protected Q_SLOTS:
00261 void slotData(const QByteArray &);
00262 void error(int errid, const QString &text);
00263 void proxyAuthenticationForSocket(const QNetworkProxy &, QAuthenticator *);
00264 void saveProxyAuthenticationForSocket();
00265
00266 protected:
00267 int readChunked();
00268 int readLimited();
00269 int readUnlimited();
00270
00277 ssize_t write(const void *buf, size_t nbytes);
00278
00284 ssize_t read (void *b, size_t nbytes);
00285
00286 char *gets (char *str, int size);
00287
00288 void setRewindMarker();
00289 void rewind();
00290
00296 void addEncoding(const QString &, QStringList &);
00297
00298 void configAuth( char *, bool );
00299
00300 bool sendQuery();
00301 void httpClose(bool keepAlive);
00302
00303 bool httpOpenConnection();
00304 void httpCloseConnection();
00305 bool httpShouldCloseConnection();
00306
00307 void forwardHttpResponseHeader();
00308
00309 bool readResponseHeader();
00310 bool readHeaderFromCache();
00311 void parseContentDisposition(const QString &disposition);
00312
00313 bool sendBody();
00314
00315
00316
00317 bool readBody( bool dataInternal = false );
00318
00322 void davSetRequest( const QByteArray& requestXML );
00323 void davStatList( const KUrl& url, bool stat = true );
00324 void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
00325 void davParseActiveLocks( const QDomNodeList& activeLocks,
00326 uint& lockCount );
00327
00331 long parseDateTime( const QString& input, const QString& type );
00332
00336 int codeFromResponse( const QString& response );
00337
00342 QString davProcessLocks();
00343
00347 void addCookies( const QString &url, const QByteArray &cookieHeader);
00348
00352 QString findCookies( const QString &url);
00353
00365 gzFile checkCacheEntry(bool readWrite = false);
00366
00372 void createCacheEntry(const QString &mimetype, time_t expireDate);
00373
00379 void writeCacheEntry( const char *buffer, int nbytes);
00380
00384 void closeCacheEntry();
00385
00389 void updateExpireDate(time_t expireDate, bool updateCreationDate=false);
00390
00394 void cleanCache();
00395
00401
00402
00403 void proceedUntilResponseContent( bool dataInternal = false );
00404
00408 bool proceedUntilResponseHeader();
00409
00413 void resetSessionSettings();
00414
00418 void resetResponseSettings();
00419
00426 void resetConnectionSettings();
00427
00432 QString proxyAuthenticationHeader();
00433
00437 bool getAuthorization();
00438
00442 void saveAuthorization(bool isForProxy);
00443
00447 QString createBasicAuth( bool isForProxy = false );
00448
00452 QString createDigestAuth( bool isForProxy = false );
00453
00457 QString createNTLMAuth( bool isForProxy = false );
00458
00462 QString createNegotiateAuth();
00463
00467 QByteArray gssError( int major_status, int minor_status );
00468
00472 void calculateResponse( DigestAuthInfo &info, QByteArray &Response );
00473
00477 bool retryPrompt();
00478
00482 void promptInfo( KIO::AuthInfo& info );
00483
00484 protected:
00485 HTTPState m_state;
00486 HTTPRequest m_request;
00487 QList<HTTPRequest*> m_requestQueue;
00488 quint16 m_defaultPort;
00489
00490
00491 KIO::filesize_t m_iSize;
00492 KIO::filesize_t m_iBytesLeft;
00493 KIO::filesize_t m_iContentLeft;
00494 QByteArray m_bufReceive;
00495 char m_lineBuf[1024];
00496 char m_rewindBuf[8192];
00497 size_t m_rewindCount;
00498 size_t m_lineCount;
00499 size_t m_lineCountUnget;
00500 char *m_linePtr;
00501 char *m_lineBufUnget;
00502 char *m_linePtrUnget;
00503 bool m_dataInternal;
00504 bool m_bChunked;
00505
00506 bool m_bBusy;
00507 bool m_bEOF;
00508 bool m_bEOD;
00509
00510
00511 bool m_bFirstRequest;
00512
00513
00514 bool m_bRedirect;
00515 QStringList m_responseHeaders;
00516
00517
00518
00519 QStringList m_qTransferEncodings;
00520 QStringList m_qContentEncodings;
00521 QString m_sContentMD5;
00522 QString m_strMimeType;
00523
00524
00525
00526
00527 QByteArray m_bufWebDavData;
00528 QStringList m_davCapabilities;
00529
00530 bool m_davHostOk;
00531 bool m_davHostUnsupported;
00532
00533
00534
00535 bool m_cpMimeBuffer;
00536 QByteArray m_mimeTypeBuffer;
00537
00538
00539
00540
00541
00542 QByteArray m_bufPOST;
00543
00544
00545 int m_maxCacheAge;
00546 long m_maxCacheSize;
00547 QString m_strCacheDir;
00548
00549
00550
00551
00552 bool m_bUseProxy;
00553 bool m_bNeedTunnel;
00554 bool m_bIsTunneled;
00555 bool m_bProxyAuthValid;
00556 int m_iProxyPort;
00557 KUrl m_proxyURL;
00558 QString m_strProxyRealm;
00559
00560
00561 QByteArray m_protocol;
00562
00563
00564 QString m_strRealm;
00565 QString m_strAuthorization;
00566 QString m_strProxyAuthorization;
00567 HTTP_AUTH Authentication;
00568 HTTP_AUTH ProxyAuthentication;
00569 short unsigned int m_iProxyAuthCount;
00570 short unsigned int m_iWWWAuthCount;
00571 bool m_bUnauthorized;
00572
00573
00574 bool m_bPersistentProxyConnection;
00575
00576
00577
00578 bool m_bError;
00579
00580
00581 bool m_bKeepAlive;
00582 int m_keepAliveTimeout;
00583
00584
00585 unsigned int m_responseCode;
00586 unsigned int m_prevResponseCode;
00587
00588
00589 int m_remoteRespTimeout;
00590 };
00591 #endif