blob: add4a12875d43106f937d3936529cb147b4d5a18 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 teama6ef0a62019-03-07 20:34:33 -05008#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 teama6ef0a62019-03-07 20:34:33 -050010#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 team5015e2e2019-12-11 09:38:06 -080012#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/spdy/core/spdy_framer.h"
14
15namespace quic {
16
17// All this does right now is aggregate data, and on fin, send an HTTP
18// response.
19class 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);
renjietangbaea59c2019-05-29 15:08:14 -070026 QuicSimpleServerStream(PendingStream* pending,
QUICHE teama6ef0a62019-03-07 20:34:33 -050027 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;
rch7bd54762019-10-15 10:53:24 -070041 void OnCanWrite() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050042
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;
vasilvvc48c8712019-03-11 13:38:16 -070059 std::string peer_host() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 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 team5015e2e2019-12-11 09:38:06 -080080 quiche::QuicheStringPiece body);
QUICHE teama6ef0a62019-03-07 20:34:33 -050081
82 void SendHeadersAndBody(spdy::SpdyHeaderBlock response_headers,
QUICHE team5015e2e2019-12-11 09:38:06 -080083 quiche::QuicheStringPiece body);
QUICHE teama6ef0a62019-03-07 20:34:33 -050084 void SendHeadersAndBodyAndTrailers(spdy::SpdyHeaderBlock response_headers,
QUICHE team5015e2e2019-12-11 09:38:06 -080085 quiche::QuicheStringPiece body,
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 spdy::SpdyHeaderBlock response_trailers);
87
88 spdy::SpdyHeaderBlock* request_headers() { return &request_headers_; }
89
vasilvvc48c8712019-03-11 13:38:16 -070090 const std::string& body() { return body_; }
QUICHE teama6ef0a62019-03-07 20:34:33 -050091
rch7bd54762019-10-15 10:53:24 -070092 // Writes the body bytes for the GENERATE_BYTES response type.
93 void WriteGeneratedBytes();
94
QUICHE teama6ef0a62019-03-07 20:34:33 -050095 // The parsed headers received from the client.
96 spdy::SpdyHeaderBlock request_headers_;
97 int64_t content_length_;
vasilvvc48c8712019-03-11 13:38:16 -070098 std::string body_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050099
100 private:
rch7bd54762019-10-15 10:53:24 -0700101 uint64_t generate_bytes_length_;
102
QUICHE teama6ef0a62019-03-07 20:34:33 -0500103 QuicSimpleServerBackend* quic_simple_server_backend_; // Not owned.
104};
105
106} // namespace quic
107
108#endif // QUICHE_QUIC_TOOLS_QUIC_SIMPLE_SERVER_STREAM_H_