• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.2 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  doNotWWWAuthenticate = false;
136  doNotProxyAuthenticate = false;
137  preferErrorPage = false;
138  useCookieJar = false;
139  }
140 
141  QByteArray methodString() const;
142 
143  KUrl url;
144  QString encoded_hostname; //### can be calculated on-the-fly
145  // Persistent connections
146  bool isKeepAlive;
147  int keepAliveTimeout; // Timeout in seconds.
148 
149  KIO::HTTP_METHOD method;
150  QString methodStringOverride; // Overrides method if non-empty.
151  KIO::filesize_t offset;
152  KIO::filesize_t endoffset;
153  QString windowId; // Window Id this request is related to.
154  // Header fields
155  QString referrer;
156  QString charsets;
157  QString languages;
158  QString userAgent;
159  // Previous and current response codes
160  unsigned int responseCode;
161  unsigned int prevResponseCode;
162  // Miscellaneous
163  QString id;
164  DAVRequest davData;
165  KUrl redirectUrl;
166  KUrl proxyUrl;
167  QStringList proxyUrls;
168 
169  bool isPersistentProxyConnection;
170  bool allowTransferCompression;
171  bool disablePassDialog;
172  bool doNotWWWAuthenticate;
173  bool doNotProxyAuthenticate;
174  // Indicates whether an error page or error message is preferred.
175  bool preferErrorPage;
176 
177  // Use the cookie jar (or pass cookies to the application as metadata instead)
178  bool useCookieJar;
179  // Cookie flags
180  enum { CookiesAuto, CookiesManual, CookiesNone } cookieMode;
181 
182  CacheTag cacheTag;
183  };
184 
186  struct HTTPServerState
187  {
188  HTTPServerState()
189  {
190  isKeepAlive = false;
191  isPersistentProxyConnection = false;
192  }
193 
194  void initFrom(const HTTPRequest &request)
195  {
196  url = request.url;
197  encoded_hostname = request.encoded_hostname;
198  isKeepAlive = request.isKeepAlive;
199  proxyUrl = request.proxyUrl;
200  isPersistentProxyConnection = request.isPersistentProxyConnection;
201  }
202 
203  void updateCredentials(const HTTPRequest &request)
204  {
205  if (url.host() == request.url.host() && url.port() == request.url.port()) {
206  url.setUserName(request.url.userName());
207  url.setPassword(request.url.password());
208  }
209  if (proxyUrl.host() == request.proxyUrl.host() &&
210  proxyUrl.port() == request.proxyUrl.port()) {
211  proxyUrl.setUserName(request.proxyUrl.userName());
212  proxyUrl.setPassword(request.proxyUrl.password());
213  }
214  }
215 
216  void clear()
217  {
218  url.clear();
219  encoded_hostname.clear();
220  proxyUrl.clear();
221  isKeepAlive = false;
222  isPersistentProxyConnection = false;
223  }
224 
225  KUrl url;
226  QString encoded_hostname;
227  KUrl proxyUrl;
228  bool isKeepAlive;
229  bool isPersistentProxyConnection;
230  };
231 
232 //---------------------- Re-implemented methods ----------------
233  virtual void setHost(const QString& host, quint16 port, const QString& user,
234  const QString& pass);
235 
236  virtual void slave_status();
237 
238  virtual void get( const KUrl& url );
239  virtual void put( const KUrl& url, int _mode, KIO::JobFlags flags );
240 
241 //----------------- Re-implemented methods for WebDAV -----------
242  virtual void listDir( const KUrl& url );
243  virtual void mkdir( const KUrl& url, int _permissions );
244 
245  virtual void rename( const KUrl& src, const KUrl& dest, KIO::JobFlags flags );
246  virtual void copy( const KUrl& src, const KUrl& dest, int _permissions, KIO::JobFlags flags );
247  virtual void del( const KUrl& url, bool _isfile );
248 
249  // ask the host whether it supports WebDAV & cache this info
250  bool davHostOk();
251 
252  // send generic DAV request
253  void davGeneric( const KUrl& url, KIO::HTTP_METHOD method, qint64 size = -1 );
254 
255  // Send requests to lock and unlock resources
256  void davLock( const KUrl& url, const QString& scope,
257  const QString& type, const QString& owner );
258  void davUnlock( const KUrl& url );
259 
260  // Calls httpClose() and finished()
261  void davFinished();
262 
263  // Handle error conditions
264  QString davError( int code = -1, const QString &url = QString() );
265 //---------------------------- End WebDAV -----------------------
266 
276  virtual void special( const QByteArray &data );
277 
278  virtual void mimetype( const KUrl& url);
279 
280  virtual void stat( const KUrl& url );
281 
282  virtual void reparseConfiguration();
283 
287  virtual void closeConnection();
288 
289  void post( const KUrl& url, qint64 size = -1 );
290  void multiGet(const QByteArray &data);
291  bool maybeSetRequestUrl(const KUrl &);
292 
296  bool sendHttpError();
297 
301  bool sendErrorPageNotification();
302 
306  bool isOffline();
307 
308 protected Q_SLOTS:
309  void slotData(const QByteArray &);
310  void slotFilterError(const QString &text);
311  void error(int errid, const QString &text);
312  void proxyAuthenticationForSocket(const QNetworkProxy &, QAuthenticator *);
313  void saveProxyAuthenticationForSocket();
314 
315 protected:
316  int readChunked();
317  int readLimited();
318  int readUnlimited();
319 
324  ssize_t write(const void *buf, size_t nbytes);
325  using SlaveBase::write;
326 
332  void addEncoding(const QString &, QStringList &);
333 
334  quint16 defaultPort() const;
335 
336  // The methods between here and sendQuery() are helpers for sendQuery().
337 
343  bool satisfyRequestFromCache(bool *cacheHasPage);
344  QString formatRequestUri() const;
348  QString authenticationHeader();
349  bool sendQuery();
350 
354  void httpClose(bool keepAlive);
358  bool httpOpenConnection();
362  void httpCloseConnection();
366  bool httpShouldCloseConnection();
367 
368  void forwardHttpResponseHeader(bool forwardImmediately = true);
369 
375  void fixupResponseMimetype();
381  void fixupResponseContentEncoding();
382 
383  bool readResponseHeader();
384  bool parseHeaderFromCache();
385  void parseContentDisposition(const QString &disposition);
386 
387  bool sendBody();
388  bool sendCachedBody();
389 
390  // where dataInternal == true, the content is to be made available
391  // to an internal function.
392  bool readBody( bool dataInternal = false );
393 
397  void davSetRequest( const QByteArray& requestXML );
398  void davStatList( const KUrl& url, bool stat = true );
399  void davParsePropstats( const QDomNodeList& propstats, KIO::UDSEntry& entry );
400  void davParseActiveLocks( const QDomNodeList& activeLocks,
401  uint& lockCount );
402 
406  long parseDateTime( const QString& input, const QString& type );
407 
411  int codeFromResponse( const QString& response );
412 
417  QString davProcessLocks();
418 
422  void addCookies( const QString &url, const QByteArray &cookieHeader);
423 
427  QString findCookies( const QString &url);
428 
429  void cacheParseResponseHeader(const HeaderTokenizer &tokenizer);
430 
431  QString cacheFilePathFromUrl(const KUrl &url) const;
432  bool cacheFileOpenRead();
433  bool cacheFileOpenWrite();
434  void cacheFileClose();
435  void sendCacheCleanerCommand(const QByteArray &command);
436 
437  QByteArray cacheFileReadPayload(int maxLength);
438  void cacheFileWritePayload(const QByteArray &d);
439  void cacheFileWriteTextHeader();
443  bool cacheFileReadTextHeader1(const KUrl &desiredUrl);
447  bool cacheFileReadTextHeader2();
448  void setCacheabilityMetadata(bool cachingAllowed);
449 
458  void proceedUntilResponseContent( bool dataInternal = false );
459 
463  bool proceedUntilResponseHeader();
464 
468  void resetSessionSettings();
469 
473  void resetResponseParsing();
474 
481  void resetConnectionSettings();
482 
489  void cachePostData(const QByteArray&);
490 
497  void clearPostDataBuffer();
498 
502  bool retrieveAllData();
503 
507  void saveAuthenticationData();
508 
512  bool handleAuthenticationHeader(const HeaderTokenizer* tokenizer);
513 
514 protected:
515  HTTPServerState m_server;
516  HTTPRequest m_request;
517  QList<HTTPRequest> m_requestQueue;
518 
519  // Processing related
520  KIO::filesize_t m_iSize;
521  KIO::filesize_t m_iPostDataSize;
522  KIO::filesize_t m_iBytesLeft;
523  KIO::filesize_t m_iContentLeft;
524  QByteArray m_receiveBuf;
525  bool m_dataInternal;
526  bool m_isChunked;
527 
528  bool m_isBusy;
529  bool m_isEOF;
530  bool m_isEOD;
531 
532 //--- Settings related to a single response only
533  bool m_isRedirection;
534  QStringList m_responseHeaders;
535 
536 
537  // Language/Encoding related
538  QStringList m_transferEncodings;
539  QStringList m_contentEncodings;
540  QString m_contentMD5;
541  QString m_mimeType; // TODO QByteArray?
542 
543 
544 //--- WebDAV
545  // Data structure to hold data which will be passed to an internal func.
546  QByteArray m_webDavDataBuf;
547  QStringList m_davCapabilities;
548 
549  bool m_davHostOk;
550  bool m_davHostUnsupported;
551 //----------
552 
553  // Mimetype determination
554  bool m_cpMimeBuffer;
555  QByteArray m_mimeTypeBuffer;
556 
557 
558  // Holds the POST data so it won't get lost on if we
559  // happend to get a 401/407 response when submitting
560  // a form.
561  QIODevice* m_POSTbuf;
562 
563  // Cache related
564  int m_maxCacheAge;
565  long m_maxCacheSize;
566  QString m_strCacheDir;
567  QLocalSocket m_cacheCleanerConnection;
568 
569  // Operation mode
570  QByteArray m_protocol;
571 
572  KAbstractHttpAuthentication *m_wwwAuth;
573  KAbstractHttpAuthentication *m_proxyAuth;
574  // For proxy auth when it's handled by the Qt/KDE socket classes
575  QAuthenticator *m_socketProxyAuth;
576 
577  // Indicates whether there was some error.
578  int m_iError;
579  // Whether we are loading an error page (we should close the connection afterwards)
580  bool m_isLoadingErrorPage;
581 
582  // Values that determine the remote connection timeouts.
583  int m_remoteRespTimeout;
584 
585  QByteArray m_unreadBuf;
586  void clearUnreadBuffer();
587  void unread(char *buf, size_t size);
588  size_t readBuffered(char *buf, size_t size, bool unlimited = true);
589  bool readDelimitedText(char *buf, int *idx, int end, int numNewlines);
590 };
591 #endif
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 21:08:39 by doxygen 1.8.3.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.10.2 API Reference

Skip menu "kdelibs-4.10.2 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