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