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