QtNetworkAccessManager.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2011 Zeno Albisser <zeno@webkit.org>
  3. * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  15. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  24. * THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "QtNetworkAccessManager.h"
  28. #include "SharedMemory.h"
  29. #include "WebFrameNetworkingContext.h"
  30. #include "WebPage.h"
  31. #include "WebPageProxyMessages.h"
  32. #include "WebProcess.h"
  33. #include <QAuthenticator>
  34. #include <QNetworkProxy>
  35. #include <QNetworkReply>
  36. #include <QNetworkRequest>
  37. namespace WebKit {
  38. QtNetworkAccessManager::QtNetworkAccessManager(WebProcess* webProcess)
  39. : QNetworkAccessManager()
  40. , m_webProcess(webProcess)
  41. {
  42. connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), SLOT(onAuthenticationRequired(QNetworkReply*, QAuthenticator*)));
  43. connect(this, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), SLOT(onProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
  44. #ifndef QT_NO_SSL
  45. connect(this, SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)), SLOT(onSslErrors(QNetworkReply*, QList<QSslError>)));
  46. #endif
  47. }
  48. WebPage* QtNetworkAccessManager::obtainOriginatingWebPage(const QNetworkRequest& request)
  49. {
  50. QObject* originatingObject = request.originatingObject();
  51. if (!originatingObject)
  52. return 0;
  53. qulonglong pageID = originatingObject->property("pageID").toULongLong();
  54. return m_webProcess->webPage(pageID);
  55. }
  56. QNetworkReply* QtNetworkAccessManager::createRequest(Operation operation, const QNetworkRequest& request, QIODevice* outData)
  57. {
  58. WebPage* webPage = obtainOriginatingWebPage(request);
  59. if (webPage && m_applicationSchemes.contains(webPage, request.url().scheme().toLower())) {
  60. QtNetworkReply* reply = new QtNetworkReply(request, this);
  61. webPage->receivedApplicationSchemeRequest(request, reply);
  62. return reply;
  63. }
  64. return QNetworkAccessManager::createRequest(operation, request, outData);
  65. }
  66. void QtNetworkAccessManager::registerApplicationScheme(const WebPage* page, const QString& scheme)
  67. {
  68. m_applicationSchemes.insert(page, scheme.toLower());
  69. }
  70. void QtNetworkAccessManager::onProxyAuthenticationRequired(const QNetworkProxy& proxy, QAuthenticator* authenticator)
  71. {
  72. // FIXME: Check if there is a better way to get a reference to the page.
  73. WebPage* webPage = m_webProcess->focusedWebPage();
  74. if (!webPage)
  75. return;
  76. String hostname = proxy.hostName();
  77. uint16_t port = static_cast<uint16_t>(proxy.port());
  78. String prefilledUsername = authenticator->user();
  79. String username;
  80. String password;
  81. if (webPage->sendSync(
  82. Messages::WebPageProxy::ProxyAuthenticationRequiredRequest(hostname, port, prefilledUsername),
  83. Messages::WebPageProxy::ProxyAuthenticationRequiredRequest::Reply(username, password))) {
  84. if (!username.isEmpty())
  85. authenticator->setUser(username);
  86. if (!password.isEmpty())
  87. authenticator->setPassword(password);
  88. }
  89. }
  90. void QtNetworkAccessManager::onAuthenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
  91. {
  92. WebPage* webPage = obtainOriginatingWebPage(reply->request());
  93. // FIXME: This check can go away once our Qt version is up-to-date. See: QTBUG-23512.
  94. if (!webPage)
  95. return;
  96. String hostname = reply->url().toString(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::StripTrailingSlash);
  97. String realm = authenticator->realm();
  98. String prefilledUsername = authenticator->user();
  99. String username;
  100. String password;
  101. if (webPage->sendSync(
  102. Messages::WebPageProxy::AuthenticationRequiredRequest(hostname, realm, prefilledUsername),
  103. Messages::WebPageProxy::AuthenticationRequiredRequest::Reply(username, password))) {
  104. if (!username.isEmpty())
  105. authenticator->setUser(username);
  106. if (!password.isEmpty())
  107. authenticator->setPassword(password);
  108. }
  109. }
  110. void QtNetworkAccessManager::onSslErrors(QNetworkReply* reply, const QList<QSslError>& qSslErrors)
  111. {
  112. #ifndef QT_NO_SSL
  113. WebPage* webPage = obtainOriginatingWebPage(reply->request());
  114. // FIXME: This check can go away once our Qt version is up-to-date. See: QTBUG-23512.
  115. if (!webPage)
  116. return;
  117. String hostname = reply->url().host();
  118. bool ignoreErrors = false;
  119. if (webPage->sendSync(
  120. Messages::WebPageProxy::CertificateVerificationRequest(hostname),
  121. Messages::WebPageProxy::CertificateVerificationRequest::Reply(ignoreErrors))) {
  122. if (ignoreErrors)
  123. reply->ignoreSslErrors(qSslErrors);
  124. }
  125. #endif
  126. }
  127. }
  128. #include "moc_QtNetworkAccessManager.cpp"