diff --git a/wsd/ClientRequestDispatcher.cpp b/wsd/ClientRequestDispatcher.cpp index 9bf55e222096b..3331c87e02f25 100644 --- a/wsd/ClientRequestDispatcher.cpp +++ b/wsd/ClientRequestDispatcher.cpp @@ -9,17 +9,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include -#include -#include #include -#include - -#include -#include -#include -#include -#include #if ENABLE_FEATURE_LOCK #include "CommandControl.hpp" @@ -37,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -61,15 +50,9 @@ #include #include #include -#include #include -#if !MOBILEAPP -#include -#include -#endif // !MOBILEAPP #include #include -#include #include #include @@ -1224,167 +1207,88 @@ bool ClientRequestDispatcher::handleWopiAccessCheckRequest(const Poco::Net::HTTP { CheckStatus::NotHttps, "NOT_HTTPS" }, { CheckStatus::NoScheme, "NO_SCHEME" }, { CheckStatus::Timeout, "TIMEOUT" } + // TODO allowed host check }; - CheckStatus result = CheckStatus::Ok; + auto sendResult = [=, this](CheckStatus result) + { + Poco::JSON::Object::Ptr status = new Poco::JSON::Object; + status->set("status", (int)result); + status->set("details", checkStatusNames.at(result)); + + std::ostringstream ostrJSON; + status->stringify(ostrJSON); + const auto output = ostrJSON.str(); + + http::Response jsonResponse(http::StatusCode::OK); + FileServerRequestHandler::hstsHeaders(jsonResponse); + jsonResponse.set("Last-Modified", Util::getHttpTimeNow()); + jsonResponse.setBody(output, "application/json"); + jsonResponse.set("X-Content-Type-Options", "nosniff"); + + socket->sendAndShutdown(jsonResponse); + LOG_INF("Sent wopiAccessCheck.json successfully."); + }; if (scheme.empty()) { - result = CheckStatus::NoScheme; + sendResult(CheckStatus::NoScheme); + return true; } else if (protocol != http::Session::Protocol::HttpSsl) { - result = CheckStatus::NotHttps; + sendResult(CheckStatus::NotHttps); + return true; } - else - { - LOG_DBG("Wopi Access Check about to prepare http session"); - - http::Request httpRequest(url); - auto httpProbeSession = http::Session::create(host, protocol, port); - httpProbeSession->setTimeout(std::chrono::seconds(2)); - - LOG_DBG("Wopi Access Check preparing http session"); + LOG_DBG("Wopi Access Check about to prepare http session"); - // thread to probe http - -/* - std::shared_ptr factory; -#if ENABLE_SSL - if (protocol == http::Session::Protocol::HttpUnencrypted) - factory = std::make_shared(); - else -#endif - factory = std::make_shared(); + http::Request httpRequest(url); + auto httpProbeSession = http::Session::create(host, protocol, port); + httpProbeSession->setTimeout(std::chrono::seconds(2)); - static std::shared_ptr httpProbingServerPoll(new TerminatingPoll("HttpProbeThread")); - auto httpServerSocket = ServerSocket::create(ServerSocket::Type::Local, port, Socket::Type::IPv4, - *httpProbingServerPoll, factory); - httpProbingServerPoll->insertNewSocket(httpServerSocket); - */ + LOG_DBG("Wopi Access Check preparing http session"); - httpProbeSession->setFinishedHandler([=, this, &result](const std::shared_ptr&probeSession) { - LOG_INF("contacted successfully."); + httpProbeSession->setFinishedHandler( + [=, this](const std::shared_ptr& probeSession) + { auto httpResponse = probeSession->response(); + LOG_DBG("Wopi Access Check: got response" << httpResponse->state() + << httpResponse->statusCode()); - if (httpResponse->state() == http::Response::State::Timeout) { - result = CheckStatus::Timeout; - } + CheckStatus result = CheckStatus::Ok; - if (httpResponse->state() == http::Response::State::Error) { + if (httpResponse->state() != http::Response::State::Complete) + { // are TLS errors here ? result = CheckStatus::UnspecifiedError; } - // construct the result - Poco::JSON::Object::Ptr status = new Poco::JSON::Object; - status->set("status", (int)result); - status->set("details", checkStatusNames.at(result)); - - std::ostringstream ostrJSON; - status->stringify(ostrJSON); - const auto output = ostrJSON.str(); - - http::Response jsonResponse(http::StatusCode::OK); - FileServerRequestHandler::hstsHeaders(jsonResponse); - jsonResponse.set("Last-Modified", Util::getHttpTimeNow()); - jsonResponse.setBody(output, "application/json"); - jsonResponse.set("X-Content-Type-Options", "nosniff"); - - - socket->sendAndShutdown(jsonResponse); - LOG_INF("Sent wopiAccessCheck.json successfully."); - - }); - - LOG_DBG("Wopi Access Check requesting: " << httpRequest.getUrl()); - - TerminatingPoll wopiCheckPoll ("wopi_check_poll"); - wopiCheckPoll.startThread(); - - httpProbeSession->asyncRequest(httpRequest, wopiCheckPoll); + if (!probeSession->isConnected()) + { + result = CheckStatus::ConnectionAborted; + } - ///auto response = session->response(); + if (httpResponse->state() == http::Response::State::Timeout) + { + result = CheckStatus::Timeout; + } - // httpRequest.async - //if (!httpRequest->asyncRequest(requestProxy, *COOLWSD::getWebServerPoll())) { + // TODO complete error coverage - /* - // request the url - try - { - const auto hostAddress(Poco::Net::DNS::resolve(uri.getHost())); - - Poco::Net::HTTPSClientSession httpSession(uri.getHost(), 443); - httpSession.setConnectTimeout(Poco::Timespan(0, 300)); - Poco::Net::HTTPRequest httpRequest(Poco::Net::HTTPRequest::HTTP_GET, - uri.getPathAndQuery()); - httpSession.sendRequest(httpRequest); - Poco::Net::HTTPResponse response; - std::istream* responseStream = &httpSession.receiveResponse(response); - - std::cout << responseStream->rdbuf(); - if (response.getStatus() != 200) + if (!probeSession->getSslVerifyMessage().empty()) { - result = CheckStatus::NotHttpSucess; - } - } - catch (Poco::Net::HostNotFoundException& hostNotfound) - { - result = CheckStatus::HostNotFound; - } - catch (Poco::Net::NoAddressFoundException& noAddressFound) - { - result = CheckStatus::HostUnReachable; - } - catch (Poco::Net::ConnectionAbortedException& connectionAborted) - { - result = CheckStatus::ConnectionAborted; - } - catch (Poco::Net::ConnectionRefusedException& exception) - { - result = CheckStatus::ConnectionRefused; - } - catch (Poco::Net::InvalidCertificateException& invalidCertification) - { - result = CheckStatus::InvalidCertificate; - } - catch (Poco::Net::CertificateValidationException& certificateValidation) - { - result = CheckStatus::CertificateValidation; - } - catch (Poco::TimeoutException& timeout) - { - result = CheckStatus::Timeout; - } - catch (Poco::Exception& exception) - { - LOG_ERR_S("Wopi Access Check request error, query to callback [" - << uri.toString() << "] failed:" << exception.what() << exception.className() - << exception.name() << exception.message()); + result = CheckStatus::CertificateValidation; - result = CheckStatus::UnspecifiedError; - } - */ - } + LOG_DBG("Result ssl: " << probeSession->getSslVerifyMessage()); + } - // construct the result - Poco::JSON::Object::Ptr status = new Poco::JSON::Object; - status->set("status", (int)result); - status->set("details", checkStatusNames.at(result)); + sendResult(result); + }); - std::ostringstream ostrJSON; - status->stringify(ostrJSON); - const auto output = ostrJSON.str(); + LOG_DBG("Wopi Access Check requesting: " << httpRequest.getUrl()); - http::Response httpResponse(http::StatusCode::OK); - FileServerRequestHandler::hstsHeaders(httpResponse); - httpResponse.set("Last-Modified", Util::getHttpTimeNow()); - httpResponse.setBody(output, "application/json"); - httpResponse.set("X-Content-Type-Options", "nosniff"); - socket->sendAndShutdown(httpResponse); - LOG_INF("Sent capabilities.json successfully."); + httpProbeSession->asyncRequest(httpRequest, *COOLWSD::getWebServerPoll()); return true; }