ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 1 | package com.google.net.stubby.http2.netty; |
| 2 | |
lryan | 3465040 | 2014-07-14 13:39:39 -0700 | [diff] [blame] | 3 | import com.google.common.collect.ImmutableMap; |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 4 | import com.google.net.stubby.NoOpRequest; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 5 | import com.google.net.stubby.Operation; |
| 6 | import com.google.net.stubby.Operation.Phase; |
| 7 | import com.google.net.stubby.Request; |
| 8 | import com.google.net.stubby.RequestRegistry; |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 9 | import com.google.net.stubby.Response; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 10 | import com.google.net.stubby.Session; |
| 11 | import com.google.net.stubby.Status; |
| 12 | import com.google.net.stubby.transport.MessageFramer; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 13 | import com.google.net.stubby.transport.Transport.Code; |
| 14 | |
| 15 | import io.netty.buffer.ByteBuf; |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 16 | import io.netty.channel.ChannelFuture; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 17 | import io.netty.channel.ChannelHandlerContext; |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 18 | import io.netty.handler.codec.http2.AbstractHttp2ConnectionHandler; |
lryan | 0359907 | 2014-07-07 18:44:00 -0700 | [diff] [blame] | 19 | import io.netty.handler.codec.http2.DefaultHttp2Connection; |
lryan | 0359907 | 2014-07-07 18:44:00 -0700 | [diff] [blame] | 20 | import io.netty.handler.codec.http2.Http2Connection; |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 21 | import io.netty.handler.codec.http2.Http2Error; |
| 22 | import io.netty.handler.codec.http2.Http2Exception; |
| 23 | import io.netty.handler.codec.http2.Http2Headers; |
| 24 | import io.netty.handler.codec.http2.Http2Settings; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 25 | |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Codec used by clients and servers to interpret HTTP2 frames in the context of an ongoing |
| 30 | * request-response dialog |
| 31 | */ |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 32 | public class Http2Codec extends AbstractHttp2ConnectionHandler { |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 33 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 34 | public static final int PADDING = 0; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 35 | private final RequestRegistry requestRegistry; |
| 36 | private final Session session; |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 37 | private Http2Codec.Http2Writer http2Writer; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Constructor used by servers, takes a session which will receive operation events. |
| 41 | */ |
| 42 | public Http2Codec(Session session, RequestRegistry requestRegistry) { |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 43 | this(new DefaultHttp2Connection(true), session, requestRegistry); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Constructor used by clients to send operations to a remote server |
| 48 | */ |
| 49 | public Http2Codec(RequestRegistry requestRegistry) { |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 50 | this(new DefaultHttp2Connection(false), null, requestRegistry); |
lryan | 0359907 | 2014-07-07 18:44:00 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor used by servers, takes a session which will receive operation events. |
| 55 | */ |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 56 | private Http2Codec(Http2Connection connection, |
lryan | 0359907 | 2014-07-07 18:44:00 -0700 | [diff] [blame] | 57 | Session session, |
| 58 | RequestRegistry requestRegistry) { |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 59 | super(connection); |
lryan | 0359907 | 2014-07-07 18:44:00 -0700 | [diff] [blame] | 60 | this.session = session; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 61 | this.requestRegistry = requestRegistry; |
| 62 | } |
| 63 | |
| 64 | @Override |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 65 | public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
| 66 | http2Writer = new Http2Writer(ctx); |
| 67 | } |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 68 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 69 | public Http2Writer getWriter() { |
| 70 | return http2Writer; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 74 | public void onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 75 | boolean endOfStream) |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 76 | throws Http2Exception { |
| 77 | Request request = requestRegistry.lookup(streamId); |
| 78 | if (request == null) { |
| 79 | // Stream may have been terminated already or this is just plain spurious |
| 80 | throw Http2Exception.format(Http2Error.STREAM_CLOSED, "Stream does not exist"); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 81 | } |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 82 | Operation operation = isClient() ? request.getResponse() : request; |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 83 | try { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 84 | ByteBufDeframer deframer = getOrCreateDeframer(operation, ctx); |
| 85 | deframer.deframe(data, operation); |
| 86 | if (endOfStream) { |
| 87 | finish(operation); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 88 | } |
| 89 | } catch (Throwable e) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 90 | // TODO(user): Need to disambiguate between stream corruption as well as client/server |
| 91 | // generated errors. For stream corruption we always just send reset stream. For |
| 92 | // clients we will also generally reset-stream on error, servers may send a more detailed |
| 93 | // status. |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 94 | Status status = Status.fromThrowable(e); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 95 | closeWithError(request, status); |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, |
nathanmittler | 56c1d8c | 2014-06-23 08:57:25 -0700 | [diff] [blame] | 101 | int streamDependency, short weight, boolean exclusive, int padding, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 102 | boolean endStream) throws Http2Exception { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 103 | Request operation = requestRegistry.lookup(streamId); |
| 104 | if (operation == null) { |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 105 | if (isClient()) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 106 | // For clients an operation must already exist in the registry |
| 107 | throw Http2Exception.format(Http2Error.REFUSED_STREAM, "Stream does not exist"); |
| 108 | } else { |
| 109 | operation = serverStart(ctx, streamId, headers); |
| 110 | if (operation == null) { |
| 111 | closeWithError(new NoOpRequest(createResponse(new Http2Writer(ctx), streamId).build()), |
| 112 | new Status(Code.NOT_FOUND)); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | if (endStream) { |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 117 | finish(isClient() ? operation.getResponse() : operation); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 122 | public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, |
| 123 | short weight, boolean exclusive) throws Http2Exception { |
| 124 | // TODO |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onRstStreamRead(ChannelHandlerContext ctx, int streamId, long errorCode) |
| 129 | throws Http2Exception { |
| 130 | Request request = requestRegistry.lookup(streamId); |
| 131 | if (request != null) { |
| 132 | closeWithError(request, new Status(Code.CANCELLED, "Stream reset")); |
| 133 | requestRegistry.remove(streamId); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onSettingsAckRead(ChannelHandlerContext ctx) throws Http2Exception { |
| 139 | // TOOD |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings) |
| 144 | throws Http2Exception { |
| 145 | // TOOD |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { |
| 150 | // TODO |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onPingAckRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception { |
| 155 | // TODO |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, |
| 160 | Http2Headers headers, int padding) throws Http2Exception { |
| 161 | // TODO |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, |
| 166 | ByteBuf debugData) throws Http2Exception { |
| 167 | // TODO |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onWindowUpdateRead(ChannelHandlerContext ctx, int streamId, int windowSizeIncrement) |
| 172 | throws Http2Exception { |
| 173 | // TODO |
| 174 | } |
| 175 | |
nathanmittler | bf8870b | 2014-07-16 13:28:33 -0700 | [diff] [blame] | 176 | private boolean isClient() { |
| 177 | return !connection().isServer(); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 178 | } |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Closes the request and its associated response with an internal error. |
| 182 | */ |
| 183 | private void closeWithError(Request request, Status status) { |
| 184 | try { |
| 185 | request.close(status); |
| 186 | request.getResponse().close(status); |
| 187 | } finally { |
| 188 | requestRegistry.remove(request.getId()); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 189 | disposeDeframer(request); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
lryan | f8bbc12 | 2014-06-03 14:22:33 -0700 | [diff] [blame] | 194 | * Create an HTTP2 response handler |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 195 | */ |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 196 | private Response.ResponseBuilder createResponse(Http2Writer writer, int streamId) { |
| 197 | return Http2Response.builder(streamId, writer, new MessageFramer(4096)); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Start the Request operation on the server |
| 202 | */ |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 203 | private Request serverStart(ChannelHandlerContext ctx, int streamId, Http2Headers headers) { |
| 204 | if (!Http2Session.PROTORPC.equals(headers.get("content-type"))) { |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 205 | return null; |
| 206 | } |
| 207 | // Use Path to specify the operation |
| 208 | String operationName = |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 209 | normalizeOperationName(headers.get(Http2Headers.PseudoHeaderName.PATH.value())); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 210 | if (operationName == null) { |
| 211 | return null; |
| 212 | } |
lryan | 3465040 | 2014-07-14 13:39:39 -0700 | [diff] [blame] | 213 | ImmutableMap.Builder<String, String> headerMap = ImmutableMap.builder(); |
| 214 | for (Map.Entry<String, String> header : headers) { |
| 215 | headerMap.put(header); |
| 216 | } |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 217 | // Create the operation and bind a HTTP2 response operation |
lryan | 3465040 | 2014-07-14 13:39:39 -0700 | [diff] [blame] | 218 | Request op = session.startRequest(operationName, headerMap.build(), |
| 219 | createResponse(new Http2Writer(ctx), |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 220 | streamId)); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 221 | if (op == null) { |
| 222 | return null; |
| 223 | } |
| 224 | requestRegistry.register(op); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 225 | return op; |
| 226 | } |
| 227 | |
| 228 | // TODO(user): This needs proper namespacing support, this is currently just a hack |
| 229 | private static String normalizeOperationName(String path) { |
| 230 | return path.substring(1); |
| 231 | } |
| 232 | |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Called when a HTTP2 stream is closed. |
| 235 | */ |
| 236 | private void finish(Operation operation) { |
| 237 | disposeDeframer(operation); |
| 238 | requestRegistry.remove(operation.getId()); |
| 239 | if (operation.getPhase() != Phase.CLOSED) { |
| 240 | operation.close(Status.OK); |
| 241 | } |
| 242 | } |
| 243 | |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 244 | public ByteBufDeframer getOrCreateDeframer(Operation operation, ChannelHandlerContext ctx) { |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 245 | ByteBufDeframer deframer = operation.get(ByteBufDeframer.class); |
| 246 | if (deframer == null) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 247 | deframer = new ByteBufDeframer(ctx.alloc()); |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 248 | operation.put(ByteBufDeframer.class, deframer); |
| 249 | } |
| 250 | return deframer; |
| 251 | } |
| 252 | |
| 253 | public void disposeDeframer(Operation operation) { |
| 254 | ByteBufDeframer deframer = operation.remove(ByteBufDeframer.class); |
| 255 | if (deframer != null) { |
| 256 | deframer.dispose(); |
| 257 | } |
| 258 | } |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 259 | |
| 260 | public class Http2Writer { |
| 261 | private final ChannelHandlerContext ctx; |
| 262 | |
| 263 | public Http2Writer(ChannelHandlerContext ctx) { |
| 264 | this.ctx = ctx; |
| 265 | } |
| 266 | |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 267 | public ChannelFuture writeData(int streamId, ByteBuf data, boolean endStream) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 268 | return Http2Codec.this.writeData(ctx, ctx.newPromise(), |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 269 | streamId, data, PADDING, endStream); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | public ChannelFuture writeHeaders(int streamId, |
| 273 | Http2Headers headers, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 274 | boolean endStream) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 275 | |
| 276 | return Http2Codec.this.writeHeaders(ctx, ctx.newPromise(), streamId, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 277 | headers, PADDING, endStream); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | public ChannelFuture writeHeaders(int streamId, Http2Headers headers, int streamDependency, |
| 281 | short weight, boolean exclusive, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 282 | boolean endStream) { |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 283 | return Http2Codec.this.writeHeaders(ctx, ctx.newPromise(), streamId, |
nathanmittler | 42af07c | 2014-08-19 12:26:11 -0700 | [diff] [blame^] | 284 | headers, streamDependency, weight, exclusive, PADDING, endStream); |
lryan | 0cf7d4c | 2014-06-11 11:50:49 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | public ChannelFuture writeRstStream(int streamId, long errorCode) { |
| 288 | return Http2Codec.this.writeRstStream(ctx, ctx.newPromise(), |
| 289 | streamId, |
| 290 | errorCode); |
| 291 | } |
| 292 | } |
ejona | 07d3f6a | 2014-05-14 11:26:57 -0700 | [diff] [blame] | 293 | } |