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 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_default_proof_providers.h" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 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 | |
| 29 | DEFINE_QUIC_COMMAND_LINE_FLAG( |
| 30 | int32_t, |
| 31 | quic_ietf_draft, |
| 32 | 0, |
| 33 | "QUIC IETF draft number to use over the wire, e.g. 18. " |
| 34 | "This also enables required internal QUIC flags."); |
| 35 | |
| 36 | namespace quic { |
| 37 | |
| 38 | std::unique_ptr<quic::QuicSimpleServerBackend> |
| 39 | QuicToyServer::MemoryCacheBackendFactory::CreateBackend() { |
| 40 | auto memory_cache_backend = QuicMakeUnique<QuicMemoryCacheBackend>(); |
| 41 | if (!GetQuicFlag(FLAGS_quic_response_cache_dir).empty()) { |
| 42 | memory_cache_backend->InitializeBackend( |
| 43 | GetQuicFlag(FLAGS_quic_response_cache_dir)); |
| 44 | } |
| 45 | return memory_cache_backend; |
| 46 | } |
| 47 | |
| 48 | QuicToyServer::QuicToyServer(BackendFactory* backend_factory, |
| 49 | ServerFactory* server_factory) |
| 50 | : backend_factory_(backend_factory), server_factory_(server_factory) {} |
| 51 | |
| 52 | int QuicToyServer::Start() { |
| 53 | const int32_t quic_ietf_draft = GetQuicFlag(FLAGS_quic_ietf_draft); |
| 54 | if (quic_ietf_draft > 0) { |
| 55 | quic::QuicVersionInitializeSupportForIetfDraft(quic_ietf_draft); |
| 56 | quic::QuicEnableVersion( |
| 57 | quic::ParsedQuicVersion(quic::PROTOCOL_TLS1_3, quic::QUIC_VERSION_99)); |
| 58 | } |
| 59 | auto proof_source = quic::CreateDefaultProofSource(); |
| 60 | auto backend = backend_factory_->CreateBackend(); |
| 61 | auto server = |
| 62 | server_factory_->CreateServer(backend.get(), std::move(proof_source)); |
| 63 | |
| 64 | if (!server->CreateUDPSocketAndListen(quic::QuicSocketAddress( |
| 65 | quic::QuicIpAddress::Any6(), GetQuicFlag(FLAGS_port)))) { |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | server->HandleEventsForever(); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | } // namespace quic |