xref: /aosp_15_r20/external/cronet/net/tools/quic/quic_simple_client_bin.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // A binary wrapper for QuicClient.
6 // Connects to a host using QUIC, sends a request to the provided URL, and
7 // displays the response.
8 //
9 // Some usage examples:
10 //
11 // Standard request/response:
12 //   quic_client http://www.google.com
13 //   quic_client http://www.google.com --quiet
14 //   quic_client https://www.google.com --port=443
15 //
16 // Use a specific version:
17 //   quic_client http://www.google.com --quic_version=23
18 //
19 // Send a POST instead of a GET:
20 //   quic_client http://www.google.com --body="this is a POST body"
21 //
22 // Append additional headers to the request:
23 //   quic_client http://www.google.com  --host=${IP}
24 //               --headers="Header-A: 1234; Header-B: 5678"
25 //
26 // Connect to a host different to the URL being requested:
27 //   quic_client mail.google.com --host=www.google.com
28 //
29 // Connect to a specific IP:
30 //   IP=`dig www.google.com +short | head -1`
31 //   quic_client www.google.com --host=${IP}
32 //
33 // Try to connect to a host which does not speak QUIC:
34 //   quic_client http://www.example.com
35 
36 #include "base/logging.h"
37 #include "base/ranges/algorithm.h"
38 #include "net/base/address_family.h"
39 #include "net/base/net_errors.h"
40 #include "net/quic/address_utils.h"
41 #include "net/third_party/quiche/src/quiche/common/platform/api/quiche_command_line_flags.h"
42 #include "net/third_party/quiche/src/quiche/common/platform/api/quiche_system_event_loop.h"
43 #include "net/third_party/quiche/src/quiche/quic/core/quic_error_codes.h"
44 #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
45 #include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
46 #include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
47 #include "net/third_party/quiche/src/quiche/quic/platform/api/quic_socket_address.h"
48 #include "net/third_party/quiche/src/quiche/quic/tools/quic_toy_client.h"
49 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
50 #include "net/tools/quic/quic_simple_client.h"
51 #include "net/tools/quic/synchronous_host_resolver.h"
52 #include "url/scheme_host_port.h"
53 #include "url/url_constants.h"
54 
55 using quic::ProofVerifier;
56 
57 namespace {
58 
59 class QuicSimpleClientFactory : public quic::QuicToyClient::ClientFactory {
60  public:
CreateClient(std::string host_for_handshake,std::string host_for_lookup,int address_family_for_lookup,uint16_t port,quic::ParsedQuicVersionVector versions,const quic::QuicConfig & config,std::unique_ptr<quic::ProofVerifier> verifier,std::unique_ptr<quic::SessionCache>)61   std::unique_ptr<quic::QuicSpdyClientBase> CreateClient(
62       std::string host_for_handshake,
63       std::string host_for_lookup,
64       int address_family_for_lookup,
65       uint16_t port,
66       quic::ParsedQuicVersionVector versions,
67       const quic::QuicConfig& config,
68       std::unique_ptr<quic::ProofVerifier> verifier,
69       std::unique_ptr<quic::SessionCache> /*session_cache*/) override {
70     // Determine IP address to connect to from supplied hostname.
71     quic::QuicIpAddress ip_addr;
72     if (!ip_addr.FromString(host_for_lookup)) {
73       net::AddressList addresses;
74       // TODO(https://crbug.com/1300660) Let the caller pass in the scheme
75       // rather than guessing "https"
76       int rv = net::SynchronousHostResolver::Resolve(
77           url::SchemeHostPort(url::kHttpsScheme, host_for_lookup, port),
78           &addresses);
79       if (rv != net::OK) {
80         LOG(ERROR) << "Unable to resolve '" << host_for_lookup
81                    << "' : " << net::ErrorToShortString(rv);
82         return nullptr;
83       }
84       const auto endpoint = base::ranges::find_if(
85           addresses,
86           [address_family_for_lookup](net::AddressFamily family) {
87             if (address_family_for_lookup == AF_INET)
88               return family == net::AddressFamily::ADDRESS_FAMILY_IPV4;
89             if (address_family_for_lookup == AF_INET6)
90               return family == net::AddressFamily::ADDRESS_FAMILY_IPV6;
91             return address_family_for_lookup == AF_UNSPEC;
92           },
93           &net::IPEndPoint::GetFamily);
94       if (endpoint == addresses.end()) {
95         LOG(ERROR) << "No results for '" << host_for_lookup
96                    << "' with appropriate address family";
97         return nullptr;
98       }
99       // Arbitrarily select the first result with a matching address family,
100       // ignoring any subsequent matches.
101       ip_addr = net::ToQuicIpAddress(endpoint->address());
102       port = endpoint->port();
103     }
104 
105     quic::QuicServerId server_id(host_for_handshake, port, false);
106     return std::make_unique<net::QuicSimpleClient>(
107         quic::QuicSocketAddress(ip_addr, port), server_id, versions, config,
108         std::move(verifier));
109   }
110 };
111 
112 }  // namespace
113 
main(int argc,char * argv[])114 int main(int argc, char* argv[]) {
115   quiche::QuicheSystemEventLoop event_loop("quic_client");
116   const char* usage = "Usage: quic_client [options] <url>";
117 
118   // All non-flag arguments should be interpreted as URLs to fetch.
119   std::vector<std::string> urls =
120       quiche::QuicheParseCommandLineFlags(usage, argc, argv);
121   if (urls.size() != 1) {
122     quiche::QuichePrintCommandLineFlagHelp(usage);
123     exit(0);
124   }
125 
126   QuicSimpleClientFactory factory;
127   quic::QuicToyClient client(&factory);
128   return client.SendRequestsAndPrintResponses(urls);
129 }
130