File: quic_simple_client_bin.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (129 lines) | stat: -rw-r--r-- 4,970 bytes parent folder | download | duplicates (6)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// A binary wrapper for QuicClient.
// Connects to a host using QUIC, sends a request to the provided URL, and
// displays the response.
//
// Some usage examples:
//
// Standard request/response:
//   quic_client http://www.google.com
//   quic_client http://www.google.com --quiet
//   quic_client https://www.google.com --port=443
//
// Use a specific version:
//   quic_client http://www.google.com --quic_version=23
//
// Send a POST instead of a GET:
//   quic_client http://www.google.com --body="this is a POST body"
//
// Append additional headers to the request:
//   quic_client http://www.google.com  --host=${IP}
//               --headers="Header-A: 1234; Header-B: 5678"
//
// Connect to a host different to the URL being requested:
//   quic_client mail.google.com --host=www.google.com
//
// Connect to a specific IP:
//   IP=`dig www.google.com +short | head -1`
//   quic_client www.google.com --host=${IP}
//
// Try to connect to a host which does not speak QUIC:
//   quic_client http://www.example.com

#include <algorithm>

#include "base/logging.h"
#include "net/base/address_family.h"
#include "net/base/net_errors.h"
#include "net/quic/address_utils.h"
#include "net/third_party/quiche/src/quiche/common/platform/api/quiche_command_line_flags.h"
#include "net/third_party/quiche/src/quiche/common/platform/api/quiche_system_event_loop.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_error_codes.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_server_id.h"
#include "net/third_party/quiche/src/quiche/quic/core/quic_versions.h"
#include "net/third_party/quiche/src/quiche/quic/platform/api/quic_socket_address.h"
#include "net/third_party/quiche/src/quiche/quic/tools/quic_toy_client.h"
#include "net/tools/quic/quic_simple_client.h"
#include "net/tools/quic/synchronous_host_resolver.h"
#include "url/scheme_host_port.h"
#include "url/url_constants.h"

using quic::ProofVerifier;

namespace {

class QuicSimpleClientFactory : public quic::QuicToyClient::ClientFactory {
 public:
  std::unique_ptr<quic::QuicSpdyClientBase> 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> /*session_cache*/) override {
    // Determine IP address to connect to from supplied hostname.
    quiche::QuicheIpAddress ip_addr;
    if (!ip_addr.FromString(host_for_lookup)) {
      net::AddressList addresses;
      // TODO(crbug.com/40216365) Let the caller pass in the scheme
      // rather than guessing "https"
      int rv = net::SynchronousHostResolver::Resolve(
          url::SchemeHostPort(url::kHttpsScheme, host_for_lookup, port),
          &addresses);
      if (rv != net::OK) {
        LOG(ERROR) << "Unable to resolve '" << host_for_lookup
                   << "' : " << net::ErrorToShortString(rv);
        return nullptr;
      }
      const auto endpoint = std::ranges::find_if(
          addresses,
          [address_family_for_lookup](net::AddressFamily family) {
            if (address_family_for_lookup == AF_INET)
              return family == net::AddressFamily::ADDRESS_FAMILY_IPV4;
            if (address_family_for_lookup == AF_INET6)
              return family == net::AddressFamily::ADDRESS_FAMILY_IPV6;
            return address_family_for_lookup == AF_UNSPEC;
          },
          &net::IPEndPoint::GetFamily);
      if (endpoint == addresses.end()) {
        LOG(ERROR) << "No results for '" << host_for_lookup
                   << "' with appropriate address family";
        return nullptr;
      }
      // Arbitrarily select the first result with a matching address family,
      // ignoring any subsequent matches.
      ip_addr = net::ToQuicheIpAddress(endpoint->address());
      port = endpoint->port();
    }

    quic::QuicServerId server_id(host_for_handshake, port);
    return std::make_unique<net::QuicSimpleClient>(
        quic::QuicSocketAddress(ip_addr, port), server_id, versions, config,
        std::move(verifier));
  }
};

}  // namespace

int main(int argc, char* argv[]) {
  quiche::QuicheSystemEventLoop event_loop("quic_client");
  const char* usage = "Usage: quic_client [options] <url>";

  // All non-flag arguments should be interpreted as URLs to fetch.
  std::vector<std::string> urls =
      quiche::QuicheParseCommandLineFlags(usage, argc, argv);
  if (urls.size() != 1) {
    quiche::QuichePrintCommandLineFlagHelp(usage);
    exit(0);
  }

  QuicSimpleClientFactory factory;
  quic::QuicToyClient client(&factory);
  return client.SendRequestsAndPrintResponses(urls);
}