QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/core/http/quic_spdy_server_stream_base.h" |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/tools/quic_backend_response.h" |
| 11 | #include "net/third_party/quiche/src/quic/tools/quic_simple_server_backend.h" |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/spdy/core/spdy_framer.h" |
| 14 | |
| 15 | namespace quic { |
| 16 | |
| 17 | // All this does right now is aggregate data, and on fin, send an HTTP |
| 18 | // response. |
| 19 | class QuicSimpleServerStream : public QuicSpdyServerStreamBase, |
| 20 | public QuicSimpleServerBackend::RequestHandler { |
| 21 | public: |
| 22 | QuicSimpleServerStream(QuicStreamId id, |
| 23 | QuicSpdySession* session, |
| 24 | StreamType type, |
| 25 | QuicSimpleServerBackend* quic_simple_server_backend); |
renjietang | baea59c | 2019-05-29 15:08:14 -0700 | [diff] [blame] | 26 | QuicSimpleServerStream(PendingStream* pending, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | QuicSpdySession* session, |
| 28 | StreamType type, |
| 29 | QuicSimpleServerBackend* quic_simple_server_backend); |
| 30 | QuicSimpleServerStream(const QuicSimpleServerStream&) = delete; |
| 31 | QuicSimpleServerStream& operator=(const QuicSimpleServerStream&) = delete; |
| 32 | ~QuicSimpleServerStream() override; |
| 33 | |
| 34 | // QuicSpdyStream |
| 35 | void OnInitialHeadersComplete(bool fin, |
| 36 | size_t frame_len, |
| 37 | const QuicHeaderList& header_list) override; |
| 38 | void OnTrailingHeadersComplete(bool fin, |
| 39 | size_t frame_len, |
| 40 | const QuicHeaderList& header_list) override; |
rch | 7bd5476 | 2019-10-15 10:53:24 -0700 | [diff] [blame] | 41 | void OnCanWrite() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | |
| 43 | // QuicStream implementation called by the sequencer when there is |
| 44 | // data (or a FIN) to be read. |
| 45 | void OnBodyAvailable() override; |
| 46 | |
| 47 | // Make this stream start from as if it just finished parsing an incoming |
| 48 | // request whose headers are equivalent to |push_request_headers|. |
| 49 | // Doing so will trigger this toy stream to fetch response and send it back. |
| 50 | virtual void PushResponse(spdy::SpdyHeaderBlock push_request_headers); |
| 51 | |
| 52 | // The response body of error responses. |
| 53 | static const char* const kErrorResponseBody; |
| 54 | static const char* const kNotFoundResponseBody; |
| 55 | |
| 56 | // Implements QuicSimpleServerBackend::RequestHandler callbacks |
| 57 | QuicConnectionId connection_id() const override; |
| 58 | QuicStreamId stream_id() const override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 59 | std::string peer_host() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | void OnResponseBackendComplete( |
| 61 | const QuicBackendResponse* response, |
| 62 | std::list<QuicBackendResponse::ServerPushInfo> resources) override; |
| 63 | |
| 64 | protected: |
| 65 | // Sends a basic 200 response using SendHeaders for the headers and WriteData |
| 66 | // for the body. |
| 67 | virtual void SendResponse(); |
| 68 | |
| 69 | // Sends a basic 500 response using SendHeaders for the headers and WriteData |
| 70 | // for the body. |
| 71 | virtual void SendErrorResponse(); |
| 72 | void SendErrorResponse(int resp_code); |
| 73 | |
| 74 | // Sends a basic 404 response using SendHeaders for the headers and WriteData |
| 75 | // for the body. |
| 76 | void SendNotFoundResponse(); |
| 77 | |
| 78 | // Sends the response header and body, but not the fin. |
| 79 | void SendIncompleteResponse(spdy::SpdyHeaderBlock response_headers, |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 80 | quiche::QuicheStringPiece body); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | |
| 82 | void SendHeadersAndBody(spdy::SpdyHeaderBlock response_headers, |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 83 | quiche::QuicheStringPiece body); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | void SendHeadersAndBodyAndTrailers(spdy::SpdyHeaderBlock response_headers, |
QUICHE team | 5015e2e | 2019-12-11 09:38:06 -0800 | [diff] [blame] | 85 | quiche::QuicheStringPiece body, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | spdy::SpdyHeaderBlock response_trailers); |
| 87 | |
| 88 | spdy::SpdyHeaderBlock* request_headers() { return &request_headers_; } |
| 89 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 90 | const std::string& body() { return body_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | |
rch | 7bd5476 | 2019-10-15 10:53:24 -0700 | [diff] [blame] | 92 | // Writes the body bytes for the GENERATE_BYTES response type. |
| 93 | void WriteGeneratedBytes(); |
| 94 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | // The parsed headers received from the client. |
| 96 | spdy::SpdyHeaderBlock request_headers_; |
| 97 | int64_t content_length_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 98 | std::string body_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | |
| 100 | private: |
rch | 7bd5476 | 2019-10-15 10:53:24 -0700 | [diff] [blame] | 101 | uint64_t generate_bytes_length_; |
| 102 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 103 | QuicSimpleServerBackend* quic_simple_server_backend_; // Not owned. |
| 104 | }; |
| 105 | |
| 106 | } // namespace quic |
| 107 | |
| 108 | #endif // QUICHE_QUIC_TOOLS_QUIC_SIMPLE_SERVER_STREAM_H_ |