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

KDE's Doxygen guidelines are available online.

KIOSlave

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

kdelibs-4.11.5 API Reference

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