• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/quic/tools/quic_url.h"
11 #include "quiche/spdy/core/http2_header_block.h"
12 #include "quiche/spdy/core/spdy_protocol.h"
13 
14 namespace quic {
15 
16 // Container for HTTP response header/body pairs
17 // fetched by the QuicSimpleServerBackend
18 class QuicBackendResponse {
19  public:
20   // A ServerPushInfo contains path of the push request and everything needed in
21   // comprising a response for the push request.
22   // TODO(b/171463363): Remove.
23   struct ServerPushInfo {
24     ServerPushInfo(QuicUrl request_url, spdy::Http2HeaderBlock headers,
25                    spdy::SpdyPriority priority, std::string body);
26     ServerPushInfo(const ServerPushInfo& other);
27 
28     QuicUrl request_url;
29     spdy::Http2HeaderBlock headers;
30     spdy::SpdyPriority priority;
31     std::string body;
32   };
33 
34   enum SpecialResponseType {
35     REGULAR_RESPONSE,      // Send the headers and body like a server should.
36     CLOSE_CONNECTION,      // Close the connection (sending the close packet).
37     IGNORE_REQUEST,        // Do nothing, expect the client to time out.
38     BACKEND_ERR_RESPONSE,  // There was an error fetching the response from
39                            // the backend, for example as a TCP connection
40                            // error.
41     INCOMPLETE_RESPONSE,   // The server will act as if there is a non-empty
42                            // trailer but it will not be sent, as a result, FIN
43                            // will not be sent too.
44     GENERATE_BYTES         // Sends a response with a length equal to the number
45                            // of bytes in the URL path.
46   };
47   QuicBackendResponse();
48 
49   QuicBackendResponse(const QuicBackendResponse& other) = delete;
50   QuicBackendResponse& operator=(const QuicBackendResponse& other) = delete;
51 
52   ~QuicBackendResponse();
53 
early_hints()54   const std::vector<spdy::Http2HeaderBlock>& early_hints() const {
55     return early_hints_;
56   }
response_type()57   SpecialResponseType response_type() const { return response_type_; }
headers()58   const spdy::Http2HeaderBlock& headers() const { return headers_; }
trailers()59   const spdy::Http2HeaderBlock& trailers() const { return trailers_; }
body()60   const absl::string_view body() const { return absl::string_view(body_); }
61 
AddEarlyHints(const spdy::Http2HeaderBlock & headers)62   void AddEarlyHints(const spdy::Http2HeaderBlock& headers) {
63     spdy::Http2HeaderBlock hints = headers.Clone();
64     hints[":status"] = "103";
65     early_hints_.push_back(std::move(hints));
66   }
67 
set_response_type(SpecialResponseType response_type)68   void set_response_type(SpecialResponseType response_type) {
69     response_type_ = response_type;
70   }
71 
set_headers(spdy::Http2HeaderBlock headers)72   void set_headers(spdy::Http2HeaderBlock headers) {
73     headers_ = std::move(headers);
74   }
set_trailers(spdy::Http2HeaderBlock trailers)75   void set_trailers(spdy::Http2HeaderBlock trailers) {
76     trailers_ = std::move(trailers);
77   }
set_body(absl::string_view body)78   void set_body(absl::string_view body) {
79     body_.assign(body.data(), body.size());
80   }
81 
82   // This would simulate a delay before sending the response
83   // back to the client. Intended for testing purposes.
set_delay(QuicTime::Delta delay)84   void set_delay(QuicTime::Delta delay) { delay_ = delay; }
delay()85   QuicTime::Delta delay() const { return delay_; }
86 
87  private:
88   std::vector<spdy::Http2HeaderBlock> early_hints_;
89   SpecialResponseType response_type_;
90   spdy::Http2HeaderBlock headers_;
91   spdy::Http2HeaderBlock trailers_;
92   std::string body_;
93   QuicTime::Delta delay_;
94 };
95 
96 }  // namespace quic
97 
98 #endif  // QUICHE_QUIC_TOOLS_QUIC_BACKEND_RESPONSE_H_
99