1 // Copyright (c) 2018 The Chromium 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 "quiche/quic/tools/quic_simple_client_session.h"
6
7 #include <utility>
8
9 #include "quiche/quic/core/quic_path_validator.h"
10 #include "quiche/spdy/core/http2_header_block.h"
11
12 namespace quic {
13
QuicSimpleClientSession(const QuicConfig & config,const ParsedQuicVersionVector & supported_versions,QuicConnection * connection,QuicClientBase::NetworkHelper * network_helper,const QuicServerId & server_id,QuicCryptoClientConfig * crypto_config,bool drop_response_body,bool enable_web_transport)14 QuicSimpleClientSession::QuicSimpleClientSession(
15 const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
16 QuicConnection* connection, QuicClientBase::NetworkHelper* network_helper,
17 const QuicServerId& server_id, QuicCryptoClientConfig* crypto_config,
18 bool drop_response_body, bool enable_web_transport)
19 : QuicSimpleClientSession(config, supported_versions, connection,
20 /*visitor=*/nullptr, network_helper, server_id,
21 crypto_config, drop_response_body,
22 enable_web_transport) {}
23
QuicSimpleClientSession(const QuicConfig & config,const ParsedQuicVersionVector & supported_versions,QuicConnection * connection,QuicSession::Visitor * visitor,QuicClientBase::NetworkHelper * network_helper,const QuicServerId & server_id,QuicCryptoClientConfig * crypto_config,bool drop_response_body,bool enable_web_transport)24 QuicSimpleClientSession::QuicSimpleClientSession(
25 const QuicConfig& config, const ParsedQuicVersionVector& supported_versions,
26 QuicConnection* connection, QuicSession::Visitor* visitor,
27 QuicClientBase::NetworkHelper* network_helper,
28 const QuicServerId& server_id, QuicCryptoClientConfig* crypto_config,
29 bool drop_response_body, bool enable_web_transport)
30 : QuicSpdyClientSession(config, supported_versions, connection, visitor,
31 server_id, crypto_config),
32 network_helper_(network_helper),
33 drop_response_body_(drop_response_body),
34 enable_web_transport_(enable_web_transport) {}
35
36 std::unique_ptr<QuicSpdyClientStream>
CreateClientStream()37 QuicSimpleClientSession::CreateClientStream() {
38 auto stream = std::make_unique<QuicSimpleClientStream>(
39 GetNextOutgoingBidirectionalStreamId(), this, BIDIRECTIONAL,
40 drop_response_body_);
41 stream->set_on_interim_headers([this](const spdy::Http2HeaderBlock& headers) {
42 on_interim_headers_(headers);
43 });
44 return stream;
45 }
46
47 WebTransportHttp3VersionSet
LocallySupportedWebTransportVersions() const48 QuicSimpleClientSession::LocallySupportedWebTransportVersions() const {
49 return enable_web_transport_ ? kDefaultSupportedWebTransportVersions
50 : WebTransportHttp3VersionSet();
51 }
52
LocalHttpDatagramSupport()53 HttpDatagramSupport QuicSimpleClientSession::LocalHttpDatagramSupport() {
54 return enable_web_transport_ ? HttpDatagramSupport::kRfcAndDraft04
55 : HttpDatagramSupport::kNone;
56 }
57
CreateContextForMultiPortPath(std::unique_ptr<MultiPortPathContextObserver> context_observer)58 void QuicSimpleClientSession::CreateContextForMultiPortPath(
59 std::unique_ptr<MultiPortPathContextObserver> context_observer) {
60 if (!network_helper_ || connection()->multi_port_stats() == nullptr) {
61 return;
62 }
63 auto self_address = connection()->self_address();
64 auto server_address = connection()->peer_address();
65 if (!network_helper_->CreateUDPSocketAndBind(
66 server_address, self_address.host(), self_address.port() + 1)) {
67 return;
68 }
69 QuicPacketWriter* writer = network_helper_->CreateQuicPacketWriter();
70 if (writer == nullptr) {
71 return;
72 }
73 context_observer->OnMultiPortPathContextAvailable(
74 std::make_unique<PathMigrationContext>(
75 std::unique_ptr<QuicPacketWriter>(writer),
76 network_helper_->GetLatestClientAddress(), peer_address()));
77 }
78
MigrateToMultiPortPath(std::unique_ptr<QuicPathValidationContext> context)79 void QuicSimpleClientSession::MigrateToMultiPortPath(
80 std::unique_ptr<QuicPathValidationContext> context) {
81 auto* path_migration_context =
82 static_cast<PathMigrationContext*>(context.get());
83 MigratePath(path_migration_context->self_address(),
84 path_migration_context->peer_address(),
85 path_migration_context->ReleaseWriter(), /*owns_writer=*/true);
86 }
87
88 } // namespace quic
89