View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  
21  /**
22   * An listener of HTTP/2 frames.
23   */
24  public interface Http2FrameListener {
25      /**
26       * Handles an inbound {@code DATA} frame.
27       *
28       * @param ctx the context from the handler where the frame was read.
29       * @param streamId the subject stream for the frame.
30       * @param data payload buffer for the frame. This buffer will be released by the codec.
31       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
32       *                256 (inclusive).
33       * @param endOfStream Indicates whether this is the last frame to be sent from the remote endpoint for this stream.
34       * @return the number of bytes that have been processed by the application. The returned bytes are used by the
35       * inbound flow controller to determine the appropriate time to expand the inbound flow control window (i.e. send
36       * {@code WINDOW_UPDATE}). Returning a value equal to the length of {@code data} + {@code padding} will effectively
37       * opt-out of application-level flow control for this frame. Returning a value less than the length of {@code data}
38       * + {@code padding} will defer the returning of the processed bytes, which the application must later return via
39       * {@link Http2LocalFlowController#consumeBytes(Http2Stream, int)}. The returned value must
40       * be >= {@code 0} and <= {@code data.readableBytes()} + {@code padding}.
41       */
42      int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding,
43                     boolean endOfStream) throws Http2Exception;
44  
45      /**
46       * Handles an inbound {@code HEADERS} frame.
47       * <p>
48       * Only one of the following methods will be called for each {@code HEADERS} frame sequence.
49       * One will be called when the {@code END_HEADERS} flag has been received.
50       * <ul>
51       * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, boolean)}</li>
52       * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, short, boolean, int, boolean)}</li>
53       * <li>{@link #onPushPromiseRead(ChannelHandlerContext, int, int, Http2Headers, int)}</li>
54       * </ul>
55       * <p>
56       * To say it another way; the {@link Http2Headers} will contain all of the headers
57       * for the current message exchange step (additional queuing is not necessary).
58       *
59       * @param ctx the context from the handler where the frame was read.
60       * @param streamId the subject stream for the frame.
61       * @param headers the received headers.
62       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
63       *                256 (inclusive).
64       * @param endOfStream Indicates whether this is the last frame to be sent from the remote endpoint
65       *            for this stream.
66       */
67      void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding,
68              boolean endOfStream) throws Http2Exception;
69  
70      /**
71       * Handles an inbound {@code HEADERS} frame with priority information specified.
72       * Only called if {@code END_HEADERS} encountered.
73       * <p>
74       * Only one of the following methods will be called for each {@code HEADERS} frame sequence.
75       * One will be called when the {@code END_HEADERS} flag has been received.
76       * <ul>
77       * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, boolean)}</li>
78       * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, short, boolean, int, boolean)}</li>
79       * <li>{@link #onPushPromiseRead(ChannelHandlerContext, int, int, Http2Headers, int)}</li>
80       * </ul>
81       * <p>
82       * To say it another way; the {@link Http2Headers} will contain all of the headers
83       * for the current message exchange step (additional queuing is not necessary).
84       *
85       * @param ctx the context from the handler where the frame was read.
86       * @param streamId the subject stream for the frame.
87       * @param headers the received headers.
88       * @param streamDependency the stream on which this stream depends, or 0 if dependent on the
89       *            connection.
90       * @param weight the new weight for the stream.
91       * @param exclusive whether or not the stream should be the exclusive dependent of its parent.
92       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
93       *                256 (inclusive).
94       * @param endOfStream Indicates whether this is the last frame to be sent from the remote endpoint
95       *            for this stream.
96       */
97      void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
98              int streamDependency, short weight, boolean exclusive, int padding, boolean endOfStream)
99              throws Http2Exception;
100 
101     /**
102      * Handles an inbound {@code PRIORITY} frame.
103      * <p>
104      * Note that is it possible to have this method called and no stream object exist for either
105      * {@code streamId}, {@code streamDependency}, or both. This is because the {@code PRIORITY} frame can be
106      * sent/received when streams are in the {@code CLOSED} state.
107      *
108      * @param ctx the context from the handler where the frame was read.
109      * @param streamId the subject stream for the frame.
110      * @param streamDependency the stream on which this stream depends, or 0 if dependent on the
111      *            connection.
112      * @param weight the new weight for the stream.
113      * @param exclusive whether or not the stream should be the exclusive dependent of its parent.
114      */
115     void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency,
116             short weight, boolean exclusive) throws Http2Exception;
117 
118     /**
119      * Handles an inbound {@code RST_STREAM} frame.
120      *
121      * @param ctx the context from the handler where the frame was read.
122      * @param streamId the stream that is terminating.
123      * @param errorCode the error code identifying the type of failure.
124      */
125     void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) throws Http2Exception;
126 
127     /**
128      * Handles an inbound {@code SETTINGS} acknowledgment frame.
129      * @param ctx the context from the handler where the frame was read.
130      */
131     void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception;
132 
133     /**
134      * Handles an inbound {@code SETTINGS} frame.
135      *
136      * @param ctx the context from the handler where the frame was read.
137      * @param settings the settings received from the remote endpoint.
138      */
139     void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) throws Http2Exception;
140 
141     /**
142      * Handles an inbound {@code PING} frame.
143      *
144      * @param ctx the context from the handler where the frame was read.
145      * @param data the payload of the frame.
146      */
147     void onPingRead(ChannelHandlerContext ctx, long data) throws Http2Exception;
148 
149     /**
150      * Handles an inbound {@code PING} acknowledgment.
151      *
152      * @param ctx the context from the handler where the frame was read.
153      * @param data the payload of the frame.
154      */
155     void onPingAckRead(ChannelHandlerContext ctx, long data) throws Http2Exception;
156 
157     /**
158      * Handles an inbound {@code PUSH_PROMISE} frame. Only called if {@code END_HEADERS} encountered.
159      * <p>
160      * Promised requests MUST be authoritative, cacheable, and safe.
161      * See <a href="https://tools.ietf.org/html/rfc7540#section-8.2">[RFC 7540], Section 8.2</a>.
162      * <p>
163      * Only one of the following methods will be called for each {@code HEADERS} frame sequence.
164      * One will be called when the {@code END_HEADERS} flag has been received.
165      * <ul>
166      * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, boolean)}</li>
167      * <li>{@link #onHeadersRead(ChannelHandlerContext, int, Http2Headers, int, short, boolean, int, boolean)}</li>
168      * <li>{@link #onPushPromiseRead(ChannelHandlerContext, int, int, Http2Headers, int)}</li>
169      * </ul>
170      * <p>
171      * To say it another way; the {@link Http2Headers} will contain all of the headers
172      * for the current message exchange step (additional queuing is not necessary).
173      *
174      * @param ctx the context from the handler where the frame was read.
175      * @param streamId the stream the frame was sent on.
176      * @param promisedStreamId the ID of the promised stream.
177      * @param headers the received headers.
178      * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
179      *                256 (inclusive).
180      */
181     void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
182             Http2Headers headers, int padding) throws Http2Exception;
183 
184     /**
185      * Handles an inbound {@code GO_AWAY} frame.
186      *
187      * @param ctx the context from the handler where the frame was read.
188      * @param lastStreamId the last known stream of the remote endpoint.
189      * @param errorCode the error code, if abnormal closure.
190      * @param debugData application-defined debug data. If this buffer needs to be retained by the
191      *            listener they must make a copy.
192      */
193     void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData)
194             throws Http2Exception;
195 
196     /**
197      * Handles an inbound {@code WINDOW_UPDATE} frame.
198      *
199      * @param ctx the context from the handler where the frame was read.
200      * @param streamId the stream the frame was sent on.
201      * @param windowSizeIncrement the increased number of bytes of the remote endpoint's flow
202      *            control window.
203      */
204     void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement)
205             throws Http2Exception;
206 
207     /**
208      * Handler for a frame not defined by the HTTP/2 spec.
209      *
210      * @param ctx the context from the handler where the frame was read.
211      * @param frameType the frame type from the HTTP/2 header.
212      * @param streamId the stream the frame was sent on.
213      * @param flags the flags in the frame header.
214      * @param payload the payload of the frame.
215      */
216     void onUnknownFrame(ChannelHandlerContext ctx, byte frameType, int streamId, Http2Flags flags, ByteBuf payload)
217             throws Http2Exception;
218 }