blob: 390dcf6aa0cc0f85296e9497b7052260fc141428 [file] [log] [blame]
rch034c98c2019-05-17 15:46:09 -07001// Copyright 2014 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 "net/third_party/quiche/src/quic/tools/quic_toy_server.h"
6
bnc463f2352019-10-10 04:49:34 -07007#include <utility>
rch034c98c2019-05-17 15:46:09 -07008#include <vector>
9
10#include "net/third_party/quiche/src/quic/core/quic_versions.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_default_proof_providers.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
rch034c98c2019-05-17 15:46:09 -070013#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
14#include "net/third_party/quiche/src/quic/tools/quic_memory_cache_backend.h"
15
16DEFINE_QUIC_COMMAND_LINE_FLAG(int32_t,
17 port,
18 6121,
19 "The port the quic server will listen on.");
20
21DEFINE_QUIC_COMMAND_LINE_FLAG(
22 std::string,
23 quic_response_cache_dir,
24 "",
25 "Specifies the directory used during QuicHttpResponseCache "
26 "construction to seed the cache. Cache directory can be "
27 "generated using `wget -p --save-headers <url>`");
28
rch7bd54762019-10-15 10:53:24 -070029DEFINE_QUIC_COMMAND_LINE_FLAG(
30 bool,
31 generate_dynamic_responses,
32 false,
33 "If true, then URLs which have a numeric path will send a dynamically "
34 "generated response of that many bytes.");
35
rcha702be22019-08-30 15:20:12 -070036DEFINE_QUIC_COMMAND_LINE_FLAG(bool,
37 quic_ietf_draft,
38 false,
39 "Use the IETF draft version. This also enables "
40 "required internal QUIC flags.");
rch034c98c2019-05-17 15:46:09 -070041
42namespace quic {
43
44std::unique_ptr<quic::QuicSimpleServerBackend>
45QuicToyServer::MemoryCacheBackendFactory::CreateBackend() {
vasilvv0fc587f2019-09-06 13:33:08 -070046 auto memory_cache_backend = std::make_unique<QuicMemoryCacheBackend>();
rch7bd54762019-10-15 10:53:24 -070047 if (GetQuicFlag(FLAGS_generate_dynamic_responses)) {
48 memory_cache_backend->GenerateDynamicResponses();
49 }
rch034c98c2019-05-17 15:46:09 -070050 if (!GetQuicFlag(FLAGS_quic_response_cache_dir).empty()) {
51 memory_cache_backend->InitializeBackend(
52 GetQuicFlag(FLAGS_quic_response_cache_dir));
53 }
54 return memory_cache_backend;
55}
56
57QuicToyServer::QuicToyServer(BackendFactory* backend_factory,
58 ServerFactory* server_factory)
59 : backend_factory_(backend_factory), server_factory_(server_factory) {}
60
61int QuicToyServer::Start() {
dschinazic5589bd2019-09-12 14:50:11 -070062 ParsedQuicVersionVector supported_versions;
rcha702be22019-08-30 15:20:12 -070063 if (GetQuicFlag(FLAGS_quic_ietf_draft)) {
64 QuicVersionInitializeSupportForIetfDraft();
dschinazic4bde0b2019-11-04 10:09:12 -080065 supported_versions = {ParsedQuicVersion(PROTOCOL_TLS1_3, QUIC_VERSION_99)};
dschinazic5589bd2019-09-12 14:50:11 -070066 } else {
67 supported_versions = AllSupportedVersions();
rch034c98c2019-05-17 15:46:09 -070068 }
dschinazic4bde0b2019-11-04 10:09:12 -080069 for (const auto& version : supported_versions) {
70 QuicEnableVersion(version);
71 }
rch034c98c2019-05-17 15:46:09 -070072 auto proof_source = quic::CreateDefaultProofSource();
73 auto backend = backend_factory_->CreateBackend();
dschinazic5589bd2019-09-12 14:50:11 -070074 auto server = server_factory_->CreateServer(
75 backend.get(), std::move(proof_source), supported_versions);
rch034c98c2019-05-17 15:46:09 -070076
77 if (!server->CreateUDPSocketAndListen(quic::QuicSocketAddress(
78 quic::QuicIpAddress::Any6(), GetQuicFlag(FLAGS_port)))) {
79 return 1;
80 }
81
82 server->HandleEventsForever();
83 return 0;
84}
85
86} // namespace quic