• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KIOSlave

  • kioslave
  • http
http.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000,2001 Dawit Alemayehu <adawit@kde.org>
3  Copyright (C) 2000,2001 Waldo Bastian <bastian@kde.org>
4  Copyright (C) 2000,2001 George Staikos <staikos@kde.org>
5  Copyright (C) 2001,2002 Hamish Rodda <rodda@kde.org>
6  Copyright (C) 2007 Daniel Nicoletti <mirttex@users.sourceforge.net>
7  Copyright (C) 2008,2009 Andreas Hartmetz <ahartmetz@gmail.com>
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Library General Public
11  License as published by the Free Software Foundation; either
12  version 2 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Library General Public License for more details.
18 
19  You should have received a copy of the GNU Library General Public License
20  along with this library; see the file COPYING.LIB. If not, write to
21  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  Boston, MA 02110-1301, USA.
23 */
24 
25 #ifndef HTTP_H
26 #define HTTP_H
27 
28 
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <time.h>
35 
36 #include <QtCore/QList>
37 #include <QtCore/QStringList>
38 #include <QtNetwork/QLocalSocket>
39 
40 #include <kurl.h>
41 #include "kio/tcpslavebase.h"
42 #include "kio/http.h"
43 
44 
45 class QDomNodeList;
46 class QFile;
47 class QIODevice;
48 
49 namespace KIO {
50  class AuthInfo;
51 }
52 
53 class HeaderTokenizer;
54 class KAbstractHttpAuthentication;
55 
56 class HTTPProtocol : public QObject, public KIO::TCPSlaveBase
57 {
58  Q_OBJECT
59 public:
60  HTTPProtocol( const QByteArray &protocol, const QByteArray &pool,
61  const QByteArray &app );
62  virtual ~HTTPProtocol();
63 
65  enum HTTP_REV {HTTP_None, HTTP_Unknown, HTTP_10, HTTP_11, SHOUTCAST};
66 
68  enum AUTH_SCHEME {AUTH_None, AUTH_Basic, AUTH_NTLM, AUTH_Digest, AUTH_Negotiate};
69 
71  struct DAVRequest
72  {
73  DAVRequest ()
74  {
75  overwrite = false;
76  depth = 0;
77  }
78 
79  QString desturl;
80  bool overwrite;
81  int depth;
82  };
83 
84  enum CacheIOMode {
85  NoCache = 0,
86  ReadFromCache = 1,
87  WriteToCache = 2
88  };
89 
90  struct CacheTag
91  {
92  CacheTag()
93  {
94  useCache = false;
95  ioMode = NoCache;
96  bytesCached = 0;
97  file = 0;
98  expireDate = 0;
99  servedDate = 0;
100  }
101 
102  enum CachePlan {
103  UseCached = 0,
104  ValidateCached,
105  IgnoreCached
106  };
107  CachePlan plan(time_t maxCacheAge) const;
108 
109  QByteArray serialize() const;
110  bool deserialize(const QByteArray &);
111 
112  KIO::CacheControl policy; // ### initialize in the constructor?
113  bool useCache; // Whether the cache should be used
114  enum CacheIOMode ioMode; // Write to cache file, read from it, or don't use it.
115  quint32 fileUseCount;
116  quint32 bytesCached;
117  QString etag; // entity tag header as described in the HTTP standard.
118  QFile *file; // file on disk - either a QTemporaryFile (write) or QFile (read)
119  time_t servedDate; // Date when the resource was served by the origin server
120  time_t lastModifiedDate; // Last modified.
121  time_t expireDate; // Date when the cache entry will expire
122  QString charset;
123  };
124 
126  struct HTTPRequest
127  {
128  HTTPRequest ()
129  {
130  method = KIO::HTTP_UNKNOWN;
131  offset = 0;
132  endoffset = 0;
133  allowTransferCompression = false;
134  disablePassDialog = false;
135  doNotAuthenticate = false;
136  preferErrorPage = false;
137  useCookieJar = false;
138  }
139 
140  QByteArray methodString() const;
141 
142  KUrl url;
143  QString encoded_hostname; //### can be calculated on-the-fly
144  // Persistent connections
145  bool isKeepAlive;
146  int keepAliveTimeout; // Timeout in seconds.
147 
148  KIO::HTTP_METHOD method;
149  QString methodStringOverride; // Overrides method if non-empty.
150  KIO::filesize_t offset;
151  KIO::filesize_t endoffset;
152  QString windowId; // Window Id this request is related to.
153  // Header fields
154  QString referrer;
155  QString charsets;
156  QString languages;
157  QString userAgent;
158  // Previous and current response codes
159  unsigned int responseCode;
160  unsigned int prevResponseCode;
161  // Miscellaneous
162  QString id;
163  DAVRequest davData;
164  KUrl redirectUrl;
165  KUrl proxyUrl;
166  QStringList proxyUrls;
167 
168  bool isPersistentProxyConnection;
169  bool allowTransferCompression;
170  bool disablePassDialog;
171  bool doNotAuthenticate;
172  // Indicates whether an error page or error message is preferred.
173  bool preferErrorPage;
174 
175  // Use the cookie jar (or pass cookies to the application as metadata instead)
176  bool useCookieJar;
177  // Cookie flags
178  enum { CookiesAuto, CookiesManual, CookiesNone } cookieMode;
179 
180  CacheTag cacheTag;
181  };
182 
184  struct HTTPServerState
185  {
186  HTTPServerState()
187  {
188  isKeepAlive = false;
189  isPersistentProxyConnection = false;
190  }
191 
192  void initFrom(const HTTPRequest &request)
193  {
194  url = request.url;
195  encoded_hostname = request.encoded_hostname;
196  isKeepAlive = request.isKeepAlive;
197  proxyUrl = request.proxyUrl;
198  isPersistentProxyConnection = request.isPersistentProxyConnection;
199  }
200 
201  void updateCredentials(const HTTPRequest &request)
202  {
203  if (url.host() == request.url.host() && url.port() == request.url.port()) {
204  url.setUserName(request.url.userName());
205  url.setPassword(request.url.password());
206  }
207  if (proxyUrl.host() == request.proxyUrl.host() &&
208  proxyUrl.port() == request.proxyUrl.port()) {
209  proxyUrl.setUserName(request.proxyUrl.userName());
210  proxyUrl.setPassword(request.proxyUrl.password());
211  }
212  }
213 
214  void clear()
215  {
216  url.clear();
217  encoded_hostname.clear();
218  proxyUrl.clear();
219  isKeepAlive = false;
220  isPersistentProxyConnection = false;
221  }
222 
223  KUrl url;
224  QString encoded_hostname;
225  KUrl proxyUrl;
226  bool isKeepAlive;
227  bool isPersistentProxyConnection;
228  };
229 
230 //---------------------- Re-implemented methods ----------------
231  virtual void setHost(const QString& host, quint16 port, const QString& user,
232  const QString& pass);
233 
234  virtual void slave_status();
235 
236  virtual void get( const KUrl& url );
237  virtual void put( const KUrl& url, int _mode, KIO::JobFlags flags );
238 
239 //----------------- Re-implemented methods for WebDAV -----------
240  virtual void listDir( const KUrl& url );
241  virtual void mkdir( const KUrl& url, int _permissions );
242 
243  virtual void rename( const KUrl& src, const KUrl& dest, KIO::JobFlags flags );
244  virtual void copy( const KUrl& src, const KUrl& dest, int _permissions, KIO::JobFlags flags );
245  virtual void del( const KUrl& url, bool _isfile );
246 
247  // ask the host whether it supports WebDAV & cache this info
248  bool davHostOk();
249 
250  // send generic DAV request
251  void davGeneric( const KUrl& url, KIO::HTTP_METHOD method, qint64 size = -1 );
252 
253  // Send requests to lock and unlock resources
254  void davLock( const KUrl& url, const QString& scope,
255  const QString& type, const QString& owner );
256  void davUnlock( const KUrl& url );
257 
258  // Calls httpClose() and finished()
259  void davFinished();
260 
261  // Handle error conditions
262  QString davError( int code = -1, const QString &url = QString() );
263 //---------------------------- End WebDAV -----------------------
264 
274  virtual void special( const QByteArray &data );
275 
276  virtual void mimetype( const KUrl& url);
277 
278  virtual void stat( const KUrl& url );
279 
280  virtual void reparseConfiguration();
281 
285  virtual void closeConnection();
286 
287  void post( const KUrl& url, qint64 size = -1 );
288  void multiGet(const QByteArray &data);
289  bool maybeSetRequestUrl(const KUrl &);
290 
294  bool sendHttpError();
295 
299  bool sendErrorPageNotification();
300 
304  bool isOffline();
305 
306 protected Q_SLOTS:
307  void slotData(const QByteArray &);
308  void slotFilterError(const QString &text);
309  void error(int errid, const QString &text);
310  void proxyAuthenticationForSocket(const QNetworkProxy &, QAuthenticator *);
311  void saveProxyAuthenticationForSocket();
312 
313 protected:
314  int readChunked();
315  int readLimited();
316  int readUnlimited();
317 
322  ssize_t write(const void *buf, size_t nbytes);
323  using SlaveBase::write;
324 
330  void addEncoding(const QString &, QStringList &);
331 
332  quint16 defaultPort() const;
333 
334  // The methods between here and sendQuery() are helpers for sendQuery().
335 
341  bool satisfyRequestFromCache(bool *cacheHasPage);
342  QString formatRequestUri() const;
346  QString authenticationHeader();
347  bool sendQuery();
348 
352  void httpClose(bool keepAlive);
356  bool httpOpenConnection();
360  void httpCloseConnection();
364  bool httpShouldCloseConnection();
365 
366  void forwardHttpResponseHeader(bool forwardImmediately = true);
367 
373  void fixupResponseMimetype();
379  void fixupResponseContentEncoding();
380 
381  bool readResponseHeader();
382  bool parseHeaderFromCache();
383  void parseContentDisposition(const QString &disposition);
384 
385  bool sendBody();
386  bool sendCachedBody();
387 
388  // where dataInternal == true, the content is to be made available
389  // to an internal function.
390  bool readBody( bool dataInternal = false );
391 
395  void davSetRequest( const QByteArray& requestXML );
396  void davStatList( const KUrl& url, bool stat = true );
397  void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
398  void davParseActiveLocks( const QDomNodeList& activeLocks,
399  uint& lockCount );
400 
404  long parseDateTime( const QString& input, const QString& type );
405 
409  int codeFromResponse( const QString& response );
410 
415  QString davProcessLocks();
416 
420  void addCookies( const QString &url, const QByteArray &cookieHeader);
421 
425  QString findCookies( const QString &url);
426 
427  void cacheParseResponseHeader(const HeaderTokenizer &tokenizer);
428 
429  QString cacheFilePathFromUrl(const KUrl &url) const;
430  bool cacheFileOpenRead();
431  bool cacheFileOpenWrite();
432  void cacheFileClose();
433  void sendCacheCleanerCommand(const QByteArray &command);
434 
435  QByteArray cacheFileReadPayload(int maxLength);
436  void cacheFileWritePayload(const QByteArray &d);
437  void cacheFileWriteTextHeader();
441  bool cacheFileReadTextHeader1(const KUrl &desiredUrl);
445  bool cacheFileReadTextHeader2();
446  void setCacheabilityMetadata(bool cachingAllowed);
447 
456  void proceedUntilResponseContent( bool dataInternal = false );
457 
461  bool proceedUntilResponseHeader();
462 
466  void resetSessionSettings();
467 
471  void resetResponseParsing();
472 
479  void resetConnectionSettings();
480 
487  void cachePostData(const QByteArray&);
488 
495  void clearPostDataBuffer();
496 
500  bool retrieveAllData();
501 
505  void saveAuthenticationData();
506 
510  bool handleAuthenticationHeader(const HeaderTokenizer* tokenizer);
511 
512 protected:
513  HTTPServerState m_server;
514  HTTPRequest m_request;
515  QList<HTTPRequest> m_requestQueue;
516 
517  // Processing related
518  KIO::filesize_t m_iSize;
519  KIO::filesize_t m_iPostDataSize;
520  KIO::filesize_t m_iBytesLeft;
521  KIO::filesize_t m_iContentLeft;
522  QByteArray m_receiveBuf;
523  bool m_dataInternal;
524  bool m_isChunked;
525 
526  bool m_isBusy;
527  bool m_isEOF;
528  bool m_isEOD;
529 
530 //--- Settings related to a single response only
531  bool m_isRedirection;
532  QStringList m_responseHeaders;
533 
534 
535  // Language/Encoding related
536  QStringList m_transferEncodings;
537  QStringList m_contentEncodings;
538  QString m_contentMD5;
539  QString m_mimeType; // TODO QByteArray?
540 
541 
542 //--- WebDAV
543  // Data structure to hold data which will be passed to an internal func.
544  QByteArray m_webDavDataBuf;
545  QStringList m_davCapabilities;
546 
547  bool m_davHostOk;
548  bool m_davHostUnsupported;
549 //----------
550 
551  // Mimetype determination
552  bool m_cpMimeBuffer;
553  QByteArray m_mimeTypeBuffer;
554 
555 
556  // Holds the POST data so it won't get lost on if we
557  // happend to get a 401/407 response when submitting
558  // a form.
559  QIODevice* m_POSTbuf;
560 
561  // Cache related
562  int m_maxCacheAge;
563  long m_maxCacheSize;
564  QString m_strCacheDir;
565  QLocalSocket m_cacheCleanerConnection;
566 
567  // Operation mode
568  QByteArray m_protocol;
569 
570  KAbstractHttpAuthentication *m_wwwAuth;
571  KAbstractHttpAuthentication *m_proxyAuth;
572  // For proxy auth when it's handled by the Qt/KDE socket classes
573  QAuthenticator *m_socketProxyAuth;
574 
575  // Indicates whether there was some error.
576  int m_iError;
577  // Whether we are loading an error page (we should close the connection afterwards)
578  bool m_isLoadingErrorPage;
579 
580  // Values that determine the remote connection timeouts.
581  int m_remoteRespTimeout;
582 
583  QByteArray m_unreadBuf;
584  void clearUnreadBuffer();
585  void unread(char *buf, size_t size);
586  size_t readBuffered(char *buf, size_t size, bool unlimited = true);
587  bool readDelimitedText(char *buf, int *idx, int end, int numNewlines);
588 };
589 #endif
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Thu Feb 21 2013 11:13:57 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIOSlave

Skip menu "KIOSlave"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal