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