Make the toy QUIC server send responses of arbitrary length based a number specified in the path.

gfe-relnote: n/a - Tools-only code
PiperOrigin-RevId: 274840831
Change-Id: Ia22584f9ae202896f268a47172e15e8d1a9a75f8
diff --git a/quic/tools/quic_memory_cache_backend.cc b/quic/tools/quic_memory_cache_backend.cc
index fb3dd1f..9fe9dc5 100644
--- a/quic/tools/quic_memory_cache_backend.cc
+++ b/quic/tools/quic_memory_cache_backend.cc
@@ -136,6 +136,15 @@
 
   auto it = responses_.find(GetKey(host, path));
   if (it == responses_.end()) {
+    uint64_t ignored = 0;
+    if (generate_bytes_response_) {
+      if (QuicTextUtils::StringToUint64(
+              QuicStringPiece(&path[1], path.size() - 1), &ignored)) {
+        // The actual parsed length is ignored here and will be recomputed
+        // by the caller.
+        return generate_bytes_response_.get();
+      }
+    }
     QUIC_DVLOG(1) << "Get response for resource failed: host " << host
                   << " path " << path;
     if (default_response_) {
@@ -271,10 +280,23 @@
     MaybeAddServerPushResources(resource_file->host(), resource_file->path(),
                                 push_resources);
   }
+
   cache_initialized_ = true;
   return true;
 }
 
+void QuicMemoryCacheBackend::GenerateDynamicResponses() {
+  QuicWriterMutexLock lock(&response_mutex_);
+  // Add a generate bytes response.
+  spdy::SpdyHeaderBlock response_headers;
+  response_headers[":version"] = "HTTP/1.1";
+  response_headers[":status"] = "200";
+  generate_bytes_response_ = std::make_unique<QuicBackendResponse>();
+  generate_bytes_response_->set_headers(std::move(response_headers));
+  generate_bytes_response_->set_response_type(
+      QuicBackendResponse::GENERATE_BYTES);
+}
+
 bool QuicMemoryCacheBackend::IsBackendInitialized() const {
   return cache_initialized_;
 }