blob: c2ba1bc01e41e1f109cc6fbf943eb7a1d2a993f3 [file] [log] [blame]
Andreea Costinase45d54b2020-03-10 09:21:14 +01001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "system-proxy/proxy_connect_job.h"
6
7#include <algorithm>
8#include <utility>
9#include <vector>
10
Andreea Costinase45d54b2020-03-10 09:21:14 +010011#include <curl/easy.h>
12
Andreea Costinase45d54b2020-03-10 09:21:14 +010013#include <base/base64.h>
14#include <base/bind.h>
15#include <base/bind_helpers.h>
16#include <base/callback_helpers.h>
17#include <base/files/file_util.h>
18#include <base/strings/stringprintf.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010019#include <base/strings/string_util.h>
20#include <base/time/time.h>
Andreea Costinas08a5d182020-04-29 22:12:47 +020021#include <base/threading/thread.h>
22#include <base/threading/thread_task_runner_handle.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010023#include <brillo/http/http_transport.h>
Garrick Evanscd8c2972020-04-14 14:35:52 +090024#include <chromeos/patchpanel/net_util.h>
25#include <chromeos/patchpanel/socket.h>
26#include <chromeos/patchpanel/socket_forwarder.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010027
28#include "system-proxy/curl_socket.h"
Andreea Costinas90b71642020-06-12 10:18:25 +020029#include "system-proxy/http_util.h"
Andreea Costinase45d54b2020-03-10 09:21:14 +010030
Garrick Evans2d5e7c92020-06-08 14:14:28 +090031// The libpatchpanel-util library overloads << for socket data structures.
Andreea Costinase45d54b2020-03-10 09:21:14 +010032// By C++'s argument-dependent lookup rules, operators defined in a
33// different namespace are not visible. We need the using directive to make
34// the overload available this namespace.
Garrick Evans3388a032020-03-24 11:25:55 +090035using patchpanel::operator<<;
Andreea Costinase45d54b2020-03-10 09:21:14 +010036
37namespace {
38// There's no RFC recomandation for the max size of http request headers but
39// popular http server implementations (Apache, IIS, Tomcat) set the lower limit
40// to 8000.
41constexpr int kMaxHttpRequestHeadersSize = 8000;
Andreea Costinas350e4aa2020-07-20 20:29:46 +020042constexpr base::TimeDelta kCurlConnectTimeout =
43 base::TimeDelta::FromSeconds(30);
Andreea Costinas08a5d182020-04-29 22:12:47 +020044constexpr base::TimeDelta kWaitClientConnectTimeout =
Andreea Costinas435851b2020-05-25 14:18:41 +020045 base::TimeDelta::FromSeconds(2);
Andreea Costinased9e6122020-08-12 12:06:19 +020046// Time to wait for proxy authentication credentials to be fetched from the
47// browser. The credentials are retrieved either from the Network Service or, if
48// the Network Service doesn't have them, directly from the user via a login
49// dialogue.
50constexpr base::TimeDelta kCredentialsRequestTimeout =
51 base::TimeDelta::FromMinutes(1);
Andreea Costinase45d54b2020-03-10 09:21:14 +010052
Andreea Costinasf90a4c02020-06-12 22:30:51 +020053constexpr int64_t kHttpCodeProxyAuthRequired = 407;
54
Andreea Costinase45d54b2020-03-10 09:21:14 +010055// HTTP error codes and messages with origin information for debugging (RFC723,
56// section 6.1).
57const std::string_view kHttpBadRequest =
58 "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n";
Andreea Costinas08a5d182020-04-29 22:12:47 +020059const std::string_view kHttpConnectionTimeout =
60 "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n";
Andreea Costinase45d54b2020-03-10 09:21:14 +010061const std::string_view kHttpInternalServerError =
62 "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n";
63const std::string_view kHttpBadGateway =
64 "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n";
Andreea Costinasf90a4c02020-06-12 22:30:51 +020065const std::string_view kHttpProxyAuthRequired =
66 "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n";
67constexpr char kHttpErrorTunnelFailed[] =
68 "HTTP/1.1 %s Error creating tunnel - Origin: local proxy\r\n\r\n";
Andreea Costinas90b71642020-06-12 10:18:25 +020069} // namespace
Andreea Costinasa2246592020-04-12 23:24:01 +020070
Andreea Costinas90b71642020-06-12 10:18:25 +020071namespace system_proxy {
Andreea Costinasa2246592020-04-12 23:24:01 +020072// CURLOPT_HEADERFUNCTION callback implementation that only returns the headers
73// from the last response sent by the sever. This is to make sure that we
74// send back valid HTTP replies and auhentication data from the HTTP messages is
75// not being leaked to the client. |userdata| is set on the libcurl CURL handle
76// used to configure the request, using the the CURLOPT_HEADERDATA option. Note,
77// from the libcurl documentation: This callback is being called for all the
78// responses received from the proxy server after intiating the connection
79// request. Multiple responses can be received in an authentication sequence.
80// Only the last response's headers should be forwarded to the System-proxy
81// client. The header callback will be called once for each header and only
82// complete header lines are passed on to the callback.
83static size_t WriteHeadersCallback(char* contents,
84 size_t size,
85 size_t nmemb,
86 void* userdata) {
87 std::vector<char>* vec = (std::vector<char>*)userdata;
88
89 // Check if we are receiving a new HTTP message (after the last one was
90 // terminated with an empty line).
Andreea Costinas90b71642020-06-12 10:18:25 +020091 if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) {
Andreea Costinasa2246592020-04-12 23:24:01 +020092 VLOG(1) << "Removing the http reply headers from the server "
93 << base::StringPiece(vec->data(), vec->size());
94 vec->clear();
Andreea Costinase45d54b2020-03-10 09:21:14 +010095 }
Andreea Costinasa2246592020-04-12 23:24:01 +020096 vec->insert(vec->end(), contents, contents + (nmemb * size));
Andreea Costinase45d54b2020-03-10 09:21:14 +010097 return size * nmemb;
98}
99
Andreea Costinasa2246592020-04-12 23:24:01 +0200100// CONNECT requests may have a reply body. This method will capture the reply
101// and save it in |userdata|. |userdata| is set on the libcurl CURL handle
102// used to configure the request, using the the CURLOPT_WRITEDATA option.
103static size_t WriteCallback(char* contents,
104 size_t size,
105 size_t nmemb,
106 void* userdata) {
107 std::vector<char>* vec = (std::vector<char>*)userdata;
108 vec->insert(vec->end(), contents, contents + (nmemb * size));
109 return size * nmemb;
110}
111
Andreea Costinase45d54b2020-03-10 09:21:14 +0100112ProxyConnectJob::ProxyConnectJob(
Garrick Evans3388a032020-03-24 11:25:55 +0900113 std::unique_ptr<patchpanel::Socket> socket,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100114 const std::string& credentials,
115 ResolveProxyCallback resolve_proxy_callback,
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200116 AuthenticationRequiredCallback auth_required_callback,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100117 OnConnectionSetupFinishedCallback setup_finished_callback)
118 : credentials_(credentials),
119 resolve_proxy_callback_(std::move(resolve_proxy_callback)),
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200120 auth_required_callback_(std::move(auth_required_callback)),
Andreea Costinas08a5d182020-04-29 22:12:47 +0200121 setup_finished_callback_(std::move(setup_finished_callback)),
122 // Safe to use |base::Unretained| because the callback will be canceled
123 // when it goes out of scope.
124 client_connect_timeout_callback_(base::Bind(
Andreea Costinased9e6122020-08-12 12:06:19 +0200125 &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))),
126 credentials_request_timeout_callback_(base::Bind(
127 &ProxyConnectJob::OnAuthenticationTimeout, base::Unretained(this))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100128 client_socket_ = std::move(socket);
129}
130
131ProxyConnectJob::~ProxyConnectJob() = default;
132
133bool ProxyConnectJob::Start() {
134 // Make the socket non-blocking.
135 if (!base::SetNonBlocking(client_socket_->fd())) {
Andreea Costinas435851b2020-05-25 14:18:41 +0200136 PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking";
Andreea Costinase45d54b2020-03-10 09:21:14 +0100137 client_socket_->SendTo(kHttpInternalServerError.data(),
138 kHttpInternalServerError.size());
139 return false;
140 }
Andreea Costinas08a5d182020-04-29 22:12:47 +0200141 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
142 FROM_HERE, client_connect_timeout_callback_.callback(),
143 kWaitClientConnectTimeout);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100144 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200145 client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady,
146 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100147 return true;
148}
149
150void ProxyConnectJob::OnClientReadReady() {
Andreea Costinas435851b2020-05-25 14:18:41 +0200151 // The first message should be a HTTP CONNECT request.
152 std::vector<char> buf(kMaxHttpRequestHeadersSize);
153 size_t read_byte_count = 0;
154
155 read_byte_count = client_socket_->RecvFrom(buf.data(), buf.size());
156 if (read_byte_count < 0) {
157 LOG(ERROR) << *this << " Failure to read client request";
158 OnError(kHttpBadRequest);
159 return;
160 }
161 connect_data_.insert(connect_data_.end(), buf.begin(),
162 buf.begin() + read_byte_count);
163
164 std::vector<char> connect_request, payload_data;
165 if (!ExtractHTTPRequest(connect_data_, &connect_request, &payload_data)) {
166 LOG(INFO) << "Received partial HTTP request";
167 return;
168 }
169 connect_data_ = payload_data;
170 HandleClientHTTPRequest(
171 base::StringPiece(connect_request.data(), connect_request.size()));
172}
173
174void ProxyConnectJob::HandleClientHTTPRequest(
175 const base::StringPiece& http_request) {
Andreea Costinas08a5d182020-04-29 22:12:47 +0200176 if (!read_watcher_) {
177 // The connection has timed out while waiting for the client's HTTP CONNECT
178 // request. See |OnClientConnectTimeout|.
179 return;
180 }
181 client_connect_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100182 // Stop watching.
183 read_watcher_.reset();
Andreea Costinas435851b2020-05-25 14:18:41 +0200184 target_url_ = GetUriAuthorityFromHttpHeader(http_request);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100185 if (target_url_.empty()) {
Andreea Costinas435851b2020-05-25 14:18:41 +0200186 std::string encoded;
187 base::Base64Encode(http_request, &encoded);
188 LOG(ERROR) << *this << " Failed to parse HTTP CONNECT request " << encoded;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100189 OnError(kHttpBadRequest);
190 return;
191 }
192
Andreea Costinasa89309d2020-05-08 15:51:12 +0200193 // The proxy resolution service in Chrome expects a proper URL, formatted as
194 // scheme://host:port. It's safe to assume only https will be used for the
195 // target url.
Andreea Costinase45d54b2020-03-10 09:21:14 +0100196 std::move(resolve_proxy_callback_)
Andreea Costinasa89309d2020-05-08 15:51:12 +0200197 .Run(base::StringPrintf("https://%s", target_url_.c_str()),
198 base::Bind(&ProxyConnectJob::OnProxyResolution,
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200199 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100200}
201
Andreea Costinase45d54b2020-03-10 09:21:14 +0100202void ProxyConnectJob::OnProxyResolution(
203 const std::list<std::string>& proxy_servers) {
204 proxy_servers_ = proxy_servers;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200205 DoCurlServerConnection();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100206}
207
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200208void ProxyConnectJob::AuthenticationRequired(
209 const std::vector<char>& http_response_headers) {
210 DCHECK(!proxy_servers_.empty());
211 SchemeRealmPairList scheme_realm_pairs = ParseAuthChallenge(base::StringPiece(
212 http_response_headers.data(), http_response_headers.size()));
213 if (scheme_realm_pairs.empty()) {
214 LOG(ERROR) << "Failed to parse authentication challenge";
215 OnError(kHttpBadGateway);
216 return;
217 }
218
Andreea Costinased9e6122020-08-12 12:06:19 +0200219 if (!authentication_timer_started_) {
220 authentication_timer_started_ = true;
221 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
222 FROM_HERE, credentials_request_timeout_callback_.callback(),
223 kCredentialsRequestTimeout);
224 }
225
226 auth_required_callback_.Run(
227 proxy_servers_.front(), scheme_realm_pairs.front().first,
228 scheme_realm_pairs.front().second, credentials_,
229 base::BindRepeating(&ProxyConnectJob::OnAuthCredentialsProvided,
230 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200231}
232
233void ProxyConnectJob::OnAuthCredentialsProvided(
234 const std::string& credentials) {
Andreea Costinased9e6122020-08-12 12:06:19 +0200235 // If no credentials were returned or if the same bad credentials were
236 // returned twice, quit the connection. This is to ensure that bad credentials
237 // acquired from the Network Service won't trigger an authentication loop.
238 if (credentials.empty() || credentials_ == credentials) {
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200239 SendHttpResponseToClient(/* http_response_headers= */ {},
240 /* http_response_body= */ {});
241 std::move(setup_finished_callback_).Run(nullptr, this);
242 return;
243 }
244 credentials_ = credentials;
245 VLOG(1) << "Connecting to the remote server with provided credentials";
246 DoCurlServerConnection();
247}
248
249bool ProxyConnectJob::AreAuthCredentialsRequired(CURL* easyhandle) {
250 if (http_response_code_ != kHttpCodeProxyAuthRequired) {
251 return false;
252 }
253
254 CURLcode res;
255 int64_t server_proxy_auth_scheme = 0;
256 res = curl_easy_getinfo(easyhandle, CURLINFO_PROXYAUTH_AVAIL,
257 &server_proxy_auth_scheme);
258 if (res != CURLE_OK || !server_proxy_auth_scheme) {
259 return false;
260 }
261
262 // If kerberos is enabled, then we need to wait for the user to request a
263 // kerberos ticket from Chrome.
264 return !(server_proxy_auth_scheme & CURLAUTH_NEGOTIATE);
265}
266
267void ProxyConnectJob::DoCurlServerConnection() {
268 DCHECK(!proxy_servers_.empty());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100269 CURL* easyhandle = curl_easy_init();
270 CURLcode res;
Andreea Costinasa2246592020-04-12 23:24:01 +0200271 curl_socket_t newSocket = -1;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100272
273 if (!easyhandle) {
274 // Unfortunately it's not possible to get the failure reason.
275 LOG(ERROR) << *this << " Failure to create curl handle.";
276 curl_easy_cleanup(easyhandle);
277 OnError(kHttpInternalServerError);
278 return;
279 }
280 curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str());
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200281 std::vector<char> http_response_headers;
282 std::vector<char> http_response_body;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200283
284 if (proxy_servers_.front().c_str() != brillo::http::kDirectProxy) {
285 curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_servers_.front().c_str());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100286 curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
287 curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1);
288 // Allow libcurl to pick authentication method. Curl will use the most
289 // secure one the remote site claims to support.
290 curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
291 curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str());
292 }
293 curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
294 kCurlConnectTimeout.InMilliseconds());
Andreea Costinasa2246592020-04-12 23:24:01 +0200295 curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200296 curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &http_response_headers);
Andreea Costinasa2246592020-04-12 23:24:01 +0200297 curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200298 curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &http_response_body);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100299
300 res = curl_easy_perform(easyhandle);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200301 curl_easy_getinfo(easyhandle, CURLINFO_HTTP_CONNECTCODE,
302 &http_response_code_);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100303
304 if (res != CURLE_OK) {
Andreea Costinas90b71642020-06-12 10:18:25 +0200305 LOG(ERROR) << *this << " curl_easy_perform() failed with error: "
306 << curl_easy_strerror(res);
Andreea Costinased9e6122020-08-12 12:06:19 +0200307 if (AreAuthCredentialsRequired(easyhandle)) {
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200308 AuthenticationRequired(http_response_headers);
309 curl_easy_cleanup(easyhandle);
310 return;
311 }
Andreea Costinased9e6122020-08-12 12:06:19 +0200312 credentials_request_timeout_callback_.Cancel();
313
Andreea Costinase45d54b2020-03-10 09:21:14 +0100314 curl_easy_cleanup(easyhandle);
Andreea Costinasa2246592020-04-12 23:24:01 +0200315
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200316 SendHttpResponseToClient(/* http_response_headers= */ {},
317 /* http_response_body= */ {});
318 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100319 return;
320 }
Andreea Costinased9e6122020-08-12 12:06:19 +0200321 credentials_request_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100322 // Extract the socket from the curl handle.
323 res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket);
324 if (res != CURLE_OK) {
325 LOG(ERROR) << *this << " Failed to get socket from curl with error: "
326 << curl_easy_strerror(res);
327 curl_easy_cleanup(easyhandle);
328 OnError(kHttpBadGateway);
329 return;
330 }
331
332 ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle());
333 auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket),
334 std::move(scoped_handle));
335
336 // Send the server reply to the client. If the connection is successful, the
Andreea Costinasa2246592020-04-12 23:24:01 +0200337 // reply headers should be "HTTP/1.1 200 Connection Established".
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200338 if (!SendHttpResponseToClient(http_response_headers, http_response_body)) {
339 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinasa2246592020-04-12 23:24:01 +0200340 return;
341 }
Andreea Costinas435851b2020-05-25 14:18:41 +0200342 // Send the buffered playload data to the remote server.
343 if (!connect_data_.empty()) {
344 server_conn->SendTo(connect_data_.data(), connect_data_.size());
345 connect_data_.clear();
346 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100347
Andreea-Elena Costinasfae5c152020-09-28 18:18:31 +0000348 auto fwd = std::make_unique<patchpanel::SocketForwarder>(
349 base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()),
350 std::move(client_socket_), std::move(server_conn));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100351 // Start forwarding data between sockets.
352 fwd->Start();
353 std::move(setup_finished_callback_).Run(std::move(fwd), this);
354}
355
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200356bool ProxyConnectJob::SendHttpResponseToClient(
357 const std::vector<char>& http_response_headers,
358 const std::vector<char>& http_response_body) {
359 if (http_response_code_ == 0) {
360 // No HTTP CONNECT response code is available.
361 return client_socket_->SendTo(kHttpInternalServerError.data(),
362 kHttpInternalServerError.size());
363 }
364
365 if (http_response_code_ == kHttpCodeProxyAuthRequired) {
366 // This will be a hint for the user to authenticate via the Browser or
367 // acquire a Kerberos ticket.
368 return client_socket_->SendTo(kHttpProxyAuthRequired.data(),
369 kHttpProxyAuthRequired.size());
370 }
371
372 if (http_response_code_ >= 400) {
373 VLOG(1) << "Failed to set up HTTP tunnel with code " << http_response_code_;
374 std::string http_error = base::StringPrintf(
375 kHttpErrorTunnelFailed, std::to_string(http_response_code_).c_str());
376 return client_socket_->SendTo(http_error.c_str(), http_error.size());
377 }
378
379 if (http_response_headers.empty()) {
380 return client_socket_->SendTo(kHttpInternalServerError.data(),
381 kHttpInternalServerError.size());
382 }
383
384 VLOG(1) << "Sending server reply to client";
385 if (!client_socket_->SendTo(http_response_headers.data(),
386 http_response_headers.size())) {
387 PLOG(ERROR) << "Failed to send HTTP server response headers to client";
388 return false;
389 }
390 if (!http_response_body.empty()) {
391 if (!client_socket_->SendTo(http_response_body.data(),
392 http_response_body.size())) {
393 PLOG(ERROR) << "Failed to send HTTP server response payload to client";
394 return false;
395 }
396 }
397 return true;
398}
399
Andreea Costinase45d54b2020-03-10 09:21:14 +0100400void ProxyConnectJob::OnError(const std::string_view& http_error_message) {
401 client_socket_->SendTo(http_error_message.data(), http_error_message.size());
402 std::move(setup_finished_callback_).Run(nullptr, this);
403}
404
Andreea Costinas08a5d182020-04-29 22:12:47 +0200405void ProxyConnectJob::OnClientConnectTimeout() {
406 // Stop listening for client connect requests.
407 read_watcher_.reset();
408 LOG(ERROR) << *this
409 << " Connection timed out while waiting for the client to send a "
Andreea Costinas435851b2020-05-25 14:18:41 +0200410 "connect request";
Andreea Costinas08a5d182020-04-29 22:12:47 +0200411 OnError(kHttpConnectionTimeout);
412}
413
Andreea Costinased9e6122020-08-12 12:06:19 +0200414void ProxyConnectJob::OnAuthenticationTimeout() {
415 LOG(ERROR)
416 << *this
417 << "The connect job timed out while waiting for proxy authentication "
418 "credentials";
419 OnError(kHttpProxyAuthRequired);
420}
421
Andreea Costinase45d54b2020-03-10 09:21:14 +0100422std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) {
423 stream << "{fd: " << job.client_socket_->fd();
424 if (!job.target_url_.empty()) {
425 stream << ", url: " << job.target_url_;
426 }
427 stream << "}";
428 return stream;
429}
430
431} // namespace system_proxy