blob: d8b51cfb92e76b5ed820359ebb5bddd52896a2b1 [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 Costinas350e4aa2020-07-20 20:29:46 +020045 base::TimeDelta::FromSeconds(15);
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 +010052constexpr size_t kMaxBadRequestPrintSize = 120;
53
Andreea Costinasf90a4c02020-06-12 22:30:51 +020054constexpr int64_t kHttpCodeProxyAuthRequired = 407;
55
Andreea Costinase45d54b2020-03-10 09:21:14 +010056// HTTP error codes and messages with origin information for debugging (RFC723,
57// section 6.1).
58const std::string_view kHttpBadRequest =
59 "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n";
Andreea Costinas08a5d182020-04-29 22:12:47 +020060const std::string_view kHttpConnectionTimeout =
61 "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n";
Andreea Costinase45d54b2020-03-10 09:21:14 +010062const std::string_view kHttpInternalServerError =
63 "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n";
64const std::string_view kHttpBadGateway =
65 "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n";
Andreea Costinasf90a4c02020-06-12 22:30:51 +020066const std::string_view kHttpProxyAuthRequired =
67 "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n";
68constexpr char kHttpErrorTunnelFailed[] =
69 "HTTP/1.1 %s Error creating tunnel - Origin: local proxy\r\n\r\n";
Andreea Costinas90b71642020-06-12 10:18:25 +020070} // namespace
Andreea Costinasa2246592020-04-12 23:24:01 +020071
Andreea Costinas90b71642020-06-12 10:18:25 +020072namespace system_proxy {
Andreea Costinasa2246592020-04-12 23:24:01 +020073// CURLOPT_HEADERFUNCTION callback implementation that only returns the headers
74// from the last response sent by the sever. This is to make sure that we
75// send back valid HTTP replies and auhentication data from the HTTP messages is
76// not being leaked to the client. |userdata| is set on the libcurl CURL handle
77// used to configure the request, using the the CURLOPT_HEADERDATA option. Note,
78// from the libcurl documentation: This callback is being called for all the
79// responses received from the proxy server after intiating the connection
80// request. Multiple responses can be received in an authentication sequence.
81// Only the last response's headers should be forwarded to the System-proxy
82// client. The header callback will be called once for each header and only
83// complete header lines are passed on to the callback.
84static size_t WriteHeadersCallback(char* contents,
85 size_t size,
86 size_t nmemb,
87 void* userdata) {
88 std::vector<char>* vec = (std::vector<char>*)userdata;
89
90 // Check if we are receiving a new HTTP message (after the last one was
91 // terminated with an empty line).
Andreea Costinas90b71642020-06-12 10:18:25 +020092 if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) {
Andreea Costinasa2246592020-04-12 23:24:01 +020093 VLOG(1) << "Removing the http reply headers from the server "
94 << base::StringPiece(vec->data(), vec->size());
95 vec->clear();
Andreea Costinase45d54b2020-03-10 09:21:14 +010096 }
Andreea Costinasa2246592020-04-12 23:24:01 +020097 vec->insert(vec->end(), contents, contents + (nmemb * size));
Andreea Costinase45d54b2020-03-10 09:21:14 +010098 return size * nmemb;
99}
100
Andreea Costinasa2246592020-04-12 23:24:01 +0200101// CONNECT requests may have a reply body. This method will capture the reply
102// and save it in |userdata|. |userdata| is set on the libcurl CURL handle
103// used to configure the request, using the the CURLOPT_WRITEDATA option.
104static size_t WriteCallback(char* contents,
105 size_t size,
106 size_t nmemb,
107 void* userdata) {
108 std::vector<char>* vec = (std::vector<char>*)userdata;
109 vec->insert(vec->end(), contents, contents + (nmemb * size));
110 return size * nmemb;
111}
112
Andreea Costinase45d54b2020-03-10 09:21:14 +0100113ProxyConnectJob::ProxyConnectJob(
Garrick Evans3388a032020-03-24 11:25:55 +0900114 std::unique_ptr<patchpanel::Socket> socket,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100115 const std::string& credentials,
116 ResolveProxyCallback resolve_proxy_callback,
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200117 AuthenticationRequiredCallback auth_required_callback,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100118 OnConnectionSetupFinishedCallback setup_finished_callback)
119 : credentials_(credentials),
120 resolve_proxy_callback_(std::move(resolve_proxy_callback)),
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200121 auth_required_callback_(std::move(auth_required_callback)),
Andreea Costinas08a5d182020-04-29 22:12:47 +0200122 setup_finished_callback_(std::move(setup_finished_callback)),
123 // Safe to use |base::Unretained| because the callback will be canceled
124 // when it goes out of scope.
125 client_connect_timeout_callback_(base::Bind(
Andreea Costinased9e6122020-08-12 12:06:19 +0200126 &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))),
127 credentials_request_timeout_callback_(base::Bind(
128 &ProxyConnectJob::OnAuthenticationTimeout, base::Unretained(this))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100129 client_socket_ = std::move(socket);
130}
131
132ProxyConnectJob::~ProxyConnectJob() = default;
133
134bool ProxyConnectJob::Start() {
135 // Make the socket non-blocking.
136 if (!base::SetNonBlocking(client_socket_->fd())) {
137 PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking.";
138 client_socket_->SendTo(kHttpInternalServerError.data(),
139 kHttpInternalServerError.size());
140 return false;
141 }
Andreea Costinas08a5d182020-04-29 22:12:47 +0200142 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
143 FROM_HERE, client_connect_timeout_callback_.callback(),
144 kWaitClientConnectTimeout);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100145 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200146 client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady,
147 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100148 return true;
149}
150
151void ProxyConnectJob::OnClientReadReady() {
Andreea Costinas08a5d182020-04-29 22:12:47 +0200152 if (!read_watcher_) {
153 // The connection has timed out while waiting for the client's HTTP CONNECT
154 // request. See |OnClientConnectTimeout|.
155 return;
156 }
157 client_connect_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100158 // Stop watching.
159 read_watcher_.reset();
160 // The first message should be a HTTP CONNECT request.
161 std::vector<char> connect_request;
162 if (!TryReadHttpHeader(&connect_request)) {
163 std::string encoded;
164 base::Base64Encode(
165 base::StringPiece(connect_request.data(), connect_request.size()),
166 &encoded);
167 LOG(ERROR) << *this
168 << " Failure to read proxy CONNECT request. Base 64 encoded "
169 "request message from client: "
170 << encoded;
171 OnError(kHttpBadRequest);
172 return;
173 }
Andreea Costinas90b71642020-06-12 10:18:25 +0200174 base::StringPiece request(connect_request.data(), connect_request.size());
175 target_url_ = GetUriAuthorityFromHttpHeader(request);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100176 if (target_url_.empty()) {
177 LOG(ERROR)
178 << *this
179 << " Failed to extract target url from the HTTP CONNECT request.";
180 OnError(kHttpBadRequest);
181 return;
182 }
183
Andreea Costinasa89309d2020-05-08 15:51:12 +0200184 // The proxy resolution service in Chrome expects a proper URL, formatted as
185 // scheme://host:port. It's safe to assume only https will be used for the
186 // target url.
Andreea Costinase45d54b2020-03-10 09:21:14 +0100187 std::move(resolve_proxy_callback_)
Andreea Costinasa89309d2020-05-08 15:51:12 +0200188 .Run(base::StringPrintf("https://%s", target_url_.c_str()),
189 base::Bind(&ProxyConnectJob::OnProxyResolution,
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200190 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100191}
192
193bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100194 size_t read_byte_count = 0;
195 raw_request->resize(kMaxHttpRequestHeadersSize);
196
197 // Read byte-by-byte and stop when reading an empty line (only CRLF) or when
198 // exceeding the max buffer size.
199 // TODO(acostinas, chromium:1064536) This may have some measurable performance
200 // impact. We should read larger blocks of data, consume the HTTP headers,
201 // cache the tunneled payload that may have already been included (e.g. TLS
202 // ClientHello) and send it to server after the connection is established.
203 while (read_byte_count < kMaxHttpRequestHeadersSize) {
204 if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <=
205 0) {
206 raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize));
207 return false;
208 }
209 ++read_byte_count;
210
Andreea Costinas90b71642020-06-12 10:18:25 +0200211 if (IsEndingWithHttpEmptyLine(
212 base::StringPiece(raw_request->data(), read_byte_count))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100213 raw_request->resize(read_byte_count);
214 return true;
215 }
216 }
217 return false;
218}
219
220void ProxyConnectJob::OnProxyResolution(
221 const std::list<std::string>& proxy_servers) {
222 proxy_servers_ = proxy_servers;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200223 DoCurlServerConnection();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100224}
225
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200226void ProxyConnectJob::AuthenticationRequired(
227 const std::vector<char>& http_response_headers) {
228 DCHECK(!proxy_servers_.empty());
229 SchemeRealmPairList scheme_realm_pairs = ParseAuthChallenge(base::StringPiece(
230 http_response_headers.data(), http_response_headers.size()));
231 if (scheme_realm_pairs.empty()) {
232 LOG(ERROR) << "Failed to parse authentication challenge";
233 OnError(kHttpBadGateway);
234 return;
235 }
236
Andreea Costinased9e6122020-08-12 12:06:19 +0200237 if (!authentication_timer_started_) {
238 authentication_timer_started_ = true;
239 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
240 FROM_HERE, credentials_request_timeout_callback_.callback(),
241 kCredentialsRequestTimeout);
242 }
243
244 auth_required_callback_.Run(
245 proxy_servers_.front(), scheme_realm_pairs.front().first,
246 scheme_realm_pairs.front().second, credentials_,
247 base::BindRepeating(&ProxyConnectJob::OnAuthCredentialsProvided,
248 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200249}
250
251void ProxyConnectJob::OnAuthCredentialsProvided(
252 const std::string& credentials) {
Andreea Costinased9e6122020-08-12 12:06:19 +0200253 // If no credentials were returned or if the same bad credentials were
254 // returned twice, quit the connection. This is to ensure that bad credentials
255 // acquired from the Network Service won't trigger an authentication loop.
256 if (credentials.empty() || credentials_ == credentials) {
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200257 SendHttpResponseToClient(/* http_response_headers= */ {},
258 /* http_response_body= */ {});
259 std::move(setup_finished_callback_).Run(nullptr, this);
260 return;
261 }
262 credentials_ = credentials;
263 VLOG(1) << "Connecting to the remote server with provided credentials";
264 DoCurlServerConnection();
265}
266
267bool ProxyConnectJob::AreAuthCredentialsRequired(CURL* easyhandle) {
268 if (http_response_code_ != kHttpCodeProxyAuthRequired) {
269 return false;
270 }
271
272 CURLcode res;
273 int64_t server_proxy_auth_scheme = 0;
274 res = curl_easy_getinfo(easyhandle, CURLINFO_PROXYAUTH_AVAIL,
275 &server_proxy_auth_scheme);
276 if (res != CURLE_OK || !server_proxy_auth_scheme) {
277 return false;
278 }
279
280 // If kerberos is enabled, then we need to wait for the user to request a
281 // kerberos ticket from Chrome.
282 return !(server_proxy_auth_scheme & CURLAUTH_NEGOTIATE);
283}
284
285void ProxyConnectJob::DoCurlServerConnection() {
286 DCHECK(!proxy_servers_.empty());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100287 CURL* easyhandle = curl_easy_init();
288 CURLcode res;
Andreea Costinasa2246592020-04-12 23:24:01 +0200289 curl_socket_t newSocket = -1;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100290
291 if (!easyhandle) {
292 // Unfortunately it's not possible to get the failure reason.
293 LOG(ERROR) << *this << " Failure to create curl handle.";
294 curl_easy_cleanup(easyhandle);
295 OnError(kHttpInternalServerError);
296 return;
297 }
298 curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str());
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200299 std::vector<char> http_response_headers;
300 std::vector<char> http_response_body;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200301
302 if (proxy_servers_.front().c_str() != brillo::http::kDirectProxy) {
303 curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_servers_.front().c_str());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100304 curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
305 curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1);
306 // Allow libcurl to pick authentication method. Curl will use the most
307 // secure one the remote site claims to support.
308 curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
309 curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str());
310 }
311 curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
312 kCurlConnectTimeout.InMilliseconds());
Andreea Costinasa2246592020-04-12 23:24:01 +0200313 curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200314 curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &http_response_headers);
Andreea Costinasa2246592020-04-12 23:24:01 +0200315 curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200316 curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &http_response_body);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100317
318 res = curl_easy_perform(easyhandle);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200319 curl_easy_getinfo(easyhandle, CURLINFO_HTTP_CONNECTCODE,
320 &http_response_code_);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100321
322 if (res != CURLE_OK) {
Andreea Costinas90b71642020-06-12 10:18:25 +0200323 LOG(ERROR) << *this << " curl_easy_perform() failed with error: "
324 << curl_easy_strerror(res);
Andreea Costinased9e6122020-08-12 12:06:19 +0200325 if (AreAuthCredentialsRequired(easyhandle)) {
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200326 AuthenticationRequired(http_response_headers);
327 curl_easy_cleanup(easyhandle);
328 return;
329 }
Andreea Costinased9e6122020-08-12 12:06:19 +0200330 credentials_request_timeout_callback_.Cancel();
331
Andreea Costinase45d54b2020-03-10 09:21:14 +0100332 curl_easy_cleanup(easyhandle);
Andreea Costinasa2246592020-04-12 23:24:01 +0200333
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200334 SendHttpResponseToClient(/* http_response_headers= */ {},
335 /* http_response_body= */ {});
336 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100337 return;
338 }
Andreea Costinased9e6122020-08-12 12:06:19 +0200339 credentials_request_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100340 // Extract the socket from the curl handle.
341 res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket);
342 if (res != CURLE_OK) {
343 LOG(ERROR) << *this << " Failed to get socket from curl with error: "
344 << curl_easy_strerror(res);
345 curl_easy_cleanup(easyhandle);
346 OnError(kHttpBadGateway);
347 return;
348 }
349
350 ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle());
351 auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket),
352 std::move(scoped_handle));
353
354 // Send the server reply to the client. If the connection is successful, the
Andreea Costinasa2246592020-04-12 23:24:01 +0200355 // reply headers should be "HTTP/1.1 200 Connection Established".
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200356 if (!SendHttpResponseToClient(http_response_headers, http_response_body)) {
357 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinasa2246592020-04-12 23:24:01 +0200358 return;
359 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100360
Andreea-Elena Costinasfae5c152020-09-28 18:18:31 +0000361 auto fwd = std::make_unique<patchpanel::SocketForwarder>(
362 base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()),
363 std::move(client_socket_), std::move(server_conn));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100364 // Start forwarding data between sockets.
365 fwd->Start();
366 std::move(setup_finished_callback_).Run(std::move(fwd), this);
367}
368
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200369bool ProxyConnectJob::SendHttpResponseToClient(
370 const std::vector<char>& http_response_headers,
371 const std::vector<char>& http_response_body) {
372 if (http_response_code_ == 0) {
373 // No HTTP CONNECT response code is available.
374 return client_socket_->SendTo(kHttpInternalServerError.data(),
375 kHttpInternalServerError.size());
376 }
377
378 if (http_response_code_ == kHttpCodeProxyAuthRequired) {
379 // This will be a hint for the user to authenticate via the Browser or
380 // acquire a Kerberos ticket.
381 return client_socket_->SendTo(kHttpProxyAuthRequired.data(),
382 kHttpProxyAuthRequired.size());
383 }
384
385 if (http_response_code_ >= 400) {
386 VLOG(1) << "Failed to set up HTTP tunnel with code " << http_response_code_;
387 std::string http_error = base::StringPrintf(
388 kHttpErrorTunnelFailed, std::to_string(http_response_code_).c_str());
389 return client_socket_->SendTo(http_error.c_str(), http_error.size());
390 }
391
392 if (http_response_headers.empty()) {
393 return client_socket_->SendTo(kHttpInternalServerError.data(),
394 kHttpInternalServerError.size());
395 }
396
397 VLOG(1) << "Sending server reply to client";
398 if (!client_socket_->SendTo(http_response_headers.data(),
399 http_response_headers.size())) {
400 PLOG(ERROR) << "Failed to send HTTP server response headers to client";
401 return false;
402 }
403 if (!http_response_body.empty()) {
404 if (!client_socket_->SendTo(http_response_body.data(),
405 http_response_body.size())) {
406 PLOG(ERROR) << "Failed to send HTTP server response payload to client";
407 return false;
408 }
409 }
410 return true;
411}
412
Andreea Costinase45d54b2020-03-10 09:21:14 +0100413void ProxyConnectJob::OnError(const std::string_view& http_error_message) {
414 client_socket_->SendTo(http_error_message.data(), http_error_message.size());
415 std::move(setup_finished_callback_).Run(nullptr, this);
416}
417
Andreea Costinas08a5d182020-04-29 22:12:47 +0200418void ProxyConnectJob::OnClientConnectTimeout() {
419 // Stop listening for client connect requests.
420 read_watcher_.reset();
421 LOG(ERROR) << *this
422 << " Connection timed out while waiting for the client to send a "
423 "connect request.";
424 OnError(kHttpConnectionTimeout);
425}
426
Andreea Costinased9e6122020-08-12 12:06:19 +0200427void ProxyConnectJob::OnAuthenticationTimeout() {
428 LOG(ERROR)
429 << *this
430 << "The connect job timed out while waiting for proxy authentication "
431 "credentials";
432 OnError(kHttpProxyAuthRequired);
433}
434
Andreea Costinase45d54b2020-03-10 09:21:14 +0100435std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) {
436 stream << "{fd: " << job.client_socket_->fd();
437 if (!job.target_url_.empty()) {
438 stream << ", url: " << job.target_url_;
439 }
440 stream << "}";
441 return stream;
442}
443
444} // namespace system_proxy