1 // Copyright (c) 2012 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_SIMPLE_SERVER_STREAM_H_ 6 #define QUICHE_QUIC_TOOLS_QUIC_SIMPLE_SERVER_STREAM_H_ 7 8 #include <cstdint> 9 #include <optional> 10 11 #include "absl/strings/string_view.h" 12 #include "quiche/quic/core/http/quic_spdy_server_stream_base.h" 13 #include "quiche/quic/core/quic_error_codes.h" 14 #include "quiche/quic/core/quic_packets.h" 15 #include "quiche/quic/tools/quic_backend_response.h" 16 #include "quiche/quic/tools/quic_simple_server_backend.h" 17 #include "quiche/spdy/core/http2_header_block.h" 18 #include "quiche/spdy/core/spdy_framer.h" 19 20 namespace quic { 21 22 // All this does right now is aggregate data, and on fin, send an HTTP 23 // response. 24 class QuicSimpleServerStream : public QuicSpdyServerStreamBase, 25 public QuicSimpleServerBackend::RequestHandler { 26 public: 27 QuicSimpleServerStream(QuicStreamId id, QuicSpdySession* session, 28 StreamType type, 29 QuicSimpleServerBackend* quic_simple_server_backend); 30 QuicSimpleServerStream(PendingStream* pending, QuicSpdySession* session, 31 QuicSimpleServerBackend* quic_simple_server_backend); 32 QuicSimpleServerStream(const QuicSimpleServerStream&) = delete; 33 QuicSimpleServerStream& operator=(const QuicSimpleServerStream&) = delete; 34 ~QuicSimpleServerStream() override; 35 36 // QuicSpdyStream 37 void OnInitialHeadersComplete(bool fin, size_t frame_len, 38 const QuicHeaderList& header_list) override; 39 void OnCanWrite() override; 40 41 // QuicStream implementation called by the sequencer when there is 42 // data (or a FIN) to be read. 43 void OnBodyAvailable() override; 44 45 void OnInvalidHeaders() override; 46 47 // The response body of error responses. 48 static const char* const kErrorResponseBody; 49 static const char* const kNotFoundResponseBody; 50 51 // Implements QuicSimpleServerBackend::RequestHandler callbacks 52 QuicConnectionId connection_id() const override; 53 QuicStreamId stream_id() const override; 54 std::string peer_host() const override; 55 QuicSpdyStream* GetStream() override; 56 void OnResponseBackendComplete(const QuicBackendResponse* response) override; 57 void SendStreamData(absl::string_view data, bool close_stream) override; 58 void TerminateStreamWithError(QuicResetStreamError error) override; 59 60 void Respond(const QuicBackendResponse* response); 61 62 protected: 63 // Handles fresh body data whenever received when method is CONNECT. 64 void HandleRequestConnectData(bool fin_received); 65 66 // Sends a response using SendHeaders for the headers and WriteData for the 67 // body. 68 virtual void SendResponse(); 69 70 // Sends a basic 500 response using SendHeaders for the headers and WriteData 71 // for the body. 72 virtual void SendErrorResponse(); 73 virtual void SendErrorResponse(int resp_code); 74 75 // Sends a basic 404 response using SendHeaders for the headers and WriteData 76 // for the body. 77 void SendNotFoundResponse(); 78 79 // Sends the response header (if not `std::nullopt`) and body, but not the 80 // fin. 81 void SendIncompleteResponse( 82 std::optional<spdy::Http2HeaderBlock> response_headers, 83 absl::string_view body); 84 85 void SendHeadersAndBody(spdy::Http2HeaderBlock response_headers, 86 absl::string_view body); 87 void SendHeadersAndBodyAndTrailers( 88 std::optional<spdy::Http2HeaderBlock> response_headers, 89 absl::string_view body, spdy::Http2HeaderBlock response_trailers); 90 request_headers()91 spdy::Http2HeaderBlock* request_headers() { return &request_headers_; } 92 93 // Returns true iff the request (per saved `request_headers_`) is a CONNECT or 94 // Extended CONNECT request. 95 bool IsConnectRequest() const; 96 body()97 const std::string& body() { return body_; } 98 99 // Writes the body bytes for the GENERATE_BYTES response type. 100 void WriteGeneratedBytes(); 101 set_quic_simple_server_backend_for_test(QuicSimpleServerBackend * backend)102 void set_quic_simple_server_backend_for_test( 103 QuicSimpleServerBackend* backend) { 104 quic_simple_server_backend_ = backend; 105 } 106 response_sent()107 bool response_sent() const { return response_sent_; } set_response_sent()108 void set_response_sent() { response_sent_ = true; } 109 // The parsed headers received from the client. 110 spdy::Http2HeaderBlock request_headers_; 111 int64_t content_length_; 112 std::string body_; 113 114 private: 115 uint64_t generate_bytes_length_; 116 // Whether response headers have already been sent. 117 bool response_sent_ = false; 118 119 std::unique_ptr<QuicAlarm> delayed_response_alarm_; 120 121 QuicSimpleServerBackend* quic_simple_server_backend_; // Not owned. 122 }; 123 124 } // namespace quic 125 126 #endif // QUICHE_QUIC_TOOLS_QUIC_SIMPLE_SERVER_STREAM_H_ 127