• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_BALSA_BALSA_VISITOR_INTERFACE_H_
6 #define QUICHE_BALSA_BALSA_VISITOR_INTERFACE_H_
7 
8 #include <cstddef>
9 
10 #include "absl/strings/string_view.h"
11 #include "quiche/balsa/balsa_enums.h"
12 #include "quiche/balsa/balsa_headers.h"
13 #include "quiche/common/platform/api/quiche_export.h"
14 
15 namespace quiche {
16 
17 // By default the BalsaFrame instantiates a class derived from this interface
18 // that does absolutely nothing. If you'd prefer to have interesting
19 // functionality execute when any of the below functions are called by the
20 // BalsaFrame, then you should subclass it, and set an instantiation of your
21 // subclass as the current visitor for the BalsaFrame class using
22 // BalsaFrame::set_visitor().
23 class QUICHE_EXPORT BalsaVisitorInterface {
24  public:
~BalsaVisitorInterface()25   virtual ~BalsaVisitorInterface() {}
26 
27   // Summary:
28   //   This is how the BalsaFrame passes you the raw input that it knows to be a
29   //   part of the body. To be clear, every byte of the Balsa that isn't part of
30   //   the header (or its framing), or trailers will be passed through this
31   //   function.  This includes data as well as chunking framing.
32   // Arguments:
33   //   input - the raw input that is part of the body.
34   virtual void OnRawBodyInput(absl::string_view input) = 0;
35 
36   // Summary:
37   //   This is like OnRawBodyInput, but it will only include those parts of the
38   //   body that would be stored by a program such as wget, i.e. the bytes
39   //   indicating chunking will have been removed. Trailers will not be passed
40   //   in through this function-- they'll be passed in through OnTrailerInput.
41   // Arguments:
42   //   input - the part of the body.
43   virtual void OnBodyChunkInput(absl::string_view input) = 0;
44 
45   // Summary:
46   //   BalsaFrame passes the raw header data through this function. This is not
47   //   cleaned up in any way.
48   // Arguments:
49   //   input - raw header data.
50   virtual void OnHeaderInput(absl::string_view input) = 0;
51 
52   // Summary:
53   //   BalsaFrame passes each header through this function as soon as it is
54   //   parsed.
55   // Argument:
56   //   key - the header name.
57   //   value - the associated header value.
58   virtual void OnHeader(absl::string_view key, absl::string_view value) = 0;
59 
60   // Summary:
61   //   BalsaFrame passes the raw trailer data through this function. This is not
62   //   cleaned up in any way.  Note that trailers only occur in a message if
63   //   there was a chunked encoding, and not always then.
64   // Arguments:
65   //   input - raw trailer data.
66   virtual void OnTrailerInput(absl::string_view input) = 0;
67 
68   // Summary:
69   //   Since the BalsaFrame already has to parse the headers in order to
70   //   determine proper framing, it might as well pass the parsed and cleaned-up
71   //   results to whatever might need it.  This function exists for that
72   //   purpose-- parsed headers are passed into this function.
73   // Arguments:
74   //   headers - contains the parsed headers in the order in which
75   //             they occurred in the header.
76   virtual void ProcessHeaders(const BalsaHeaders& headers) = 0;
77 
78   // Summary:
79   //   Since the BalsaFrame already has to parse the trailer, it might as well
80   //   pass the parsed and cleaned-up results to whatever might need it.  This
81   //   function exists for that purpose-- parsed trailer is passed into this
82   //   function. This will not be called if the trailer_ object is not set in
83   //   the framer, even if trailer exists in request/response.
84   // Arguments:
85   //   trailer - contains the parsed headers in the order in which
86   //             they occurred in the trailer.
87   virtual void ProcessTrailers(const BalsaHeaders& trailer) = 0;
88 
89   // Summary:
90   //   Called when the first line of the message is parsed, in this case, for a
91   //   request.
92   // Arguments:
93   //   line_input        - the first line string,
94   //   method_input      - the method substring,
95   //   request_uri_input - request uri substring,
96   //   version_input     - the version substring.
97   virtual void OnRequestFirstLineInput(absl::string_view line_input,
98                                        absl::string_view method_input,
99                                        absl::string_view request_uri,
100                                        absl::string_view version_input) = 0;
101 
102   // Summary:
103   //   Called when the first line of the message is parsed, in this case, for a
104   //   response.
105   // Arguments:
106   //   line_input    - the first line string,
107   //   version_input - the version substring,
108   //   status_input  - the status substring,
109   //   reason_input  - the reason substring.
110   virtual void OnResponseFirstLineInput(absl::string_view line_input,
111                                         absl::string_view version_input,
112                                         absl::string_view status_input,
113                                         absl::string_view reason_input) = 0;
114 
115   // Summary:
116   //   Called when a chunk length is parsed.
117   // Arguments:
118   //   chunk length - the length of the next incoming chunk.
119   virtual void OnChunkLength(size_t chunk_length) = 0;
120 
121   // Summary:
122   //   BalsaFrame passes the raw chunk extension data through this function.
123   //   The data is not cleaned up at all.
124   // Arguments:
125   //   input - contains the bytes available for read.
126   virtual void OnChunkExtensionInput(absl::string_view input) = 0;
127 
128   // Summary:
129   //   Called when an interim response (response code 1xx) is framed and
130   //   processed. This callback is mutually exclusive with ContinueHeaderDone().
131   // Arguments:
132   //   headers - contains the parsed headers in the order in which they occurred
133   //             in the interim response.
134   virtual void OnInterimHeaders(BalsaHeaders headers) = 0;
135 
136   // Summary:
137   //   Called when the 100 Continue headers are framed and processed. This
138   //   callback is mutually exclusive with OnInterimHeaders().
139   // TODO(b/68801833): Remove this and update the OnInterimHeaders() comment.
140   virtual void ContinueHeaderDone() = 0;
141 
142   // Summary:
143   //   Called when the header is framed and processed.
144   virtual void HeaderDone() = 0;
145 
146   // Summary:
147   //   Called when the message is framed and processed.
148   virtual void MessageDone() = 0;
149 
150   // Summary:
151   //   Called when an error is detected
152   // Arguments:
153   //   error_code - the error which is to be reported.
154   virtual void HandleError(BalsaFrameEnums::ErrorCode error_code) = 0;
155 
156   // Summary:
157   //   Called when something meriting a warning is detected
158   // Arguments:
159   //   error_code - the warning which is to be reported.
160   virtual void HandleWarning(BalsaFrameEnums::ErrorCode error_code) = 0;
161 };
162 
163 }  // namespace quiche
164 
165 #endif  // QUICHE_BALSA_BALSA_VISITOR_INTERFACE_H_
166