xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/tools/quic_backend_response.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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 #ifndef QUICHE_QUIC_TOOLS_QUIC_BACKEND_RESPONSE_H_
6 #define QUICHE_QUIC_TOOLS_QUIC_BACKEND_RESPONSE_H_
7 
8 #include "absl/strings/string_view.h"
9 #include "quiche/quic/core/quic_time.h"
10 #include "quiche/spdy/core/http2_header_block.h"
11 
12 namespace quic {
13 
14 // Container for HTTP response header/body pairs
15 // fetched by the QuicSimpleServerBackend
16 class QuicBackendResponse {
17  public:
18   enum SpecialResponseType {
19     REGULAR_RESPONSE,      // Send the headers and body like a server should.
20     CLOSE_CONNECTION,      // Close the connection (sending the close packet).
21     IGNORE_REQUEST,        // Do nothing, expect the client to time out.
22     BACKEND_ERR_RESPONSE,  // There was an error fetching the response from
23                            // the backend, for example as a TCP connection
24                            // error.
25     INCOMPLETE_RESPONSE,   // The server will act as if there is a non-empty
26                            // trailer but it will not be sent, as a result, FIN
27                            // will not be sent too.
28     GENERATE_BYTES         // Sends a response with a length equal to the number
29                            // of bytes in the URL path.
30   };
31   QuicBackendResponse();
32 
33   QuicBackendResponse(const QuicBackendResponse& other) = delete;
34   QuicBackendResponse& operator=(const QuicBackendResponse& other) = delete;
35 
36   ~QuicBackendResponse();
37 
early_hints()38   const std::vector<spdy::Http2HeaderBlock>& early_hints() const {
39     return early_hints_;
40   }
response_type()41   SpecialResponseType response_type() const { return response_type_; }
headers()42   const spdy::Http2HeaderBlock& headers() const { return headers_; }
trailers()43   const spdy::Http2HeaderBlock& trailers() const { return trailers_; }
body()44   const absl::string_view body() const { return absl::string_view(body_); }
45 
AddEarlyHints(const spdy::Http2HeaderBlock & headers)46   void AddEarlyHints(const spdy::Http2HeaderBlock& headers) {
47     spdy::Http2HeaderBlock hints = headers.Clone();
48     hints[":status"] = "103";
49     early_hints_.push_back(std::move(hints));
50   }
51 
set_response_type(SpecialResponseType response_type)52   void set_response_type(SpecialResponseType response_type) {
53     response_type_ = response_type;
54   }
55 
set_headers(spdy::Http2HeaderBlock headers)56   void set_headers(spdy::Http2HeaderBlock headers) {
57     headers_ = std::move(headers);
58   }
set_trailers(spdy::Http2HeaderBlock trailers)59   void set_trailers(spdy::Http2HeaderBlock trailers) {
60     trailers_ = std::move(trailers);
61   }
set_body(absl::string_view body)62   void set_body(absl::string_view body) {
63     body_.assign(body.data(), body.size());
64   }
65 
66   // This would simulate a delay before sending the response
67   // back to the client. Intended for testing purposes.
set_delay(QuicTime::Delta delay)68   void set_delay(QuicTime::Delta delay) { delay_ = delay; }
delay()69   QuicTime::Delta delay() const { return delay_; }
70 
71  private:
72   std::vector<spdy::Http2HeaderBlock> early_hints_;
73   SpecialResponseType response_type_;
74   spdy::Http2HeaderBlock headers_;
75   spdy::Http2HeaderBlock trailers_;
76   std::string body_;
77   QuicTime::Delta delay_;
78 };
79 
80 }  // namespace quic
81 
82 #endif  // QUICHE_QUIC_TOOLS_QUIC_BACKEND_RESPONSE_H_
83