forked from netty/netty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: Adding an example that showcases Netty’s HTTP/2 codec and that is slightly more complex than the existing hello-world example. It is based on the Gopher tiles example available here: https://http2.golang.org/gophertiles?latency=0 Modifications: Moved current http2 example to http2/helloworld. Added http2 tiles example under http2/tiles. Result: A Netty tiles example is available.
- Loading branch information
1 parent
c6d61f9
commit 781a855
Showing
220 changed files
with
756 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
example/src/main/java/io/netty/example/http2/tiles/FallbackRequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2015 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.netty.example.http2.tiles; | ||
|
||
import static io.netty.buffer.Unpooled.copiedBuffer; | ||
import static io.netty.buffer.Unpooled.unmodifiableBuffer; | ||
import static io.netty.buffer.Unpooled.unreleasableBuffer; | ||
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; | ||
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE; | ||
import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE; | ||
import static io.netty.handler.codec.http.HttpResponseStatus.OK; | ||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; | ||
import static io.netty.util.CharsetUtil.UTF_8; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.SimpleChannelInboundHandler; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpHeaderUtil; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http2.Http2CodecUtil; | ||
|
||
/** | ||
* Handles the exceptional case where HTTP 1.x was negotiated under TLS. | ||
*/ | ||
public final class FallbackRequestHandler extends SimpleChannelInboundHandler<HttpRequest> { | ||
|
||
private static final ByteBuf response = unmodifiableBuffer(unreleasableBuffer(copiedBuffer("<!DOCTYPE html>" | ||
+ "<html><body><h2>To view the example you need a browser that supports HTTP/2 (" | ||
+ Http2CodecUtil.TLS_UPGRADE_PROTOCOL_NAME | ||
+ ")</h2></body></html>", UTF_8))); | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) throws Exception { | ||
if (HttpHeaderUtil.is100ContinueExpected(req)) { | ||
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); | ||
} | ||
|
||
ByteBuf content = ctx.alloc().buffer(); | ||
content.writeBytes(response.duplicate()); | ||
|
||
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); | ||
response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); | ||
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); | ||
|
||
ctx.write(response).addListener(ChannelFutureListener.CLOSE); | ||
} | ||
|
||
@Override | ||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { | ||
ctx.flush(); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
cause.printStackTrace(); | ||
ctx.close(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
example/src/main/java/io/netty/example/http2/tiles/Html.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2015 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.netty.example.http2.tiles; | ||
|
||
import static io.netty.util.CharsetUtil.UTF_8; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* Static and dynamically generated HTML for the example. | ||
*/ | ||
public final class Html { | ||
|
||
public static final String IP = System.getProperty("ip", "127.0.0.1"); | ||
|
||
public static final byte[] FOOTER = "</body></html>".getBytes(UTF_8); | ||
|
||
public static final byte[] HEADER = ("<!DOCTYPE html><html><head lang=\"en\"><title>Netty HTTP/2 Example</title>" | ||
+ "<style>body {background:#DDD;} div#netty { line-height:0;}</style>" | ||
+ "<link rel=\"shortcut icon\" href=\"about:blank\">" | ||
+ "<meta charset=\"UTF-8\"></head><body>A grid of 200 tiled images is shown below. Compare:" | ||
+ "<p>[<a href='https://" + url(Http2Server.PORT) + "?latency=0'>HTTP/2, 0 latency</a>] [<a href='http://" | ||
+ url(HttpServer.PORT) + "?latency=0'>HTTP/1, 0 latency</a>]<br/>" + "[<a href='https://" | ||
+ url(Http2Server.PORT) + "?latency=30'>HTTP/2, 30ms latency</a>] [<a href='http://" + url(HttpServer.PORT) | ||
+ "?latency=30'>HTTP/1, 30ms latency</a>]<br/>" + "[<a href='https://" + url(Http2Server.PORT) | ||
+ "?latency=200'>HTTP/2, 200ms latency</a>] [<a href='http://" + url(HttpServer.PORT) | ||
+ "?latency=200'>HTTP/1, 200ms latency</a>]<br/>" + "[<a href='https://" + url(Http2Server.PORT) | ||
+ "?latency=1000'>HTTP/2, 1s latency</a>] [<a href='http://" + url(HttpServer.PORT) | ||
+ "?latency=1000'>HTTP/1, " + "1s latency</a>]<br/>").getBytes(UTF_8); | ||
|
||
private static final int IMAGES_X_AXIS = 20; | ||
|
||
private static final int IMAGES_Y_AXIS = 10; | ||
|
||
private Html() { | ||
} | ||
|
||
private static String url(int port) { | ||
return IP + ":" + port + "/http2"; | ||
} | ||
|
||
public static byte[] body(int latency) { | ||
int r = Math.abs(new Random().nextInt()); | ||
// The string to be built contains 13192 fixed characters plus the variable latency and random cache-bust. | ||
int numberOfCharacters = 13192 + stringLength(latency) + stringLength(r); | ||
StringBuilder sb = new StringBuilder(numberOfCharacters).append("<div id=\"netty\">"); | ||
for (int y = 0; y < IMAGES_Y_AXIS; y++) { | ||
for (int x = 0; x < IMAGES_X_AXIS; x++) { | ||
sb.append("<img width=30 height=29 src='/http2?x=") | ||
.append(x) | ||
.append("&y=").append(y) | ||
.append("&cachebust=").append(r) | ||
.append("&latency=").append(latency) | ||
.append("'>"); | ||
} | ||
sb.append("<br/>\r\n"); | ||
} | ||
sb.append("</div>"); | ||
return sb.toString().getBytes(UTF_8); | ||
} | ||
|
||
private static int stringLength(int value) { | ||
return Integer.toString(value).length() * IMAGES_X_AXIS * IMAGES_Y_AXIS; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
example/src/main/java/io/netty/example/http2/tiles/Http1RequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2015 The Netty Project | ||
* | ||
* The Netty Project licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.netty.example.http2.tiles; | ||
|
||
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; | ||
import static io.netty.handler.codec.http.HttpHeaderUtil.isKeepAlive; | ||
import static io.netty.handler.codec.http.HttpResponseStatus.CONTINUE; | ||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpHeaderUtil; | ||
import io.netty.handler.codec.http.HttpHeaderValues; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Handles the requests for the tiled image using HTTP 1.x as a protocol. | ||
*/ | ||
public final class Http1RequestHandler extends Http2RequestHandler { | ||
|
||
@Override | ||
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { | ||
if (HttpHeaderUtil.is100ContinueExpected(request)) { | ||
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); | ||
} | ||
super.channelRead0(ctx, request); | ||
} | ||
|
||
@Override | ||
protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency, | ||
final FullHttpResponse response, final FullHttpRequest request) { | ||
HttpHeaderUtil.setContentLength(response, response.content().readableBytes()); | ||
ctx.executor().schedule(new Runnable() { | ||
@Override | ||
public void run() { | ||
if (isKeepAlive(request)) { | ||
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); | ||
ctx.writeAndFlush(response); | ||
} else { | ||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); | ||
} | ||
} | ||
}, latency, TimeUnit.MILLISECONDS); | ||
} | ||
} |
Oops, something went wrong.