| 1 | // |
| 2 | // HTTPSStreamFactory.cpp |
| 3 | // |
| 4 | // Library: NetSSL_OpenSSL |
| 5 | // Package: HTTPSClient |
| 6 | // Module: HTTPSStreamFactory |
| 7 | // |
| 8 | // Copyright (c) 2006-2012, Applied Informatics Software Engineering GmbH. |
| 9 | // and Contributors. |
| 10 | // |
| 11 | // SPDX-License-Identifier: BSL-1.0 |
| 12 | // |
| 13 | |
| 14 | |
| 15 | #include "Poco/Net/HTTPSStreamFactory.h" |
| 16 | #include "Poco/Net/HTTPSClientSession.h" |
| 17 | #include "Poco/Net/HTTPIOStream.h" |
| 18 | #include "Poco/Net/HTTPRequest.h" |
| 19 | #include "Poco/Net/HTTPResponse.h" |
| 20 | #include "Poco/Net/HTTPCredentials.h" |
| 21 | #include "Poco/Net/NetException.h" |
| 22 | #include "Poco/URI.h" |
| 23 | #include "Poco/URIStreamOpener.h" |
| 24 | #include "Poco/UnbufferedStreamBuf.h" |
| 25 | #include "Poco/NullStream.h" |
| 26 | #include "Poco/StreamCopier.h" |
| 27 | #include "Poco/Format.h" |
| 28 | #include "Poco/Version.h" |
| 29 | |
| 30 | |
| 31 | using Poco::URIStreamFactory; |
| 32 | using Poco::URI; |
| 33 | using Poco::URIStreamOpener; |
| 34 | using Poco::UnbufferedStreamBuf; |
| 35 | |
| 36 | |
| 37 | namespace Poco { |
| 38 | namespace Net { |
| 39 | |
| 40 | |
| 41 | HTTPSStreamFactory::HTTPSStreamFactory(): |
| 42 | _proxyPort(HTTPSession::HTTP_PORT) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | |
| 47 | HTTPSStreamFactory::HTTPSStreamFactory(const std::string& proxyHost, Poco::UInt16 proxyPort): |
| 48 | _proxyHost(proxyHost), |
| 49 | _proxyPort(proxyPort) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | |
| 54 | HTTPSStreamFactory::HTTPSStreamFactory(const std::string& proxyHost, Poco::UInt16 proxyPort, const std::string& proxyUsername, const std::string& proxyPassword): |
| 55 | _proxyHost(proxyHost), |
| 56 | _proxyPort(proxyPort), |
| 57 | _proxyUsername(proxyUsername), |
| 58 | _proxyPassword(proxyPassword) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | |
| 63 | HTTPSStreamFactory::~HTTPSStreamFactory() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | |
| 68 | std::istream* HTTPSStreamFactory::open(const URI& uri) |
| 69 | { |
| 70 | poco_assert (uri.getScheme() == "https" || uri.getScheme() == "http" ); |
| 71 | |
| 72 | URI resolvedURI(uri); |
| 73 | URI proxyUri; |
| 74 | HTTPClientSession* pSession = 0; |
| 75 | HTTPResponse res; |
| 76 | try |
| 77 | { |
| 78 | bool retry = false; |
| 79 | bool authorize = false; |
| 80 | int redirects = 0; |
| 81 | std::string username; |
| 82 | std::string password; |
| 83 | |
| 84 | do |
| 85 | { |
| 86 | if (!pSession) |
| 87 | { |
| 88 | if (resolvedURI.getScheme() != "http" ) |
| 89 | pSession = new HTTPSClientSession(resolvedURI.getHost(), resolvedURI.getPort()); |
| 90 | else |
| 91 | pSession = new HTTPClientSession(resolvedURI.getHost(), resolvedURI.getPort()); |
| 92 | |
| 93 | if (proxyUri.empty()) |
| 94 | { |
| 95 | if (!_proxyHost.empty()) |
| 96 | { |
| 97 | pSession->setProxy(_proxyHost, _proxyPort); |
| 98 | pSession->setProxyCredentials(_proxyUsername, _proxyPassword); |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | pSession->setProxy(proxyUri.getHost(), proxyUri.getPort()); |
| 104 | if (!_proxyUsername.empty()) |
| 105 | { |
| 106 | pSession->setProxyCredentials(_proxyUsername, _proxyPassword); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | std::string path = resolvedURI.getPathAndQuery(); |
| 111 | if (path.empty()) path = "/" ; |
| 112 | HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1); |
| 113 | |
| 114 | if (authorize) |
| 115 | { |
| 116 | HTTPCredentials::extractCredentials(uri, username, password); |
| 117 | HTTPCredentials cred(username, password); |
| 118 | cred.authenticate(req, res); |
| 119 | } |
| 120 | |
| 121 | req.set("User-Agent" , Poco::format("poco/%d.%d.%d" , |
| 122 | (POCO_VERSION >> 24) & 0xFF, |
| 123 | (POCO_VERSION >> 16) & 0xFF, |
| 124 | (POCO_VERSION >> 8) & 0xFF)); |
| 125 | req.set("Accept" , "*/*" ); |
| 126 | |
| 127 | pSession->sendRequest(req); |
| 128 | std::istream& rs = pSession->receiveResponse(res); |
| 129 | bool moved = (res.getStatus() == HTTPResponse::HTTP_MOVED_PERMANENTLY || |
| 130 | res.getStatus() == HTTPResponse::HTTP_FOUND || |
| 131 | res.getStatus() == HTTPResponse::HTTP_SEE_OTHER || |
| 132 | res.getStatus() == HTTPResponse::HTTP_TEMPORARY_REDIRECT); |
| 133 | if (moved) |
| 134 | { |
| 135 | resolvedURI.resolve(res.get("Location" )); |
| 136 | if (!username.empty()) |
| 137 | { |
| 138 | resolvedURI.setUserInfo(username + ":" + password); |
| 139 | authorize = false; |
| 140 | } |
| 141 | delete pSession; |
| 142 | pSession = 0; |
| 143 | ++redirects; |
| 144 | retry = true; |
| 145 | } |
| 146 | else if (res.getStatus() == HTTPResponse::HTTP_OK) |
| 147 | { |
| 148 | return new HTTPResponseStream(rs, pSession); |
| 149 | } |
| 150 | else if (res.getStatus() == HTTPResponse::HTTP_USEPROXY && !retry) |
| 151 | { |
| 152 | // The requested resource MUST be accessed through the proxy |
| 153 | // given by the Location field. The Location field gives the |
| 154 | // URI of the proxy. The recipient is expected to repeat this |
| 155 | // single request via the proxy. 305 responses MUST only be generated by origin servers. |
| 156 | // only use for one single request! |
| 157 | proxyUri.resolve(res.get("Location" )); |
| 158 | delete pSession; |
| 159 | pSession = 0; |
| 160 | retry = true; // only allow useproxy once |
| 161 | } |
| 162 | else if (res.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED && !authorize) |
| 163 | { |
| 164 | authorize = true; |
| 165 | retry = true; |
| 166 | Poco::NullOutputStream null; |
| 167 | Poco::StreamCopier::copyStream(rs, null); |
| 168 | } |
| 169 | else throw HTTPException(res.getReason(), uri.toString()); |
| 170 | } |
| 171 | while (retry && redirects < MAX_REDIRECTS); |
| 172 | throw HTTPException("Too many redirects" , uri.toString()); |
| 173 | } |
| 174 | catch (...) |
| 175 | { |
| 176 | delete pSession; |
| 177 | throw; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | void HTTPSStreamFactory::registerFactory() |
| 183 | { |
| 184 | URIStreamOpener::defaultOpener().registerStreamFactory("https" , new HTTPSStreamFactory); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void HTTPSStreamFactory::unregisterFactory() |
| 189 | { |
| 190 | URIStreamOpener::defaultOpener().unregisterStreamFactory("https" ); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | } } // namespace Poco::Net |
| 195 | |