/
Stream.java
312 lines (280 loc) · 10.1 KB
/
Stream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http2.api;
import org.eclipse.jetty.http2.frames.DataFrame;
import org.eclipse.jetty.http2.frames.HeadersFrame;
import org.eclipse.jetty.http2.frames.PushPromiseFrame;
import org.eclipse.jetty.http2.frames.ResetFrame;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Promise;
/**
* <p>A {@link Stream} represents a bidirectional exchange of data on top of a {@link Session}.</p>
* <p>Differently from socket streams, where the input and output streams are permanently associated
* with the socket (and hence with the connection that the socket represents), there can be multiple
* HTTP/2 streams present concurrent for an HTTP/2 session.</p>
* <p>A {@link Stream} maps to an HTTP request/response cycle, and after the request/response cycle is
* completed, the stream is closed and removed from the session.</p>
* <p>Like {@link Session}, {@link Stream} is the active part and by calling its API applications
* can generate events on the stream; conversely, {@link Stream.Listener} is the passive part, and
* its callbacks are invoked when events happen on the stream.</p>
*
* @see Stream.Listener
*/
public interface Stream
{
/**
* @return the stream unique id
*/
int getId();
/**
* @return the session this stream is associated to
*/
Session getSession();
/**
* <p>Sends the given HEADERS {@code frame}.</p>
* <p>Typically used to send an HTTP response or to send the HTTP response trailers.</p>
*
* @param frame the HEADERS frame to send
* @param callback the callback that gets notified when the frame has been sent
*/
void headers(HeadersFrame frame, Callback callback);
/**
* <p>Sends the given PUSH_PROMISE {@code frame}.</p>
*
* @param frame the PUSH_PROMISE frame to send
* @param promise the promise that gets notified of the pushed stream creation
* @param listener the listener that gets notified of stream events
*/
void push(PushPromiseFrame frame, Promise<Stream> promise, Listener listener);
/**
* <p>Sends the given DATA {@code frame}.</p>
*
* @param frame the DATA frame to send
* @param callback the callback that gets notified when the frame has been sent
*/
void data(DataFrame frame, Callback callback);
/**
* <p>Sends the given RST_STREAM {@code frame}.</p>
*
* @param frame the RST_FRAME to send
* @param callback the callback that gets notified when the frame has been sent
*/
void reset(ResetFrame frame, Callback callback);
/**
* @param key the attribute key
* @return an arbitrary object associated with the given key to this stream
* or null if no object can be found for the given key.
* @see #setAttribute(String, Object)
*/
Object getAttribute(String key);
/**
* @param key the attribute key
* @param value an arbitrary object to associate with the given key to this stream
* @see #getAttribute(String)
* @see #removeAttribute(String)
*/
void setAttribute(String key, Object value);
/**
* @param key the attribute key
* @return the arbitrary object associated with the given key to this stream
* @see #setAttribute(String, Object)
*/
Object removeAttribute(String key);
/**
* @return whether this stream has been reset
*/
boolean isReset();
/**
* @return whether this stream is closed, both locally and remotely.
*/
boolean isClosed();
/**
* @return the stream idle timeout
* @see #setIdleTimeout(long)
*/
long getIdleTimeout();
/**
* @param idleTimeout the stream idle timeout
* @see #getIdleTimeout()
* @see Stream.Listener#onIdleTimeout(Stream, Throwable)
*/
void setIdleTimeout(long idleTimeout);
/**
* <p>A {@link Stream.Listener} is the passive counterpart of a {@link Stream} and receives
* events happening on an HTTP/2 stream.</p>
*
* @see Stream
*/
interface Listener
{
/**
* <p>Callback method invoked when a stream is created locally by
* {@link Session#newStream(HeadersFrame, Promise, Listener)}.</p>
*
* @param stream the newly created stream
*/
public default void onNewStream(Stream stream)
{
}
/**
* <p>Callback method invoked when a HEADERS frame representing the HTTP response has been received.</p>
*
* @param stream the stream
* @param frame the HEADERS frame received
*/
void onHeaders(Stream stream, HeadersFrame frame);
/**
* <p>Callback method invoked when a PUSH_PROMISE frame has been received.</p>
*
* @param stream the stream
* @param frame the PUSH_PROMISE frame received
* @return a Stream.Listener that will be notified of pushed stream events
*/
Listener onPush(Stream stream, PushPromiseFrame frame);
/**
* <p>Callback method invoked when a DATA frame has been received.</p>
*
* @param stream the stream
* @param frame the DATA frame received
* @param callback the callback to complete when the bytes of the DATA frame have been consumed
*/
void onData(Stream stream, DataFrame frame, Callback callback);
/**
* <p>Callback method invoked when a RST_STREAM frame has been received for this stream.</p>
*
* @param stream the stream
* @param frame the RST_FRAME received
* @param callback the callback to complete when the reset has been handled
*/
default void onReset(Stream stream, ResetFrame frame, Callback callback)
{
try
{
onReset(stream, frame);
callback.succeeded();
}
catch (Throwable x)
{
callback.failed(x);
}
}
/**
* <p>Callback method invoked when a RST_STREAM frame has been received for this stream.</p>
*
* @param stream the stream
* @param frame the RST_FRAME received
* @see Session.Listener#onReset(Session, ResetFrame)
*/
default void onReset(Stream stream, ResetFrame frame)
{
}
/**
* <p>Callback method invoked when the stream exceeds its idle timeout.</p>
*
* @param stream the stream
* @param x the timeout failure
* @see #getIdleTimeout()
* @deprecated use {@link #onIdleTimeout(Stream, Throwable)} instead
*/
@Deprecated
default void onTimeout(Stream stream, Throwable x)
{
}
/**
* <p>Callback method invoked when the stream exceeds its idle timeout.</p>
*
* @param stream the stream
* @param x the timeout failure
* @return true to reset the stream, false to ignore the idle timeout
* @see #getIdleTimeout()
*/
default boolean onIdleTimeout(Stream stream, Throwable x)
{
onTimeout(stream, x);
return true;
}
/**
* <p>Callback method invoked when the stream failed.</p>
*
* @param stream the stream
* @param error the error code
* @param reason the error reason, or null
* @param failure the failure
* @param callback the callback to complete when the failure has been handled
*/
default void onFailure(Stream stream, int error, String reason, Throwable failure, Callback callback)
{
onFailure(stream, error, reason, callback);
}
/**
* <p>Callback method invoked when the stream failed.</p>
*
* @param stream the stream
* @param error the error code
* @param reason the error reason, or null
* @param callback the callback to complete when the failure has been handled
* @deprecated use {@link #onFailure(Stream, int, String, Throwable, Callback)} instead
*/
@Deprecated
default void onFailure(Stream stream, int error, String reason, Callback callback)
{
callback.succeeded();
}
/**
* <p>Callback method invoked after the stream has been closed.</p>
*
* @param stream the stream
*/
default void onClosed(Stream stream)
{
}
/**
* <p>Empty implementation of {@link Listener}</p>
*/
class Adapter implements Listener
{
@Override
public void onHeaders(Stream stream, HeadersFrame frame)
{
}
@Override
public Listener onPush(Stream stream, PushPromiseFrame frame)
{
return null;
}
@Override
public void onData(Stream stream, DataFrame frame, Callback callback)
{
callback.succeeded();
}
@Override
public void onReset(Stream stream, ResetFrame frame)
{
}
@Override
public void onTimeout(Stream stream, Throwable x)
{
}
@Override
public boolean onIdleTimeout(Stream stream, Throwable x)
{
return true;
}
}
}
}