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  package io.netty5.handler.codec.http2;
16  
17  import io.netty5.buffer.api.Buffer;
18  import io.netty5.channel.ChannelHandlerContext;
19  import io.netty5.util.concurrent.Future;
20  import io.netty5.util.internal.UnstableApi;
21  
22  import java.io.Closeable;
23  
24  /**
25   * A writer responsible for marshaling HTTP/2 frames to the channel. All of the write methods in
26   * this interface write to the context, but DO NOT FLUSH. To perform a flush, you must separately
27   * call {@link ChannelHandlerContext#flush()}.
28   */
29  @UnstableApi
30  public interface Http2FrameWriter extends Http2DataWriter, Closeable {
31      /**
32       * Configuration specific to {@link Http2FrameWriter}
33       */
34      interface Configuration {
35          /**
36           * Get the {@link Http2HeadersEncoder.Configuration} for this {@link Http2FrameWriter}
37           */
38          Http2HeadersEncoder.Configuration headersConfiguration();
39  
40          /**
41           * Get the {@link Http2FrameSizePolicy} for this {@link Http2FrameWriter}
42           */
43          Http2FrameSizePolicy frameSizePolicy();
44      }
45  
46      /**
47       * Writes a HEADERS frame to the remote endpoint.
48       *
49       * @param ctx the context to use for writing.
50       * @param streamId the stream for which to send the frame.
51       * @param headers the headers to be sent.
52       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
53       *                256 (inclusive).
54       * @param endStream indicates if this is the last frame to be sent for the stream.
55       * @return the future for the write.
56       * <a href="https://tools.ietf.org/html/rfc7540#section-10.5.1">Section 10.5.1</a> states the following:
57       * <pre>
58       * The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
59       * </pre>
60       * If this call has modified the HPACK header state you <strong>MUST</strong> throw a connection error.
61       * <p>
62       * If this call has <strong>NOT</strong> modified the HPACK header state you are free to throw a stream error.
63       */
64      Future<Void> writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
65                          int padding, boolean endStream);
66  
67      /**
68       * Writes a HEADERS frame with priority specified to the remote endpoint.
69       *
70       * @param ctx the context to use for writing.
71       * @param streamId the stream for which to send the frame.
72       * @param headers the headers to be sent.
73       * @param streamDependency the stream on which this stream should depend, or 0 if it should
74       *            depend on the connection.
75       * @param weight the weight for this stream.
76       * @param exclusive whether this stream should be the exclusive dependant of its parent.
77       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
78       *                256 (inclusive).
79       * @param endStream indicates if this is the last frame to be sent for the stream.
80       * @return the future for the write.
81       * <a href="https://tools.ietf.org/html/rfc7540#section-10.5.1">Section 10.5.1</a> states the following:
82       * <pre>
83       * The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
84       * </pre>
85       * If this call has modified the HPACK header state you <strong>MUST</strong> throw a connection error.
86       * <p>
87       * If this call has <strong>NOT</strong> modified the HPACK header state you are free to throw a stream error.
88       */
89      Future<Void> writeHeaders(ChannelHandlerContext ctx, int streamId, Http2Headers headers,
90                                 int streamDependency, short weight, boolean exclusive, int padding, boolean endStream);
91  
92      /**
93       * Writes a PRIORITY frame to the remote endpoint.
94       *
95       * @param ctx the context to use for writing.
96       * @param streamId the stream for which to send the frame.
97       * @param streamDependency the stream on which this stream should depend, or 0 if it should
98       *            depend on the connection.
99       * @param weight the weight for this stream.
100      * @param exclusive whether this stream should be the exclusive dependant of its parent.
101      * @return the future for the write.
102      */
103     Future<Void> writePriority(ChannelHandlerContext ctx, int streamId, int streamDependency,
104             short weight, boolean exclusive);
105 
106     /**
107      * Writes a RST_STREAM frame to the remote endpoint.
108      *
109      * @param ctx the context to use for writing.
110      * @param streamId the stream for which to send the frame.
111      * @param errorCode the error code indicating the nature of the failure.
112      * @return the future for the write.
113      */
114     Future<Void> writeRstStream(ChannelHandlerContext ctx, int streamId, long errorCode);
115 
116     /**
117      * Writes a SETTINGS frame to the remote endpoint.
118      *
119      * @param ctx the context to use for writing.
120      * @param settings the settings to be sent.
121      * @return the future for the write.
122      */
123     Future<Void> writeSettings(ChannelHandlerContext ctx, Http2Settings settings);
124 
125     /**
126      * Writes a SETTINGS acknowledgment to the remote endpoint.
127      *
128      * @param ctx the context to use for writing.
129      * @return the future for the write.
130      */
131     Future<Void> writeSettingsAck(ChannelHandlerContext ctx);
132 
133     /**
134      * Writes a PING frame to the remote endpoint.
135      *
136      * @param ctx the context to use for writing.
137      * @param ack indicates whether this is an ack of a PING frame previously received from the
138      *            remote endpoint.
139      * @param data the payload of the frame.
140      * @return the future for the write.
141      */
142     Future<Void> writePing(ChannelHandlerContext ctx, boolean ack, long data);
143 
144     /**
145      * Writes a PUSH_PROMISE frame to the remote endpoint.
146      *
147      * @param ctx the context to use for writing.
148      * @param streamId the stream for which to send the frame.
149      * @param promisedStreamId the ID of the promised stream.
150      * @param headers the headers to be sent.
151      * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
152      *                256 (inclusive).
153      * @return the future for the write.
154      * <a href="https://tools.ietf.org/html/rfc7540#section-10.5.1">Section 10.5.1</a> states the following:
155      * <pre>
156      * The header block MUST be processed to ensure a consistent connection state, unless the connection is closed.
157      * </pre>
158      * If this call has modified the HPACK header state you <strong>MUST</strong> throw a connection error.
159      * <p>
160      * If this call has <strong>NOT</strong> modified the HPACK header state you are free to throw a stream error.
161      */
162     Future<Void> writePushPromise(ChannelHandlerContext ctx, int streamId, int promisedStreamId,
163                                    Http2Headers headers, int padding);
164 
165     /**
166      * Writes a GO_AWAY frame to the remote endpoint.
167      *
168      * @param ctx the context to use for writing.
169      * @param lastStreamId the last known stream of this endpoint.
170      * @param errorCode the error code, if the connection was abnormally terminated.
171      * @param debugData application-defined debug data. This will be released by this method.
172      * @return the future for the write.
173      */
174     Future<Void> writeGoAway(ChannelHandlerContext ctx, int lastStreamId, long errorCode,
175             Buffer debugData);
176 
177     /**
178      * Writes a WINDOW_UPDATE frame to the remote endpoint.
179      *
180      * @param ctx the context to use for writing.
181      * @param streamId the stream for which to send the frame.
182      * @param windowSizeIncrement the number of bytes by which the local inbound flow control window
183      *            is increasing.
184      * @return the future for the write.
185      */
186     Future<Void> writeWindowUpdate(ChannelHandlerContext ctx, int streamId,
187             int windowSizeIncrement);
188 
189     /**
190      * Generic write method for any HTTP/2 frame. This allows writing of non-standard frames.
191      *
192      * @param ctx the context to use for writing.
193      * @param frameType the frame type identifier.
194      * @param streamId the stream for which to send the frame.
195      * @param flags the flags to write for this frame.
196      * @param payload the payload to write for this frame. This will be released by this method.
197      * @return the future for the write.
198      */
199     Future<Void> writeFrame(ChannelHandlerContext ctx, byte frameType, int streamId,
200             Http2Flags flags, Buffer payload);
201 
202     /**
203      * Get the configuration related elements for this {@link Http2FrameWriter}
204      */
205     Configuration configuration();
206 
207     /**
208      * Closes this writer and frees any allocated resources.
209      */
210     @Override
211     void close();
212 }