http.h
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 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 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 // Removed to interfaces/kio/http.h 00065 //enum HTTP_METHOD {HTTP_GET, HTTP_PUT, HTTP_POST, HTTP_HEAD, HTTP_DELETE, 00066 // HTTP_OPTIONS, DAV_PROPFIND, DAV_PROPPATCH, DAV_MKCOL, 00067 // DAV_COPY, DAV_MOVE, DAV_LOCK, DAV_UNLOCK, DAV_SEARCH }; 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; // Window Id this request is related to. 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; // Do not authenticate 00147 00148 // Cache related 00149 QString cef; // Cache Entry File belonging to this URL. 00150 bool bUseCache; // Whether the cache is active 00151 bool bCachedRead; // Whether the file is to be read from m_fcache. 00152 bool bCachedWrite; // Whether the file is to be written to m_fcache. 00153 FILE* fcache; // File stream of a cache entry 00154 QString etag; // ETag header. 00155 QString lastModified; // Last modified. 00156 bool bMustRevalidate; // Cache entry is expired. 00157 long cacheExpireDateOffset; // Position in the cache entry where the 00158 // 16 byte expire date is stored. 00159 time_t expireDate; // Date when the cache entry will expire 00160 time_t creationDate; // Date when the cache entry was created 00161 QString strCharset; // Charset 00162 00163 // Indicates whether an error-page or error-msg should is preferred. 00164 bool bErrorPage; 00165 00166 // Cookie flags 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 //---------------------- Re-implemented methods ---------------- 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 //----------------- Re-implemented methods for WebDAV ----------- 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 // ask the host whether it supports WebDAV & cache this info 00205 bool davHostOk(); 00206 00207 // send generic DAV request 00208 void davGeneric( const KURL& url, KIO::HTTP_METHOD method ); 00209 00210 // Send requests to lock and unlock resources 00211 void davLock( const KURL& url, const QString& scope, 00212 const QString& type, const QString& owner ); 00213 void davUnlock( const KURL& url ); 00214 00215 // Calls httpClose() and finished() 00216 void davFinished(); 00217 00218 // Handle error conditions 00219 QString davError( int code = -1, QString url = QString::null ); 00220 //---------------------------- End WebDAV ----------------------- 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(); // Forced close of connection 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(); // Generate error message based on response code 00247 00248 bool isOffline(const KURL &url); // Check network status 00249 00250 protected slots: 00251 void slotData(const QByteArray &); 00252 void error( int _errid, const QString &_text ); 00253 00254 protected: 00255 int readChunked(); // Read a chunk 00256 int readLimited(); // Read maximum m_iSize bytes. 00257 int readUnlimited(); // Read as much as possible. 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(); // Open transfer 00289 void httpClose(bool keepAlive); // Close transfer 00290 00291 bool httpOpenConnection(); // Open connection 00292 void httpCloseConnection(); // Close connection 00293 void httpCheckConnection(); // Check whether to keep connection. 00294 00295 void forwardHttpResponseHeader(); 00296 00297 bool readHeader(); 00298 00299 bool sendBody(); 00300 00301 // where dataInternal == true, the content is to be made available 00302 // to an internal function. 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 // where dataInternal == true, the content is to be made available 00386 // to an internal function. 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; // Busy handling request queue. 00474 bool m_bEOF; 00475 bool m_bEOD; 00476 00477 //--- Settings related to a single response only 00478 QStringList m_responseHeader; // All headers 00479 KURL m_redirectLocation; 00480 bool m_bRedirect; // Indicates current request is a redirection 00481 00482 // Processing related 00483 bool m_bChunked; // Chunked tranfer encoding 00484 KIO::filesize_t m_iSize; // Expected size of message 00485 KIO::filesize_t m_iBytesLeft; // # of bytes left to receive in this message. 00486 KIO::filesize_t m_iContentLeft; // # of content bytes left 00487 QByteArray m_bufReceive; // Receive buffer 00488 bool m_dataInternal; // Data is for internal consumption 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 // Mimetype determination 00499 bool m_cpMimeBuffer; 00500 QByteArray m_mimeTypeBuffer; 00501 00502 // Language/Encoding related 00503 QStringList m_qTransferEncodings; 00504 QStringList m_qContentEncodings; 00505 QString m_sContentMD5; 00506 QString m_strMimeType; 00507 00508 00509 //--- WebDAV 00510 // Data structure to hold data which will be passed to an internal func. 00511 QByteArray m_bufWebDavData; 00512 QStringList m_davCapabilities; 00513 00514 bool m_davHostOk; 00515 bool m_davHostUnsupported; 00516 //---------- 00517 00518 // Holds the POST data so it won't get lost on if we 00519 // happend to get a 401/407 response when submitting, 00520 // a form. 00521 QByteArray m_bufPOST; 00522 00523 // Cache related 00524 int m_maxCacheAge; // Maximum age of a cache entry. 00525 long m_maxCacheSize; // Maximum cache size in Kb. 00526 QString m_strCacheDir; // Location of the cache. 00527 00528 00529 00530 //--- Proxy related members 00531 bool m_bUseProxy; 00532 bool m_bNeedTunnel; // Whether we need to make a SSL tunnel 00533 bool m_bIsTunneled; // Whether we have an active SSL tunnel 00534 bool m_bProxyAuthValid; 00535 int m_iProxyPort; 00536 KURL m_proxyURL; 00537 QString m_strProxyRealm; 00538 00539 // Operation mode 00540 QCString m_protocol; 00541 00542 // Authentication 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 // First request on a connection 00553 bool m_bFirstRequest; 00554 00555 // Persistent connections 00556 bool m_bKeepAlive; 00557 int m_keepAliveTimeout; // Timeout in seconds. 00558 00559 // Persistent proxy connections 00560 bool m_bPersistentProxyConnection; 00561 00562 00563 // Indicates whether there was some connection error. 00564 bool m_bError; 00565 00566 // Previous and current response codes 00567 unsigned int m_responseCode; 00568 unsigned int m_prevResponseCode; 00569 00570 // Values that determine the remote connection timeouts. 00571 int m_proxyConnTimeout; 00572 int m_remoteConnTimeout; 00573 int m_remoteRespTimeout; 00574 00575 int m_pid; 00576 }; 00577 #endif