00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef HTTP_H_
00024 #define HTTP_H_
00025
00026
00027 #include <netinet/in.h>
00028 #include <arpa/inet.h>
00029 #include <string.h>
00030 #include <stdio.h>
00031 #include <time.h>
00032
00033 #include <qptrlist.h>
00034 #include <qstrlist.h>
00035 #include <qstringlist.h>
00036
00037 #include <kurl.h>
00038 #include "kio/tcpslavebase.h"
00039 #include "kio/http.h"
00040
00041 class DCOPClient;
00042 class QDomElement;
00043 class QDomNodeList;
00044
00045 namespace KIO {
00046 class AuthInfo;
00047 }
00048
00049 class HTTPProtocol : public QObject, public KIO::TCPSlaveBase
00050 {
00051 Q_OBJECT
00052 public:
00053 HTTPProtocol( const QCString &protocol, const QCString &pool,
00054 const QCString &app );
00055 virtual ~HTTPProtocol();
00056
00058 enum HTTP_REV {HTTP_None, HTTP_Unknown, HTTP_10, HTTP_11, SHOUTCAST};
00059
00061 enum HTTP_AUTH {AUTH_None, AUTH_Basic, AUTH_NTLM, AUTH_Digest, AUTH_Negotiate};
00062
00064
00065
00066
00067
00068
00070 struct HTTPState
00071 {
00072 HTTPState ()
00073 {
00074 port = 0;
00075 doProxy = false;
00076 }
00077
00078 QString hostname;
00079 QString encoded_hostname;
00080 short unsigned int port;
00081 QString user;
00082 QString passwd;
00083 bool doProxy;
00084 };
00085
00087 struct DAVRequest
00088 {
00089 DAVRequest ()
00090 {
00091 overwrite = false;
00092 depth = 0;
00093 }
00094
00095 QString desturl;
00096 bool overwrite;
00097 int depth;
00098 };
00099
00101 struct HTTPRequest
00102 {
00103 HTTPRequest ()
00104 {
00105 port = 0;
00106 method = KIO::HTTP_UNKNOWN;
00107 offset = 0;
00108 doProxy = false;
00109 allowCompressedPage = false;
00110 disablePassDlg = false;
00111 bNoAuth = false;
00112 bUseCache = false;
00113 bCachedRead = false;
00114 bCachedWrite = false;
00115 fcache = 0;
00116 bMustRevalidate = false;
00117 cacheExpireDateOffset = 0;
00118 bErrorPage = false;
00119 bUseCookiejar = false;
00120 expireDate = 0;
00121 creationDate = 0;
00122 }
00123
00124 QString hostname;
00125 QString encoded_hostname;
00126 short unsigned int port;
00127 QString user;
00128 QString passwd;
00129 QString path;
00130 QString query;
00131 KIO::HTTP_METHOD method;
00132 KIO::CacheControl cache;
00133 KIO::filesize_t offset;
00134 bool doProxy;
00135 KURL url;
00136 QString window;
00137 QString referrer;
00138 QString charsets;
00139 QString languages;
00140 bool allowCompressedPage;
00141 bool disablePassDlg;
00142 QString userAgent;
00143 QString id;
00144 DAVRequest davData;
00145
00146 bool bNoAuth;
00147
00148
00149 QString cef;
00150 bool bUseCache;
00151 bool bCachedRead;
00152 bool bCachedWrite;
00153 FILE* fcache;
00154 QString etag;
00155 QString lastModified;
00156 bool bMustRevalidate;
00157 long cacheExpireDateOffset;
00158
00159 time_t expireDate;
00160 time_t creationDate;
00161 QString strCharset;
00162
00163
00164 bool bErrorPage;
00165
00166
00167 bool bUseCookiejar;
00168 enum { CookiesAuto, CookiesManual, CookiesNone } cookieMode;
00169 };
00170
00171 struct DigestAuthInfo
00172 {
00173 QCString nc;
00174 QCString qop;
00175 QCString realm;
00176 QCString nonce;
00177 QCString method;
00178 QCString cnonce;
00179 QCString username;
00180 QCString password;
00181 QStrList digestURI;
00182 QCString algorithm;
00183 QCString entityBody;
00184 };
00185
00186
00187 virtual void setHost(const QString& host, int port, const QString& user,
00188 const QString& pass);
00189
00190 virtual void slave_status();
00191
00192 virtual void get( const KURL& url );
00193 virtual void put( const KURL& url, int permissions, bool overwrite,
00194 bool resume );
00195
00196
00197 virtual void listDir( const KURL& url );
00198 virtual void mkdir( const KURL& url, int permissions );
00199
00200 virtual void rename( const KURL& src, const KURL& dest, bool overwrite );
00201 virtual void copy( const KURL& src, const KURL& dest, int permissions, bool overwrite );
00202 virtual void del( const KURL& url, bool isfile );
00203
00204
00205 bool davHostOk();
00206
00207
00208 void davGeneric( const KURL& url, KIO::HTTP_METHOD method );
00209
00210
00211 void davLock( const KURL& url, const QString& scope,
00212 const QString& type, const QString& owner );
00213 void davUnlock( const KURL& url );
00214
00215
00216 void davFinished();
00217
00218
00219 QString davError( int code = -1, QString url = QString::null );
00220
00221
00231 virtual void special( const QByteArray &data );
00232
00233 virtual void mimetype( const KURL& url);
00234
00235 virtual void stat( const KURL& url );
00236
00237 virtual void reparseConfiguration();
00238
00239 virtual void closeConnection();
00240
00241 void post( const KURL& url );
00242 void multiGet(const QByteArray &data);
00243 bool checkRequestURL( const KURL& );
00244 void cacheUpdate( const KURL &url, bool nocache, time_t expireDate);
00245
00246 void httpError();
00247
00248 bool isOffline(const KURL &url);
00249
00250 protected slots:
00251 void slotData(const QByteArray &);
00252 void error( int _errid, const QString &_text );
00253
00254 protected:
00255 int readChunked();
00256 int readLimited();
00257 int readUnlimited();
00258
00265 ssize_t write(const void *buf, size_t nbytes);
00266
00272 ssize_t read (void *b, size_t nbytes);
00273
00274 char *gets (char *str, int size);
00275
00276 void setRewindMarker();
00277 void rewind();
00278
00284 void addEncoding(QString, QStringList &);
00285
00286 void configAuth( char *, bool );
00287
00288 bool httpOpen();
00289 void httpClose(bool keepAlive);
00290
00291 bool httpOpenConnection();
00292 void httpCloseConnection();
00293 void httpCheckConnection();
00294
00295 void forwardHttpResponseHeader();
00296
00297 bool readHeader();
00298
00299 bool sendBody();
00300
00301
00302
00303 bool readBody( bool dataInternal = false );
00304
00308 void davSetRequest( const QCString& requestXML );
00309 void davStatList( const KURL& url, bool stat = true );
00310 void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
00311 void davParseActiveLocks( const QDomNodeList& activeLocks,
00312 uint& lockCount );
00313
00317 long parseDateTime( const QString& input, const QString& type );
00318
00322 int codeFromResponse( const QString& response );
00323
00328 QString davProcessLocks();
00329
00333 void addCookies( const QString &url, const QCString &cookieHeader);
00334
00338 QString findCookies( const QString &url);
00339
00351 FILE *checkCacheEntry(bool readWrite = false);
00352
00358 void createCacheEntry(const QString &mimetype, time_t expireDate);
00359
00365 void writeCacheEntry( const char *buffer, int nbytes);
00366
00370 void closeCacheEntry();
00371
00375 void updateExpireDate(time_t expireDate, bool updateCreationDate=false);
00376
00380 void cleanCache();
00381
00385
00386
00387 void retrieveContent( bool dataInternal = false );
00388
00392 bool retrieveHeader(bool close_connection = true);
00393
00397 void resetSessionSettings();
00398
00402 void resetResponseSettings();
00403
00410 void resetConnectionSettings();
00411
00416 QString proxyAuthenticationHeader();
00417
00421 bool getAuthorization();
00422
00426 void saveAuthorization();
00427
00431 QString createBasicAuth( bool isForProxy = false );
00432
00436 QString createDigestAuth( bool isForProxy = false );
00437
00441 QString createNTLMAuth( bool isForProxy = false );
00442
00446 QString createNegotiateAuth();
00447
00451 QCString gssError( int major_status, int minor_status );
00452
00456 void calculateResponse( DigestAuthInfo &info, QCString &Response );
00457
00461 bool retryPrompt();
00462
00466 void promptInfo( KIO::AuthInfo& info );
00467
00468 protected:
00469 HTTPState m_state;
00470 HTTPRequest m_request;
00471 QPtrList<HTTPRequest> m_requestQueue;
00472
00473 bool m_bBusy;
00474 bool m_bEOF;
00475 bool m_bEOD;
00476
00477
00478 QStringList m_responseHeader;
00479 KURL m_redirectLocation;
00480 bool m_bRedirect;
00481
00482
00483 bool m_bChunked;
00484 KIO::filesize_t m_iSize;
00485 KIO::filesize_t m_iBytesLeft;
00486 KIO::filesize_t m_iContentLeft;
00487 QByteArray m_bufReceive;
00488 bool m_dataInternal;
00489 char m_lineBuf[1024];
00490 char m_rewindBuf[8192];
00491 size_t m_rewindCount;
00492 char *m_linePtr;
00493 size_t m_lineCount;
00494 char *m_lineBufUnget;
00495 char *m_linePtrUnget;
00496 size_t m_lineCountUnget;
00497
00498
00499 bool m_cpMimeBuffer;
00500 QByteArray m_mimeTypeBuffer;
00501
00502
00503 QStringList m_qTransferEncodings;
00504 QStringList m_qContentEncodings;
00505 QString m_sContentMD5;
00506 QString m_strMimeType;
00507
00508
00509
00510
00511 QByteArray m_bufWebDavData;
00512 QStringList m_davCapabilities;
00513
00514 bool m_davHostOk;
00515 bool m_davHostUnsupported;
00516
00517
00518
00519
00520
00521 QByteArray m_bufPOST;
00522
00523
00524 int m_maxCacheAge;
00525 long m_maxCacheSize;
00526 QString m_strCacheDir;
00527
00528
00529
00530
00531 bool m_bUseProxy;
00532 bool m_bNeedTunnel;
00533 bool m_bIsTunneled;
00534 bool m_bProxyAuthValid;
00535 int m_iProxyPort;
00536 KURL m_proxyURL;
00537 QString m_strProxyRealm;
00538
00539
00540 QCString m_protocol;
00541
00542
00543 QString m_strRealm;
00544 QString m_strAuthorization;
00545 QString m_strProxyAuthorization;
00546 HTTP_AUTH Authentication;
00547 HTTP_AUTH ProxyAuthentication;
00548 bool m_bUnauthorized;
00549 short unsigned int m_iProxyAuthCount;
00550 short unsigned int m_iWWWAuthCount;
00551
00552
00553 bool m_bFirstRequest;
00554
00555
00556 bool m_bKeepAlive;
00557 int m_keepAliveTimeout;
00558
00559
00560 bool m_bPersistentProxyConnection;
00561
00562
00563
00564 bool m_bError;
00565
00566
00567 unsigned int m_responseCode;
00568 unsigned int m_prevResponseCode;
00569
00570
00571 int m_proxyConnTimeout;
00572 int m_remoteConnTimeout;
00573 int m_remoteRespTimeout;
00574
00575 int m_pid;
00576 };
00577 #endif