Improve MASQUE debugging information

This code is not used in production.

PiperOrigin-RevId: 360292410
Change-Id: I788988d119b9d6b3066dae043f2d890b87e39849
diff --git a/quic/masque/masque_client_session.cc b/quic/masque/masque_client_session.cc
index 79ab509..ccbe32a 100644
--- a/quic/masque/masque_client_session.cc
+++ b/quic/masque/masque_client_session.cc
@@ -6,6 +6,7 @@
 #include "absl/algorithm/container.h"
 #include "quic/core/http/spdy_utils.h"
 #include "quic/core/quic_data_reader.h"
+#include "quic/core/quic_utils.h"
 #include "common/platform/api/quiche_text_utils.h"
 
 namespace quic {
@@ -139,7 +140,8 @@
 
   QUIC_DVLOG(1) << "Sent packet to " << target_server_address
                 << " compressed with flow ID " << flow_id
-                << " and got message status " << message_status;
+                << " and got message status "
+                << MessageStatusToString(message_status);
 }
 
 void MasqueClientSession::RegisterConnectionId(
@@ -198,6 +200,16 @@
 }
 
 void MasqueClientSession::OnStreamClosed(QuicStreamId stream_id) {
+  if (QuicUtils::IsBidirectionalStreamId(stream_id, version()) &&
+      QuicUtils::IsClientInitiatedStreamId(transport_version(), stream_id)) {
+    QuicSpdyClientStream* stream =
+        reinterpret_cast<QuicSpdyClientStream*>(GetActiveStream(stream_id));
+    if (stream != nullptr) {
+      QUIC_DLOG(INFO) << "Stream " << stream_id
+                      << " closed, got response headers:"
+                      << stream->response_headers().DebugString();
+    }
+  }
   for (auto it = connect_udp_client_states_.begin();
        it != connect_udp_client_states_.end();) {
     if (it->stream()->id() == stream_id) {
diff --git a/quic/masque/masque_server_backend.cc b/quic/masque/masque_server_backend.cc
index 64c36e1..6c7f7e1 100644
--- a/quic/masque/masque_server_backend.cc
+++ b/quic/masque/masque_server_backend.cc
@@ -21,20 +21,20 @@
     const spdy::Http2HeaderBlock& request_headers,
     const std::string& request_body,
     QuicSimpleServerBackend::RequestHandler* request_handler) {
-  auto path_pair = request_headers.find(":path");
-  auto method_pair = request_headers.find(":method");
-  auto scheme_pair = request_headers.find(":scheme");
-  if (path_pair == request_headers.end() ||
-      method_pair == request_headers.end() ||
-      scheme_pair == request_headers.end()) {
-    // This request is missing required headers.
-    return false;
-  }
-  absl::string_view path = path_pair->second;
-  absl::string_view scheme = scheme_pair->second;
-  absl::string_view method = method_pair->second;
   std::string masque_path = "";
   if (masque_mode_ == MasqueMode::kLegacy) {
+    auto path_pair = request_headers.find(":path");
+    auto method_pair = request_headers.find(":method");
+    auto scheme_pair = request_headers.find(":scheme");
+    if (path_pair == request_headers.end() ||
+        method_pair == request_headers.end() ||
+        scheme_pair == request_headers.end()) {
+      // This request is missing required headers.
+      return false;
+    }
+    absl::string_view path = path_pair->second;
+    absl::string_view scheme = scheme_pair->second;
+    absl::string_view method = method_pair->second;
     if (scheme != "https" || method != "POST" || request_body.empty()) {
       // MASQUE requests MUST be a non-empty https POST.
       return false;
@@ -45,11 +45,6 @@
       return false;
     }
     masque_path = std::string(path.substr(sizeof("/.well-known/masque/") - 1));
-  } else {
-    if (method != "CONNECT-UDP") {
-      // Unexpected method.
-      return false;
-    }
   }
 
   if (!server_authority_.empty()) {
diff --git a/quic/masque/masque_server_session.cc b/quic/masque/masque_server_session.cc
index 4f065ee..9b291a7 100644
--- a/quic/masque/masque_server_session.cc
+++ b/quic/masque/masque_server_session.cc
@@ -60,13 +60,13 @@
 
 std::unique_ptr<QuicBackendResponse> CreateBackendErrorResponse(
     absl::string_view status,
-    absl::string_view body) {
+    absl::string_view error_details) {
   spdy::Http2HeaderBlock response_headers;
   response_headers[":status"] = status;
+  response_headers["masque-debug-info"] = error_details;
   auto response = std::make_unique<QuicBackendResponse>();
   response->set_response_type(QuicBackendResponse::REGULAR_RESPONSE);
   response->set_headers(std::move(response_headers));
-  response->set_body(body);
   return response;
 }
 
@@ -177,12 +177,21 @@
     auto scheme_pair = request_headers.find(":scheme");
     auto method_pair = request_headers.find(":method");
     auto authority_pair = request_headers.find(":authority");
-    if (path_pair == request_headers.end() ||
-        scheme_pair == request_headers.end() ||
-        method_pair == request_headers.end() ||
-        authority_pair == request_headers.end()) {
-      QUIC_DLOG(ERROR) << "MASQUE request is missing required headers";
-      return CreateBackendErrorResponse("400", "Missing required headers");
+    if (path_pair == request_headers.end()) {
+      QUIC_DLOG(ERROR) << "MASQUE request is missing :path";
+      return CreateBackendErrorResponse("400", "Missing :path");
+    }
+    if (scheme_pair == request_headers.end()) {
+      QUIC_DLOG(ERROR) << "MASQUE request is missing :scheme";
+      return CreateBackendErrorResponse("400", "Missing :scheme");
+    }
+    if (method_pair == request_headers.end()) {
+      QUIC_DLOG(ERROR) << "MASQUE request is missing :method";
+      return CreateBackendErrorResponse("400", "Missing :method");
+    }
+    if (authority_pair == request_headers.end()) {
+      QUIC_DLOG(ERROR) << "MASQUE request is missing :authority";
+      return CreateBackendErrorResponse("400", "Missing :authority");
     }
     absl::string_view path = path_pair->second;
     absl::string_view scheme = scheme_pair->second;
@@ -380,9 +389,10 @@
     MessageStatus message_status = SendHttp3Datagram(
         flow_id, absl::string_view(read_result.packet_buffer.buffer,
                                    read_result.packet_buffer.buffer_len));
-    QUIC_DVLOG(1) << "Sent UDP packet from target server of length "
-                  << read_result.packet_buffer.buffer_len << " with flow ID "
-                  << flow_id << " and got message status " << message_status;
+    QUIC_DVLOG(1) << "Sent UDP packet from " << expected_target_server_address
+                  << " of length " << read_result.packet_buffer.buffer_len
+                  << " with flow ID " << flow_id << " and got message status "
+                  << MessageStatusToString(message_status);
   }
 }
 
@@ -467,7 +477,8 @@
   packet_info.SetPeerAddress(target_server_address_);
   WriteResult write_result = socket_api.WritePacket(
       fd_, payload.data(), payload.length(), packet_info);
-  QUIC_DVLOG(1) << "Wrote packet to server with result " << write_result;
+  QUIC_DVLOG(1) << "Wrote packet of length " << payload.length() << " to "
+                << target_server_address_ << " with result " << write_result;
 }
 
 }  // namespace quic