forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf_http_request_base.cc
82 lines (70 loc) · 2.86 KB
/
protobuf_http_request_base.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/base/protobuf_http_request_base.h"
#include "net/base/net_errors.h"
#include "remoting/base/protobuf_http_request_config.h"
#include "remoting/base/scoped_protobuf_http_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
namespace remoting {
ProtobufHttpRequestBase::ProtobufHttpRequestBase(
std::unique_ptr<ProtobufHttpRequestConfig> config)
: config_(std::move(config)) {
config_->Validate();
}
ProtobufHttpRequestBase::~ProtobufHttpRequestBase() {
#if DCHECK_IS_ON()
DCHECK(request_deadline_.is_null() ||
request_deadline_ >= base::TimeTicks::Now())
<< "The request must have been deleted before the deadline.";
#endif // DCHECK_IS_ON()
}
std::unique_ptr<ScopedProtobufHttpRequest>
ProtobufHttpRequestBase::CreateScopedRequest() {
return std::make_unique<ScopedProtobufHttpRequest>(base::BindOnce(
&ProtobufHttpRequestBase::Invalidate, weak_factory_.GetWeakPtr()));
}
ProtobufHttpStatus ProtobufHttpRequestBase::GetUrlLoaderStatus() const {
net::Error net_error = static_cast<net::Error>(url_loader_->NetError());
if (net_error != net::Error::OK &&
net_error != net::Error::ERR_HTTP_RESPONSE_CODE_FAILURE) {
return ProtobufHttpStatus(net_error);
}
// Depending on the configuration, url_loader_->NetError() can be OK even if
// the error code is 4xx or 5xx.
if (!url_loader_->ResponseInfo() || !url_loader_->ResponseInfo()->headers ||
url_loader_->ResponseInfo()->headers->response_code() <= 0) {
return ProtobufHttpStatus(
ProtobufHttpStatus::Code::INTERNAL,
"Failed to get HTTP status from the response header.");
}
return ProtobufHttpStatus(static_cast<net::HttpStatusCode>(
url_loader_->ResponseInfo()->headers->response_code()));
}
void ProtobufHttpRequestBase::StartRequest(
network::mojom::URLLoaderFactory* loader_factory,
std::unique_ptr<network::SimpleURLLoader> url_loader,
base::OnceClosure invalidator) {
DCHECK(!url_loader_);
DCHECK(!invalidator_);
url_loader_ = std::move(url_loader);
invalidator_ = std::move(invalidator);
StartRequestInternal(loader_factory);
#if DCHECK_IS_ON()
base::TimeDelta timeout_duration = GetRequestTimeoutDuration();
if (!timeout_duration.is_zero()) {
// Add a 500ms fuzz to account for task dispatching delay and other stuff.
request_deadline_ =
base::TimeTicks::Now() + timeout_duration + base::Milliseconds(500);
}
#endif // DCHECK_IS_ON()
}
void ProtobufHttpRequestBase::Invalidate() {
// This is not necessarily true if the request has never been added to a
// client.
if (invalidator_) {
std::move(invalidator_).Run();
}
}
} // namespace remoting