rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 1 | // 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 | |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 7 | #include <utility> |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 8 | #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" |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 13 | #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 | |
| 16 | DEFINE_QUIC_COMMAND_LINE_FLAG(int32_t, |
| 17 | port, |
| 18 | 6121, |
| 19 | "The port the quic server will listen on."); |
| 20 | |
| 21 | DEFINE_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 | |
rch | 7bd5476 | 2019-10-15 10:53:24 -0700 | [diff] [blame] | 29 | DEFINE_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 | |
rch | a702be2 | 2019-08-30 15:20:12 -0700 | [diff] [blame] | 36 | DEFINE_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."); |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 41 | |
| 42 | namespace quic { |
| 43 | |
| 44 | std::unique_ptr<quic::QuicSimpleServerBackend> |
| 45 | QuicToyServer::MemoryCacheBackendFactory::CreateBackend() { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 46 | auto memory_cache_backend = std::make_unique<QuicMemoryCacheBackend>(); |
rch | 7bd5476 | 2019-10-15 10:53:24 -0700 | [diff] [blame] | 47 | if (GetQuicFlag(FLAGS_generate_dynamic_responses)) { |
| 48 | memory_cache_backend->GenerateDynamicResponses(); |
| 49 | } |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 50 | 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 | |
| 57 | QuicToyServer::QuicToyServer(BackendFactory* backend_factory, |
| 58 | ServerFactory* server_factory) |
| 59 | : backend_factory_(backend_factory), server_factory_(server_factory) {} |
| 60 | |
| 61 | int QuicToyServer::Start() { |
dschinazi | c5589bd | 2019-09-12 14:50:11 -0700 | [diff] [blame] | 62 | ParsedQuicVersionVector supported_versions; |
rch | a702be2 | 2019-08-30 15:20:12 -0700 | [diff] [blame] | 63 | if (GetQuicFlag(FLAGS_quic_ietf_draft)) { |
| 64 | QuicVersionInitializeSupportForIetfDraft(); |
dschinazi | c4bde0b | 2019-11-04 10:09:12 -0800 | [diff] [blame] | 65 | supported_versions = {ParsedQuicVersion(PROTOCOL_TLS1_3, QUIC_VERSION_99)}; |
dschinazi | c5589bd | 2019-09-12 14:50:11 -0700 | [diff] [blame] | 66 | } else { |
| 67 | supported_versions = AllSupportedVersions(); |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 68 | } |
dschinazi | c4bde0b | 2019-11-04 10:09:12 -0800 | [diff] [blame] | 69 | for (const auto& version : supported_versions) { |
| 70 | QuicEnableVersion(version); |
| 71 | } |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 72 | auto proof_source = quic::CreateDefaultProofSource(); |
| 73 | auto backend = backend_factory_->CreateBackend(); |
dschinazi | c5589bd | 2019-09-12 14:50:11 -0700 | [diff] [blame] | 74 | auto server = server_factory_->CreateServer( |
| 75 | backend.get(), std::move(proof_source), supported_versions); |
rch | 034c98c | 2019-05-17 15:46:09 -0700 | [diff] [blame] | 76 | |
| 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 |