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

KIOSlave

  • kioslave
  • http
http.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000-2003 Waldo Bastian <bastian@kde.org>
3  Copyright (C) 2000-2002 George Staikos <staikos@kde.org>
4  Copyright (C) 2000-2002 Dawit Alemayehu <adawit@kde.org>
5  Copyright (C) 2001,2002 Hamish Rodda <rodda@kde.org>
6  Copyright (C) 2007 Nick Shaforostoff <shafff@ukr.net>
7  Copyright (C) 2007 Daniel Nicoletti <mirttex@users.sourceforge.net>
8  Copyright (C) 2008,2009 Andreas Hartmetz <ahartmetz@gmail.com>
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Library General Public
12  License (LGPL) as published by the Free Software Foundation;
13  either version 2 of the License, or (at your option) any later
14  version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Library General Public License for more details.
20 
21  You should have received a copy of the GNU Library General Public License
22  along with this library; see the file COPYING.LIB. If not, write to
23  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  Boston, MA 02110-1301, USA.
25 */
26 
27 // TODO delete / do not save very big files; "very big" to be defined
28 
29 #define QT_NO_CAST_FROM_ASCII
30 
31 #include "http.h"
32 
33 #include <config.h>
34 
35 #include <fcntl.h>
36 #include <utime.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <unistd.h> // must be explicitly included for MacOSX
42 
43 #include <QtXml/qdom.h>
44 #include <QtCore/QFile>
45 #include <QtCore/QRegExp>
46 #include <QtCore/QDate>
47 #include <QtCore/QBuffer>
48 #include <QtCore/QIODevice>
49 #include <QtDBus/QtDBus>
50 #include <QtNetwork/QAuthenticator>
51 #include <QtNetwork/QNetworkProxy>
52 #include <QtNetwork/QTcpSocket>
53 
54 #include <kurl.h>
55 #include <kdebug.h>
56 #include <klocale.h>
57 #include <kconfig.h>
58 #include <kconfiggroup.h>
59 #include <kservice.h>
60 #include <kdatetime.h>
61 #include <kcomponentdata.h>
62 #include <kmimetype.h>
63 #include <ktoolinvocation.h>
64 #include <kstandarddirs.h>
65 #include <kremoteencoding.h>
66 #include <ktcpsocket.h>
67 #include <kmessagebox.h>
68 
69 #include <kio/ioslave_defaults.h>
70 #include <kio/http_slave_defaults.h>
71 
72 #include <httpfilter.h>
73 
74 #include <solid/networking.h>
75 
76 #include <kapplication.h>
77 #include <kaboutdata.h>
78 #include <kcmdlineargs.h>
79 #include <kde_file.h>
80 #include <ktemporaryfile.h>
81 
82 #include "httpauthentication.h"
83 
84 // HeaderTokenizer declarations
85 #include "parsinghelpers.h"
86 //string parsing helpers and HeaderTokenizer implementation
87 #include "parsinghelpers.cpp"
88 
89 // KDE5 TODO (QT5) : use QString::htmlEscape or whatever https://qt.gitorious.org/qt/qtbase/merge_requests/56
90 // ends up with.
91 static QString htmlEscape(const QString &plain)
92 {
93  QString rich;
94  rich.reserve(int(plain.length() * 1.1));
95  for (int i = 0; i < plain.length(); ++i) {
96  if (plain.at(i) == QLatin1Char('<'))
97  rich += QLatin1String("&lt;");
98  else if (plain.at(i) == QLatin1Char('>'))
99  rich += QLatin1String("&gt;");
100  else if (plain.at(i) == QLatin1Char('&'))
101  rich += QLatin1String("&amp;");
102  else if (plain.at(i) == QLatin1Char('"'))
103  rich += QLatin1String("&quot;");
104  else
105  rich += plain.at(i);
106  }
107  rich.squeeze();
108  return rich;
109 }
110 
111 static bool supportedProxyScheme(const QString& scheme)
112 {
113  return (scheme.startsWith(QLatin1String("http"), Qt::CaseInsensitive)
114  || scheme == QLatin1String("socks"));
115 }
116 
117 // see filenameFromUrl(): a sha1 hash is 160 bits
118 static const int s_hashedUrlBits = 160; // this number should always be divisible by eight
119 static const int s_hashedUrlNibbles = s_hashedUrlBits / 4;
120 static const int s_hashedUrlBytes = s_hashedUrlBits / 8;
121 static const int s_MaxInMemPostBufSize = 256 * 1024; // Write anyting over 256 KB to file...
122 
123 using namespace KIO;
124 
125 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
126 {
127  QCoreApplication app( argc, argv ); // needed for QSocketNotifier
128  KComponentData componentData( "kio_http", "kdelibs4" );
129  (void) KGlobal::locale();
130 
131  if (argc != 4)
132  {
133  fprintf(stderr, "Usage: kio_http protocol domain-socket1 domain-socket2\n");
134  exit(-1);
135  }
136 
137  HTTPProtocol slave(argv[1], argv[2], argv[3]);
138  slave.dispatchLoop();
139  return 0;
140 }
141 
142 /*********************************** Generic utility functions ********************/
143 
144 static QString toQString(const QByteArray& value)
145 {
146  return QString::fromLatin1(value.constData(), value.size());
147 }
148 
149 static bool isCrossDomainRequest( const QString& fqdn, const QString& originURL )
150 {
151  //TODO read the RFC
152  if (originURL == QLatin1String("true")) // Backwards compatibility
153  return true;
154 
155  KUrl url ( originURL );
156 
157  // Document Origin domain
158  QString a = url.host();
159  // Current request domain
160  QString b = fqdn;
161 
162  if (a == b)
163  return false;
164 
165  QStringList la = a.split(QLatin1Char('.'), QString::SkipEmptyParts);
166  QStringList lb = b.split(QLatin1Char('.'), QString::SkipEmptyParts);
167 
168  if (qMin(la.count(), lb.count()) < 2) {
169  return true; // better safe than sorry...
170  }
171 
172  while(la.count() > 2)
173  la.pop_front();
174  while(lb.count() > 2)
175  lb.pop_front();
176 
177  return la != lb;
178 }
179 
180 /*
181  Eliminates any custom header that could potentially alter the request
182 */
183 static QString sanitizeCustomHTTPHeader(const QString& _header)
184 {
185  QString sanitizedHeaders;
186  const QStringList headers = _header.split(QRegExp(QLatin1String("[\r\n]")));
187 
188  for(QStringList::ConstIterator it = headers.begin(); it != headers.end(); ++it)
189  {
190  // Do not allow Request line to be specified and ignore
191  // the other HTTP headers.
192  if (!(*it).contains(QLatin1Char(':')) ||
193  (*it).startsWith(QLatin1String("host"), Qt::CaseInsensitive) ||
194  (*it).startsWith(QLatin1String("proxy-authorization"), Qt::CaseInsensitive) ||
195  (*it).startsWith(QLatin1String("via"), Qt::CaseInsensitive))
196  continue;
197 
198  sanitizedHeaders += (*it);
199  sanitizedHeaders += QLatin1String("\r\n");
200  }
201  sanitizedHeaders.chop(2);
202 
203  return sanitizedHeaders;
204 }
205 
206 static bool isPotentialSpoofingAttack(const HTTPProtocol::HTTPRequest& request, const KConfigGroup* config)
207 {
208  // kDebug(7113) << request.url << "response code: " << request.responseCode << "previous response code:" << request.prevResponseCode;
209  if (config->readEntry("no-spoof-check", false)) {
210  return false;
211  }
212 
213  if (request.url.user().isEmpty()) {
214  return false;
215  }
216 
217  // NOTE: Workaround for brain dead clients that include "undefined" as
218  // username and password in the request URL (BR# 275033).
219  if (request.url.user() == QLatin1String("undefined") && request.url.pass() == QLatin1String("undefined")) {
220  return false;
221  }
222 
223  // We already have cached authentication.
224  if (config->readEntry(QLatin1String("cached-www-auth"), false)) {
225  return false;
226  }
227 
228  const QString userName = config->readEntry(QLatin1String("LastSpoofedUserName"), QString());
229  return ((userName.isEmpty() || userName != request.url.user()) && request.responseCode != 401 && request.prevResponseCode != 401);
230 }
231 
232 // for a given response code, conclude if the response is going to/likely to have a response body
233 static bool canHaveResponseBody(int responseCode, KIO::HTTP_METHOD method)
234 {
235 /* RFC 2616 says...
236  1xx: false
237  200: method HEAD: false, otherwise:true
238  201: true
239  202: true
240  203: see 200
241  204: false
242  205: false
243  206: true
244  300: see 200
245  301: see 200
246  302: see 200
247  303: see 200
248  304: false
249  305: probably like 300, RFC seems to expect disconnection afterwards...
250  306: (reserved), for simplicity do it just like 200
251  307: see 200
252  4xx: see 200
253  5xx :see 200
254 */
255  if (responseCode >= 100 && responseCode < 200) {
256  return false;
257  }
258  switch (responseCode) {
259  case 201:
260  case 202:
261  case 206:
262  // RFC 2616 does not mention HEAD in the description of the above. if the assert turns out
263  // to be a problem the response code should probably be treated just like 200 and friends.
264  Q_ASSERT(method != HTTP_HEAD);
265  return true;
266  case 204:
267  case 205:
268  case 304:
269  return false;
270  default:
271  break;
272  }
273  // safe (and for most remaining response codes exactly correct) default
274  return method != HTTP_HEAD;
275 }
276 
277 static bool isEncryptedHttpVariety(const QByteArray &p)
278 {
279  return p == "https" || p == "webdavs";
280 }
281 
282 static bool isValidProxy(const KUrl &u)
283 {
284  return u.isValid() && u.hasHost();
285 }
286 
287 static bool isHttpProxy(const KUrl &u)
288 {
289  return isValidProxy(u) && u.protocol() == QLatin1String("http");
290 }
291 
292 static QIODevice* createPostBufferDeviceFor (KIO::filesize_t size)
293 {
294  QIODevice* device;
295  if (size > static_cast<KIO::filesize_t>(s_MaxInMemPostBufSize))
296  device = new KTemporaryFile;
297  else
298  device = new QBuffer;
299 
300  if (!device->open(QIODevice::ReadWrite))
301  return 0;
302 
303  return device;
304 }
305 
306 QByteArray HTTPProtocol::HTTPRequest::methodString() const
307 {
308  if (!methodStringOverride.isEmpty())
309  return (methodStringOverride + QLatin1Char(' ')).toLatin1();
310 
311  switch(method) {
312  case HTTP_GET:
313  return "GET ";
314  case HTTP_PUT:
315  return "PUT ";
316  case HTTP_POST:
317  return "POST ";
318  case HTTP_HEAD:
319  return "HEAD ";
320  case HTTP_DELETE:
321  return "DELETE ";
322  case HTTP_OPTIONS:
323  return "OPTIONS ";
324  case DAV_PROPFIND:
325  return "PROPFIND ";
326  case DAV_PROPPATCH:
327  return "PROPPATCH ";
328  case DAV_MKCOL:
329  return "MKCOL ";
330  case DAV_COPY:
331  return "COPY ";
332  case DAV_MOVE:
333  return "MOVE ";
334  case DAV_LOCK:
335  return "LOCK ";
336  case DAV_UNLOCK:
337  return "UNLOCK ";
338  case DAV_SEARCH:
339  return "SEARCH ";
340  case DAV_SUBSCRIBE:
341  return "SUBSCRIBE ";
342  case DAV_UNSUBSCRIBE:
343  return "UNSUBSCRIBE ";
344  case DAV_POLL:
345  return "POLL ";
346  case DAV_NOTIFY:
347  return "NOTIFY ";
348  case DAV_REPORT:
349  return "REPORT ";
350  default:
351  Q_ASSERT(false);
352  return QByteArray();
353  }
354 }
355 
356 static QString formatHttpDate(qint64 date)
357 {
358  KDateTime dt;
359  dt.setTime_t(date);
360  QString ret = dt.toString(KDateTime::RFCDateDay);
361  ret.chop(6); // remove " +0000"
362  // RFCDate[Day] omits the second if zero, but HTTP requires it; see bug 240585.
363  if (!dt.time().second()) {
364  ret.append(QLatin1String(":00"));
365  }
366  ret.append(QLatin1String(" GMT"));
367  return ret;
368 }
369 
370 static bool isAuthenticationRequired(int responseCode)
371 {
372  return (responseCode == 401) || (responseCode == 407);
373 }
374 
375 #define NO_SIZE ((KIO::filesize_t) -1)
376 
377 #ifdef HAVE_STRTOLL
378 #define STRTOLL strtoll
379 #else
380 #define STRTOLL strtol
381 #endif
382 
383 
384 /************************************** HTTPProtocol **********************************************/
385 
386 
387 HTTPProtocol::HTTPProtocol( const QByteArray &protocol, const QByteArray &pool,
388  const QByteArray &app )
389  : TCPSlaveBase(protocol, pool, app, isEncryptedHttpVariety(protocol))
390  , m_iSize(NO_SIZE)
391  , m_iPostDataSize(NO_SIZE)
392  , m_isBusy(false)
393  , m_POSTbuf(0)
394  , m_maxCacheAge(DEFAULT_MAX_CACHE_AGE)
395  , m_maxCacheSize(DEFAULT_MAX_CACHE_SIZE)
396  , m_protocol(protocol)
397  , m_wwwAuth(0)
398  , m_proxyAuth(0)
399  , m_socketProxyAuth(0)
400  , m_iError(0)
401  , m_isLoadingErrorPage(false)
402  , m_remoteRespTimeout(DEFAULT_RESPONSE_TIMEOUT)
403 {
404  reparseConfiguration();
405  setBlocking(true);
406  connect(socket(), SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
407  this, SLOT(proxyAuthenticationForSocket(QNetworkProxy,QAuthenticator*)));
408 }
409 
410 HTTPProtocol::~HTTPProtocol()
411 {
412  httpClose(false);
413 }
414 
415 void HTTPProtocol::reparseConfiguration()
416 {
417  kDebug(7113);
418 
419  delete m_proxyAuth;
420  delete m_wwwAuth;
421  m_proxyAuth = 0;
422  m_wwwAuth = 0;
423  m_request.proxyUrl.clear(); //TODO revisit
424  m_request.proxyUrls.clear();
425 
426  TCPSlaveBase::reparseConfiguration();
427 }
428 
429 void HTTPProtocol::resetConnectionSettings()
430 {
431  m_isEOF = false;
432  m_iError = 0;
433  m_isLoadingErrorPage = false;
434 }
435 
436 quint16 HTTPProtocol::defaultPort() const
437 {
438  return isEncryptedHttpVariety(m_protocol) ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
439 }
440 
441 void HTTPProtocol::resetResponseParsing()
442 {
443  m_isRedirection = false;
444  m_isChunked = false;
445  m_iSize = NO_SIZE;
446  clearUnreadBuffer();
447 
448  m_responseHeaders.clear();
449  m_contentEncodings.clear();
450  m_transferEncodings.clear();
451  m_contentMD5.clear();
452  m_mimeType.clear();
453 
454  setMetaData(QLatin1String("request-id"), m_request.id);
455 }
456 
457 void HTTPProtocol::resetSessionSettings()
458 {
459  // Follow HTTP/1.1 spec and enable keep-alive by default
460  // unless the remote side tells us otherwise or we determine
461  // the persistent link has been terminated by the remote end.
462  m_request.isKeepAlive = true;
463  m_request.keepAliveTimeout = 0;
464 
465  m_request.redirectUrl = KUrl();
466  m_request.useCookieJar = config()->readEntry("Cookies", false);
467  m_request.cacheTag.useCache = config()->readEntry("UseCache", true);
468  m_request.preferErrorPage = config()->readEntry("errorPage", true);
469  m_request.doNotAuthenticate = config()->readEntry("no-auth", false);
470  m_strCacheDir = config()->readPathEntry("CacheDir", QString());
471  m_maxCacheAge = config()->readEntry("MaxCacheAge", DEFAULT_MAX_CACHE_AGE);
472  m_request.windowId = config()->readEntry("window-id");
473 
474  m_request.methodStringOverride = metaData(QLatin1String("CustomHTTPMethod"));
475 
476  kDebug(7113) << "Window Id =" << m_request.windowId;
477  kDebug(7113) << "ssl_was_in_use =" << metaData(QLatin1String("ssl_was_in_use"));
478 
479  m_request.referrer.clear();
480  // RFC 2616: do not send the referrer if the referrer page was served using SSL and
481  // the current page does not use SSL.
482  if ( config()->readEntry("SendReferrer", true) &&
483  (isEncryptedHttpVariety(m_protocol) || metaData(QLatin1String("ssl_was_in_use")) != QLatin1String("TRUE") ) )
484  {
485  KUrl refUrl(metaData(QLatin1String("referrer")));
486  if (refUrl.isValid()) {
487  // Sanitize
488  QString protocol = refUrl.protocol();
489  if (protocol.startsWith(QLatin1String("webdav"))) {
490  protocol.replace(0, 6, QLatin1String("http"));
491  refUrl.setProtocol(protocol);
492  }
493 
494  if (protocol.startsWith(QLatin1String("http"))) {
495  m_request.referrer = toQString(refUrl.toEncoded(QUrl::RemoveUserInfo | QUrl::RemoveFragment));
496  }
497  }
498  }
499 
500  if (config()->readEntry("SendLanguageSettings", true)) {
501  m_request.charsets = config()->readEntry("Charsets", DEFAULT_PARTIAL_CHARSET_HEADER);
502  if (!m_request.charsets.contains(QLatin1String("*;"), Qt::CaseInsensitive)) {
503  m_request.charsets += QLatin1String(",*;q=0.5");
504  }
505  m_request.languages = config()->readEntry("Languages", DEFAULT_LANGUAGE_HEADER);
506  } else {
507  m_request.charsets.clear();
508  m_request.languages.clear();
509  }
510 
511  // Adjust the offset value based on the "resume" meta-data.
512  QString resumeOffset = metaData(QLatin1String("resume"));
513  if (!resumeOffset.isEmpty()) {
514  m_request.offset = resumeOffset.toULongLong();
515  } else {
516  m_request.offset = 0;
517  }
518  // Same procedure for endoffset.
519  QString resumeEndOffset = metaData(QLatin1String("resume_until"));
520  if (!resumeEndOffset.isEmpty()) {
521  m_request.endoffset = resumeEndOffset.toULongLong();
522  } else {
523  m_request.endoffset = 0;
524  }
525 
526  m_request.disablePassDialog = config()->readEntry("DisablePassDlg", false);
527  m_request.allowTransferCompression = config()->readEntry("AllowCompressedPage", true);
528  m_request.id = metaData(QLatin1String("request-id"));
529 
530  // Store user agent for this host.
531  if (config()->readEntry("SendUserAgent", true)) {
532  m_request.userAgent = metaData(QLatin1String("UserAgent"));
533  } else {
534  m_request.userAgent.clear();
535  }
536 
537  m_request.cacheTag.etag.clear();
538  // -1 is also the value returned by KDateTime::toTime_t() from an invalid instance.
539  m_request.cacheTag.servedDate = -1;
540  m_request.cacheTag.lastModifiedDate = -1;
541  m_request.cacheTag.expireDate = -1;
542 
543  m_request.responseCode = 0;
544  m_request.prevResponseCode = 0;
545 
546  delete m_wwwAuth;
547  m_wwwAuth = 0;
548  delete m_socketProxyAuth;
549  m_socketProxyAuth = 0;
550 
551  // Obtain timeout values
552  m_remoteRespTimeout = responseTimeout();
553 
554  // Bounce back the actual referrer sent
555  setMetaData(QLatin1String("referrer"), m_request.referrer);
556 
557  // Reset the post data size
558  m_iPostDataSize = NO_SIZE;
559 }
560 
561 void HTTPProtocol::setHost( const QString& host, quint16 port,
562  const QString& user, const QString& pass )
563 {
564  // Reset the webdav-capable flags for this host
565  if ( m_request.url.host() != host )
566  m_davHostOk = m_davHostUnsupported = false;
567 
568  m_request.url.setHost(host);
569 
570  // is it an IPv6 address?
571  if (host.indexOf(QLatin1Char(':')) == -1) {
572  m_request.encoded_hostname = toQString(QUrl::toAce(host));
573  } else {
574  int pos = host.indexOf(QLatin1Char('%'));
575  if (pos == -1)
576  m_request.encoded_hostname = QLatin1Char('[') + host + QLatin1Char(']');
577  else
578  // don't send the scope-id in IPv6 addresses to the server
579  m_request.encoded_hostname = QLatin1Char('[') + host.left(pos) + QLatin1Char(']');
580  }
581  m_request.url.setPort((port > 0 && port != defaultPort()) ? port : -1);
582  m_request.url.setUser(user);
583  m_request.url.setPass(pass);
584 
585  // On new connection always clear previous proxy information...
586  m_request.proxyUrl.clear();
587  m_request.proxyUrls.clear();
588 
589  kDebug(7113) << "Hostname is now:" << m_request.url.host()
590  << "(" << m_request.encoded_hostname << ")";
591 }
592 
593 bool HTTPProtocol::maybeSetRequestUrl(const KUrl &u)
594 {
595  kDebug (7113) << u.url();
596 
597  m_request.url = u;
598  m_request.url.setPort(u.port(defaultPort()) != defaultPort() ? u.port() : -1);
599 
600  if (u.host().isEmpty()) {
601  error( KIO::ERR_UNKNOWN_HOST, i18n("No host specified."));
602  return false;
603  }
604 
605  if (u.path().isEmpty()) {
606  KUrl newUrl(u);
607  newUrl.setPath(QLatin1String("/"));
608  redirection(newUrl);
609  finished();
610  return false;
611  }
612 
613  return true;
614 }
615 
616 void HTTPProtocol::proceedUntilResponseContent( bool dataInternal /* = false */ )
617 {
618  kDebug (7113);
619 
620  const bool status = (proceedUntilResponseHeader() && readBody(dataInternal));
621 
622  // If not an error condition or internal request, close
623  // the connection based on the keep alive settings...
624  if (!m_iError && !dataInternal) {
625  httpClose(m_request.isKeepAlive);
626  }
627 
628  // if data is required internally or we got error, don't finish,
629  // it is processed before we finish()
630  if (dataInternal || !status) {
631  return;
632  }
633 
634  if (!sendHttpError()) {
635  finished();
636  }
637 }
638 
639 bool HTTPProtocol::proceedUntilResponseHeader()
640 {
641  kDebug (7113);
642 
643  // Retry the request until it succeeds or an unrecoverable error occurs.
644  // Recoverable errors are, for example:
645  // - Proxy or server authentication required: Ask for credentials and try again,
646  // this time with an authorization header in the request.
647  // - Server-initiated timeout on keep-alive connection: Reconnect and try again
648 
649  while (true) {
650  if (!sendQuery()) {
651  return false;
652  }
653  if (readResponseHeader()) {
654  // Success, finish the request.
655  break;
656  }
657 
658  // If not loading error page and the response code requires us to resend the query,
659  // then throw away any error message that might have been sent by the server.
660  if (!m_isLoadingErrorPage && isAuthenticationRequired(m_request.responseCode)) {
661  // This gets rid of any error page sent with 401 or 407 authentication required response...
662  readBody(true);
663  }
664 
665  // no success, close the cache file so the cache state is reset - that way most other code
666  // doesn't have to deal with the cache being in various states.
667  cacheFileClose();
668  if (m_iError || m_isLoadingErrorPage) {
669  // Unrecoverable error, abort everything.
670  // Also, if we've just loaded an error page there is nothing more to do.
671  // In that case we abort to avoid loops; some webservers manage to send 401 and
672  // no authentication request. Or an auth request we don't understand.
673  return false;
674  }
675 
676  if (!m_request.isKeepAlive) {
677  httpCloseConnection();
678  m_request.isKeepAlive = true;
679  m_request.keepAliveTimeout = 0;
680  }
681  }
682 
683  // Do not save authorization if the current response code is
684  // 4xx (client error) or 5xx (server error).
685  kDebug(7113) << "Previous Response:" << m_request.prevResponseCode;
686  kDebug(7113) << "Current Response:" << m_request.responseCode;
687 
688  setMetaData(QLatin1String("responsecode"), QString::number(m_request.responseCode));
689  setMetaData(QLatin1String("content-type"), m_mimeType);
690 
691  // At this point sendBody() should have delivered any POST data.
692  clearPostDataBuffer();
693 
694  return true;
695 }
696 
697 void HTTPProtocol::stat(const KUrl& url)
698 {
699  kDebug(7113) << url.url();
700 
701  if (!maybeSetRequestUrl(url))
702  return;
703  resetSessionSettings();
704 
705  if ( m_protocol != "webdav" && m_protocol != "webdavs" )
706  {
707  QString statSide = metaData(QLatin1String("statSide"));
708  if (statSide != QLatin1String("source"))
709  {
710  // When uploading we assume the file doesn't exit
711  error( ERR_DOES_NOT_EXIST, url.prettyUrl() );
712  return;
713  }
714 
715  // When downloading we assume it exists
716  UDSEntry entry;
717  entry.insert( KIO::UDSEntry::UDS_NAME, url.fileName() );
718  entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG ); // a file
719  entry.insert( KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IRGRP | S_IROTH ); // readable by everybody
720 
721  statEntry( entry );
722  finished();
723  return;
724  }
725 
726  davStatList( url );
727 }
728 
729 void HTTPProtocol::listDir( const KUrl& url )
730 {
731  kDebug(7113) << url.url();
732 
733  if (!maybeSetRequestUrl(url))
734  return;
735  resetSessionSettings();
736 
737  davStatList( url, false );
738 }
739 
740 void HTTPProtocol::davSetRequest( const QByteArray& requestXML )
741 {
742  // insert the document into the POST buffer, kill trailing zero byte
743  cachePostData(requestXML);
744 }
745 
746 void HTTPProtocol::davStatList( const KUrl& url, bool stat )
747 {
748  UDSEntry entry;
749 
750  // check to make sure this host supports WebDAV
751  if ( !davHostOk() )
752  return;
753 
754  // Maybe it's a disguised SEARCH...
755  QString query = metaData(QLatin1String("davSearchQuery"));
756  if ( !query.isEmpty() )
757  {
758  QByteArray request = "<?xml version=\"1.0\"?>\r\n";
759  request.append( "<D:searchrequest xmlns:D=\"DAV:\">\r\n" );
760  request.append( query.toUtf8() );
761  request.append( "</D:searchrequest>\r\n" );
762 
763  davSetRequest( request );
764  } else {
765  // We are only after certain features...
766  QByteArray request;
767  request = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
768  "<D:propfind xmlns:D=\"DAV:\">";
769 
770  // insert additional XML request from the davRequestResponse metadata
771  if ( hasMetaData(QLatin1String("davRequestResponse")) )
772  request += metaData(QLatin1String("davRequestResponse")).toUtf8();
773  else {
774  // No special request, ask for default properties
775  request += "<D:prop>"
776  "<D:creationdate/>"
777  "<D:getcontentlength/>"
778  "<D:displayname/>"
779  "<D:source/>"
780  "<D:getcontentlanguage/>"
781  "<D:getcontenttype/>"
782  "<D:getlastmodified/>"
783  "<D:getetag/>"
784  "<D:supportedlock/>"
785  "<D:lockdiscovery/>"
786  "<D:resourcetype/>"
787  "</D:prop>";
788  }
789  request += "</D:propfind>";
790 
791  davSetRequest( request );
792  }
793 
794  // WebDAV Stat or List...
795  m_request.method = query.isEmpty() ? DAV_PROPFIND : DAV_SEARCH;
796  m_request.url.setQuery(QString());
797  m_request.cacheTag.policy = CC_Reload;
798  m_request.davData.depth = stat ? 0 : 1;
799  if (!stat)
800  m_request.url.adjustPath(KUrl::AddTrailingSlash);
801 
802  proceedUntilResponseContent( true );
803  infoMessage(QLatin1String(""));
804 
805  // Has a redirection already been called? If so, we're done.
806  if (m_isRedirection || m_iError) {
807  if (m_isRedirection) {
808  davFinished();
809  }
810  return;
811  }
812 
813  QDomDocument multiResponse;
814  multiResponse.setContent( m_webDavDataBuf, true );
815 
816  bool hasResponse = false;
817 
818  // kDebug(7113) << endl << multiResponse.toString(2);
819 
820  for ( QDomNode n = multiResponse.documentElement().firstChild();
821  !n.isNull(); n = n.nextSibling()) {
822  QDomElement thisResponse = n.toElement();
823  if (thisResponse.isNull())
824  continue;
825 
826  hasResponse = true;
827 
828  QDomElement href = thisResponse.namedItem(QLatin1String("href")).toElement();
829  if ( !href.isNull() ) {
830  entry.clear();
831 
832  QString urlStr = QUrl::fromPercentEncoding(href.text().toUtf8());
833 #if 0 // qt4/kde4 say: it's all utf8...
834  int encoding = remoteEncoding()->encodingMib();
835  if ((encoding == 106) && (!KStringHandler::isUtf8(KUrl::decode_string(urlStr, 4).toLatin1())))
836  encoding = 4; // Use latin1 if the file is not actually utf-8
837 
838  KUrl thisURL ( urlStr, encoding );
839 #else
840  KUrl thisURL( urlStr );
841 #endif
842 
843  if ( thisURL.isValid() ) {
844  QString name = thisURL.fileName();
845 
846  // base dir of a listDir(): name should be "."
847  if ( !stat && thisURL.path(KUrl::AddTrailingSlash).length() == url.path(KUrl::AddTrailingSlash).length() )
848  name = QLatin1Char('.');
849 
850  entry.insert( KIO::UDSEntry::UDS_NAME, name.isEmpty() ? href.text() : name );
851  }
852 
853  QDomNodeList propstats = thisResponse.elementsByTagName(QLatin1String("propstat"));
854 
855  davParsePropstats( propstats, entry );
856 
857  // Since a lot of webdav servers seem not to send the content-type information
858  // for the requested directory listings, we attempt to guess the mime-type from
859  // the resource name so long as the resource is not a directory.
860  if (entry.stringValue(KIO::UDSEntry::UDS_MIME_TYPE).isEmpty() &&
861  entry.numberValue(KIO::UDSEntry::UDS_FILE_TYPE) != S_IFDIR) {
862  int accuracy = 0;
863  KMimeType::Ptr mime = KMimeType::findByUrl(thisURL.fileName(), 0, false, true, &accuracy);
864  if (mime && !mime->isDefault() && accuracy == 100) {
865  kDebug(7113) << "Setting" << mime->name() << "as guessed mime type for" << thisURL.fileName();
866  entry.insert( KIO::UDSEntry::UDS_GUESSED_MIME_TYPE, mime->name());
867  }
868  }
869 
870  if ( stat ) {
871  // return an item
872  statEntry( entry );
873  davFinished();
874  return;
875  }
876 
877  listEntry( entry, false );
878  } else {
879  kDebug(7113) << "Error: no URL contained in response to PROPFIND on" << url;
880  }
881  }
882 
883  if ( stat || !hasResponse ) {
884  error( ERR_DOES_NOT_EXIST, url.prettyUrl() );
885  return;
886  }
887 
888  listEntry( entry, true );
889  davFinished();
890 }
891 
892 void HTTPProtocol::davGeneric( const KUrl& url, KIO::HTTP_METHOD method, qint64 size )
893 {
894  kDebug(7113) << url.url();
895 
896  if (!maybeSetRequestUrl(url))
897  return;
898  resetSessionSettings();
899 
900  // check to make sure this host supports WebDAV
901  if ( !davHostOk() )
902  return;
903 
904  // WebDAV method
905  m_request.method = method;
906  m_request.url.setQuery(QString());
907  m_request.cacheTag.policy = CC_Reload;
908 
909  m_iPostDataSize = (size > -1 ? static_cast<KIO::filesize_t>(size) : NO_SIZE);
910  proceedUntilResponseContent();
911 }
912 
913 int HTTPProtocol::codeFromResponse( const QString& response )
914 {
915  const int firstSpace = response.indexOf( QLatin1Char(' ') );
916  const int secondSpace = response.indexOf( QLatin1Char(' '), firstSpace + 1 );
917  return response.mid( firstSpace + 1, secondSpace - firstSpace - 1 ).toInt();
918 }
919 
920 void HTTPProtocol::davParsePropstats( const QDomNodeList& propstats, UDSEntry& entry )
921 {
922  QString mimeType;
923  bool foundExecutable = false;
924  bool isDirectory = false;
925  uint lockCount = 0;
926  uint supportedLockCount = 0;
927 
928  for ( int i = 0; i < propstats.count(); i++)
929  {
930  QDomElement propstat = propstats.item(i).toElement();
931 
932  QDomElement status = propstat.namedItem(QLatin1String("status")).toElement();
933  if ( status.isNull() )
934  {
935  // error, no status code in this propstat
936  kDebug(7113) << "Error, no status code in this propstat";
937  return;
938  }
939 
940  int code = codeFromResponse( status.text() );
941 
942  if ( code != 200 )
943  {
944  kDebug(7113) << "Got status code" << code << "(this may mean that some properties are unavailable)";
945  continue;
946  }
947 
948  QDomElement prop = propstat.namedItem( QLatin1String("prop") ).toElement();
949  if ( prop.isNull() )
950  {
951  kDebug(7113) << "Error: no prop segment in this propstat.";
952  return;
953  }
954 
955  if ( hasMetaData( QLatin1String("davRequestResponse") ) )
956  {
957  QDomDocument doc;
958  doc.appendChild(prop);
959  entry.insert( KIO::UDSEntry::UDS_XML_PROPERTIES, doc.toString() );
960  }
961 
962  for ( QDomNode n = prop.firstChild(); !n.isNull(); n = n.nextSibling() )
963  {
964  QDomElement property = n.toElement();
965  if (property.isNull())
966  continue;
967 
968  if ( property.namespaceURI() != QLatin1String("DAV:") )
969  {
970  // break out - we're only interested in properties from the DAV namespace
971  continue;
972  }
973 
974  if ( property.tagName() == QLatin1String("creationdate") )
975  {
976  // Resource creation date. Should be is ISO 8601 format.
977  entry.insert( KIO::UDSEntry::UDS_CREATION_TIME, parseDateTime( property.text(), property.attribute(QLatin1String("dt")) ) );
978  }
979  else if ( property.tagName() == QLatin1String("getcontentlength") )
980  {
981  // Content length (file size)
982  entry.insert( KIO::UDSEntry::UDS_SIZE, property.text().toULong() );
983  }
984  else if ( property.tagName() == QLatin1String("displayname") )
985  {
986  // Name suitable for presentation to the user
987  setMetaData( QLatin1String("davDisplayName"), property.text() );
988  }
989  else if ( property.tagName() == QLatin1String("source") )
990  {
991  // Source template location
992  QDomElement source = property.namedItem( QLatin1String("link") ).toElement()
993  .namedItem( QLatin1String("dst") ).toElement();
994  if ( !source.isNull() )
995  setMetaData( QLatin1String("davSource"), source.text() );
996  }
997  else if ( property.tagName() == QLatin1String("getcontentlanguage") )
998  {
999  // equiv. to Content-Language header on a GET
1000  setMetaData( QLatin1String("davContentLanguage"), property.text() );
1001  }
1002  else if ( property.tagName() == QLatin1String("getcontenttype") )
1003  {
1004  // Content type (mime type)
1005  // This may require adjustments for other server-side webdav implementations
1006  // (tested with Apache + mod_dav 1.0.3)
1007  if ( property.text() == QLatin1String("httpd/unix-directory") )
1008  {
1009  isDirectory = true;
1010  }
1011  else
1012  {
1013  mimeType = property.text();
1014  }
1015  }
1016  else if ( property.tagName() == QLatin1String("executable") )
1017  {
1018  // File executable status
1019  if ( property.text() == QLatin1String("T") )
1020  foundExecutable = true;
1021 
1022  }
1023  else if ( property.tagName() == QLatin1String("getlastmodified") )
1024  {
1025  // Last modification date
1026  entry.insert( KIO::UDSEntry::UDS_MODIFICATION_TIME, parseDateTime( property.text(), property.attribute(QLatin1String("dt")) ) );
1027  }
1028  else if ( property.tagName() == QLatin1String("getetag") )
1029  {
1030  // Entity tag
1031  setMetaData( QLatin1String("davEntityTag"), property.text() );
1032  }
1033  else if ( property.tagName() == QLatin1String("supportedlock") )
1034  {
1035  // Supported locking specifications
1036  for ( QDomNode n2 = property.firstChild(); !n2.isNull(); n2 = n2.nextSibling() )
1037  {
1038  QDomElement lockEntry = n2.toElement();
1039  if ( lockEntry.tagName() == QLatin1String("lockentry") )
1040  {
1041  QDomElement lockScope = lockEntry.namedItem( QLatin1String("lockscope") ).toElement();
1042  QDomElement lockType = lockEntry.namedItem( QLatin1String("locktype") ).toElement();
1043  if ( !lockScope.isNull() && !lockType.isNull() )
1044  {
1045  // Lock type was properly specified
1046  supportedLockCount++;
1047  const QString lockCountStr = QString::number(supportedLockCount);
1048  const QString scope = lockScope.firstChild().toElement().tagName();
1049  const QString type = lockType.firstChild().toElement().tagName();
1050 
1051  setMetaData( QLatin1String("davSupportedLockScope") + lockCountStr, scope );
1052  setMetaData( QLatin1String("davSupportedLockType") + lockCountStr, type );
1053  }
1054  }
1055  }
1056  }
1057  else if ( property.tagName() == QLatin1String("lockdiscovery") )
1058  {
1059  // Lists the available locks
1060  davParseActiveLocks( property.elementsByTagName( QLatin1String("activelock") ), lockCount );
1061  }
1062  else if ( property.tagName() == QLatin1String("resourcetype") )
1063  {
1064  // Resource type. "Specifies the nature of the resource."
1065  if ( !property.namedItem( QLatin1String("collection") ).toElement().isNull() )
1066  {
1067  // This is a collection (directory)
1068  isDirectory = true;
1069  }
1070  }
1071  else
1072  {
1073  kDebug(7113) << "Found unknown webdav property:" << property.tagName();
1074  }
1075  }
1076  }
1077 
1078  setMetaData( QLatin1String("davLockCount"), QString::number(lockCount) );
1079  setMetaData( QLatin1String("davSupportedLockCount"), QString::number(supportedLockCount) );
1080 
1081  entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, isDirectory ? S_IFDIR : S_IFREG );
1082 
1083  if ( foundExecutable || isDirectory )
1084  {
1085  // File was executable, or is a directory.
1086  entry.insert( KIO::UDSEntry::UDS_ACCESS, 0700 );
1087  }
1088  else
1089  {
1090  entry.insert( KIO::UDSEntry::UDS_ACCESS, 0600 );
1091  }
1092 
1093  if ( !isDirectory && !mimeType.isEmpty() )
1094  {
1095  entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, mimeType );
1096  }
1097 }
1098 
1099 void HTTPProtocol::davParseActiveLocks( const QDomNodeList& activeLocks,
1100  uint& lockCount )
1101 {
1102  for ( int i = 0; i < activeLocks.count(); i++ )
1103  {
1104  const QDomElement activeLock = activeLocks.item(i).toElement();
1105 
1106  lockCount++;
1107  // required
1108  const QDomElement lockScope = activeLock.namedItem( QLatin1String("lockscope") ).toElement();
1109  const QDomElement lockType = activeLock.namedItem( QLatin1String("locktype") ).toElement();
1110  const QDomElement lockDepth = activeLock.namedItem( QLatin1String("depth") ).toElement();
1111  // optional
1112  const QDomElement lockOwner = activeLock.namedItem( QLatin1String("owner") ).toElement();
1113  const QDomElement lockTimeout = activeLock.namedItem( QLatin1String("timeout") ).toElement();
1114  const QDomElement lockToken = activeLock.namedItem( QLatin1String("locktoken") ).toElement();
1115 
1116  if ( !lockScope.isNull() && !lockType.isNull() && !lockDepth.isNull() )
1117  {
1118  // lock was properly specified
1119  lockCount++;
1120  const QString lockCountStr = QString::number(lockCount);
1121  const QString scope = lockScope.firstChild().toElement().tagName();
1122  const QString type = lockType.firstChild().toElement().tagName();
1123  const QString depth = lockDepth.text();
1124 
1125  setMetaData( QLatin1String("davLockScope") + lockCountStr, scope );
1126  setMetaData( QLatin1String("davLockType") + lockCountStr, type );
1127  setMetaData( QLatin1String("davLockDepth") + lockCountStr, depth );
1128 
1129  if ( !lockOwner.isNull() )
1130  setMetaData( QLatin1String("davLockOwner") + lockCountStr, lockOwner.text() );
1131 
1132  if ( !lockTimeout.isNull() )
1133  setMetaData( QLatin1String("davLockTimeout") + lockCountStr, lockTimeout.text() );
1134 
1135  if ( !lockToken.isNull() )
1136  {
1137  QDomElement tokenVal = lockScope.namedItem( QLatin1String("href") ).toElement();
1138  if ( !tokenVal.isNull() )
1139  setMetaData( QLatin1String("davLockToken") + lockCountStr, tokenVal.text() );
1140  }
1141  }
1142  }
1143 }
1144 
1145 long HTTPProtocol::parseDateTime( const QString& input, const QString& type )
1146 {
1147  if ( type == QLatin1String("dateTime.tz") )
1148  {
1149  return KDateTime::fromString( input, KDateTime::ISODate ).toTime_t();
1150  }
1151  else if ( type == QLatin1String("dateTime.rfc1123") )
1152  {
1153  return KDateTime::fromString( input, KDateTime::RFCDate ).toTime_t();
1154  }
1155 
1156  // format not advertised... try to parse anyway
1157  time_t time = KDateTime::fromString( input, KDateTime::RFCDate ).toTime_t();
1158  if ( time != 0 )
1159  return time;
1160 
1161  return KDateTime::fromString( input, KDateTime::ISODate ).toTime_t();
1162 }
1163 
1164 QString HTTPProtocol::davProcessLocks()
1165 {
1166  if ( hasMetaData( QLatin1String("davLockCount") ) )
1167  {
1168  QString response = QLatin1String("If:");
1169  int numLocks = metaData( QLatin1String("davLockCount") ).toInt();
1170  bool bracketsOpen = false;
1171  for ( int i = 0; i < numLocks; i++ )
1172  {
1173  const QString countStr = QString::number(i);
1174  if ( hasMetaData( QLatin1String("davLockToken") + countStr ) )
1175  {
1176  if ( hasMetaData( QLatin1String("davLockURL") + countStr ) )
1177  {
1178  if ( bracketsOpen )
1179  {
1180  response += QLatin1Char(')');
1181  bracketsOpen = false;
1182  }
1183  response += QLatin1String(" <") + metaData( QLatin1String("davLockURL") + countStr ) + QLatin1Char('>');
1184  }
1185 
1186  if ( !bracketsOpen )
1187  {
1188  response += QLatin1String(" (");
1189  bracketsOpen = true;
1190  }
1191  else
1192  {
1193  response += QLatin1Char(' ');
1194  }
1195 
1196  if ( hasMetaData( QLatin1String("davLockNot") + countStr ) )
1197  response += QLatin1String("Not ");
1198 
1199  response += QLatin1Char('<') + metaData( QLatin1String("davLockToken") + countStr ) + QLatin1Char('>');
1200  }
1201  }
1202 
1203  if ( bracketsOpen )
1204  response += QLatin1Char(')');
1205 
1206  response += QLatin1String("\r\n");
1207  return response;
1208  }
1209 
1210  return QString();
1211 }
1212 
1213 bool HTTPProtocol::davHostOk()
1214 {
1215  // FIXME needs to be reworked. Switched off for now.
1216  return true;
1217 
1218  // cached?
1219  if ( m_davHostOk )
1220  {
1221  kDebug(7113) << "true";
1222  return true;
1223  }
1224  else if ( m_davHostUnsupported )
1225  {
1226  kDebug(7113) << " false";
1227  davError( -2 );
1228  return false;
1229  }
1230 
1231  m_request.method = HTTP_OPTIONS;
1232 
1233  // query the server's capabilities generally, not for a specific URL
1234  m_request.url.setPath(QLatin1String("*"));
1235  m_request.url.setQuery(QString());
1236  m_request.cacheTag.policy = CC_Reload;
1237 
1238  // clear davVersions variable, which holds the response to the DAV: header
1239  m_davCapabilities.clear();
1240 
1241  proceedUntilResponseHeader();
1242 
1243  if (m_davCapabilities.count())
1244  {
1245  for (int i = 0; i < m_davCapabilities.count(); i++)
1246  {
1247  bool ok;
1248  uint verNo = m_davCapabilities[i].toUInt(&ok);
1249  if (ok && verNo > 0 && verNo < 3)
1250  {
1251  m_davHostOk = true;
1252  kDebug(7113) << "Server supports DAV version" << verNo;
1253  }
1254  }
1255 
1256  if ( m_davHostOk )
1257  return true;
1258  }
1259 
1260  m_davHostUnsupported = true;
1261  davError( -2 );
1262  return false;
1263 }
1264 
1265 // This function is for closing proceedUntilResponseHeader(); requests
1266 // Required because there may or may not be further info expected
1267 void HTTPProtocol::davFinished()
1268 {
1269  // TODO: Check with the DAV extension developers
1270  httpClose(m_request.isKeepAlive);
1271  finished();
1272 }
1273 
1274 void HTTPProtocol::mkdir( const KUrl& url, int )
1275 {
1276  kDebug(7113) << url.url();
1277 
1278  if (!maybeSetRequestUrl(url))
1279  return;
1280  resetSessionSettings();
1281 
1282  m_request.method = DAV_MKCOL;
1283  m_request.url.setQuery(QString());
1284  m_request.cacheTag.policy = CC_Reload;
1285 
1286  proceedUntilResponseHeader();
1287 
1288  if ( m_request.responseCode == 201 )
1289  davFinished();
1290  else
1291  davError();
1292 }
1293 
1294 void HTTPProtocol::get( const KUrl& url )
1295 {
1296  kDebug(7113) << url.url();
1297 
1298  if (!maybeSetRequestUrl(url))
1299  return;
1300  resetSessionSettings();
1301 
1302  m_request.method = HTTP_GET;
1303 
1304  QString tmp(metaData(QLatin1String("cache")));
1305  if (!tmp.isEmpty())
1306  m_request.cacheTag.policy = parseCacheControl(tmp);
1307  else
1308  m_request.cacheTag.policy = DEFAULT_CACHE_CONTROL;
1309 
1310  proceedUntilResponseContent();
1311 }
1312 
1313 void HTTPProtocol::put( const KUrl &url, int, KIO::JobFlags flags )
1314 {
1315  kDebug(7113) << url.url();
1316 
1317  if (!maybeSetRequestUrl(url))
1318  return;
1319 
1320  resetSessionSettings();
1321 
1322  // Webdav hosts are capable of observing overwrite == false
1323  if (m_protocol.startsWith("webdav")) { // krazy:exclude=strings
1324  if (!(flags & KIO::Overwrite)) {
1325  // check to make sure this host supports WebDAV
1326  if (!davHostOk())
1327  return;
1328 
1329  const QByteArray request ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
1330  "<D:propfind xmlns:D=\"DAV:\"><D:prop>"
1331  "<D:creationdate/>"
1332  "<D:getcontentlength/>"
1333  "<D:displayname/>"
1334  "<D:resourcetype/>"
1335  "</D:prop></D:propfind>");
1336 
1337  davSetRequest( request );
1338 
1339  // WebDAV Stat or List...
1340  m_request.method = DAV_PROPFIND;
1341  m_request.url.setQuery(QString());
1342  m_request.cacheTag.policy = CC_Reload;
1343  m_request.davData.depth = 0;
1344 
1345  proceedUntilResponseContent(true);
1346 
1347  if (!m_request.isKeepAlive) {
1348  httpCloseConnection(); // close connection if server requested it.
1349  m_request.isKeepAlive = true; // reset the keep alive flag.
1350  }
1351 
1352  if (m_request.responseCode == 207) {
1353  error(ERR_FILE_ALREADY_EXIST, QString());
1354  return;
1355  }
1356 
1357  // force re-authentication...
1358  delete m_wwwAuth;
1359  m_wwwAuth = 0;
1360  }
1361  }
1362 
1363  m_request.method = HTTP_PUT;
1364  m_request.cacheTag.policy = CC_Reload;
1365 
1366  proceedUntilResponseContent();
1367 }
1368 
1369 void HTTPProtocol::copy( const KUrl& src, const KUrl& dest, int, KIO::JobFlags flags )
1370 {
1371  kDebug(7113) << src.url() << "->" << dest.url();
1372 
1373  if (!maybeSetRequestUrl(dest) || !maybeSetRequestUrl(src))
1374  return;
1375  resetSessionSettings();
1376 
1377  // destination has to be "http(s)://..."
1378  KUrl newDest = dest;
1379  if (newDest.protocol() == QLatin1String("webdavs"))
1380  newDest.setProtocol(QLatin1String("https"));
1381  else if (newDest.protocol() == QLatin1String("webdav"))
1382  newDest.setProtocol(QLatin1String("http"));
1383 
1384  m_request.method = DAV_COPY;
1385  m_request.davData.desturl = newDest.url();
1386  m_request.davData.overwrite = (flags & KIO::Overwrite);
1387  m_request.url.setQuery(QString());
1388  m_request.cacheTag.policy = CC_Reload;
1389 
1390  proceedUntilResponseHeader();
1391 
1392  // The server returns a HTTP/1.1 201 Created or 204 No Content on successful completion
1393  if ( m_request.responseCode == 201 || m_request.responseCode == 204 )
1394  davFinished();
1395  else
1396  davError();
1397 }
1398 
1399 void HTTPProtocol::rename( const KUrl& src, const KUrl& dest, KIO::JobFlags flags )
1400 {
1401  kDebug(7113) << src.url() << "->" << dest.url();
1402 
1403  if (!maybeSetRequestUrl(dest) || !maybeSetRequestUrl(src))
1404  return;
1405  resetSessionSettings();
1406 
1407  // destination has to be "http://..."
1408  KUrl newDest = dest;
1409  if (newDest.protocol() == QLatin1String("webdavs"))
1410  newDest.setProtocol(QLatin1String("https"));
1411  else if (newDest.protocol() == QLatin1String("webdav"))
1412  newDest.setProtocol(QLatin1String("http"));
1413 
1414  m_request.method = DAV_MOVE;
1415  m_request.davData.desturl = newDest.url();
1416  m_request.davData.overwrite = (flags & KIO::Overwrite);
1417  m_request.url.setQuery(QString());
1418  m_request.cacheTag.policy = CC_Reload;
1419 
1420  proceedUntilResponseHeader();
1421 
1422  // Work around strict Apache-2 WebDAV implementation which refuses to cooperate
1423  // with webdav://host/directory, instead requiring webdav://host/directory/
1424  // (strangely enough it accepts Destination: without a trailing slash)
1425  // See BR# 209508 and BR#187970
1426  if ( m_request.responseCode == 301) {
1427  m_request.url = m_request.redirectUrl;
1428  m_request.method = DAV_MOVE;
1429  m_request.davData.desturl = newDest.url();
1430  m_request.davData.overwrite = (flags & KIO::Overwrite);
1431  m_request.url.setQuery(QString());
1432  m_request.cacheTag.policy = CC_Reload;
1433  // force re-authentication...
1434  delete m_wwwAuth;
1435  m_wwwAuth = 0;
1436  proceedUntilResponseHeader();
1437  }
1438 
1439  if ( m_request.responseCode == 201 )
1440  davFinished();
1441  else
1442  davError();
1443 }
1444 
1445 void HTTPProtocol::del( const KUrl& url, bool )
1446 {
1447  kDebug(7113) << url.url();
1448 
1449  if (!maybeSetRequestUrl(url))
1450  return;
1451 
1452  resetSessionSettings();
1453 
1454  m_request.method = HTTP_DELETE;
1455  m_request.cacheTag.policy = CC_Reload;
1456 
1457  if (m_protocol.startsWith("webdav")) {
1458  m_request.url.setQuery(QString());
1459  if (!proceedUntilResponseHeader()) {
1460  return;
1461  }
1462 
1463  // The server returns a HTTP/1.1 200 Ok or HTTP/1.1 204 No Content
1464  // on successful completion.
1465  if ( m_request.responseCode == 200 || m_request.responseCode == 204 || m_isRedirection)
1466  davFinished();
1467  else
1468  davError();
1469 
1470  return;
1471  }
1472 
1473  proceedUntilResponseContent();
1474 }
1475 
1476 void HTTPProtocol::post( const KUrl& url, qint64 size )
1477 {
1478  kDebug(7113) << url.url();
1479 
1480  if (!maybeSetRequestUrl(url))
1481  return;
1482  resetSessionSettings();
1483 
1484  m_request.method = HTTP_POST;
1485  m_request.cacheTag.policy= CC_Reload;
1486 
1487  m_iPostDataSize = (size > -1 ? static_cast<KIO::filesize_t>(size) : NO_SIZE);
1488  proceedUntilResponseContent();
1489 }
1490 
1491 void HTTPProtocol::davLock( const KUrl& url, const QString& scope,
1492  const QString& type, const QString& owner )
1493 {
1494  kDebug(7113) << url.url();
1495 
1496  if (!maybeSetRequestUrl(url))
1497  return;
1498  resetSessionSettings();
1499 
1500  m_request.method = DAV_LOCK;
1501  m_request.url.setQuery(QString());
1502  m_request.cacheTag.policy= CC_Reload;
1503 
1504  /* Create appropriate lock XML request. */
1505  QDomDocument lockReq;
1506 
1507  QDomElement lockInfo = lockReq.createElementNS( QLatin1String("DAV:"), QLatin1String("lockinfo") );
1508  lockReq.appendChild( lockInfo );
1509 
1510  QDomElement lockScope = lockReq.createElement( QLatin1String("lockscope") );
1511  lockInfo.appendChild( lockScope );
1512 
1513  lockScope.appendChild( lockReq.createElement( scope ) );
1514 
1515  QDomElement lockType = lockReq.createElement( QLatin1String("locktype") );
1516  lockInfo.appendChild( lockType );
1517 
1518  lockType.appendChild( lockReq.createElement( type ) );
1519 
1520  if ( !owner.isNull() ) {
1521  QDomElement ownerElement = lockReq.createElement( QLatin1String("owner") );
1522  lockReq.appendChild( ownerElement );
1523 
1524  QDomElement ownerHref = lockReq.createElement( QLatin1String("href") );
1525  ownerElement.appendChild( ownerHref );
1526 
1527  ownerHref.appendChild( lockReq.createTextNode( owner ) );
1528  }
1529 
1530  // insert the document into the POST buffer
1531  cachePostData(lockReq.toByteArray());
1532 
1533  proceedUntilResponseContent( true );
1534 
1535  if ( m_request.responseCode == 200 ) {
1536  // success
1537  QDomDocument multiResponse;
1538  multiResponse.setContent( m_webDavDataBuf, true );
1539 
1540  QDomElement prop = multiResponse.documentElement().namedItem( QLatin1String("prop") ).toElement();
1541 
1542  QDomElement lockdiscovery = prop.namedItem( QLatin1String("lockdiscovery") ).toElement();
1543 
1544  uint lockCount = 0;
1545  davParseActiveLocks( lockdiscovery.elementsByTagName( QLatin1String("activelock") ), lockCount );
1546 
1547  setMetaData( QLatin1String("davLockCount"), QString::number( lockCount ) );
1548 
1549  finished();
1550 
1551  } else
1552  davError();
1553 }
1554 
1555 void HTTPProtocol::davUnlock( const KUrl& url )
1556 {
1557  kDebug(7113) << url.url();
1558 
1559  if (!maybeSetRequestUrl(url))
1560  return;
1561  resetSessionSettings();
1562 
1563  m_request.method = DAV_UNLOCK;
1564  m_request.url.setQuery(QString());
1565  m_request.cacheTag.policy= CC_Reload;
1566 
1567  proceedUntilResponseContent( true );
1568 
1569  if ( m_request.responseCode == 200 )
1570  finished();
1571  else
1572  davError();
1573 }
1574 
1575 QString HTTPProtocol::davError( int code /* = -1 */, const QString &_url )
1576 {
1577  bool callError = false;
1578  if ( code == -1 ) {
1579  code = m_request.responseCode;
1580  callError = true;
1581  }
1582  if ( code == -2 ) {
1583  callError = true;
1584  }
1585 
1586  QString url = _url;
1587  if ( !url.isNull() )
1588  url = m_request.url.url();
1589 
1590  QString action, errorString;
1591  int errorCode = ERR_SLAVE_DEFINED;
1592 
1593  // for 412 Precondition Failed
1594  QString ow = i18n( "Otherwise, the request would have succeeded." );
1595 
1596  switch ( m_request.method ) {
1597  case DAV_PROPFIND:
1598  action = i18nc( "request type", "retrieve property values" );
1599  break;
1600  case DAV_PROPPATCH:
1601  action = i18nc( "request type", "set property values" );
1602  break;
1603  case DAV_MKCOL:
1604  action = i18nc( "request type", "create the requested folder" );
1605  break;
1606  case DAV_COPY:
1607  action = i18nc( "request type", "copy the specified file or folder" );
1608  break;
1609  case DAV_MOVE:
1610  action = i18nc( "request type", "move the specified file or folder" );
1611  break;
1612  case DAV_SEARCH:
1613  action = i18nc( "request type", "search in the specified folder" );
1614  break;
1615  case DAV_LOCK:
1616  action = i18nc( "request type", "lock the specified file or folder" );
1617  break;
1618  case DAV_UNLOCK:
1619  action = i18nc( "request type", "unlock the specified file or folder" );
1620  break;
1621  case HTTP_DELETE:
1622  action = i18nc( "request type", "delete the specified file or folder" );
1623  break;
1624  case HTTP_OPTIONS:
1625  action = i18nc( "request type", "query the server's capabilities" );
1626  break;
1627  case HTTP_GET:
1628  action = i18nc( "request type", "retrieve the contents of the specified file or folder" );
1629  break;
1630  case DAV_REPORT:
1631  action = i18nc( "request type", "run a report in the specified folder" );
1632  break;
1633  case HTTP_PUT:
1634  case HTTP_POST:
1635  case HTTP_HEAD:
1636  default:
1637  // this should not happen, this function is for webdav errors only
1638  Q_ASSERT(0);
1639  }
1640 
1641  // default error message if the following code fails
1642  errorString = i18nc("%1: code, %2: request type", "An unexpected error (%1) occurred "
1643  "while attempting to %2.", code, action);
1644 
1645  switch ( code )
1646  {
1647  case -2:
1648  // internal error: OPTIONS request did not specify DAV compliance
1649  // ERR_UNSUPPORTED_PROTOCOL
1650  errorString = i18n("The server does not support the WebDAV protocol.");
1651  break;
1652  case 207:
1653  // 207 Multi-status
1654  {
1655  // our error info is in the returned XML document.
1656  // retrieve the XML document
1657 
1658  // there was an error retrieving the XML document.
1659  // ironic, eh?
1660  if ( !readBody( true ) && m_iError )
1661  return QString();
1662 
1663  QStringList errors;
1664  QDomDocument multiResponse;
1665 
1666  multiResponse.setContent( m_webDavDataBuf, true );
1667 
1668  QDomElement multistatus = multiResponse.documentElement().namedItem( QLatin1String("multistatus") ).toElement();
1669 
1670  QDomNodeList responses = multistatus.elementsByTagName( QLatin1String("response") );
1671 
1672  for (int i = 0; i < responses.count(); i++)
1673  {
1674  int errCode;
1675  QString errUrl;
1676 
1677  QDomElement response = responses.item(i).toElement();
1678  QDomElement code = response.namedItem( QLatin1String("status") ).toElement();
1679 
1680  if ( !code.isNull() )
1681  {
1682  errCode = codeFromResponse( code.text() );
1683  QDomElement href = response.namedItem( QLatin1String("href") ).toElement();
1684  if ( !href.isNull() )
1685  errUrl = href.text();
1686  errors << davError( errCode, errUrl );
1687  }
1688  }
1689 
1690  //kError = ERR_SLAVE_DEFINED;
1691  errorString = i18nc( "%1: request type, %2: url",
1692  "An error occurred while attempting to %1, %2. A "
1693  "summary of the reasons is below.", action, url );
1694 
1695  errorString += QLatin1String("<ul>");
1696 
1697  Q_FOREACH(const QString& error, errors)
1698  errorString += QLatin1String("<li>") + error + QLatin1String("</li>");
1699 
1700  errorString += QLatin1String("</ul>");
1701  }
1702  case 403:
1703  case 500: // hack: Apache mod_dav returns this instead of 403 (!)
1704  // 403 Forbidden
1705  // ERR_ACCESS_DENIED
1706  errorString = i18nc( "%1: request type", "Access was denied while attempting to %1.", action );
1707  break;
1708  case 405:
1709  // 405 Method Not Allowed
1710  if ( m_request.method == DAV_MKCOL ) {
1711  // ERR_DIR_ALREADY_EXIST
1712  errorString = url;
1713  errorCode = ERR_DIR_ALREADY_EXIST;
1714  }
1715  break;
1716  case 409:
1717  // 409 Conflict
1718  // ERR_ACCESS_DENIED
1719  errorString = i18n("A resource cannot be created at the destination "
1720  "until one or more intermediate collections (folders) "
1721  "have been created.");
1722  break;
1723  case 412:
1724  // 412 Precondition failed
1725  if ( m_request.method == DAV_COPY || m_request.method == DAV_MOVE ) {
1726  // ERR_ACCESS_DENIED
1727  errorString = i18n("The server was unable to maintain the liveness of "
1728  "the properties listed in the propertybehavior XML "
1729  "element or you attempted to overwrite a file while "
1730  "requesting that files are not overwritten. %1",
1731  ow );
1732 
1733  } else if ( m_request.method == DAV_LOCK ) {
1734  // ERR_ACCESS_DENIED
1735  errorString = i18n("The requested lock could not be granted. %1", ow );
1736  }
1737  break;
1738  case 415:
1739  // 415 Unsupported Media Type
1740  // ERR_ACCESS_DENIED
1741  errorString = i18n("The server does not support the request type of the body.");
1742  break;
1743  case 423:
1744  // 423 Locked
1745  // ERR_ACCESS_DENIED
1746  errorString = i18nc( "%1: request type", "Unable to %1 because the resource is locked.", action );
1747  break;
1748  case 425:
1749  // 424 Failed Dependency
1750  errorString = i18n("This action was prevented by another error.");
1751  break;
1752  case 502:
1753  // 502 Bad Gateway
1754  if ( m_request.method == DAV_COPY || m_request.method == DAV_MOVE ) {
1755  // ERR_WRITE_ACCESS_DENIED
1756  errorString = i18nc( "%1: request type", "Unable to %1 because the destination server refuses "
1757  "to accept the file or folder.", action );
1758  }
1759  break;
1760  case 507:
1761  // 507 Insufficient Storage
1762  // ERR_DISK_FULL
1763  errorString = i18n("The destination resource does not have sufficient space "
1764  "to record the state of the resource after the execution "
1765  "of this method.");
1766  break;
1767  default:
1768  break;
1769  }
1770 
1771  // if ( kError != ERR_SLAVE_DEFINED )
1772  //errorString += " (" + url + ')';
1773 
1774  if ( callError )
1775  error( errorCode, errorString );
1776 
1777  return errorString;
1778 }
1779 
1780 // HTTP generic error
1781 static int httpGenericError(const HTTPProtocol::HTTPRequest& request, QString* errorString)
1782 {
1783  Q_ASSERT(errorString);
1784 
1785  int errorCode = 0;
1786  errorString->clear();
1787 
1788  if (request.responseCode == 204) {
1789  errorCode = ERR_NO_CONTENT;
1790  }
1791 
1792  return errorCode;
1793 }
1794 
1795 // HTTP DELETE specific errors
1796 static int httpDelError(const HTTPProtocol::HTTPRequest& request, QString* errorString)
1797 {
1798  Q_ASSERT(errorString);
1799 
1800  int errorCode = 0;
1801  const int responseCode = request.responseCode;
1802  errorString->clear();
1803 
1804  switch (responseCode) {
1805  case 204:
1806  errorCode = ERR_NO_CONTENT;
1807  break;
1808  default:
1809  break;
1810  }
1811 
1812  if (!errorCode
1813  && (responseCode < 200 || responseCode > 400)
1814  && responseCode != 404) {
1815  errorCode = ERR_SLAVE_DEFINED;
1816  *errorString = i18n( "The resource cannot be deleted." );
1817  }
1818 
1819  return errorCode;
1820 }
1821 
1822 // HTTP PUT specific errors
1823 static int httpPutError(const HTTPProtocol::HTTPRequest& request, QString* errorString)
1824 {
1825  Q_ASSERT(errorString);
1826 
1827  int errorCode = 0;
1828  const int responseCode = request.responseCode;
1829  const QString action (i18nc("request type", "upload %1", request.url.prettyUrl()));
1830 
1831  switch (responseCode) {
1832  case 403:
1833  case 405:
1834  case 500: // hack: Apache mod_dav returns this instead of 403 (!)
1835  // 403 Forbidden
1836  // 405 Method Not Allowed
1837  // ERR_ACCESS_DENIED
1838  *errorString = i18nc( "%1: request type", "Access was denied while attempting to %1.", action );
1839  errorCode = ERR_SLAVE_DEFINED;
1840  break;
1841  case 409:
1842  // 409 Conflict
1843  // ERR_ACCESS_DENIED
1844  *errorString = i18n("A resource cannot be created at the destination "
1845  "until one or more intermediate collections (folders) "
1846  "have been created.");
1847  errorCode = ERR_SLAVE_DEFINED;
1848  break;
1849  case 423:
1850  // 423 Locked
1851  // ERR_ACCESS_DENIED
1852  *errorString = i18nc( "%1: request type", "Unable to %1 because the resource is locked.", action );
1853  errorCode = ERR_SLAVE_DEFINED;
1854  break;
1855  case 502:
1856  // 502 Bad Gateway
1857  // ERR_WRITE_ACCESS_DENIED;
1858  *errorString = i18nc( "%1: request type", "Unable to %1 because the destination server refuses "
1859  "to accept the file or folder.", action );
1860  errorCode = ERR_SLAVE_DEFINED;
1861  break;
1862  case 507:
1863  // 507 Insufficient Storage
1864  // ERR_DISK_FULL
1865  *errorString = i18n("The destination resource does not have sufficient space "
1866  "to record the state of the resource after the execution "
1867  "of this method.");
1868  errorCode = ERR_SLAVE_DEFINED;
1869  break;
1870  default:
1871  break;
1872  }
1873 
1874  if (!errorCode
1875  && (responseCode < 200 || responseCode > 400)
1876  && responseCode != 404) {
1877  errorCode = ERR_SLAVE_DEFINED;
1878  *errorString = i18nc("%1: response code, %2: request type",
1879  "An unexpected error (%1) occurred while attempting to %2.",
1880  responseCode, action);
1881  }
1882 
1883  return errorCode;
1884 }
1885 
1886 bool HTTPProtocol::sendHttpError()
1887 {
1888  QString errorString;
1889  int errorCode = 0;
1890 
1891  switch (m_request.method) {
1892  case HTTP_GET:
1893  case HTTP_POST:
1894  errorCode = httpGenericError(m_request, &errorString);
1895  break;
1896  case HTTP_PUT:
1897  errorCode = httpPutError(m_request, &errorString);
1898  break;
1899  case HTTP_DELETE:
1900  errorCode = httpDelError(m_request, &errorString);
1901  break;
1902  default:
1903  break;
1904  }
1905 
1906  // Force any message previously shown by the client to be cleared.
1907  infoMessage(QLatin1String(""));
1908 
1909  if (errorCode) {
1910  error( errorCode, errorString );
1911  return true;
1912  }
1913 
1914  return false;
1915 }
1916 
1917 bool HTTPProtocol::sendErrorPageNotification()
1918 {
1919  if (!m_request.preferErrorPage)
1920  return false;
1921 
1922  if (m_isLoadingErrorPage)
1923  kWarning(7113) << "called twice during one request, something is probably wrong.";
1924 
1925  m_isLoadingErrorPage = true;
1926  SlaveBase::errorPage();
1927  return true;
1928 }
1929 
1930 bool HTTPProtocol::isOffline()
1931 {
1932  // ### TEMPORARY WORKAROUND (While investigating why solid may
1933  // produce false positives)
1934  return false;
1935 
1936  Solid::Networking::Status status = Solid::Networking::status();
1937 
1938  kDebug(7113) << "networkstatus:" << status;
1939 
1940  // on error or unknown, we assume online
1941  return status == Solid::Networking::Unconnected;
1942 }
1943 
1944 void HTTPProtocol::multiGet(const QByteArray &data)
1945 {
1946  QDataStream stream(data);
1947  quint32 n;
1948  stream >> n;
1949 
1950  kDebug(7113) << n;
1951 
1952  HTTPRequest saveRequest;
1953  if (m_isBusy)
1954  saveRequest = m_request;
1955 
1956  resetSessionSettings();
1957 
1958  for (unsigned i = 0; i < n; ++i) {
1959  KUrl url;
1960  stream >> url >> mIncomingMetaData;
1961 
1962  if (!maybeSetRequestUrl(url))
1963  continue;
1964 
1965  //### should maybe call resetSessionSettings() if the server/domain is
1966  // different from the last request!
1967 
1968  kDebug(7113) << url.url();
1969 
1970  m_request.method = HTTP_GET;
1971  m_request.isKeepAlive = true; //readResponseHeader clears it if necessary
1972 
1973  QString tmp = metaData(QLatin1String("cache"));
1974  if (!tmp.isEmpty())
1975  m_request.cacheTag.policy= parseCacheControl(tmp);
1976  else
1977  m_request.cacheTag.policy= DEFAULT_CACHE_CONTROL;
1978 
1979  m_requestQueue.append(m_request);
1980  }
1981 
1982  if (m_isBusy)
1983  m_request = saveRequest;
1984 #if 0
1985  if (!m_isBusy) {
1986  m_isBusy = true;
1987  QMutableListIterator<HTTPRequest> it(m_requestQueue);
1988  while (it.hasNext()) {
1989  m_request = it.next();
1990  it.remove();
1991  proceedUntilResponseContent();
1992  }
1993  m_isBusy = false;
1994  }
1995 #endif
1996  if (!m_isBusy) {
1997  m_isBusy = true;
1998  QMutableListIterator<HTTPRequest> it(m_requestQueue);
1999  // send the requests
2000  while (it.hasNext()) {
2001  m_request = it.next();
2002  sendQuery();
2003  // save the request state so we can pick it up again in the collection phase
2004  it.setValue(m_request);
2005  kDebug(7113) << "check one: isKeepAlive =" << m_request.isKeepAlive;
2006  if (m_request.cacheTag.ioMode != ReadFromCache) {
2007  m_server.initFrom(m_request);
2008  }
2009  }
2010  // collect the responses
2011  //### for the moment we use a hack: instead of saving and restoring request-id
2012  // we just count up like ParallelGetJobs does.
2013  int requestId = 0;
2014  Q_FOREACH (const HTTPRequest &r, m_requestQueue) {
2015  m_request = r;
2016  kDebug(7113) << "check two: isKeepAlive =" << m_request.isKeepAlive;
2017  setMetaData(QLatin1String("request-id"), QString::number(requestId++));
2018  sendAndKeepMetaData();
2019  if (!(readResponseHeader() && readBody())) {
2020  return;
2021  }
2022  // the "next job" signal for ParallelGetJob is data of size zero which
2023  // readBody() sends without our intervention.
2024  kDebug(7113) << "check three: isKeepAlive =" << m_request.isKeepAlive;
2025  httpClose(m_request.isKeepAlive); //actually keep-alive is mandatory for pipelining
2026  }
2027 
2028  finished();
2029  m_requestQueue.clear();
2030  m_isBusy = false;
2031  }
2032 }
2033 
2034 ssize_t HTTPProtocol::write (const void *_buf, size_t nbytes)
2035 {
2036  size_t sent = 0;
2037  const char* buf = static_cast<const char*>(_buf);
2038  while (sent < nbytes)
2039  {
2040  int n = TCPSlaveBase::write(buf + sent, nbytes - sent);
2041 
2042  if (n < 0) {
2043  // some error occurred
2044  return -1;
2045  }
2046 
2047  sent += n;
2048  }
2049 
2050  return sent;
2051 }
2052 
2053 void HTTPProtocol::clearUnreadBuffer()
2054 {
2055  m_unreadBuf.clear();
2056 }
2057 
2058 // Note: the implementation of unread/readBuffered assumes that unread will only
2059 // be used when there is extra data we don't want to handle, and not to wait for more data.
2060 void HTTPProtocol::unread(char *buf, size_t size)
2061 {
2062  // implement LIFO (stack) semantics
2063  const int newSize = m_unreadBuf.size() + size;
2064  m_unreadBuf.resize(newSize);
2065  for (size_t i = 0; i < size; i++) {
2066  m_unreadBuf.data()[newSize - i - 1] = buf[i];
2067  }
2068  if (size) {
2069  //hey, we still have data, closed connection or not!
2070  m_isEOF = false;
2071  }
2072 }
2073 
2074 size_t HTTPProtocol::readBuffered(char *buf, size_t size, bool unlimited)
2075 {
2076  size_t bytesRead = 0;
2077  if (!m_unreadBuf.isEmpty()) {
2078  const int bufSize = m_unreadBuf.size();
2079  bytesRead = qMin((int)size, bufSize);
2080 
2081  for (size_t i = 0; i < bytesRead; i++) {
2082  buf[i] = m_unreadBuf.constData()[bufSize - i - 1];
2083  }
2084  m_unreadBuf.truncate(bufSize - bytesRead);
2085 
2086  // If we have an unread buffer and the size of the content returned by the
2087  // server is unknown, e.g. chuncked transfer, return the bytes read here since
2088  // we may already have enough data to complete the response and don't want to
2089  // wait for more. See BR# 180631.
2090  if (unlimited)
2091  return bytesRead;
2092  }
2093  if (bytesRead < size) {
2094  int rawRead = TCPSlaveBase::read(buf + bytesRead, size - bytesRead);
2095  if (rawRead < 1) {
2096  m_isEOF = true;
2097  return bytesRead;
2098  }
2099  bytesRead += rawRead;
2100  }
2101  return bytesRead;
2102 }
2103 
2104 //### this method will detect an n*(\r\n) sequence if it crosses invocations.
2105 // it will look (n*2 - 1) bytes before start at most and never before buf, naturally.
2106 // supported number of newlines are one and two, in line with HTTP syntax.
2107 // return true if numNewlines newlines were found.
2108 bool HTTPProtocol::readDelimitedText(char *buf, int *idx, int end, int numNewlines)
2109 {
2110  Q_ASSERT(numNewlines >=1 && numNewlines <= 2);
2111  char mybuf[64]; //somewhere close to the usual line length to avoid unread()ing too much
2112  int pos = *idx;
2113  while (pos < end && !m_isEOF) {
2114  int step = qMin((int)sizeof(mybuf), end - pos);
2115  if (m_isChunked) {
2116  //we might be reading the end of the very last chunk after which there is no data.
2117  //don't try to read any more bytes than there are because it causes stalls
2118  //(yes, it shouldn't stall but it does)
2119  step = 1;
2120  }
2121  size_t bufferFill = readBuffered(mybuf, step);
2122 
2123  for (size_t i = 0; i < bufferFill ; ++i, ++pos) {
2124  // we copy the data from mybuf to buf immediately and look for the newlines in buf.
2125  // that way we don't miss newlines split over several invocations of this method.
2126  buf[pos] = mybuf[i];
2127 
2128  // did we just copy one or two times the (usually) \r\n delimiter?
2129  // until we find even more broken webservers in the wild let's assume that they either
2130  // send \r\n (RFC compliant) or \n (broken) as delimiter...
2131  if (buf[pos] == '\n') {
2132  bool found = numNewlines == 1;
2133  if (!found) { // looking for two newlines
2134  found = ((pos >= 1 && buf[pos - 1] == '\n') ||
2135  (pos >= 3 && buf[pos - 3] == '\r' && buf[pos - 2] == '\n' &&
2136  buf[pos - 1] == '\r'));
2137  }
2138  if (found) {
2139  i++; // unread bytes *after* CRLF
2140  unread(&mybuf[i], bufferFill - i);
2141  *idx = pos + 1;
2142  return true;
2143  }
2144  }
2145  }
2146  }
2147  *idx = pos;
2148  return false;
2149 }
2150 
2151 static bool isCompatibleNextUrl(const KUrl &previous, const KUrl &now)
2152 {
2153  if (previous.host() != now.host() || previous.port() != now.port()) {
2154  return false;
2155  }
2156  if (previous.user().isEmpty() && previous.pass().isEmpty()) {
2157  return true;
2158  }
2159  return previous.user() == now.user() && previous.pass() == now.pass();
2160 }
2161 
2162 bool HTTPProtocol::httpShouldCloseConnection()
2163 {
2164  kDebug(7113);
2165 
2166  if (!isConnected()) {
2167  return false;
2168  }
2169 
2170  if (!m_request.proxyUrls.isEmpty() && !isAutoSsl()) {
2171  Q_FOREACH(const QString& url, m_request.proxyUrls) {
2172  if (url != QLatin1String("DIRECT")) {
2173  if (isCompatibleNextUrl(m_server.proxyUrl, KUrl(url))) {
2174  return false;
2175  }
2176  }
2177  }
2178  return true;
2179  }
2180 
2181  return !isCompatibleNextUrl(m_server.url, m_request.url);
2182 }
2183 
2184 bool HTTPProtocol::httpOpenConnection()
2185 {
2186  kDebug(7113);
2187  m_server.clear();
2188 
2189  // Only save proxy auth information after proxy authentication has
2190  // actually taken place, which will set up exactly this connection.
2191  disconnect(socket(), SIGNAL(connected()),
2192  this, SLOT(saveProxyAuthenticationForSocket()));
2193 
2194  clearUnreadBuffer();
2195 
2196  int connectError = 0;
2197  QString errorString;
2198 
2199  // Get proxy information...
2200  if (m_request.proxyUrls.isEmpty()) {
2201  m_request.proxyUrls = config()->readEntry("ProxyUrls", QStringList());
2202  kDebug(7113) << "Proxy URLs:" << m_request.proxyUrls;
2203  }
2204 
2205  if (m_request.proxyUrls.isEmpty()) {
2206  connectError = connectToHost(m_request.url.host(), m_request.url.port(defaultPort()), &errorString);
2207  } else {
2208  KUrl::List badProxyUrls;
2209  Q_FOREACH(const QString& proxyUrl, m_request.proxyUrls) {
2210  const KUrl url (proxyUrl);
2211  const QString scheme (url.protocol());
2212 
2213  if (!supportedProxyScheme(scheme)) {
2214  connectError = ERR_COULD_NOT_CONNECT;
2215  errorString = url.url();
2216  continue;
2217  }
2218 
2219  const bool isDirectConnect = (proxyUrl == QLatin1String("DIRECT"));
2220  QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
2221  if (url.protocol() == QLatin1String("socks")) {
2222  proxyType = QNetworkProxy::Socks5Proxy;
2223  } else if (!isDirectConnect && isAutoSsl()) {
2224  proxyType = QNetworkProxy::HttpProxy;
2225  }
2226 
2227  kDebug(7113) << "Connecting to proxy: address=" << proxyUrl << "type=" << proxyType;
2228 
2229  if (proxyType == QNetworkProxy::NoProxy) {
2230  // Only way proxy url and request url are the same is when the
2231  // proxy URL list contains a "DIRECT" entry. See resetSessionSettings().
2232  if (isDirectConnect) {
2233  connectError = connectToHost(m_request.url.host(), m_request.url.port(defaultPort()), &errorString);
2234  kDebug(7113) << "Connected DIRECT: host=" << m_request.url.host() << "post=" << m_request.url.port(defaultPort());
2235  } else {
2236  connectError = connectToHost(url.host(), url.port(), &errorString);
2237  if (connectError == 0) {
2238  m_request.proxyUrl = url;
2239  kDebug(7113) << "Connected to proxy: host=" << url.host() << "port=" << url.port();
2240  } else {
2241  if (connectError == ERR_UNKNOWN_HOST)
2242  connectError = ERR_UNKNOWN_PROXY_HOST;
2243  kDebug(7113) << "Failed to connect to proxy:" << proxyUrl;
2244  badProxyUrls << url;
2245  }
2246  }
2247  if (connectError == 0) {
2248  break;
2249  }
2250  } else {
2251  QNetworkProxy proxy (proxyType, url.host(), url.port(), url.user(), url.pass());
2252  QNetworkProxy::setApplicationProxy(proxy);
2253  connectError = connectToHost(m_request.url.host(), m_request.url.port(defaultPort()), &errorString);
2254  if (connectError == 0) {
2255  kDebug(7113) << "Tunneling thru proxy: host=" << url.host() << "port=" << url.port();
2256  break;
2257  } else {
2258  if (connectError == ERR_UNKNOWN_HOST)
2259  connectError = ERR_UNKNOWN_PROXY_HOST;
2260  kDebug(7113) << "Failed to connect to proxy:" << proxyUrl;
2261  badProxyUrls << url;
2262  QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
2263  }
2264  }
2265  }
2266 
2267  if (!badProxyUrls.isEmpty()) {
2268  //TODO: Notify the client of BAD proxy addresses (needed for PAC setups).
2269  }
2270  }
2271 
2272  if (connectError != 0) {
2273  error (connectError, errorString);
2274  return false;
2275  }
2276 
2277  // Disable Nagle's algorithm, i.e turn on TCP_NODELAY.
2278  KTcpSocket *sock = qobject_cast<KTcpSocket*>(socket());
2279  if (sock) {
2280  // kDebug(7113) << "TCP_NODELAY:" << sock->socketOption(QAbstractSocket::LowDelayOption);
2281  sock->setSocketOption(QAbstractSocket::LowDelayOption, 1);
2282  }
2283 
2284  m_server.initFrom(m_request);
2285  connected();
2286  return true;
2287 }
2288 
2289 bool HTTPProtocol::satisfyRequestFromCache(bool *cacheHasPage)
2290 {
2291  kDebug(7113);
2292 
2293  if (m_request.cacheTag.useCache) {
2294  const bool offline = isOffline();
2295 
2296  if (offline && m_request.cacheTag.policy != KIO::CC_Reload) {
2297  m_request.cacheTag.policy= KIO::CC_CacheOnly;
2298  }
2299 
2300  const bool isCacheOnly = m_request.cacheTag.policy == KIO::CC_CacheOnly;
2301  const CacheTag::CachePlan plan = m_request.cacheTag.plan(m_maxCacheAge);
2302 
2303  bool openForReading = false;
2304  if (plan == CacheTag::UseCached || plan == CacheTag::ValidateCached) {
2305  openForReading = cacheFileOpenRead();
2306 
2307  if (!openForReading && (isCacheOnly || offline)) {
2308  // cache-only or offline -> we give a definite answer and it is "no"
2309  *cacheHasPage = false;
2310  if (isCacheOnly) {
2311  error(ERR_DOES_NOT_EXIST, m_request.url.url());
2312  } else if (offline) {
2313  error(ERR_COULD_NOT_CONNECT, m_request.url.url());
2314  }
2315  return true;
2316  }
2317  }
2318 
2319  if (openForReading) {
2320  m_request.cacheTag.ioMode = ReadFromCache;
2321  *cacheHasPage = true;
2322  // return false if validation is required, so a network request will be sent
2323  return m_request.cacheTag.plan(m_maxCacheAge) == CacheTag::UseCached;
2324  }
2325  }
2326  *cacheHasPage = false;
2327  return false;
2328 }
2329 
2330 QString HTTPProtocol::formatRequestUri() const
2331 {
2332  // Only specify protocol, host and port when they are not already clear, i.e. when
2333  // we handle HTTP proxying ourself and the proxy server needs to know them.
2334  // Sending protocol/host/port in other cases confuses some servers, and it's not their fault.
2335  if (isHttpProxy(m_request.proxyUrl) && !isAutoSsl()) {
2336  KUrl u;
2337 
2338  QString protocol = m_request.url.protocol();
2339  if (protocol.startsWith(QLatin1String("webdav"))) {
2340  protocol.replace(0, qstrlen("webdav"), QLatin1String("http"));
2341  }
2342  u.setProtocol(protocol);
2343 
2344  u.setHost(m_request.url.host());
2345  // if the URL contained the default port it should have been stripped earlier
2346  Q_ASSERT(m_request.url.port() != defaultPort());
2347  u.setPort(m_request.url.port());
2348  u.setEncodedPathAndQuery(m_request.url.encodedPathAndQuery(
2349  KUrl::LeaveTrailingSlash, KUrl::AvoidEmptyPath));
2350  return u.url();
2351  } else {
2352  return m_request.url.encodedPathAndQuery(KUrl::LeaveTrailingSlash, KUrl::AvoidEmptyPath);
2353  }
2354 }
2355 
2371 bool HTTPProtocol::sendQuery()
2372 {
2373  kDebug(7113);
2374 
2375  // Cannot have an https request without autoSsl! This can
2376  // only happen if the current installation does not support SSL...
2377  if (isEncryptedHttpVariety(m_protocol) && !isAutoSsl()) {
2378  error(ERR_UNSUPPORTED_PROTOCOL, toQString(m_protocol));
2379  return false;
2380  }
2381 
2382  // Check the reusability of the current connection.
2383  if (httpShouldCloseConnection()) {
2384  httpCloseConnection();
2385  }
2386 
2387  // Create a new connection to the remote machine if we do
2388  // not already have one...
2389  // NB: the !m_socketProxyAuth condition is a workaround for a proxied Qt socket sometimes
2390  // looking disconnected after receiving the initial 407 response.
2391  // I guess the Qt socket fails to hide the effect of proxy-connection: close after receiving
2392  // the 407 header.
2393  if ((!isConnected() && !m_socketProxyAuth))
2394  {
2395  if (!httpOpenConnection())
2396  {
2397  kDebug(7113) << "Couldn't connect, oopsie!";
2398  return false;
2399  }
2400  }
2401 
2402  m_request.cacheTag.ioMode = NoCache;
2403  m_request.cacheTag.servedDate = -1;
2404  m_request.cacheTag.lastModifiedDate = -1;
2405  m_request.cacheTag.expireDate = -1;
2406 
2407  QString header;
2408 
2409  bool hasBodyData = false;
2410  bool hasDavData = false;
2411 
2412  {
2413  header = toQString(m_request.methodString());
2414  QString davHeader;
2415 
2416  // Fill in some values depending on the HTTP method to guide further processing
2417  switch (m_request.method)
2418  {
2419  case HTTP_GET: {
2420  bool cacheHasPage = false;
2421  if (satisfyRequestFromCache(&cacheHasPage)) {
2422  kDebug(7113) << "cacheHasPage =" << cacheHasPage;
2423  return cacheHasPage;
2424  }
2425  if (!cacheHasPage) {
2426  // start a new cache file later if appropriate
2427  m_request.cacheTag.ioMode = WriteToCache;
2428  }
2429  break;
2430  }
2431  case HTTP_HEAD:
2432  break;
2433  case HTTP_PUT:
2434  case HTTP_POST:
2435  hasBodyData = true;
2436  break;
2437  case HTTP_DELETE:
2438  case HTTP_OPTIONS:
2439  break;
2440  case DAV_PROPFIND:
2441  hasDavData = true;
2442  davHeader = QLatin1String("Depth: ");
2443  if ( hasMetaData( QLatin1String("davDepth") ) )
2444  {
2445  kDebug(7113) << "Reading DAV depth from metadata:" << metaData( QLatin1String("davDepth") );
2446  davHeader += metaData( QLatin1String("davDepth") );
2447  }
2448  else
2449  {
2450  if ( m_request.davData.depth == 2 )
2451  davHeader += QLatin1String("infinity");
2452  else
2453  davHeader += QString::number( m_request.davData.depth );
2454  }
2455  davHeader += QLatin1String("\r\n");
2456  break;
2457  case DAV_PROPPATCH:
2458  hasDavData = true;
2459  break;
2460  case DAV_MKCOL:
2461  break;
2462  case DAV_COPY:
2463  case DAV_MOVE:
2464  davHeader = QLatin1String("Destination: ") + m_request.davData.desturl;
2465  // infinity depth means copy recursively
2466  // (optional for copy -> but is the desired action)
2467  davHeader += QLatin1String("\r\nDepth: infinity\r\nOverwrite: ");
2468  davHeader += QLatin1Char(m_request.davData.overwrite ? 'T' : 'F');
2469  davHeader += QLatin1String("\r\n");
2470  break;
2471  case DAV_LOCK:
2472  davHeader = QLatin1String("Timeout: ");
2473  {
2474  uint timeout = 0;
2475  if ( hasMetaData( QLatin1String("davTimeout") ) )
2476  timeout = metaData( QLatin1String("davTimeout") ).toUInt();
2477  if ( timeout == 0 )
2478  davHeader += QLatin1String("Infinite");
2479  else
2480  davHeader += QLatin1String("Seconds-") + QString::number(timeout);
2481  }
2482  davHeader += QLatin1String("\r\n");
2483  hasDavData = true;
2484  break;
2485  case DAV_UNLOCK:
2486  davHeader = QLatin1String("Lock-token: ") + metaData(QLatin1String("davLockToken")) + QLatin1String("\r\n");
2487  break;
2488  case DAV_SEARCH:
2489  case DAV_REPORT:
2490  hasDavData = true;
2491  /* fall through */
2492  case DAV_SUBSCRIBE:
2493  case DAV_UNSUBSCRIBE:
2494  case DAV_POLL:
2495  break;
2496  default:
2497  error (ERR_UNSUPPORTED_ACTION, QString());
2498  return false;
2499  }
2500  // DAV_POLL; DAV_NOTIFY
2501 
2502  header += formatRequestUri() + QLatin1String(" HTTP/1.1\r\n"); /* start header */
2503 
2504  /* support for virtual hosts and required by HTTP 1.1 */
2505  header += QLatin1String("Host: ") + m_request.encoded_hostname;
2506  if (m_request.url.port(defaultPort()) != defaultPort()) {
2507  header += QLatin1Char(':') + QString::number(m_request.url.port());
2508  }
2509  header += QLatin1String("\r\n");
2510 
2511  // Support old HTTP/1.0 style keep-alive header for compatibility
2512  // purposes as well as performance improvements while giving end
2513  // users the ability to disable this feature for proxy servers that
2514  // don't support it, e.g. junkbuster proxy server.
2515  if (isHttpProxy(m_request.proxyUrl) && !isAutoSsl()) {
2516  header += QLatin1String("Proxy-Connection: ");
2517  } else {
2518  header += QLatin1String("Connection: ");
2519  }
2520  if (m_request.isKeepAlive) {
2521  header += QLatin1String("keep-alive\r\n");
2522  } else {
2523  header += QLatin1String("close\r\n");
2524  }
2525 
2526  if (!m_request.userAgent.isEmpty())
2527  {
2528  header += QLatin1String("User-Agent: ");
2529  header += m_request.userAgent;
2530  header += QLatin1String("\r\n");
2531  }
2532 
2533  if (!m_request.referrer.isEmpty())
2534  {
2535  header += QLatin1String("Referer: "); //Don't try to correct spelling!
2536  header += m_request.referrer;
2537  header += QLatin1String("\r\n");
2538  }
2539 
2540  if ( m_request.endoffset > m_request.offset )
2541  {
2542  header += QLatin1String("Range: bytes=");
2543  header += KIO::number(m_request.offset);
2544  header += QLatin1Char('-');
2545  header += KIO::number(m_request.endoffset);
2546  header += QLatin1String("\r\n");
2547  kDebug(7103) << "kio_http : Range =" << KIO::number(m_request.offset)
2548  << "-" << KIO::number(m_request.endoffset);
2549  }
2550  else if ( m_request.offset > 0 && m_request.endoffset == 0 )
2551  {
2552  header += QLatin1String("Range: bytes=");
2553  header += KIO::number(m_request.offset);
2554  header += QLatin1String("-\r\n");
2555  kDebug(7103) << "kio_http: Range =" << KIO::number(m_request.offset);
2556  }
2557 
2558  if ( !m_request.cacheTag.useCache || m_request.cacheTag.policy==CC_Reload )
2559  {
2560  /* No caching for reload */
2561  header += QLatin1String("Pragma: no-cache\r\n"); /* for HTTP/1.0 caches */
2562  header += QLatin1String("Cache-control: no-cache\r\n"); /* for HTTP >=1.1 caches */
2563  }
2564  else if (m_request.cacheTag.plan(m_maxCacheAge) == CacheTag::ValidateCached)
2565  {
2566  kDebug(7113) << "needs validation, performing conditional get.";
2567  /* conditional get */
2568  if (!m_request.cacheTag.etag.isEmpty())
2569  header += QLatin1String("If-None-Match: ") + m_request.cacheTag.etag + QLatin1String("\r\n");
2570 
2571  if (m_request.cacheTag.lastModifiedDate != -1) {
2572  const QString httpDate = formatHttpDate(m_request.cacheTag.lastModifiedDate);
2573  header += QLatin1String("If-Modified-Since: ") + httpDate + QLatin1String("\r\n");
2574  setMetaData(QLatin1String("modified"), httpDate);
2575  }
2576  }
2577 
2578  header += QLatin1String("Accept: ");
2579  const QString acceptHeader = metaData(QLatin1String("accept"));
2580  if (!acceptHeader.isEmpty())
2581  header += acceptHeader;
2582  else
2583  header += QLatin1String(DEFAULT_ACCEPT_HEADER);
2584  header += QLatin1String("\r\n");
2585 
2586  if (m_request.allowTransferCompression)
2587  header += QLatin1String("Accept-Encoding: gzip, deflate, x-gzip, x-deflate\r\n");
2588 
2589  if (!m_request.charsets.isEmpty())
2590  header += QLatin1String("Accept-Charset: ") + m_request.charsets + QLatin1String("\r\n");
2591 
2592  if (!m_request.languages.isEmpty())
2593  header += QLatin1String("Accept-Language: ") + m_request.languages + QLatin1String("\r\n");
2594 
2595  QString cookieStr;
2596  const QString cookieMode = metaData(QLatin1String("cookies")).toLower();
2597 
2598  if (cookieMode == QLatin1String("none"))
2599  {
2600  m_request.cookieMode = HTTPRequest::CookiesNone;
2601  }
2602  else if (cookieMode == QLatin1String("manual"))
2603  {
2604  m_request.cookieMode = HTTPRequest::CookiesManual;
2605  cookieStr = metaData(QLatin1String("setcookies"));
2606  }
2607  else
2608  {
2609  m_request.cookieMode = HTTPRequest::CookiesAuto;
2610  if (m_request.useCookieJar)
2611  cookieStr = findCookies(m_request.url.url());
2612  }
2613 
2614  if (!cookieStr.isEmpty())
2615  header += cookieStr + QLatin1String("\r\n");
2616 
2617  const QString customHeader = metaData( QLatin1String("customHTTPHeader") );
2618  if (!customHeader.isEmpty())
2619  {
2620  header += sanitizeCustomHTTPHeader(customHeader);
2621  header += QLatin1String("\r\n");
2622  }
2623 
2624  const QString contentType = metaData(QLatin1String("content-type"));
2625  if (!contentType.isEmpty())
2626  {
2627  if (!contentType.startsWith(QLatin1String("content-type"), Qt::CaseInsensitive))
2628  header += QLatin1String("Content-Type: ");
2629  header += contentType;
2630  header += QLatin1String("\r\n");
2631  }
2632 
2633  // DoNotTrack feature...
2634  if (config()->readEntry("DoNotTrack", false))
2635  header += QLatin1String("DNT: 1\r\n");
2636 
2637  // Remember that at least one failed (with 401 or 407) request/response
2638  // roundtrip is necessary for the server to tell us that it requires
2639  // authentication. However, we proactively add authentication headers if when
2640  // we have cached credentials to avoid the extra roundtrip where possible.
2641  header += authenticationHeader();
2642 
2643  if ( m_protocol == "webdav" || m_protocol == "webdavs" )
2644  {
2645  header += davProcessLocks();
2646 
2647  // add extra webdav headers, if supplied
2648  davHeader += metaData(QLatin1String("davHeader"));
2649 
2650  // Set content type of webdav data
2651  if (hasDavData)
2652  davHeader += QLatin1String("Content-Type: text/xml; charset=utf-8\r\n");
2653 
2654  // add extra header elements for WebDAV
2655  header += davHeader;
2656  }
2657  }
2658 
2659  kDebug(7103) << "============ Sending Header:";
2660  Q_FOREACH (const QString &s, header.split(QLatin1String("\r\n"), QString::SkipEmptyParts)) {
2661  kDebug(7103) << s;
2662  }
2663 
2664  // End the header iff there is no payload data. If we do have payload data
2665  // sendBody() will add another field to the header, Content-Length.
2666  if (!hasBodyData && !hasDavData)
2667  header += QLatin1String("\r\n");
2668 
2669 
2670  // Now that we have our formatted header, let's send it!
2671 
2672  // Clear out per-connection settings...
2673  resetConnectionSettings();
2674 
2675  // Send the data to the remote machine...
2676  ssize_t written = write(header.toLatin1(), header.length());
2677  bool sendOk = (written == (ssize_t) header.length());
2678  if (!sendOk)
2679  {
2680  kDebug(7113) << "Connection broken! (" << m_request.url.host() << ")"
2681  << " -- intended to write" << header.length()
2682  << "bytes but wrote" << (int)written << ".";
2683 
2684  // The server might have closed the connection due to a timeout, or maybe
2685  // some transport problem arose while the connection was idle.
2686  if (m_request.isKeepAlive)
2687  {
2688  httpCloseConnection();
2689  return true; // Try again
2690  }
2691 
2692  kDebug(7113) << "sendOk == false. Connection broken !"
2693  << " -- intended to write" << header.length()
2694  << "bytes but wrote" << (int)written << ".";
2695  error( ERR_CONNECTION_BROKEN, m_request.url.host() );
2696  return false;
2697  }
2698  else
2699  kDebug(7113) << "sent it!";
2700 
2701  bool res = true;
2702  if (hasBodyData || hasDavData)
2703  res = sendBody();
2704 
2705  infoMessage(i18n("%1 contacted. Waiting for reply...", m_request.url.host()));
2706 
2707  return res;
2708 }
2709 
2710 void HTTPProtocol::forwardHttpResponseHeader(bool forwardImmediately)
2711 {
2712  // Send the response header if it was requested...
2713  if (!config()->readEntry("PropagateHttpHeader", false))
2714  return;
2715 
2716  setMetaData(QLatin1String("HTTP-Headers"), m_responseHeaders.join(QString(QLatin1Char('\n'))));
2717 
2718  if (forwardImmediately)
2719  sendMetaData();
2720 }
2721 
2722 bool HTTPProtocol::parseHeaderFromCache()
2723 {
2724  kDebug(7113);
2725  if (!cacheFileReadTextHeader2()) {
2726  return false;
2727  }
2728 
2729  Q_FOREACH (const QString &str, m_responseHeaders) {
2730  const QString header = str.trimmed();
2731  if (header.startsWith(QLatin1String("content-type:")), Qt::CaseInsensitive) {
2732  int pos = header.indexOf(QLatin1String("charset="), Qt::CaseInsensitive);
2733  if (pos != -1) {
2734  const QString charset = header.mid(pos + 8).toLower();
2735  m_request.cacheTag.charset = charset;
2736  setMetaData(QLatin1String("charset"), charset);
2737  }
2738  } else if (header.startsWith(QLatin1String("content-language:")), Qt::CaseInsensitive) {
2739  const QString language = header.mid(17).trimmed().toLower();
2740  setMetaData(QLatin1String("content-language"), language);
2741  } else if (header.startsWith(QLatin1String("content-disposition:")), Qt::CaseInsensitive) {
2742  parseContentDisposition(header.mid(20).toLower());
2743  }
2744  }
2745 
2746  if (m_request.cacheTag.lastModifiedDate != -1) {
2747  setMetaData(QLatin1String("modified"), formatHttpDate(m_request.cacheTag.lastModifiedDate));
2748  }
2749 
2750  // this header comes from the cache, so the response must have been cacheable :)
2751  setCacheabilityMetadata(true);
2752  kDebug(7113) << "Emitting mimeType" << m_mimeType;
2753  forwardHttpResponseHeader(false);
2754  mimeType(m_mimeType);
2755  // IMPORTANT: Do not remove the call below or the http response headers will
2756  // not be available to the application if this slave is put on hold.
2757  forwardHttpResponseHeader();
2758  return true;
2759 }
2760 
2761 void HTTPProtocol::fixupResponseMimetype()
2762 {
2763  if (m_mimeType.isEmpty())
2764  return;
2765 
2766  kDebug(7113) << "before fixup" << m_mimeType;
2767  // Convert some common mimetypes to standard mimetypes
2768  if (m_mimeType == QLatin1String("application/x-targz"))
2769  m_mimeType = QLatin1String("application/x-compressed-tar");
2770  else if (m_mimeType == QLatin1String("image/x-png"))
2771  m_mimeType = QLatin1String("image/png");
2772  else if (m_mimeType == QLatin1String("audio/x-mp3") || m_mimeType == QLatin1String("audio/x-mpeg") || m_mimeType == QLatin1String("audio/mp3"))
2773  m_mimeType = QLatin1String("audio/mpeg");
2774  else if (m_mimeType == QLatin1String("audio/microsoft-wave"))
2775  m_mimeType = QLatin1String("audio/x-wav");
2776  else if (m_mimeType == QLatin1String("image/x-ms-bmp"))
2777  m_mimeType = QLatin1String("image/bmp");
2778 
2779  // Crypto ones....
2780  else if (m_mimeType == QLatin1String("application/pkix-cert") ||
2781  m_mimeType == QLatin1String("application/binary-certificate")) {
2782  m_mimeType = QLatin1String("application/x-x509-ca-cert");
2783  }
2784 
2785  // Prefer application/x-compressed-tar or x-gzpostscript over application/x-gzip.
2786  else if (m_mimeType == QLatin1String("application/x-gzip")) {
2787  if ((m_request.url.path().endsWith(QLatin1String(".tar.gz"))) ||
2788  (m_request.url.path().endsWith(QLatin1String(".tar"))))
2789  m_mimeType = QLatin1String("application/x-compressed-tar");
2790  if ((m_request.url.path().endsWith(QLatin1String(".ps.gz"))))
2791  m_mimeType = QLatin1String("application/x-gzpostscript");
2792  }
2793 
2794  // Prefer application/x-xz-compressed-tar over application/x-xz for LMZA compressed
2795  // tar files. Arch Linux AUR servers notoriously send the wrong mimetype for this.
2796  else if(m_mimeType == QLatin1String("application/x-xz")) {
2797  if (m_request.url.path().endsWith(QLatin1String(".tar.xz")) ||
2798  m_request.url.path().endsWith(QLatin1String(".txz"))) {
2799  m_mimeType = QLatin1String("application/x-xz-compressed-tar");
2800  }
2801  }
2802 
2803  // Some webservers say "text/plain" when they mean "application/x-bzip"
2804  else if ((m_mimeType == QLatin1String("text/plain")) || (m_mimeType == QLatin1String("application/octet-stream"))) {
2805  const QString ext = QFileInfo(m_request.url.path()).suffix().toUpper();
2806  if (ext == QLatin1String("BZ2"))
2807  m_mimeType = QLatin1String("application/x-bzip");
2808  else if (ext == QLatin1String("PEM"))
2809  m_mimeType = QLatin1String("application/x-x509-ca-cert");
2810  else if (ext == QLatin1String("SWF"))
2811  m_mimeType = QLatin1String("application/x-shockwave-flash");
2812  else if (ext == QLatin1String("PLS"))
2813  m_mimeType = QLatin1String("audio/x-scpls");
2814  else if (ext == QLatin1String("WMV"))
2815  m_mimeType = QLatin1String("video/x-ms-wmv");
2816  else if (ext == QLatin1String("WEBM"))
2817  m_mimeType = QLatin1String("video/webm");
2818  else if (ext == QLatin1String("DEB"))
2819  m_mimeType = QLatin1String("application/x-deb");
2820  }
2821  kDebug(7113) << "after fixup" << m_mimeType;
2822 }
2823 
2824 
2825 void HTTPProtocol::fixupResponseContentEncoding()
2826 {
2827  // WABA: Correct for tgz files with a gzip-encoding.
2828  // They really shouldn't put gzip in the Content-Encoding field!
2829  // Web-servers really shouldn't do this: They let Content-Size refer
2830  // to the size of the tgz file, not to the size of the tar file,
2831  // while the Content-Type refers to "tar" instead of "tgz".
2832  if (!m_contentEncodings.isEmpty() && m_contentEncodings.last() == QLatin1String("gzip")) {
2833  if (m_mimeType == QLatin1String("application/x-tar")) {
2834  m_contentEncodings.removeLast();
2835  m_mimeType = QLatin1String("application/x-compressed-tar");
2836  } else if (m_mimeType == QLatin1String("application/postscript")) {
2837  // LEONB: Adding another exception for psgz files.
2838  // Could we use the mimelnk files instead of hardcoding all this?
2839  m_contentEncodings.removeLast();
2840  m_mimeType = QLatin1String("application/x-gzpostscript");
2841  } else if ((m_request.allowTransferCompression &&
2842  m_mimeType == QLatin1String("text/html"))
2843  ||
2844  (m_request.allowTransferCompression &&
2845  m_mimeType != QLatin1String("application/x-compressed-tar") &&
2846  m_mimeType != QLatin1String("application/x-tgz") && // deprecated name
2847  m_mimeType != QLatin1String("application/x-targz") && // deprecated name
2848  m_mimeType != QLatin1String("application/x-gzip"))) {
2849  // Unzip!
2850  } else {
2851  m_contentEncodings.removeLast();
2852  m_mimeType = QLatin1String("application/x-gzip");
2853  }
2854  }
2855 
2856  // We can't handle "bzip2" encoding (yet). So if we get something with
2857  // bzip2 encoding, we change the mimetype to "application/x-bzip".
2858  // Note for future changes: some web-servers send both "bzip2" as
2859  // encoding and "application/x-bzip[2]" as mimetype. That is wrong.
2860  // currently that doesn't bother us, because we remove the encoding
2861  // and set the mimetype to x-bzip anyway.
2862  if (!m_contentEncodings.isEmpty() && m_contentEncodings.last() == QLatin1String("bzip2")) {
2863  m_contentEncodings.removeLast();
2864  m_mimeType = QLatin1String("application/x-bzip");
2865  }
2866 }
2867 
2868 //Return true if the term was found, false otherwise. Advance *pos.
2869 //If (*pos + strlen(term) >= end) just advance *pos to end and return false.
2870 //This means that users should always search for the shortest terms first.
2871 static bool consume(const char input[], int *pos, int end, const char *term)
2872 {
2873  // note: gcc/g++ is quite good at optimizing away redundant strlen()s
2874  int idx = *pos;
2875  if (idx + (int)strlen(term) >= end) {
2876  *pos = end;
2877  return false;
2878  }
2879  if (strncasecmp(&input[idx], term, strlen(term)) == 0) {
2880  *pos = idx + strlen(term);
2881  return true;
2882  }
2883  return false;
2884 }
2885 
2892 bool HTTPProtocol::readResponseHeader()
2893 {
2894  resetResponseParsing();
2895  if (m_request.cacheTag.ioMode == ReadFromCache &&
2896  m_request.cacheTag.plan(m_maxCacheAge) == CacheTag::UseCached) {
2897  // parseHeaderFromCache replaces this method in case of cached content
2898  return parseHeaderFromCache();
2899  }
2900 
2901 try_again:
2902  kDebug(7113);
2903 
2904  bool upgradeRequired = false; // Server demands that we upgrade to something
2905  // This is also true if we ask to upgrade and
2906  // the server accepts, since we are now
2907  // committed to doing so
2908  bool noHeadersFound = false;
2909 
2910  m_request.cacheTag.charset.clear();
2911  m_responseHeaders.clear();
2912 
2913  static const int maxHeaderSize = 128 * 1024;
2914 
2915  char buffer[maxHeaderSize];
2916  bool cont = false;
2917  bool bCanResume = false;
2918 
2919  if (!isConnected()) {
2920  kDebug(7113) << "No connection.";
2921  return false; // Reestablish connection and try again
2922  }
2923 
2924 #if 0
2925  // NOTE: This is unnecessary since TCPSlaveBase::read does the same exact
2926  // thing. Plus, if we are unable to read from the socket we need to resend
2927  // the request as done below, not error out! Do not assume remote server
2928  // will honor persistent connections!!
2929  if (!waitForResponse(m_remoteRespTimeout)) {
2930  kDebug(7113) << "Got socket error:" << socket()->errorString();
2931  // No response error
2932  error(ERR_SERVER_TIMEOUT , m_request.url.host());
2933  return false;
2934  }
2935 #endif
2936 
2937  int bufPos = 0;
2938  bool foundDelimiter = readDelimitedText(buffer, &bufPos, maxHeaderSize, 1);
2939  if (!foundDelimiter && bufPos < maxHeaderSize) {
2940  kDebug(7113) << "EOF while waiting for header start.";
2941  if (m_request.isKeepAlive) {
2942  // Try to reestablish connection.
2943  httpCloseConnection();
2944  return false; // Reestablish connection and try again.
2945  }
2946 
2947  if (m_request.method == HTTP_HEAD) {
2948  // HACK
2949  // Some web-servers fail to respond properly to a HEAD request.
2950  // We compensate for their failure to properly implement the HTTP standard
2951  // by assuming that they will be sending html.
2952  kDebug(7113) << "HEAD -> returned mimetype:" << DEFAULT_MIME_TYPE;
2953  mimeType(QLatin1String(DEFAULT_MIME_TYPE));
2954  return true;
2955  }
2956 
2957  kDebug(7113) << "Connection broken !";
2958  error( ERR_CONNECTION_BROKEN, m_request.url.host() );
2959  return false;
2960  }
2961  if (!foundDelimiter) {
2962  //### buffer too small for first line of header(!)
2963  Q_ASSERT(0);
2964  }
2965 
2966  kDebug(7103) << "============ Received Status Response:";
2967  kDebug(7103) << QByteArray(buffer, bufPos).trimmed();
2968 
2969  HTTP_REV httpRev = HTTP_None;
2970  int idx = 0;
2971 
2972  if (idx != bufPos && buffer[idx] == '<') {
2973  kDebug(7103) << "No valid HTTP header found! Document starts with XML/HTML tag";
2974  // document starts with a tag, assume HTML instead of text/plain
2975  m_mimeType = QLatin1String("text/html");
2976  m_request.responseCode = 200; // Fake it
2977  httpRev = HTTP_Unknown;
2978  m_request.isKeepAlive = false;
2979  noHeadersFound = true;
2980  // put string back
2981  unread(buffer, bufPos);
2982  goto endParsing;
2983  }
2984 
2985  // "HTTP/1.1" or similar
2986  if (consume(buffer, &idx, bufPos, "ICY ")) {
2987  httpRev = SHOUTCAST;
2988  m_request.isKeepAlive = false;
2989  } else if (consume(buffer, &idx, bufPos, "HTTP/")) {
2990  if (consume(buffer, &idx, bufPos, "1.0")) {
2991  httpRev = HTTP_10;
2992  m_request.isKeepAlive = false;
2993  } else if (consume(buffer, &idx, bufPos, "1.1")) {
2994  httpRev = HTTP_11;
2995  }
2996  }
2997 
2998  if (httpRev == HTTP_None && bufPos != 0) {
2999  // Remote server does not seem to speak HTTP at all
3000  // Put the crap back into the buffer and hope for the best
3001  kDebug(7113) << "DO NOT WANT." << bufPos;
3002  unread(buffer, bufPos);
3003  if (m_request.responseCode) {
3004  m_request.prevResponseCode = m_request.responseCode;
3005  }
3006  m_request.responseCode = 200; // Fake it
3007  httpRev = HTTP_Unknown;
3008  m_request.isKeepAlive = false;
3009  noHeadersFound = true;
3010  goto endParsing;
3011  }
3012 
3013  // response code //### maybe wrong if we need several iterations for this response...
3014  //### also, do multiple iterations (cf. try_again) to parse one header work w/ pipelining?
3015  if (m_request.responseCode) {
3016  m_request.prevResponseCode = m_request.responseCode;
3017  }
3018  skipSpace(buffer, &idx, bufPos);
3019  //TODO saner handling of invalid response code strings
3020  if (idx != bufPos) {
3021  m_request.responseCode = atoi(&buffer[idx]);
3022  } else {
3023  m_request.responseCode = 200;
3024  }
3025  // move idx to start of (yet to be fetched) next line, skipping the "OK"
3026  idx = bufPos;
3027  // (don't bother parsing the "OK", what do we do if it isn't there anyway?)
3028 
3029  // immediately act on most response codes...
3030 
3031  // Protect users against bogus username intended to fool them into visiting
3032  // sites they had no intention of visiting.
3033  if (isPotentialSpoofingAttack(m_request, config())) {
3034  // kDebug(7113) << "**** POTENTIAL ADDRESS SPOOFING:" << m_request.url;
3035  const int result = messageBox(WarningYesNo,
3036  i18nc("@warning: Security check on url "
3037  "being accessed", "You are about to "
3038  "log in to the site \"%1\" with the "
3039  "username \"%2\", but the website "
3040  "does not require authentication. "
3041  "This may be an attempt to trick you."
3042  "<p>Is \"%1\" the site you want to visit?",
3043  m_request.url.host(), m_request.url.user()),
3044  i18nc("@title:window", "Confirm Website Access"));
3045  if (result == KMessageBox::No) {
3046  error(ERR_USER_CANCELED, m_request.url.url());
3047  return false;
3048  }
3049  setMetaData(QLatin1String("{internal~currenthost}LastSpoofedUserName"), m_request.url.user());
3050  }
3051 
3052  if (m_request.responseCode != 200 && m_request.responseCode != 304) {
3053  m_request.cacheTag.ioMode = NoCache;
3054  }
3055 
3056  if (m_request.responseCode >= 500 && m_request.responseCode <= 599) {
3057  // Server side errors
3058 
3059  if (m_request.method == HTTP_HEAD) {
3060  ; // Ignore error
3061  } else {
3062  if (!sendErrorPageNotification()) {
3063  error(ERR_INTERNAL_SERVER, m_request.url.url());
3064  return false;
3065  }
3066  }
3067  } else if (m_request.responseCode == 416) {
3068  // Range not supported
3069  m_request.offset = 0;
3070  return false; // Try again.
3071  } else if (m_request.responseCode == 426) {
3072  // Upgrade Required
3073  upgradeRequired = true;
3074  } else if (!isAuthenticationRequired(m_request.responseCode) && m_request.responseCode >= 400 && m_request.responseCode <= 499) {
3075  // Any other client errors
3076  // Tell that we will only get an error page here.
3077  if (!sendErrorPageNotification()) {
3078  if (m_request.responseCode == 403)
3079  error(ERR_ACCESS_DENIED, m_request.url.url());
3080  else
3081  error(ERR_DOES_NOT_EXIST, m_request.url.url());
3082  return false;
3083  }
3084  } else if (m_request.responseCode >= 301 && m_request.responseCode<= 303) {
3085  // 301 Moved permanently
3086  if (m_request.responseCode == 301) {
3087  setMetaData(QLatin1String("permanent-redirect"), QLatin1String("true"));
3088  }
3089  // 302 Found (temporary location)
3090  // 303 See Other
3091  // NOTE: This is wrong according to RFC 2616 (section 10.3.[2-4,8]).
3092  // However, because almost all client implementations treat a 301/302
3093  // response as a 303 response in violation of the spec, many servers
3094  // have simply adapted to this way of doing things! Thus, we are
3095  // forced to do the same thing. Otherwise, we loose compatability and
3096  // might not be able to correctly retrieve sites that redirect.
3097  if (m_request.method != HTTP_HEAD) {
3098  m_request.method = HTTP_GET; // Force a GET
3099  }
3100  } else if (m_request.responseCode == 204) {
3101  // No content
3102 
3103  // error(ERR_NO_CONTENT, i18n("Data have been successfully sent."));
3104  // Short circuit and do nothing!
3105 
3106  // The original handling here was wrong, this is not an error: eg. in the
3107  // example of a 204 No Content response to a PUT completing.
3108  // m_iError = true;
3109  // return false;
3110  } else if (m_request.responseCode == 206) {
3111  if (m_request.offset) {
3112  bCanResume = true;
3113  }
3114  } else if (m_request.responseCode == 102) {
3115  // Processing (for WebDAV)
3116  /***
3117  * This status code is given when the server expects the
3118  * command to take significant time to complete. So, inform
3119  * the user.
3120  */
3121  infoMessage( i18n( "Server processing request, please wait..." ) );
3122  cont = true;
3123  } else if (m_request.responseCode == 100) {
3124  // We got 'Continue' - ignore it
3125  cont = true;
3126  }
3127 
3128 endParsing:
3129  bool authRequiresAnotherRoundtrip = false;
3130 
3131  // Skip the whole header parsing if we got no HTTP headers at all
3132  if (!noHeadersFound) {
3133  // Auth handling
3134  const bool wasAuthError = isAuthenticationRequired(m_request.prevResponseCode);
3135  const bool isAuthError = isAuthenticationRequired(m_request.responseCode);
3136  const bool sameAuthError = (m_request.responseCode == m_request.prevResponseCode);
3137  kDebug(7113) << "wasAuthError=" << wasAuthError << "isAuthError=" << isAuthError
3138  << "sameAuthError=" << sameAuthError;
3139  // Not the same authorization error as before and no generic error?
3140  // -> save the successful credentials.
3141  if (wasAuthError && (m_request.responseCode < 400 || (isAuthError && !sameAuthError))) {
3142  saveAuthenticationData();
3143  }
3144 
3145  // done with the first line; now tokenize the other lines
3146 
3147  // TODO review use of STRTOLL vs. QByteArray::toInt()
3148 
3149  foundDelimiter = readDelimitedText(buffer, &bufPos, maxHeaderSize, 2);
3150  kDebug(7113) << " -- full response:" << endl << QByteArray(buffer, bufPos).trimmed();
3151  Q_ASSERT(foundDelimiter);
3152 
3153  //NOTE because tokenizer will overwrite newlines in case of line continuations in the header
3154  // unread(buffer, bufSize) will not generally work anymore. we don't need it either.
3155  // either we have a http response line -> try to parse the header, fail if it doesn't work
3156  // or we have garbage -> fail.
3157  HeaderTokenizer tokenizer(buffer);
3158  tokenizer.tokenize(idx, sizeof(buffer));
3159 
3160  // Note that not receiving "accept-ranges" means that all bets are off
3161  // wrt the server supporting ranges.
3162  TokenIterator tIt = tokenizer.iterator("accept-ranges");
3163  if (tIt.hasNext() && tIt.next().toLower().startsWith("none")) { // krazy:exclude=strings
3164  bCanResume = false;
3165  }
3166 
3167  tIt = tokenizer.iterator("keep-alive");
3168  while (tIt.hasNext()) {
3169  QByteArray ka = tIt.next().trimmed().toLower();
3170  if (ka.startsWith("timeout=")) { // krazy:exclude=strings
3171  int ka_timeout = ka.mid(qstrlen("timeout=")).trimmed().toInt();
3172  if (ka_timeout > 0)
3173  m_request.keepAliveTimeout = ka_timeout;
3174  if (httpRev == HTTP_10) {
3175  m_request.isKeepAlive = true;
3176  }
3177 
3178  break; // we want to fetch ka timeout only
3179  }
3180  }
3181 
3182  // get the size of our data
3183  tIt = tokenizer.iterator("content-length");
3184  if (tIt.hasNext()) {
3185  m_iSize = STRTOLL(tIt.next().constData(), 0, 10);
3186  }
3187 
3188  tIt = tokenizer.iterator("content-location");
3189  if (tIt.hasNext()) {
3190  setMetaData(QLatin1String("content-location"), toQString(tIt.next().trimmed()));
3191  }
3192 
3193  // which type of data do we have?
3194  QString mediaValue;
3195  QString mediaAttribute;
3196  tIt = tokenizer.iterator("content-type");
3197  if (tIt.hasNext()) {
3198  QList<QByteArray> l = tIt.next().split(';');
3199  if (!l.isEmpty()) {
3200  // Assign the mime-type.
3201  m_mimeType = toQString(l.first().trimmed().toLower());
3202  kDebug(7113) << "Content-type:" << m_mimeType;
3203  l.removeFirst();
3204  }
3205 
3206  // If we still have text, then it means we have a mime-type with a
3207  // parameter (eg: charset=iso-8851) ; so let's get that...
3208  Q_FOREACH (const QByteArray &statement, l) {
3209  const int index = statement.indexOf('=');
3210  if (index <= 0) {
3211  mediaAttribute = toQString(statement.mid(0, index));
3212  } else {
3213  mediaAttribute = toQString(statement.mid(0, index));
3214  mediaValue = toQString(statement.mid(index+1));
3215  }
3216  mediaAttribute = mediaAttribute.trimmed();
3217  mediaValue = mediaValue.trimmed();
3218 
3219  bool quoted = false;
3220  if (mediaValue.startsWith(QLatin1Char('"'))) {
3221  quoted = true;
3222  mediaValue.remove(QLatin1Char('"'));
3223  }
3224 
3225  if (mediaValue.endsWith(QLatin1Char('"'))) {
3226  mediaValue.truncate(mediaValue.length()-1);
3227  }
3228 
3229  kDebug (7113) << "Encoding-type:" << mediaAttribute << "=" << mediaValue;
3230 
3231  if (mediaAttribute == QLatin1String("charset")) {
3232  mediaValue = mediaValue.toLower();
3233  m_request.cacheTag.charset = mediaValue;
3234  setMetaData(QLatin1String("charset"), mediaValue);
3235  } else {
3236  setMetaData(QLatin1String("media-") + mediaAttribute, mediaValue);
3237  if (quoted) {
3238  setMetaData(QLatin1String("media-") + mediaAttribute + QLatin1String("-kio-quoted"),
3239  QLatin1String("true"));
3240  }
3241  }
3242  }
3243  }
3244 
3245  // content?
3246  tIt = tokenizer.iterator("content-encoding");
3247  while (tIt.hasNext()) {
3248  // This is so wrong !! No wonder kio_http is stripping the
3249  // gzip encoding from downloaded files. This solves multiple
3250  // bug reports and caitoo's problem with downloads when such a
3251  // header is encountered...
3252 
3253  // A quote from RFC 2616:
3254  // " When present, its (Content-Encoding) value indicates what additional
3255  // content have been applied to the entity body, and thus what decoding
3256  // mechanism must be applied to obtain the media-type referenced by the
3257  // Content-Type header field. Content-Encoding is primarily used to allow
3258  // a document to be compressed without loosing the identity of its underlying
3259  // media type. Simply put if it is specified, this is the actual mime-type
3260  // we should use when we pull the resource !!!
3261  addEncoding(toQString(tIt.next()), m_contentEncodings);
3262  }
3263  // Refer to RFC 2616 sec 15.5/19.5.1 and RFC 2183
3264  tIt = tokenizer.iterator("content-disposition");
3265  if (tIt.hasNext()) {
3266  parseContentDisposition(toQString(tIt.next()));
3267  }
3268  tIt = tokenizer.iterator("content-language");
3269  if (tIt.hasNext()) {
3270  QString language = toQString(tIt.next().trimmed());
3271  if (!language.isEmpty()) {
3272  setMetaData(QLatin1String("content-language"), language);
3273  }
3274  }
3275 
3276  tIt = tokenizer.iterator("proxy-connection");
3277  if (tIt.hasNext() && isHttpProxy(m_request.proxyUrl) && !isAutoSsl()) {
3278  QByteArray pc = tIt.next().toLower();
3279  if (pc.startsWith("close")) { // krazy:exclude=strings
3280  m_request.isKeepAlive = false;
3281  } else if (pc.startsWith("keep-alive")) { // krazy:exclude=strings
3282  m_request.isKeepAlive = true;
3283  }
3284  }
3285 
3286  tIt = tokenizer.iterator("link");
3287  if (tIt.hasNext()) {
3288  // We only support Link: <url>; rel="type" so far
3289  QStringList link = toQString(tIt.next()).split(QLatin1Char(';'), QString::SkipEmptyParts);
3290  if (link.count() == 2) {
3291  QString rel = link[1].trimmed();
3292  if (rel.startsWith(QLatin1String("rel=\""))) {
3293  rel = rel.mid(5, rel.length() - 6);
3294  if (rel.toLower() == QLatin1String("pageservices")) {
3295  //### the remove() part looks fishy!
3296  QString url = link[0].remove(QRegExp(QLatin1String("[<>]"))).trimmed();
3297  setMetaData(QLatin1String("PageServices"), url);
3298  }
3299  }
3300  }
3301  }
3302 
3303  tIt = tokenizer.iterator("p3p");
3304  if (tIt.hasNext()) {
3305  // P3P privacy policy information
3306  QStringList policyrefs, compact;
3307  while (tIt.hasNext()) {
3308  QStringList policy = toQString(tIt.next().simplified())
3309  .split(QLatin1Char('='), QString::SkipEmptyParts);
3310  if (policy.count() == 2) {
3311  if (policy[0].toLower() == QLatin1String("policyref")) {
3312  policyrefs << policy[1].remove(QRegExp(QLatin1String("[\")\']"))).trimmed();
3313  } else if (policy[0].toLower() == QLatin1String("cp")) {
3314  // We convert to cp\ncp\ncp\n[...]\ncp to be consistent with
3315  // other metadata sent in strings. This could be a bit more
3316  // efficient but I'm going for correctness right now.
3317  const QString s = policy[1].remove(QRegExp(QLatin1String("[\")\']")));
3318  const QStringList cps = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
3319  compact << cps;
3320  }
3321  }
3322  }
3323  if (!policyrefs.isEmpty()) {
3324  setMetaData(QLatin1String("PrivacyPolicy"), policyrefs.join(QLatin1String("\n")));
3325  }
3326  if (!compact.isEmpty()) {
3327  setMetaData(QLatin1String("PrivacyCompactPolicy"), compact.join(QLatin1String("\n")));
3328  }
3329  }
3330 
3331  // continue only if we know that we're at least HTTP/1.0
3332  if (httpRev == HTTP_11 || httpRev == HTTP_10) {
3333  // let them tell us if we should stay alive or not
3334  tIt = tokenizer.iterator("connection");
3335  while (tIt.hasNext()) {
3336  QByteArray connection = tIt.next().toLower();
3337  if (!(isHttpProxy(m_request.proxyUrl) && !isAutoSsl())) {
3338  if (connection.startsWith("close")) { // krazy:exclude=strings
3339  m_request.isKeepAlive = false;
3340  } else if (connection.startsWith("keep-alive")) { // krazy:exclude=strings
3341  m_request.isKeepAlive = true;
3342  }
3343  }
3344  if (connection.startsWith("upgrade")) { // krazy:exclude=strings
3345  if (m_request.responseCode == 101) {
3346  // Ok, an upgrade was accepted, now we must do it
3347  upgradeRequired = true;
3348  } else if (upgradeRequired) { // 426
3349  // Nothing to do since we did it above already
3350  }
3351  }
3352  }
3353  // what kind of encoding do we have? transfer?
3354  tIt = tokenizer.iterator("transfer-encoding");
3355  while (tIt.hasNext()) {
3356  // If multiple encodings have been applied to an entity, the
3357  // transfer-codings MUST be listed in the order in which they
3358  // were applied.
3359  addEncoding(toQString(tIt.next().trimmed()), m_transferEncodings);
3360  }
3361 
3362  // md5 signature
3363  tIt = tokenizer.iterator("content-md5");
3364  if (tIt.hasNext()) {
3365  m_contentMD5 = toQString(tIt.next().trimmed());
3366  }
3367 
3368  // *** Responses to the HTTP OPTIONS method follow
3369  // WebDAV capabilities
3370  tIt = tokenizer.iterator("dav");
3371  while (tIt.hasNext()) {
3372  m_davCapabilities << toQString(tIt.next());
3373  }
3374  // *** Responses to the HTTP OPTIONS method finished
3375  }
3376 
3377 
3378  // Now process the HTTP/1.1 upgrade
3379  QStringList upgradeOffers;
3380  tIt = tokenizer.iterator("upgrade");
3381  if (tIt.hasNext()) {
3382  // Now we have to check to see what is offered for the upgrade
3383  QString offered = toQString(tIt.next());
3384  upgradeOffers = offered.split(QRegExp(QLatin1String("[ \n,\r\t]")), QString::SkipEmptyParts);
3385  }
3386  Q_FOREACH (const QString &opt, upgradeOffers) {
3387  if (opt == QLatin1String("TLS/1.0")) {
3388  if (!startSsl() && upgradeRequired) {
3389  error(ERR_UPGRADE_REQUIRED, opt);
3390  return false;
3391  }
3392  } else if (opt == QLatin1String("HTTP/1.1")) {
3393  httpRev = HTTP_11;
3394  } else if (upgradeRequired) {
3395  // we are told to do an upgrade we don't understand
3396  error(ERR_UPGRADE_REQUIRED, opt);
3397  return false;
3398  }
3399  }
3400 
3401  // Harvest cookies (mmm, cookie fields!)
3402  QByteArray cookieStr; // In case we get a cookie.
3403  tIt = tokenizer.iterator("set-cookie");
3404  while (tIt.hasNext()) {
3405  cookieStr += "Set-Cookie: ";
3406  cookieStr += tIt.next();
3407  cookieStr += '\n';
3408  }
3409  if (!cookieStr.isEmpty()) {
3410  if ((m_request.cookieMode == HTTPRequest::CookiesAuto) && m_request.useCookieJar) {
3411  // Give cookies to the cookiejar.
3412  const QString domain = config()->readEntry("cross-domain");
3413  if (!domain.isEmpty() && isCrossDomainRequest(m_request.url.host(), domain)) {
3414  cookieStr = "Cross-Domain\n" + cookieStr;
3415  }
3416  addCookies( m_request.url.url(), cookieStr );
3417  } else if (m_request.cookieMode == HTTPRequest::CookiesManual) {
3418  // Pass cookie to application
3419  setMetaData(QLatin1String("setcookies"), QString::fromUtf8(cookieStr)); // ## is encoding ok?
3420  }
3421  }
3422 
3423  // We need to reread the header if we got a '100 Continue' or '102 Processing'
3424  // This may be a non keepalive connection so we handle this kind of loop internally
3425  if ( cont )
3426  {
3427  kDebug(7113) << "cont; returning to mark try_again";
3428  goto try_again;
3429  }
3430 
3431  if (!m_isChunked && (m_iSize == NO_SIZE) && m_request.isKeepAlive &&
3432  canHaveResponseBody(m_request.responseCode, m_request.method)) {
3433  kDebug(7113) << "Ignoring keep-alive: otherwise unable to determine response body length.";
3434  m_request.isKeepAlive = false;
3435  }
3436 
3437  // TODO cache the proxy auth data (not doing this means a small performance regression for now)
3438 
3439  // we may need to send (Proxy or WWW) authorization data
3440  if (!m_request.doNotAuthenticate && isAuthenticationRequired(m_request.responseCode)) {
3441  authRequiresAnotherRoundtrip = handleAuthenticationHeader(&tokenizer);
3442  if (m_iError) {
3443  // If error is set, then handleAuthenticationHeader failed.
3444  return false;
3445  }
3446  } else {
3447  authRequiresAnotherRoundtrip = false;
3448  }
3449 
3450  QString locationStr;
3451  // In fact we should do redirection only if we have a redirection response code (300 range)
3452  tIt = tokenizer.iterator("location");
3453  if (tIt.hasNext() && m_request.responseCode > 299 && m_request.responseCode < 400) {
3454  locationStr = QString::fromUtf8(tIt.next().trimmed());
3455  }
3456  // We need to do a redirect
3457  if (!locationStr.isEmpty())
3458  {
3459  KUrl u(m_request.url, locationStr);
3460  if(!u.isValid())
3461  {
3462  error(ERR_MALFORMED_URL, u.url());
3463  return false;
3464  }
3465 
3466  // preserve #ref: (bug 124654)
3467  // if we were at http://host/resource1#ref, we sent a GET for "/resource1"
3468  // if we got redirected to http://host/resource2, then we have to re-add
3469  // the fragment:
3470  if (m_request.url.hasRef() && !u.hasRef() &&
3471  (m_request.url.host() == u.host()) &&
3472  (m_request.url.protocol() == u.protocol()))
3473  u.setRef(m_request.url.ref());
3474 
3475  m_isRedirection = true;
3476 
3477  if (!m_request.id.isEmpty())
3478  {
3479  sendMetaData();
3480  }
3481 
3482  // If we're redirected to a http:// url, remember that we're doing webdav...
3483  if (m_protocol == "webdav" || m_protocol == "webdavs"){
3484  if(u.protocol() == QLatin1String("http")){
3485  u.setProtocol(QLatin1String("webdav"));
3486  }else if(u.protocol() == QLatin1String("https")){
3487  u.setProtocol(QLatin1String("webdavs"));
3488  }
3489 
3490  m_request.redirectUrl = u;
3491  }
3492 
3493  kDebug(7113) << "Re-directing from" << m_request.url.url()
3494  << "to" << u.url();
3495 
3496  redirection(u);
3497 
3498  // It would be hard to cache the redirection response correctly. The possible benefit
3499  // is small (if at all, assuming fast disk and slow network), so don't do it.
3500  cacheFileClose();
3501  setCacheabilityMetadata(false);
3502  }
3503 
3504  // Inform the job that we can indeed resume...
3505  if (bCanResume && m_request.offset) {
3506  //TODO turn off caching???
3507  canResume();
3508  } else {
3509  m_request.offset = 0;
3510  }
3511 
3512  // Correct a few common wrong content encodings
3513  fixupResponseContentEncoding();
3514 
3515  // Correct some common incorrect pseudo-mimetypes
3516  fixupResponseMimetype();
3517 
3518  // parse everything related to expire and other dates, and cache directives; also switch
3519  // between cache reading and writing depending on cache validation result.
3520  cacheParseResponseHeader(tokenizer);
3521  }
3522 
3523  if (m_request.cacheTag.ioMode == ReadFromCache) {
3524  if (m_request.cacheTag.policy == CC_Verify &&
3525  m_request.cacheTag.plan(m_maxCacheAge) != CacheTag::UseCached) {
3526  kDebug(7113) << "Reading resource from cache even though the cache plan is not "
3527  "UseCached; the server is probably sending wrong expiry information.";
3528  }
3529  // parseHeaderFromCache replaces this method in case of cached content
3530  return parseHeaderFromCache();
3531  }
3532 
3533  if (config()->readEntry("PropagateHttpHeader", false) ||
3534  m_request.cacheTag.ioMode == WriteToCache) {
3535  // store header lines if they will be used; note that the tokenizer removing
3536  // line continuation special cases is probably more good than bad.
3537  int nextLinePos = 0;
3538  int prevLinePos = 0;
3539  bool haveMore = true;
3540  while (haveMore) {
3541  haveMore = nextLine(buffer, &nextLinePos, bufPos);
3542  int prevLineEnd = nextLinePos;
3543  while (buffer[prevLineEnd - 1] == '\r' || buffer[prevLineEnd - 1] == '\n') {
3544  prevLineEnd--;
3545  }
3546 
3547  m_responseHeaders.append(QString::fromLatin1(&buffer[prevLinePos],
3548  prevLineEnd - prevLinePos));
3549  prevLinePos = nextLinePos;
3550  }
3551 
3552  // IMPORTANT: Do not remove this line because forwardHttpResponseHeader
3553  // is called below. This line is here to ensure the response headers are
3554  // available to the client before it receives mimetype information.
3555  // The support for putting ioslaves on hold in the KIO-QNAM integration
3556  // will break if this line is removed.
3557  setMetaData(QLatin1String("HTTP-Headers"), m_responseHeaders.join(QString(QLatin1Char('\n'))));
3558  }
3559 
3560  // Let the app know about the mime-type iff this is not a redirection and
3561  // the mime-type string is not empty.
3562  if (!m_isRedirection && m_request.responseCode != 204 &&
3563  (!m_mimeType.isEmpty() || m_request.method == HTTP_HEAD) &&
3564  (m_isLoadingErrorPage || !authRequiresAnotherRoundtrip)) {
3565  kDebug(7113) << "Emitting mimetype " << m_mimeType;
3566  mimeType( m_mimeType );
3567  }
3568 
3569  // IMPORTANT: Do not move the function call below before doing any
3570  // redirection. Otherwise it might mess up some sites, see BR# 150904.
3571  forwardHttpResponseHeader();
3572 
3573  if (m_request.method == HTTP_HEAD)
3574  return true;
3575 
3576  return !authRequiresAnotherRoundtrip; // return true if no more credentials need to be sent
3577 }
3578 
3579 void HTTPProtocol::parseContentDisposition(const QString &disposition)
3580 {
3581  const QMap<QString, QString> parameters = contentDispositionParser(disposition);
3582 
3583  QMap<QString, QString>::const_iterator i = parameters.constBegin();
3584  while (i != parameters.constEnd()) {
3585  setMetaData(QLatin1String("content-disposition-") + i.key(), i.value());
3586  kDebug(7113) << "Content-Disposition:" << i.key() << "=" << i.value();
3587  ++i;
3588  }
3589 }
3590 
3591 void HTTPProtocol::addEncoding(const QString &_encoding, QStringList &encs)
3592 {
3593  QString encoding = _encoding.trimmed().toLower();
3594  // Identity is the same as no encoding
3595  if (encoding == QLatin1String("identity")) {
3596  return;
3597  } else if (encoding == QLatin1String("8bit")) {
3598  // Strange encoding returned by http://linac.ikp.physik.tu-darmstadt.de
3599  return;
3600  } else if (encoding == QLatin1String("chunked")) {
3601  m_isChunked = true;
3602  // Anyone know of a better way to handle unknown sizes possibly/ideally with unsigned ints?
3603  //if ( m_cmd != CMD_COPY )
3604  m_iSize = NO_SIZE;
3605  } else if ((encoding == QLatin1String("x-gzip")) || (encoding == QLatin1String("gzip"))) {
3606  encs.append(QLatin1String("gzip"));
3607  } else if ((encoding == QLatin1String("x-bzip2")) || (encoding == QLatin1String("bzip2"))) {
3608  encs.append(QLatin1String("bzip2")); // Not yet supported!
3609  } else if ((encoding == QLatin1String("x-deflate")) || (encoding == QLatin1String("deflate"))) {
3610  encs.append(QLatin1String("deflate"));
3611  } else {
3612  kDebug(7113) << "Unknown encoding encountered. "
3613  << "Please write code. Encoding =" << encoding;
3614  }
3615 }
3616 
3617 void HTTPProtocol::cacheParseResponseHeader(const HeaderTokenizer &tokenizer)
3618 {
3619  if (!m_request.cacheTag.useCache)
3620  return;
3621 
3622  // might have to add more response codes
3623  if (m_request.responseCode != 200 && m_request.responseCode != 304) {
3624  return;
3625  }
3626 
3627  // -1 is also the value returned by KDateTime::toTime_t() from an invalid instance.
3628  m_request.cacheTag.servedDate = -1;
3629  m_request.cacheTag.lastModifiedDate = -1;
3630  m_request.cacheTag.expireDate = -1;
3631 
3632  const qint64 currentDate = time(0);
3633  bool mayCache = m_request.cacheTag.ioMode != NoCache;
3634 
3635  TokenIterator tIt = tokenizer.iterator("last-modified");
3636  if (tIt.hasNext()) {
3637  m_request.cacheTag.lastModifiedDate =
3638  KDateTime::fromString(toQString(tIt.next()), KDateTime::RFCDate).toTime_t();
3639 
3640  //### might be good to canonicalize the date by using KDateTime::toString()
3641  if (m_request.cacheTag.lastModifiedDate != -1) {
3642  setMetaData(QLatin1String("modified"), toQString(tIt.current()));
3643  }
3644  }
3645 
3646  // determine from available information when the response was served by the origin server
3647  {
3648  qint64 dateHeader = -1;
3649  tIt = tokenizer.iterator("date");
3650  if (tIt.hasNext()) {
3651  dateHeader = KDateTime::fromString(toQString(tIt.next()), KDateTime::RFCDate).toTime_t();
3652  // -1 on error
3653  }
3654 
3655  qint64 ageHeader = 0;
3656  tIt = tokenizer.iterator("age");
3657  if (tIt.hasNext()) {
3658  ageHeader = tIt.next().toLongLong();
3659  // 0 on error
3660  }
3661 
3662  if (dateHeader != -1) {
3663  m_request.cacheTag.servedDate = dateHeader;
3664  } else if (ageHeader) {
3665  m_request.cacheTag.servedDate = currentDate - ageHeader;
3666  } else {
3667  m_request.cacheTag.servedDate = currentDate;
3668  }
3669  }
3670 
3671  bool hasCacheDirective = false;
3672  // determine when the response "expires", i.e. becomes stale and needs revalidation
3673  {
3674  // (we also parse other cache directives here)
3675  qint64 maxAgeHeader = 0;
3676  tIt = tokenizer.iterator("cache-control");
3677  while (tIt.hasNext()) {
3678  QByteArray cacheStr = tIt.next().toLower();
3679  if (cacheStr.startsWith("no-cache") || cacheStr.startsWith("no-store")) { // krazy:exclude=strings
3680  // Don't put in cache
3681  mayCache = false;
3682  hasCacheDirective = true;
3683  } else if (cacheStr.startsWith("max-age=")) { // krazy:exclude=strings
3684  QByteArray ba = cacheStr.mid(qstrlen("max-age=")).trimmed();
3685  bool ok = false;
3686  maxAgeHeader = ba.toLongLong(&ok);
3687  if (ok) {
3688  hasCacheDirective = true;
3689  }
3690  }
3691  }
3692 
3693  qint64 expiresHeader = -1;
3694  tIt = tokenizer.iterator("expires");
3695  if (tIt.hasNext()) {
3696  expiresHeader = KDateTime::fromString(toQString(tIt.next()), KDateTime::RFCDate).toTime_t();
3697  kDebug(7113) << "parsed expire date from 'expires' header:" << tIt.current();
3698  }
3699 
3700  if (maxAgeHeader) {
3701  m_request.cacheTag.expireDate = m_request.cacheTag.servedDate + maxAgeHeader;
3702  } else if (expiresHeader != -1) {
3703  m_request.cacheTag.expireDate = expiresHeader;
3704  } else {
3705  // heuristic expiration date
3706  if (m_request.cacheTag.lastModifiedDate != -1) {
3707  // expAge is following the RFC 2616 suggestion for heuristic expiration
3708  qint64 expAge = (m_request.cacheTag.servedDate -
3709  m_request.cacheTag.lastModifiedDate) / 10;
3710  // not in the RFC: make sure not to have a huge heuristic cache lifetime
3711  expAge = qMin(expAge, qint64(3600 * 24));
3712  m_request.cacheTag.expireDate = m_request.cacheTag.servedDate + expAge;
3713  } else {
3714  m_request.cacheTag.expireDate = m_request.cacheTag.servedDate +
3715  DEFAULT_CACHE_EXPIRE;
3716  }
3717  }
3718  // make sure that no future clock monkey business causes the cache entry to un-expire
3719  if (m_request.cacheTag.expireDate < currentDate) {
3720  m_request.cacheTag.expireDate = 0; // January 1, 1970 :)
3721  }
3722  }
3723 
3724  tIt = tokenizer.iterator("etag");
3725  if (tIt.hasNext()) {
3726  QString prevEtag = m_request.cacheTag.etag;
3727  m_request.cacheTag.etag = toQString(tIt.next());
3728  if (m_request.cacheTag.etag != prevEtag && m_request.responseCode == 304) {
3729  kDebug(7103) << "304 Not Modified but new entity tag - I don't think this is legal HTTP.";
3730  }
3731  }
3732 
3733  // whoops.. we received a warning
3734  tIt = tokenizer.iterator("warning");
3735  if (tIt.hasNext()) {
3736  //Don't use warning() here, no need to bother the user.
3737  //Those warnings are mostly about caches.
3738  infoMessage(toQString(tIt.next()));
3739  }
3740 
3741  // Cache management (HTTP 1.0)
3742  tIt = tokenizer.iterator("pragma");
3743  while (tIt.hasNext()) {
3744  if (tIt.next().toLower().startsWith("no-cache")) { // krazy:exclude=strings
3745  mayCache = false;
3746  hasCacheDirective = true;
3747  }
3748  }
3749 
3750  // The deprecated Refresh Response
3751  tIt = tokenizer.iterator("refresh");
3752  if (tIt.hasNext()) {
3753  mayCache = false;
3754  setMetaData(QLatin1String("http-refresh"), toQString(tIt.next().trimmed()));
3755  }
3756 
3757  // We don't cache certain text objects
3758  if (m_mimeType.startsWith(QLatin1String("text/")) && (m_mimeType != QLatin1String("text/css")) &&
3759  (m_mimeType != QLatin1String("text/x-javascript")) && !hasCacheDirective) {
3760  // Do not cache secure pages or pages
3761  // originating from password protected sites
3762  // unless the webserver explicitly allows it.
3763  if (isUsingSsl() || m_wwwAuth) {
3764  mayCache = false;
3765  }
3766  }
3767 
3768  // note that we've updated cacheTag, so the plan() is with current data
3769  if (m_request.cacheTag.plan(m_maxCacheAge) == CacheTag::ValidateCached) {
3770  kDebug(7113) << "Cache needs validation";
3771  if (m_request.responseCode == 304) {
3772  kDebug(7113) << "...was revalidated by response code but not by updated expire times. "
3773  "We're going to set the expire date to 60 seconds in the future...";
3774  m_request.cacheTag.expireDate = currentDate + 60;
3775  if (m_request.cacheTag.policy == CC_Verify &&
3776  m_request.cacheTag.plan(m_maxCacheAge) != CacheTag::UseCached) {
3777  // "apparently" because we /could/ have made an error ourselves, but the errors I
3778  // witnessed were all the server's fault.
3779  kDebug(7113) << "this proxy or server apparently sends bogus expiry information.";
3780  }
3781  }
3782  }
3783 
3784  // validation handling
3785  if (mayCache && m_request.responseCode == 200 && !m_mimeType.isEmpty()) {
3786  kDebug(7113) << "Cache, adding" << m_request.url.url();
3787  // ioMode can still be ReadFromCache here if we're performing a conditional get
3788  // aka validation
3789  m_request.cacheTag.ioMode = WriteToCache;
3790  if (!cacheFileOpenWrite()) {
3791  kDebug(7113) << "Error creating cache entry for " << m_request.url.url()<<"!\n";
3792  }
3793  m_maxCacheSize = config()->readEntry("MaxCacheSize", DEFAULT_MAX_CACHE_SIZE);
3794  } else if (m_request.responseCode == 304 && m_request.cacheTag.file) {
3795  if (!mayCache) {
3796  kDebug(7113) << "This webserver is confused about the cacheability of the data it sends.";
3797  }
3798  // the cache file should still be open for reading, see satisfyRequestFromCache().
3799  Q_ASSERT(m_request.cacheTag.file->openMode() == QIODevice::ReadOnly);
3800  Q_ASSERT(m_request.cacheTag.ioMode == ReadFromCache);
3801  } else {
3802  cacheFileClose();
3803  }
3804 
3805  setCacheabilityMetadata(mayCache);
3806 }
3807 
3808 void HTTPProtocol::setCacheabilityMetadata(bool cachingAllowed)
3809 {
3810  if (!cachingAllowed) {
3811  setMetaData(QLatin1String("no-cache"), QLatin1String("true"));
3812  setMetaData(QLatin1String("expire-date"), QLatin1String("1")); // Expired
3813  } else {
3814  QString tmp;
3815  tmp.setNum(m_request.cacheTag.expireDate);
3816  setMetaData(QLatin1String("expire-date"), tmp);
3817  // slightly changed semantics from old creationDate, probably more correct now
3818  tmp.setNum(m_request.cacheTag.servedDate);
3819  setMetaData(QLatin1String("cache-creation-date"), tmp);
3820  }
3821 }
3822 
3823 bool HTTPProtocol::sendCachedBody()
3824 {
3825  infoMessage(i18n("Sending data to %1" , m_request.url.host()));
3826 
3827  QByteArray cLength ("Content-Length: ");
3828  cLength += QByteArray::number(m_POSTbuf->size());
3829  cLength += "\r\n\r\n";
3830 
3831  kDebug(7113) << "sending cached data (size=" << m_POSTbuf->size() << ")";
3832 
3833  // Send the content length...
3834  bool sendOk = (write(cLength.data(), cLength.size()) == (ssize_t) cLength.size());
3835  if (!sendOk) {
3836  kDebug( 7113 ) << "Connection broken when sending "
3837  << "content length: (" << m_request.url.host() << ")";
3838  error( ERR_CONNECTION_BROKEN, m_request.url.host() );
3839  return false;
3840  }
3841 
3842  // Make sure the read head is at the beginning...
3843  m_POSTbuf->reset();
3844 
3845  // Send the data...
3846  while (!m_POSTbuf->atEnd()) {
3847  const QByteArray buffer = m_POSTbuf->read(s_MaxInMemPostBufSize);
3848  sendOk = (write(buffer.data(), buffer.size()) == (ssize_t) buffer.size());
3849  if (!sendOk) {
3850  kDebug(7113) << "Connection broken when sending message body: ("
3851  << m_request.url.host() << ")";
3852  error( ERR_CONNECTION_BROKEN, m_request.url.host() );
3853  return false;
3854  }
3855  }
3856 
3857  return true;
3858 }
3859 
3860 bool HTTPProtocol::sendBody()
3861 {
3862  // If we have cached data, the it is either a repost or a DAV request so send
3863  // the cached data...
3864  if (m_POSTbuf)
3865  return sendCachedBody();
3866 
3867  if (m_iPostDataSize == NO_SIZE) {
3868  // Try the old approach of retireving content data from the job
3869  // before giving up.
3870  if (retrieveAllData())
3871  return sendCachedBody();
3872 
3873  error(ERR_POST_NO_SIZE, m_request.url.host());
3874  return false;
3875  }
3876 
3877  kDebug(7113) << "sending data (size=" << m_iPostDataSize << ")";
3878 
3879  infoMessage(i18n("Sending data to %1", m_request.url.host()));
3880 
3881  QByteArray cLength ("Content-Length: ");
3882  cLength += QByteArray::number(m_iPostDataSize);
3883  cLength += "\r\n\r\n";
3884 
3885  kDebug(7113) << cLength.trimmed();
3886 
3887  // Send the content length...
3888  bool sendOk = (write(cLength.data(), cLength.size()) == (ssize_t) cLength.size());
3889  if (!sendOk) {
3890  // The server might have closed the connection due to a timeout, or maybe
3891  // some transport problem arose while the connection was idle.
3892  if (m_request.isKeepAlive)
3893  {
3894  httpCloseConnection();
3895  return true; // Try again
3896  }
3897 
3898  kDebug(7113) << "Connection broken while sending POST content size to" << m_request.url.host();
3899  error( ERR_CONNECTION_BROKEN, m_request.url.host() );
3900  return false;
3901  }
3902 
3903  // Send the amount
3904  totalSize(m_iPostDataSize);
3905 
3906  // If content-length is 0, then do nothing but simply return true.
3907  if (m_iPostDataSize == 0)
3908  return true;
3909 
3910  sendOk = true;
3911  KIO::filesize_t bytesSent = 0;
3912 
3913  while (true) {
3914  dataReq();
3915 
3916  QByteArray buffer;
3917  const int bytesRead = readData(buffer);
3918 
3919  // On done...
3920  if (bytesRead == 0) {
3921  sendOk = (bytesSent == m_iPostDataSize);
3922  break;
3923  }
3924 
3925  // On error return false...
3926  if (bytesRead < 0) {
3927  error(ERR_ABORTED, m_request.url.host());
3928  sendOk = false;
3929  break;
3930  }
3931 
3932  // Cache the POST data in case of a repost request.
3933  cachePostData(buffer);
3934 
3935  // This will only happen if transmitting the data fails, so we will simply
3936  // cache the content locally for the potential re-transmit...
3937  if (!sendOk)
3938  continue;
3939 
3940  if (write(buffer.data(), bytesRead) == static_cast<ssize_t>(bytesRead)) {
3941  bytesSent += bytesRead;
3942  processedSize(bytesSent); // Send update status...
3943  continue;
3944  }
3945 
3946  kDebug(7113) << "Connection broken while sending POST content to" << m_request.url.host();
3947  error(ERR_CONNECTION_BROKEN, m_request.url.host());
3948  sendOk = false;
3949  }
3950 
3951  return sendOk;
3952 }
3953 
3954 void HTTPProtocol::httpClose( bool keepAlive )
3955 {
3956  kDebug(7113) << "keepAlive =" << keepAlive;
3957 
3958  cacheFileClose();
3959 
3960  // Only allow persistent connections for GET requests.
3961  // NOTE: we might even want to narrow this down to non-form
3962  // based submit requests which will require a meta-data from
3963  // khtml.
3964  if (keepAlive) {
3965  if (!m_request.keepAliveTimeout)
3966  m_request.keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
3967  else if (m_request.keepAliveTimeout > 2*DEFAULT_KEEP_ALIVE_TIMEOUT)
3968  m_request.keepAliveTimeout = 2*DEFAULT_KEEP_ALIVE_TIMEOUT;
3969 
3970  kDebug(7113) << "keep alive (" << m_request.keepAliveTimeout << ")";
3971  QByteArray data;
3972  QDataStream stream( &data, QIODevice::WriteOnly );
3973  stream << int(99); // special: Close connection
3974  setTimeoutSpecialCommand(m_request.keepAliveTimeout, data);
3975 
3976  return;
3977  }
3978 
3979  httpCloseConnection();
3980 }
3981 
3982 void HTTPProtocol::closeConnection()
3983 {
3984  kDebug(7113);
3985  httpCloseConnection();
3986 }
3987 
3988 void HTTPProtocol::httpCloseConnection()
3989 {
3990  kDebug(7113);
3991  m_server.clear();
3992  disconnectFromHost();
3993  clearUnreadBuffer();
3994  setTimeoutSpecialCommand(-1); // Cancel any connection timeout
3995 }
3996 
3997 void HTTPProtocol::slave_status()
3998 {
3999  kDebug(7113);
4000 
4001  if ( !isConnected() )
4002  httpCloseConnection();
4003 
4004  slaveStatus( m_server.url.host(), isConnected() );
4005 }
4006 
4007 void HTTPProtocol::mimetype( const KUrl& url )
4008 {
4009  kDebug(7113) << url.url();
4010 
4011  if (!maybeSetRequestUrl(url))
4012  return;
4013  resetSessionSettings();
4014 
4015  m_request.method = HTTP_HEAD;
4016  m_request.cacheTag.policy= CC_Cache;
4017 
4018  if (proceedUntilResponseHeader()) {
4019  httpClose(m_request.isKeepAlive);
4020  finished();
4021  }
4022 
4023  kDebug(7113) << m_mimeType;
4024 }
4025 
4026 void HTTPProtocol::special( const QByteArray &data )
4027 {
4028  kDebug(7113);
4029 
4030  int tmp;
4031  QDataStream stream(data);
4032 
4033  stream >> tmp;
4034  switch (tmp) {
4035  case 1: // HTTP POST
4036  {
4037  KUrl url;
4038  qint64 size;
4039  stream >> url >> size;
4040  post( url, size );
4041  break;
4042  }
4043  case 2: // cache_update
4044  {
4045  KUrl url;
4046  bool no_cache;
4047  qint64 expireDate;
4048  stream >> url >> no_cache >> expireDate;
4049  if (no_cache) {
4050  QString filename = cacheFilePathFromUrl(url);
4051  // there is a tiny risk of deleting the wrong file due to hash collisions here.
4052  // this is an unimportant performance issue.
4053  // FIXME on Windows we may be unable to delete the file if open
4054  QFile::remove(filename);
4055  finished();
4056  break;
4057  }
4058  // let's be paranoid and inefficient here...
4059  HTTPRequest savedRequest = m_request;
4060 
4061  m_request.url = url;
4062  if (cacheFileOpenRead()) {
4063  m_request.cacheTag.expireDate = expireDate;
4064  cacheFileClose(); // this sends an update command to the cache cleaner process
4065  }
4066 
4067  m_request = savedRequest;
4068  finished();
4069  break;
4070  }
4071  case 5: // WebDAV lock
4072  {
4073  KUrl url;
4074  QString scope, type, owner;
4075  stream >> url >> scope >> type >> owner;
4076  davLock( url, scope, type, owner );
4077  break;
4078  }
4079  case 6: // WebDAV unlock
4080  {
4081  KUrl url;
4082  stream >> url;
4083  davUnlock( url );
4084  break;
4085  }
4086  case 7: // Generic WebDAV
4087  {
4088  KUrl url;
4089  int method;
4090  qint64 size;
4091  stream >> url >> method >> size;
4092  davGeneric( url, (KIO::HTTP_METHOD) method, size );
4093  break;
4094  }
4095  case 99: // Close Connection
4096  {
4097  httpCloseConnection();
4098  break;
4099  }
4100  default:
4101  // Some command we don't understand.
4102  // Just ignore it, it may come from some future version of KDE.
4103  break;
4104  }
4105 }
4106 
4110 int HTTPProtocol::readChunked()
4111 {
4112  if ((m_iBytesLeft == 0) || (m_iBytesLeft == NO_SIZE))
4113  {
4114  // discard CRLF from previous chunk, if any, and read size of next chunk
4115 
4116  int bufPos = 0;
4117  m_receiveBuf.resize(4096);
4118 
4119  bool foundCrLf = readDelimitedText(m_receiveBuf.data(), &bufPos, m_receiveBuf.size(), 1);
4120 
4121  if (foundCrLf && bufPos == 2) {
4122  // The previous read gave us the CRLF from the previous chunk. As bufPos includes
4123  // the trailing CRLF it has to be > 2 to possibly include the next chunksize.
4124  bufPos = 0;
4125  foundCrLf = readDelimitedText(m_receiveBuf.data(), &bufPos, m_receiveBuf.size(), 1);
4126  }
4127  if (!foundCrLf) {
4128  kDebug(7113) << "Failed to read chunk header.";
4129  return -1;
4130  }
4131  Q_ASSERT(bufPos > 2);
4132 
4133  long long nextChunkSize = STRTOLL(m_receiveBuf.data(), 0, 16);
4134  if (nextChunkSize < 0)
4135  {
4136  kDebug(7113) << "Negative chunk size";
4137  return -1;
4138  }
4139  m_iBytesLeft = nextChunkSize;
4140 
4141  kDebug(7113) << "Chunk size =" << m_iBytesLeft << "bytes";
4142 
4143  if (m_iBytesLeft == 0)
4144  {
4145  // Last chunk; read and discard chunk trailer.
4146  // The last trailer line ends with CRLF and is followed by another CRLF
4147  // so we have CRLFCRLF like at the end of a standard HTTP header.
4148  // Do not miss a CRLFCRLF spread over two of our 4K blocks: keep three previous bytes.
4149  //NOTE the CRLF after the chunksize also counts if there is no trailer. Copy it over.
4150  char trash[4096];
4151  trash[0] = m_receiveBuf.constData()[bufPos - 2];
4152  trash[1] = m_receiveBuf.constData()[bufPos - 1];
4153  int trashBufPos = 2;
4154  bool done = false;
4155  while (!done && !m_isEOF) {
4156  if (trashBufPos > 3) {
4157  // shift everything but the last three bytes out of the buffer
4158  for (int i = 0; i < 3; i++) {
4159  trash[i] = trash[trashBufPos - 3 + i];
4160  }
4161  trashBufPos = 3;
4162  }
4163  done = readDelimitedText(trash, &trashBufPos, 4096, 2);
4164  }
4165  if (m_isEOF && !done) {
4166  kDebug(7113) << "Failed to read chunk trailer.";
4167  return -1;
4168  }
4169 
4170  return 0;
4171  }
4172  }
4173 
4174  int bytesReceived = readLimited();
4175  if (!m_iBytesLeft) {
4176  m_iBytesLeft = NO_SIZE; // Don't stop, continue with next chunk
4177  }
4178  return bytesReceived;
4179 }
4180 
4181 int HTTPProtocol::readLimited()
4182 {
4183  if (!m_iBytesLeft)
4184  return 0;
4185 
4186  m_receiveBuf.resize(4096);
4187 
4188  int bytesToReceive;
4189  if (m_iBytesLeft > KIO::filesize_t(m_receiveBuf.size()))
4190  bytesToReceive = m_receiveBuf.size();
4191  else
4192  bytesToReceive = m_iBytesLeft;
4193 
4194  const int bytesReceived = readBuffered(m_receiveBuf.data(), bytesToReceive, false);
4195 
4196  if (bytesReceived <= 0)
4197  return -1; // Error: connection lost
4198 
4199  m_iBytesLeft -= bytesReceived;
4200  return bytesReceived;
4201 }
4202 
4203 int HTTPProtocol::readUnlimited()
4204 {
4205  if (m_request.isKeepAlive)
4206  {
4207  kDebug(7113) << "Unbounded datastream on a Keep-alive connection!";
4208  m_request.isKeepAlive = false;
4209  }
4210 
4211  m_receiveBuf.resize(4096);
4212 
4213  int result = readBuffered(m_receiveBuf.data(), m_receiveBuf.size());
4214  if (result > 0)
4215  return result;
4216 
4217  m_isEOF = true;
4218  m_iBytesLeft = 0;
4219  return 0;
4220 }
4221 
4222 void HTTPProtocol::slotData(const QByteArray &_d)
4223 {
4224  if (!_d.size())
4225  {
4226  m_isEOD = true;
4227  return;
4228  }
4229 
4230  if (m_iContentLeft != NO_SIZE)
4231  {
4232  if (m_iContentLeft >= KIO::filesize_t(_d.size()))
4233  m_iContentLeft -= _d.size();
4234  else
4235  m_iContentLeft = NO_SIZE;
4236  }
4237 
4238  QByteArray d = _d;
4239  if ( !m_dataInternal )
4240  {
4241  // If a broken server does not send the mime-type,
4242  // we try to id it from the content before dealing
4243  // with the content itself.
4244  if ( m_mimeType.isEmpty() && !m_isRedirection &&
4245  !( m_request.responseCode >= 300 && m_request.responseCode <=399) )
4246  {
4247  kDebug(7113) << "Determining mime-type from content...";
4248  int old_size = m_mimeTypeBuffer.size();
4249  m_mimeTypeBuffer.resize( old_size + d.size() );
4250  memcpy( m_mimeTypeBuffer.data() + old_size, d.data(), d.size() );
4251  if ( (m_iBytesLeft != NO_SIZE) && (m_iBytesLeft > 0)
4252  && (m_mimeTypeBuffer.size() < 1024) )
4253  {
4254  m_cpMimeBuffer = true;
4255  return; // Do not send up the data since we do not yet know its mimetype!
4256  }
4257 
4258  kDebug(7113) << "Mimetype buffer size:" << m_mimeTypeBuffer.size();
4259 
4260  KMimeType::Ptr mime = KMimeType::findByNameAndContent(m_request.url.fileName(), m_mimeTypeBuffer);
4261  if( mime && !mime->isDefault() )
4262  {
4263  m_mimeType = mime->name();
4264  kDebug(7113) << "Mimetype from content:" << m_mimeType;
4265  }
4266 
4267  if ( m_mimeType.isEmpty() )
4268  {
4269  m_mimeType = QLatin1String( DEFAULT_MIME_TYPE );
4270  kDebug(7113) << "Using default mimetype:" << m_mimeType;
4271  }
4272 
4273  //### we could also open the cache file here
4274 
4275  if ( m_cpMimeBuffer )
4276  {
4277  d.resize(0);
4278  d.resize(m_mimeTypeBuffer.size());
4279  memcpy(d.data(), m_mimeTypeBuffer.data(), d.size());
4280  }
4281  mimeType(m_mimeType);
4282  m_mimeTypeBuffer.resize(0);
4283  }
4284 
4285  //kDebug(7113) << "Sending data of size" << d.size();
4286  data( d );
4287  if (m_request.cacheTag.ioMode == WriteToCache) {
4288  cacheFileWritePayload(d);
4289  }
4290  }
4291  else
4292  {
4293  uint old_size = m_webDavDataBuf.size();
4294  m_webDavDataBuf.resize (old_size + d.size());
4295  memcpy (m_webDavDataBuf.data() + old_size, d.data(), d.size());
4296  }
4297 }
4298 
4308 bool HTTPProtocol::readBody( bool dataInternal /* = false */ )
4309 {
4310  // special case for reading cached body since we also do it in this function. oh well.
4311  if (!canHaveResponseBody(m_request.responseCode, m_request.method) &&
4312  !(m_request.cacheTag.ioMode == ReadFromCache && m_request.responseCode == 304 &&
4313  m_request.method != HTTP_HEAD)) {
4314  return true;
4315  }
4316 
4317  m_isEOD = false;
4318  // Note that when dataInternal is true, we are going to:
4319  // 1) save the body data to a member variable, m_webDavDataBuf
4320  // 2) _not_ advertise the data, speed, size, etc., through the
4321  // corresponding functions.
4322  // This is used for returning data to WebDAV.
4323  m_dataInternal = dataInternal;
4324  if (dataInternal) {
4325  m_webDavDataBuf.clear();
4326  }
4327 
4328  // Check if we need to decode the data.
4329  // If we are in copy mode, then use only transfer decoding.
4330  bool useMD5 = !m_contentMD5.isEmpty();
4331 
4332  // Deal with the size of the file.
4333  KIO::filesize_t sz = m_request.offset;
4334  if ( sz )
4335  m_iSize += sz;
4336 
4337  if (!m_isRedirection) {
4338  // Update the application with total size except when
4339  // it is compressed, or when the data is to be handled
4340  // internally (webDAV). If compressed we have to wait
4341  // until we uncompress to find out the actual data size
4342  if ( !dataInternal ) {
4343  if ((m_iSize > 0) && (m_iSize != NO_SIZE)) {
4344  totalSize(m_iSize);
4345  infoMessage(i18n("Retrieving %1 from %2...", KIO::convertSize(m_iSize),
4346  m_request.url.host()));
4347  } else {
4348  totalSize(0);
4349  }
4350  }
4351 
4352  if (m_request.cacheTag.ioMode == ReadFromCache) {
4353  kDebug(7113) << "reading data from cache...";
4354 
4355  m_iContentLeft = NO_SIZE;
4356 
4357  QByteArray d;
4358  while (true) {
4359  d = cacheFileReadPayload(MAX_IPC_SIZE);
4360  if (d.isEmpty()) {
4361  break;
4362  }
4363  slotData(d);
4364  sz += d.size();
4365  if (!dataInternal) {
4366  processedSize(sz);
4367  }
4368  }
4369 
4370  m_receiveBuf.resize(0);
4371 
4372  if (!dataInternal) {
4373  data(QByteArray());
4374  }
4375 
4376  return true;
4377  }
4378  }
4379 
4380  if (m_iSize != NO_SIZE)
4381  m_iBytesLeft = m_iSize - sz;
4382  else
4383  m_iBytesLeft = NO_SIZE;
4384 
4385  m_iContentLeft = m_iBytesLeft;
4386 
4387  if (m_isChunked)
4388  m_iBytesLeft = NO_SIZE;
4389 
4390  kDebug(7113) << KIO::number(m_iBytesLeft) << "bytes left.";
4391 
4392  // Main incoming loop... Gather everything while we can...
4393  m_cpMimeBuffer = false;
4394  m_mimeTypeBuffer.resize(0);
4395 
4396  HTTPFilterChain chain;
4397 
4398  // redirection ignores the body
4399  if (!m_isRedirection) {
4400  QObject::connect(&chain, SIGNAL(output(QByteArray)),
4401  this, SLOT(slotData(QByteArray)));
4402  }
4403  QObject::connect(&chain, SIGNAL(error(QString)),
4404  this, SLOT(slotFilterError(QString)));
4405 
4406  // decode all of the transfer encodings
4407  while (!m_transferEncodings.isEmpty())
4408  {
4409  QString enc = m_transferEncodings.takeLast();
4410  if ( enc == QLatin1String("gzip") )
4411  chain.addFilter(new HTTPFilterGZip);
4412  else if ( enc == QLatin1String("deflate") )
4413  chain.addFilter(new HTTPFilterDeflate);
4414  }
4415 
4416  // From HTTP 1.1 Draft 6:
4417  // The MD5 digest is computed based on the content of the entity-body,
4418  // including any content-coding that has been applied, but not including
4419  // any transfer-encoding applied to the message-body. If the message is
4420  // received with a transfer-encoding, that encoding MUST be removed
4421  // prior to checking the Content-MD5 value against the received entity.
4422  HTTPFilterMD5 *md5Filter = 0;
4423  if ( useMD5 )
4424  {
4425  md5Filter = new HTTPFilterMD5;
4426  chain.addFilter(md5Filter);
4427  }
4428 
4429  // now decode all of the content encodings
4430  // -- Why ?? We are not
4431  // -- a proxy server, be a client side implementation!! The applications
4432  // -- are capable of determinig how to extract the encoded implementation.
4433  // WB: That's a misunderstanding. We are free to remove the encoding.
4434  // WB: Some braindead www-servers however, give .tgz files an encoding
4435  // WB: of "gzip" (or even "x-gzip") and a content-type of "applications/tar"
4436  // WB: They shouldn't do that. We can work around that though...
4437  while (!m_contentEncodings.isEmpty())
4438  {
4439  QString enc = m_contentEncodings.takeLast();
4440  if ( enc == QLatin1String("gzip") )
4441  chain.addFilter(new HTTPFilterGZip);
4442  else if ( enc == QLatin1String("deflate") )
4443  chain.addFilter(new HTTPFilterDeflate);
4444  }
4445 
4446  while (!m_isEOF)
4447  {
4448  int bytesReceived;
4449 
4450  if (m_isChunked)
4451  bytesReceived = readChunked();
4452  else if (m_iSize != NO_SIZE)
4453  bytesReceived = readLimited();
4454  else
4455  bytesReceived = readUnlimited();
4456 
4457  // make sure that this wasn't an error, first
4458  // kDebug(7113) << "bytesReceived:"
4459  // << (int) bytesReceived << " m_iSize:" << (int) m_iSize << " Chunked:"
4460  // << m_isChunked << " BytesLeft:"<< (int) m_iBytesLeft;
4461  if (bytesReceived == -1)
4462  {
4463  if (m_iContentLeft == 0)
4464  {
4465  // gzip'ed data sometimes reports a too long content-length.
4466  // (The length of the unzipped data)
4467  m_iBytesLeft = 0;
4468  break;
4469  }
4470  // Oh well... log an error and bug out
4471  kDebug(7113) << "bytesReceived==-1 sz=" << (int)sz
4472  << " Connection broken !";
4473  error(ERR_CONNECTION_BROKEN, m_request.url.host());
4474  return false;
4475  }
4476 
4477  // I guess that nbytes == 0 isn't an error.. but we certainly
4478  // won't work with it!
4479  if (bytesReceived > 0)
4480  {
4481  // Important: truncate the buffer to the actual size received!
4482  // Otherwise garbage will be passed to the app
4483  m_receiveBuf.truncate( bytesReceived );
4484 
4485  chain.slotInput(m_receiveBuf);
4486 
4487  if (m_iError)
4488  return false;
4489 
4490  sz += bytesReceived;
4491  if (!dataInternal)
4492  processedSize( sz );
4493  }
4494  m_receiveBuf.resize(0); // res
4495 
4496  if (m_iBytesLeft && m_isEOD && !m_isChunked)
4497  {
4498  // gzip'ed data sometimes reports a too long content-length.
4499  // (The length of the unzipped data)
4500  m_iBytesLeft = 0;
4501  }
4502 
4503  if (m_iBytesLeft == 0)
4504  {
4505  kDebug(7113) << "EOD received! Left ="<< KIO::number(m_iBytesLeft);
4506  break;
4507  }
4508  }
4509  chain.slotInput(QByteArray()); // Flush chain.
4510 
4511  if ( useMD5 )
4512  {
4513  QString calculatedMD5 = md5Filter->md5();
4514 
4515  if ( m_contentMD5 != calculatedMD5 )
4516  kWarning(7113) << "MD5 checksum MISMATCH! Expected:"
4517  << calculatedMD5 << ", Got:" << m_contentMD5;
4518  }
4519 
4520  // Close cache entry
4521  if (m_iBytesLeft == 0) {
4522  cacheFileClose(); // no-op if not necessary
4523  }
4524 
4525  if (!dataInternal && sz <= 1)
4526  {
4527  if (m_request.responseCode >= 500 && m_request.responseCode <= 599) {
4528  error(ERR_INTERNAL_SERVER, m_request.url.host());
4529  return false;
4530  } else if (m_request.responseCode >= 400 && m_request.responseCode <= 499 &&
4531  !isAuthenticationRequired(m_request.responseCode)) {
4532  error(ERR_DOES_NOT_EXIST, m_request.url.host());
4533  return false;
4534  }
4535  }
4536 
4537  if (!dataInternal && !m_isRedirection)
4538  data( QByteArray() );
4539 
4540  return true;
4541 }
4542 
4543 void HTTPProtocol::slotFilterError(const QString &text)
4544 {
4545  error(KIO::ERR_SLAVE_DEFINED, text);
4546 }
4547 
4548 void HTTPProtocol::error( int _err, const QString &_text )
4549 {
4550  // Close the connection only on connection errors. Otherwise, honor the
4551  // keep alive flag.
4552  if (_err == ERR_CONNECTION_BROKEN || _err == ERR_COULD_NOT_CONNECT)
4553  httpClose(false);
4554  else
4555  httpClose(m_request.isKeepAlive);
4556 
4557  if (!m_request.id.isEmpty())
4558  {
4559  forwardHttpResponseHeader();
4560  sendMetaData();
4561  }
4562 
4563  // It's over, we don't need it anymore
4564  clearPostDataBuffer();
4565 
4566  SlaveBase::error( _err, _text );
4567  m_iError = _err;
4568 }
4569 
4570 
4571 void HTTPProtocol::addCookies( const QString &url, const QByteArray &cookieHeader )
4572 {
4573  qlonglong windowId = m_request.windowId.toLongLong();
4574  QDBusInterface kcookiejar( QLatin1String("org.kde.kded"), QLatin1String("/modules/kcookiejar"), QLatin1String("org.kde.KCookieServer") );
4575  (void)kcookiejar.call( QDBus::NoBlock, QLatin1String("addCookies"), url,
4576  cookieHeader, windowId );
4577 }
4578 
4579 QString HTTPProtocol::findCookies( const QString &url)
4580 {
4581  qlonglong windowId = m_request.windowId.toLongLong();
4582  QDBusInterface kcookiejar( QLatin1String("org.kde.kded"), QLatin1String("/modules/kcookiejar"), QLatin1String("org.kde.KCookieServer") );
4583  QDBusReply<QString> reply = kcookiejar.call( QLatin1String("findCookies"), url, windowId );
4584 
4585  if ( !reply.isValid() )
4586  {
4587  kWarning(7113) << "Can't communicate with kded_kcookiejar!";
4588  return QString();
4589  }
4590  return reply;
4591 }
4592 
4593 /******************************* CACHING CODE ****************************/
4594 
4595 HTTPProtocol::CacheTag::CachePlan HTTPProtocol::CacheTag::plan(time_t maxCacheAge) const
4596 {
4597  //notable omission: we're not checking cache file presence or integrity
4598  switch (policy) {
4599  case KIO::CC_Refresh:
4600  // Conditional GET requires the presence of either an ETag or
4601  // last modified date.
4602  if (lastModifiedDate != -1 || !etag.isEmpty()) {
4603  return ValidateCached;
4604  }
4605  break;
4606  case KIO::CC_Reload:
4607  return IgnoreCached;
4608  case KIO::CC_CacheOnly:
4609  case KIO::CC_Cache:
4610  return UseCached;
4611  default:
4612  break;
4613  }
4614 
4615  Q_ASSERT((policy == CC_Verify || policy == CC_Refresh));
4616  time_t currentDate = time(0);
4617  if ((servedDate != -1 && currentDate > (servedDate + maxCacheAge)) ||
4618  (expireDate != -1 && currentDate > expireDate)) {
4619  return ValidateCached;
4620  }
4621  return UseCached;
4622 }
4623 
4624 // !START SYNC!
4625 // The following code should be kept in sync
4626 // with the code in http_cache_cleaner.cpp
4627 
4628 // we use QDataStream; this is just an illustration
4629 struct BinaryCacheFileHeader
4630 {
4631  quint8 version[2];
4632  quint8 compression; // for now fixed to 0
4633  quint8 reserved; // for now; also alignment
4634  qint32 useCount;
4635  qint64 servedDate;
4636  qint64 lastModifiedDate;
4637  qint64 expireDate;
4638  qint32 bytesCached;
4639  // packed size should be 36 bytes; we explicitly set it here to make sure that no compiler
4640  // padding ruins it. We write the fields to disk without any padding.
4641  static const int size = 36;
4642 };
4643 
4644 enum CacheCleanerCommandCode {
4645  InvalidCommand = 0,
4646  CreateFileNotificationCommand,
4647  UpdateFileCommand
4648 };
4649 
4650 // illustration for cache cleaner update "commands"
4651 struct CacheCleanerCommand
4652 {
4653  BinaryCacheFileHeader header;
4654  quint32 commandCode;
4655  // filename in ASCII, binary isn't worth the coding and decoding
4656  quint8 filename[s_hashedUrlNibbles];
4657 };
4658 
4659 QByteArray HTTPProtocol::CacheTag::serialize() const
4660 {
4661  QByteArray ret;
4662  QDataStream stream(&ret, QIODevice::WriteOnly);
4663  stream << quint8('A');
4664  stream << quint8('\n');
4665  stream << quint8(0);
4666  stream << quint8(0);
4667 
4668  stream << fileUseCount;
4669 
4670  // time_t overflow will only be checked when reading; we have no way to tell here.
4671  stream << qint64(servedDate);
4672  stream << qint64(lastModifiedDate);
4673  stream << qint64(expireDate);
4674 
4675  stream << bytesCached;
4676  Q_ASSERT(ret.size() == BinaryCacheFileHeader::size);
4677  return ret;
4678 }
4679 
4680 
4681 static bool compareByte(QDataStream *stream, quint8 value)
4682 {
4683  quint8 byte;
4684  *stream >> byte;
4685  return byte == value;
4686 }
4687 
4688 static bool readTime(QDataStream *stream, time_t *time)
4689 {
4690  qint64 intTime = 0;
4691  *stream >> intTime;
4692  *time = static_cast<time_t>(intTime);
4693 
4694  qint64 check = static_cast<qint64>(*time);
4695  return check == intTime;
4696 }
4697 
4698 // If starting a new file cacheFileWriteVariableSizeHeader() must have been called *before*
4699 // calling this! This is to fill in the headerEnd field.
4700 // If the file is not new headerEnd has already been read from the file and in fact the variable
4701 // size header *may* not be rewritten because a size change would mess up the file layout.
4702 bool HTTPProtocol::CacheTag::deserialize(const QByteArray &d)
4703 {
4704  if (d.size() != BinaryCacheFileHeader::size) {
4705  return false;
4706  }
4707  QDataStream stream(d);
4708  stream.setVersion(QDataStream::Qt_4_5);
4709 
4710  bool ok = true;
4711  ok = ok && compareByte(&stream, 'A');
4712  ok = ok && compareByte(&stream, '\n');
4713  ok = ok && compareByte(&stream, 0);
4714  ok = ok && compareByte(&stream, 0);
4715  if (!ok) {
4716  return false;
4717  }
4718 
4719  stream >> fileUseCount;
4720 
4721  // read and check for time_t overflow
4722  ok = ok && readTime(&stream, &servedDate);
4723  ok = ok && readTime(&stream, &lastModifiedDate);
4724  ok = ok && readTime(&stream, &expireDate);
4725  if (!ok) {
4726  return false;
4727  }
4728 
4729  stream >> bytesCached;
4730 
4731  return true;
4732 }
4733 
4734 /* Text part of the header, directly following the binary first part:
4735 URL\n
4736 etag\n
4737 mimetype\n
4738 header line\n
4739 header line\n
4740 ...
4741 \n
4742 */
4743 
4744 static KUrl storableUrl(const KUrl &url)
4745 {
4746  KUrl ret(url);
4747  ret.setPassword(QString());
4748  ret.setFragment(QString());
4749  return ret;
4750 }
4751 
4752 static void writeLine(QIODevice *dev, const QByteArray &line)
4753 {
4754  static const char linefeed = '\n';
4755  dev->write(line);
4756  dev->write(&linefeed, 1);
4757 }
4758 
4759 void HTTPProtocol::cacheFileWriteTextHeader()
4760 {
4761  QFile *&file = m_request.cacheTag.file;
4762  Q_ASSERT(file);
4763  Q_ASSERT(file->openMode() & QIODevice::WriteOnly);
4764 
4765  file->seek(BinaryCacheFileHeader::size);
4766  writeLine(file, storableUrl(m_request.url).toEncoded());
4767  writeLine(file, m_request.cacheTag.etag.toLatin1());
4768  writeLine(file, m_mimeType.toLatin1());
4769  writeLine(file, m_responseHeaders.join(QString(QLatin1Char('\n'))).toLatin1());
4770  // join("\n") adds no \n to the end, but writeLine() does.
4771  // Add another newline to mark the end of text.
4772  writeLine(file, QByteArray());
4773 }
4774 
4775 static bool readLineChecked(QIODevice *dev, QByteArray *line)
4776 {
4777  *line = dev->readLine(MAX_IPC_SIZE);
4778  // if nothing read or the line didn't fit into 8192 bytes(!)
4779  if (line->isEmpty() || !line->endsWith('\n')) {
4780  return false;
4781  }
4782  // we don't actually want the newline!
4783  line->chop(1);
4784  return true;
4785 }
4786 
4787 bool HTTPProtocol::cacheFileReadTextHeader1(const KUrl &desiredUrl)
4788 {
4789  QFile *&file = m_request.cacheTag.file;
4790  Q_ASSERT(file);
4791  Q_ASSERT(file->openMode() == QIODevice::ReadOnly);
4792 
4793  QByteArray readBuf;
4794  bool ok = readLineChecked(file, &readBuf);
4795  if (storableUrl(desiredUrl).toEncoded() != readBuf) {
4796  kDebug(7103) << "You have witnessed a very improbable hash collision!";
4797  return false;
4798  }
4799 
4800  ok = ok && readLineChecked(file, &readBuf);
4801  m_request.cacheTag.etag = toQString(readBuf);
4802 
4803  return ok;
4804 }
4805 
4806 bool HTTPProtocol::cacheFileReadTextHeader2()
4807 {
4808  QFile *&file = m_request.cacheTag.file;
4809  Q_ASSERT(file);
4810  Q_ASSERT(file->openMode() == QIODevice::ReadOnly);
4811 
4812  bool ok = true;
4813  QByteArray readBuf;
4814 #ifndef NDEBUG
4815  // we assume that the URL and etag have already been read
4816  qint64 oldPos = file->pos();
4817  file->seek(BinaryCacheFileHeader::size);
4818  ok = ok && readLineChecked(file, &readBuf);
4819  ok = ok && readLineChecked(file, &readBuf);
4820  Q_ASSERT(file->pos() == oldPos);
4821 #endif
4822  ok = ok && readLineChecked(file, &readBuf);
4823  m_mimeType = toQString(readBuf);
4824 
4825  m_responseHeaders.clear();
4826  // read as long as no error and no empty line found
4827  while (true) {
4828  ok = ok && readLineChecked(file, &readBuf);
4829  if (ok && !readBuf.isEmpty()) {
4830  m_responseHeaders.append(toQString(readBuf));
4831  } else {
4832  break;
4833  }
4834  }
4835  return ok; // it may still be false ;)
4836 }
4837 
4838 static QString filenameFromUrl(const KUrl &url)
4839 {
4840  QCryptographicHash hash(QCryptographicHash::Sha1);
4841  hash.addData(storableUrl(url).toEncoded());
4842  return toQString(hash.result().toHex());
4843 }
4844 
4845 QString HTTPProtocol::cacheFilePathFromUrl(const KUrl &url) const
4846 {
4847  QString filePath = m_strCacheDir;
4848  if (!filePath.endsWith(QLatin1Char('/'))) {
4849  filePath.append(QLatin1Char('/'));
4850  }
4851  filePath.append(filenameFromUrl(url));
4852  return filePath;
4853 }
4854 
4855 bool HTTPProtocol::cacheFileOpenRead()
4856 {
4857  kDebug(7113);
4858  QString filename = cacheFilePathFromUrl(m_request.url);
4859 
4860  QFile *&file = m_request.cacheTag.file;
4861  if (file) {
4862  kDebug(7113) << "File unexpectedly open; old file is" << file->fileName()
4863  << "new name is" << filename;
4864  Q_ASSERT(file->fileName() == filename);
4865  }
4866  Q_ASSERT(!file);
4867  file = new QFile(filename);
4868  if (file->open(QIODevice::ReadOnly)) {
4869  QByteArray header = file->read(BinaryCacheFileHeader::size);
4870  if (!m_request.cacheTag.deserialize(header)) {
4871  kDebug(7103) << "Cache file header is invalid.";
4872 
4873  file->close();
4874  }
4875  }
4876 
4877  if (file->isOpen() && !cacheFileReadTextHeader1(m_request.url)) {
4878  file->close();
4879  }
4880 
4881  if (!file->isOpen()) {
4882  cacheFileClose();
4883  return false;
4884  }
4885  return true;
4886 }
4887 
4888 
4889 bool HTTPProtocol::cacheFileOpenWrite()
4890 {
4891  kDebug(7113);
4892  QString filename = cacheFilePathFromUrl(m_request.url);
4893 
4894  // if we open a cache file for writing while we have a file open for reading we must have
4895  // found out that the old cached content is obsolete, so delete the file.
4896  QFile *&file = m_request.cacheTag.file;
4897  if (file) {
4898  // ensure that the file is in a known state - either open for reading or null
4899  Q_ASSERT(!qobject_cast<QTemporaryFile *>(file));
4900  Q_ASSERT((file->openMode() & QIODevice::WriteOnly) == 0);
4901  Q_ASSERT(file->fileName() == filename);
4902  kDebug(7113) << "deleting expired cache entry and recreating.";
4903  file->remove();
4904  delete file;
4905  file = 0;
4906  }
4907 
4908  // note that QTemporaryFile will automatically append random chars to filename
4909  file = new QTemporaryFile(filename);
4910  file->open(QIODevice::WriteOnly);
4911 
4912  // if we have started a new file we have not initialized some variables from disk data.
4913  m_request.cacheTag.fileUseCount = 0; // the file has not been *read* yet
4914  m_request.cacheTag.bytesCached = 0;
4915 
4916  if ((file->openMode() & QIODevice::WriteOnly) == 0) {
4917  kDebug(7113) << "Could not open file for writing:" << file->fileName()
4918  << "due to error" << file->error();
4919  cacheFileClose();
4920  return false;
4921  }
4922  return true;
4923 }
4924 
4925 static QByteArray makeCacheCleanerCommand(const HTTPProtocol::CacheTag &cacheTag,
4926  CacheCleanerCommandCode cmd)
4927 {
4928  QByteArray ret = cacheTag.serialize();
4929  QDataStream stream(&ret, QIODevice::WriteOnly);
4930  stream.setVersion(QDataStream::Qt_4_5);
4931 
4932  stream.skipRawData(BinaryCacheFileHeader::size);
4933  // append the command code
4934  stream << quint32(cmd);
4935  // append the filename
4936  QString fileName = cacheTag.file->fileName();
4937  int basenameStart = fileName.lastIndexOf(QLatin1Char('/')) + 1;
4938  QByteArray baseName = fileName.mid(basenameStart, s_hashedUrlNibbles).toLatin1();
4939  stream.writeRawData(baseName.constData(), baseName.size());
4940 
4941  Q_ASSERT(ret.size() == BinaryCacheFileHeader::size + sizeof(quint32) + s_hashedUrlNibbles);
4942  return ret;
4943 }
4944 
4945 //### not yet 100% sure when and when not to call this
4946 void HTTPProtocol::cacheFileClose()
4947 {
4948  kDebug(7113);
4949 
4950  QFile *&file = m_request.cacheTag.file;
4951  if (!file) {
4952  return;
4953  }
4954 
4955  m_request.cacheTag.ioMode = NoCache;
4956 
4957  QByteArray ccCommand;
4958  QTemporaryFile *tempFile = qobject_cast<QTemporaryFile *>(file);
4959 
4960  if (file->openMode() & QIODevice::WriteOnly) {
4961  Q_ASSERT(tempFile);
4962 
4963  if (m_request.cacheTag.bytesCached && !m_iError) {
4964  QByteArray header = m_request.cacheTag.serialize();
4965  tempFile->seek(0);
4966  tempFile->write(header);
4967 
4968  ccCommand = makeCacheCleanerCommand(m_request.cacheTag, CreateFileNotificationCommand);
4969 
4970  QString oldName = tempFile->fileName();
4971  QString newName = oldName;
4972  int basenameStart = newName.lastIndexOf(QLatin1Char('/')) + 1;
4973  // remove the randomized name part added by QTemporaryFile
4974  newName.chop(newName.length() - basenameStart - s_hashedUrlNibbles);
4975  kDebug(7113) << "Renaming temporary file" << oldName << "to" << newName;
4976 
4977  // on windows open files can't be renamed
4978  tempFile->setAutoRemove(false);
4979  delete tempFile;
4980  file = 0;
4981 
4982  if (!QFile::rename(oldName, newName)) {
4983  // ### currently this hides a minor bug when force-reloading a resource. We
4984  // should not even open a new file for writing in that case.
4985  kDebug(7113) << "Renaming temporary file failed, deleting it instead.";
4986  QFile::remove(oldName);
4987  ccCommand.clear(); // we have nothing of value to tell the cache cleaner
4988  }
4989  } else {
4990  // oh, we've never written payload data to the cache file.
4991  // the temporary file is closed and removed and no proper cache entry is created.
4992  }
4993  } else if (file->openMode() == QIODevice::ReadOnly) {
4994  Q_ASSERT(!tempFile);
4995  ccCommand = makeCacheCleanerCommand(m_request.cacheTag, UpdateFileCommand);
4996  }
4997  delete file;
4998  file = 0;
4999 
5000  if (!ccCommand.isEmpty()) {
5001  sendCacheCleanerCommand(ccCommand);
5002  }
5003 }
5004 
5005 void HTTPProtocol::sendCacheCleanerCommand(const QByteArray &command)
5006 {
5007  kDebug(7113);
5008  Q_ASSERT(command.size() == BinaryCacheFileHeader::size + s_hashedUrlNibbles + sizeof(quint32));
5009  int attempts = 0;
5010  while (m_cacheCleanerConnection.state() != QLocalSocket::ConnectedState && attempts < 6) {
5011  if (attempts == 2) {
5012  KToolInvocation::startServiceByDesktopPath(QLatin1String("http_cache_cleaner.desktop"));
5013  }
5014  QString socketFileName = KStandardDirs::locateLocal("socket", QLatin1String("kio_http_cache_cleaner"));
5015  m_cacheCleanerConnection.connectToServer(socketFileName, QIODevice::WriteOnly);
5016  m_cacheCleanerConnection.waitForConnected(1500);
5017  attempts++;
5018  }
5019 
5020  if (m_cacheCleanerConnection.state() == QLocalSocket::ConnectedState) {
5021  m_cacheCleanerConnection.write(command);
5022  m_cacheCleanerConnection.flush();
5023  } else {
5024  // updating the stats is not vital, so we just give up.
5025  kDebug(7113) << "Could not connect to cache cleaner, not updating stats of this cache file.";
5026  }
5027 }
5028 
5029 QByteArray HTTPProtocol::cacheFileReadPayload(int maxLength)
5030 {
5031  Q_ASSERT(m_request.cacheTag.file);
5032  Q_ASSERT(m_request.cacheTag.ioMode == ReadFromCache);
5033  Q_ASSERT(m_request.cacheTag.file->openMode() == QIODevice::ReadOnly);
5034  QByteArray ret = m_request.cacheTag.file->read(maxLength);
5035  if (ret.isEmpty()) {
5036  cacheFileClose();
5037  }
5038  return ret;
5039 }
5040 
5041 
5042 void HTTPProtocol::cacheFileWritePayload(const QByteArray &d)
5043 {
5044  if (!m_request.cacheTag.file) {
5045  return;
5046  }
5047 
5048  // If the file being downloaded is so big that it exceeds the max cache size,
5049  // do not cache it! See BR# 244215. NOTE: this can be improved upon in the
5050  // future...
5051  if (m_iSize >= KIO::filesize_t(m_maxCacheSize * 1024)) {
5052  kDebug(7113) << "Caching disabled because content size is too big.";
5053  cacheFileClose();
5054  return;
5055  }
5056 
5057  Q_ASSERT(m_request.cacheTag.ioMode == WriteToCache);
5058  Q_ASSERT(m_request.cacheTag.file->openMode() & QIODevice::WriteOnly);
5059 
5060  if (d.isEmpty()) {
5061  cacheFileClose();
5062  }
5063 
5064  //TODO: abort if file grows too big!
5065 
5066  // write the variable length text header as soon as we start writing to the file
5067  if (!m_request.cacheTag.bytesCached) {
5068  cacheFileWriteTextHeader();
5069  }
5070  m_request.cacheTag.bytesCached += d.size();
5071  m_request.cacheTag.file->write(d);
5072 }
5073 
5074 void HTTPProtocol::cachePostData(const QByteArray& data)
5075 {
5076  if (!m_POSTbuf) {
5077  m_POSTbuf = createPostBufferDeviceFor(qMax(m_iPostDataSize, static_cast<KIO::filesize_t>(data.size())));
5078  if (!m_POSTbuf)
5079  return;
5080  }
5081 
5082  m_POSTbuf->write (data.constData(), data.size());
5083 }
5084 
5085 void HTTPProtocol::clearPostDataBuffer()
5086 {
5087  if (!m_POSTbuf)
5088  return;
5089 
5090  delete m_POSTbuf;
5091  m_POSTbuf = 0;
5092 }
5093 
5094 bool HTTPProtocol::retrieveAllData()
5095 {
5096  if (!m_POSTbuf) {
5097  m_POSTbuf = createPostBufferDeviceFor(s_MaxInMemPostBufSize + 1);
5098  }
5099 
5100  if (!m_POSTbuf) {
5101  error (ERR_OUT_OF_MEMORY, m_request.url.host());
5102  return false;
5103  }
5104 
5105  while (true) {
5106  dataReq();
5107  QByteArray buffer;
5108  const int bytesRead = readData(buffer);
5109 
5110  if (bytesRead < 0) {
5111  error(ERR_ABORTED, m_request.url.host());
5112  return false;
5113  }
5114 
5115  if (bytesRead == 0) {
5116  break;
5117  }
5118 
5119  m_POSTbuf->write(buffer.constData(), buffer.size());
5120  }
5121 
5122  return true;
5123 }
5124 
5125 // The above code should be kept in sync
5126 // with the code in http_cache_cleaner.cpp
5127 // !END SYNC!
5128 
5129 //************************** AUTHENTICATION CODE ********************/
5130 
5131 QString HTTPProtocol::authenticationHeader()
5132 {
5133  QByteArray ret;
5134 
5135  // If the internal meta-data "cached-www-auth" is set, then check for cached
5136  // authentication data and preemtively send the authentication header if a
5137  // matching one is found.
5138  if (!m_wwwAuth && config()->readEntry("cached-www-auth", false)) {
5139  KIO::AuthInfo authinfo;
5140  authinfo.url = m_request.url;
5141  authinfo.realmValue = config()->readEntry("www-auth-realm", QString());
5142  // If no relam metadata, then make sure path matching is turned on.
5143  authinfo.verifyPath = (authinfo.realmValue.isEmpty());
5144 
5145  const bool useCachedAuth = (m_request.responseCode == 401 || !config()->readEntry("no-preemptive-auth-reuse", false));
5146 
5147  if (useCachedAuth && checkCachedAuthentication(authinfo)) {
5148  const QByteArray cachedChallenge = config()->readEntry("www-auth-challenge", QByteArray());
5149  if (!cachedChallenge.isEmpty()) {
5150  m_wwwAuth = KAbstractHttpAuthentication::newAuth(cachedChallenge, config());
5151  if (m_wwwAuth) {
5152  kDebug(7113) << "creating www authentcation header from cached info";
5153  m_wwwAuth->setChallenge(cachedChallenge, m_request.url, m_request.methodString());
5154  m_wwwAuth->generateResponse(authinfo.username, authinfo.password);
5155  }
5156  }
5157  }
5158  }
5159 
5160  // If the internal meta-data "cached-proxy-auth" is set, then check for cached
5161  // authentication data and preemtively send the authentication header if a
5162  // matching one is found.
5163  if (!m_proxyAuth && config()->readEntry("cached-proxy-auth", false)) {
5164  KIO::AuthInfo authinfo;
5165  authinfo.url = m_request.proxyUrl;
5166  authinfo.realmValue = config()->readEntry("proxy-auth-realm", QString());
5167  // If no relam metadata, then make sure path matching is turned on.
5168  authinfo.verifyPath = (authinfo.realmValue.isEmpty());
5169 
5170  if (checkCachedAuthentication(authinfo)) {
5171  const QByteArray cachedChallenge = config()->readEntry("proxy-auth-challenge", QByteArray());
5172  if (!cachedChallenge.isEmpty()) {
5173  m_proxyAuth = KAbstractHttpAuthentication::newAuth(cachedChallenge, config());
5174  if (m_proxyAuth) {
5175  kDebug(7113) << "creating proxy authentcation header from cached info";
5176  m_proxyAuth->setChallenge(cachedChallenge, m_request.proxyUrl, m_request.methodString());
5177  m_proxyAuth->generateResponse(authinfo.username, authinfo.password);
5178  }
5179  }
5180  }
5181  }
5182 
5183  // the authentication classes don't know if they are for proxy or webserver authentication...
5184  if (m_wwwAuth && !m_wwwAuth->isError()) {
5185  ret += "Authorization: ";
5186  ret += m_wwwAuth->headerFragment();
5187  }
5188 
5189  if (m_proxyAuth && !m_proxyAuth->isError()) {
5190  ret += "Proxy-Authorization: ";
5191  ret += m_proxyAuth->headerFragment();
5192  }
5193 
5194  return toQString(ret); // ## encoding ok?
5195 }
5196 
5197 static QString protocolForProxyType(QNetworkProxy::ProxyType type)
5198 {
5199  switch (type) {
5200  case QNetworkProxy::DefaultProxy:
5201  break;
5202  case QNetworkProxy::Socks5Proxy:
5203  return QLatin1String("socks");
5204  case QNetworkProxy::NoProxy:
5205  break;
5206  case QNetworkProxy::HttpProxy:
5207  case QNetworkProxy::HttpCachingProxy:
5208  case QNetworkProxy::FtpCachingProxy:
5209  default:
5210  break;
5211  }
5212 
5213  return QLatin1String("http");
5214 }
5215 
5216 void HTTPProtocol::proxyAuthenticationForSocket(const QNetworkProxy &proxy, QAuthenticator *authenticator)
5217 {
5218  kDebug(7113) << "realm:" << authenticator->realm() << "user:" << authenticator->user();
5219 
5220  // Set the proxy URL...
5221  m_request.proxyUrl.setProtocol(protocolForProxyType(proxy.type()));
5222  m_request.proxyUrl.setUser(proxy.user());
5223  m_request.proxyUrl.setHost(proxy.hostName());
5224  m_request.proxyUrl.setPort(proxy.port());
5225 
5226  AuthInfo info;
5227  info.url = m_request.proxyUrl;
5228  info.realmValue = authenticator->realm();
5229  info.username = authenticator->user();
5230  info.verifyPath = info.realmValue.isEmpty();
5231 
5232  const bool haveCachedCredentials = checkCachedAuthentication(info);
5233  const bool retryAuth = (m_socketProxyAuth != 0);
5234 
5235  // if m_socketProxyAuth is a valid pointer then authentication has been attempted before,
5236  // and it was not successful. see below and saveProxyAuthenticationForSocket().
5237  if (!haveCachedCredentials || retryAuth) {
5238  // Save authentication info if the connection succeeds. We need to disconnect
5239  // this after saving the auth data (or an error) so we won't save garbage afterwards!
5240  connect(socket(), SIGNAL(connected()),
5241  this, SLOT(saveProxyAuthenticationForSocket()));
5242  //### fillPromptInfo(&info);
5243  info.prompt = i18n("You need to supply a username and a password for "
5244  "the proxy server listed below before you are allowed "
5245  "to access any sites.");
5246  info.keepPassword = true;
5247  info.commentLabel = i18n("Proxy:");
5248  info.comment = i18n("<b>%1</b> at <b>%2</b>", htmlEscape(info.realmValue), m_request.proxyUrl.host());
5249 
5250  const QString errMsg ((retryAuth ? i18n("Proxy Authentication Failed.") : QString()));
5251 
5252  if (!openPasswordDialog(info, errMsg)) {
5253  kDebug(7113) << "looks like the user canceled proxy authentication.";
5254  error(ERR_USER_CANCELED, m_request.proxyUrl.host());
5255  delete m_proxyAuth;
5256  m_proxyAuth = 0;
5257  return;
5258  }
5259  }
5260  authenticator->setUser(info.username);
5261  authenticator->setPassword(info.password);
5262  authenticator->setOption(QLatin1String("keepalive"), info.keepPassword);
5263 
5264  if (m_socketProxyAuth) {
5265  *m_socketProxyAuth = *authenticator;
5266  } else {
5267  m_socketProxyAuth = new QAuthenticator(*authenticator);
5268  }
5269 
5270  if (!m_request.proxyUrl.user().isEmpty()) {
5271  m_request.proxyUrl.setUser(info.username);
5272  }
5273 }
5274 
5275 void HTTPProtocol::saveProxyAuthenticationForSocket()
5276 {
5277  kDebug(7113) << "Saving authenticator";
5278  disconnect(socket(), SIGNAL(connected()),
5279  this, SLOT(saveProxyAuthenticationForSocket()));
5280  Q_ASSERT(m_socketProxyAuth);
5281  if (m_socketProxyAuth) {
5282  kDebug(7113) << "realm:" << m_socketProxyAuth->realm() << "user:" << m_socketProxyAuth->user();
5283  KIO::AuthInfo a;
5284  a.verifyPath = true;
5285  a.url = m_request.proxyUrl;
5286  a.realmValue = m_socketProxyAuth->realm();
5287  a.username = m_socketProxyAuth->user();
5288  a.password = m_socketProxyAuth->password();
5289  a.keepPassword = m_socketProxyAuth->option(QLatin1String("keepalive")).toBool();
5290  cacheAuthentication(a);
5291  }
5292  delete m_socketProxyAuth;
5293  m_socketProxyAuth = 0;
5294 }
5295 
5296 void HTTPProtocol::saveAuthenticationData()
5297 {
5298  KIO::AuthInfo authinfo;
5299  bool alreadyCached = false;
5300  KAbstractHttpAuthentication *auth = 0;
5301  switch (m_request.prevResponseCode) {
5302  case 401:
5303  auth = m_wwwAuth;
5304  alreadyCached = config()->readEntry("cached-www-auth", false);
5305  break;
5306  case 407:
5307  auth = m_proxyAuth;
5308  alreadyCached = config()->readEntry("cached-proxy-auth", false);
5309  break;
5310  default:
5311  Q_ASSERT(false); // should never happen!
5312  }
5313 
5314  // Prevent recaching of the same credentials over and over again.
5315  if (auth && (!auth->realm().isEmpty() || !alreadyCached)) {
5316  auth->fillKioAuthInfo(&authinfo);
5317  if (auth == m_wwwAuth) {
5318  setMetaData(QLatin1String("{internal~currenthost}cached-www-auth"), QLatin1String("true"));
5319  if (!authinfo.realmValue.isEmpty())
5320  setMetaData(QLatin1String("{internal~currenthost}www-auth-realm"), authinfo.realmValue);
5321  if (!authinfo.digestInfo.isEmpty())
5322  setMetaData(QLatin1String("{internal~currenthost}www-auth-challenge"), authinfo.digestInfo);
5323  } else {
5324  setMetaData(QLatin1String("{internal~allhosts}cached-proxy-auth"), QLatin1String("true"));
5325  if (!authinfo.realmValue.isEmpty())
5326  setMetaData(QLatin1String("{internal~allhosts}proxy-auth-realm"), authinfo.realmValue);
5327  if (!authinfo.digestInfo.isEmpty())
5328  setMetaData(QLatin1String("{internal~allhosts}proxy-auth-challenge"), authinfo.digestInfo);
5329  }
5330 
5331  kDebug(7113) << "Cache authentication info ?" << authinfo.keepPassword;
5332 
5333  if (authinfo.keepPassword) {
5334  cacheAuthentication(authinfo);
5335  kDebug(7113) << "Cached authentication for" << m_request.url;
5336  }
5337  }
5338  // Update our server connection state which includes www and proxy username and password.
5339  m_server.updateCredentials(m_request);
5340 }
5341 
5342 bool HTTPProtocol::handleAuthenticationHeader(const HeaderTokenizer* tokenizer)
5343 {
5344  KIO::AuthInfo authinfo;
5345  QList<QByteArray> authTokens;
5346  KAbstractHttpAuthentication **auth;
5347 
5348  if (m_request.responseCode == 401) {
5349  auth = &m_wwwAuth;
5350  authTokens = tokenizer->iterator("www-authenticate").all();
5351  authinfo.url = m_request.url;
5352  authinfo.username = m_server.url.user();
5353  authinfo.prompt = i18n("You need to supply a username and a "
5354  "password to access this site.");
5355  authinfo.commentLabel = i18n("Site:");
5356  } else {
5357  // make sure that the 407 header hasn't escaped a lower layer when it shouldn't.
5358  // this may break proxy chains which were never tested anyway, and AFAIK they are
5359  // rare to nonexistent in the wild.
5360  Q_ASSERT(QNetworkProxy::applicationProxy().type() == QNetworkProxy::NoProxy);
5361  auth = &m_proxyAuth;
5362  authTokens = tokenizer->iterator("proxy-authenticate").all();
5363  authinfo.url = m_request.proxyUrl;
5364  authinfo.username = m_request.proxyUrl.user();
5365  authinfo.prompt = i18n("You need to supply a username and a password for "
5366  "the proxy server listed below before you are allowed "
5367  "to access any sites." );
5368  authinfo.commentLabel = i18n("Proxy:");
5369  }
5370 
5371  bool authRequiresAnotherRoundtrip = false;
5372 
5373  // Workaround brain dead server responses that violate the spec and
5374  // incorrectly return a 401/407 without the required WWW/Proxy-Authenticate
5375  // header fields. See bug 215736...
5376  if (!authTokens.isEmpty()) {
5377  QString errorMsg;
5378  authRequiresAnotherRoundtrip = true;
5379 
5380  if (m_request.responseCode == m_request.prevResponseCode && *auth) {
5381  // Authentication attempt failed. Retry...
5382  if ((*auth)->wasFinalStage()) {
5383  errorMsg = (m_request.responseCode == 401 ?
5384  i18n("Authentication Failed.") :
5385  i18n("Proxy Authentication Failed."));
5386  delete *auth;
5387  *auth = 0;
5388  } else { // Create authentication header
5389  // WORKAROUND: The following piece of code prevents brain dead IIS
5390  // servers that send back multiple "WWW-Authenticate" headers from
5391  // screwing up our authentication logic during the challenge
5392  // phase (Type 2) of NTLM authenticaiton.
5393  QMutableListIterator<QByteArray> it (authTokens);
5394  const QByteArray authScheme ((*auth)->scheme().trimmed());
5395  while (it.hasNext()) {
5396  if (qstrnicmp(authScheme.constData(), it.next().constData(), authScheme.length()) != 0) {
5397  it.remove();
5398  }
5399  }
5400  }
5401  }
5402 
5403 try_next_auth_scheme:
5404  QByteArray bestOffer = KAbstractHttpAuthentication::bestOffer(authTokens);
5405  if (*auth) {
5406  const QByteArray authScheme ((*auth)->scheme().trimmed());
5407  if (qstrnicmp(authScheme.constData(), bestOffer.constData(), authScheme.length()) != 0) {
5408  // huh, the strongest authentication scheme offered has changed.
5409  delete *auth;
5410  *auth = 0;
5411  }
5412  }
5413 
5414  if (!(*auth)) {
5415  *auth = KAbstractHttpAuthentication::newAuth(bestOffer, config());
5416  }
5417 
5418  if (*auth) {
5419  kDebug(7113) << "Trying authentication scheme:" << (*auth)->scheme();
5420 
5421  // remove trailing space from the method string, or digest auth will fail
5422  (*auth)->setChallenge(bestOffer, authinfo.url, m_request.methodString());
5423 
5424  QString username, password;
5425  bool generateAuthHeader = true;
5426  if ((*auth)->needCredentials()) {
5427  // use credentials supplied by the application if available
5428  if (!m_request.url.user().isEmpty() && !m_request.url.pass().isEmpty()) {
5429  username = m_request.url.user();
5430  password = m_request.url.pass();
5431  // don't try this password any more
5432  m_request.url.setPass(QString());
5433  } else {
5434  // try to get credentials from kpasswdserver's cache, then try asking the user.
5435  authinfo.verifyPath = false; // we have realm, no path based checking please!
5436  authinfo.realmValue = (*auth)->realm();
5437  if (authinfo.realmValue.isEmpty() && !(*auth)->supportsPathMatching())
5438  authinfo.realmValue = QLatin1String((*auth)->scheme());
5439 
5440  // Save the current authinfo url because it can be modified by the call to
5441  // checkCachedAuthentication. That way we can restore it if the call
5442  // modified it.
5443  const KUrl reqUrl = authinfo.url;
5444  if (!errorMsg.isEmpty() || !checkCachedAuthentication(authinfo)) {
5445  // Reset url to the saved url...
5446  authinfo.url = reqUrl;
5447  authinfo.keepPassword = true;
5448  authinfo.comment = i18n("<b>%1</b> at <b>%2</b>",
5449  htmlEscape(authinfo.realmValue), authinfo.url.host());
5450 
5451  if (!openPasswordDialog(authinfo, errorMsg)) {
5452  generateAuthHeader = false;
5453  authRequiresAnotherRoundtrip = false;
5454  if (!sendErrorPageNotification()) {
5455  error(ERR_ACCESS_DENIED, reqUrl.host());
5456  }
5457  kDebug(7113) << "looks like the user canceled the authentication dialog";
5458  delete *auth;
5459  *auth = 0;
5460  }
5461  }
5462  username = authinfo.username;
5463  password = authinfo.password;
5464  }
5465  }
5466 
5467  if (generateAuthHeader) {
5468  (*auth)->generateResponse(username, password);
5469  (*auth)->setCachePasswordEnabled(authinfo.keepPassword);
5470 
5471  kDebug(7113) << "isError=" << (*auth)->isError()
5472  << "needCredentials=" << (*auth)->needCredentials()
5473  << "forceKeepAlive=" << (*auth)->forceKeepAlive()
5474  << "forceDisconnect=" << (*auth)->forceDisconnect();
5475 
5476  if ((*auth)->isError()) {
5477  authTokens.removeOne(bestOffer);
5478  if (!authTokens.isEmpty()) {
5479  goto try_next_auth_scheme;
5480  } else {
5481  error(ERR_UNSUPPORTED_ACTION, i18n("Authorization failed."));
5482  authRequiresAnotherRoundtrip = false;
5483  }
5484  //### return false; ?
5485  } else if ((*auth)->forceKeepAlive()) {
5486  //### think this through for proxied / not proxied
5487  m_request.isKeepAlive = true;
5488  } else if ((*auth)->forceDisconnect()) {
5489  //### think this through for proxied / not proxied
5490  m_request.isKeepAlive = false;
5491  httpCloseConnection();
5492  }
5493  }
5494  } else {
5495  authRequiresAnotherRoundtrip = false;
5496  if (!sendErrorPageNotification()) {
5497  error(ERR_UNSUPPORTED_ACTION, i18n("Unknown Authorization method."));
5498  }
5499  }
5500  }
5501 
5502  return authRequiresAnotherRoundtrip;
5503 }
5504 
5505 
5506 #include "http.moc"
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