Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74e0805

Browse files
committed
add
1 parent 2f33d0d commit 74e0805

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

‎NETTY BY EXAMPLE/Implementation.md‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
实现
2+
====
3+
4+
SPDY 使用 TLS 的扩展称为 Next Protocol Negotiation (NPN)。在Java 中,我们有两种不同的方式选择的基于 NPN 的协议:
5+
* 使用 ssl_npn,NPN 的开源 SSL 提供者。
6+
* 使用通过 Jetty 的 NPN 扩展库。
7+
8+
在这个例子中使用 Jetty 库。如果你想使用 ssl_npn,请参阅<https://github.com/benmmurphy/ssl_npn>项目文档
9+
10+
*Jetty NPN 库*
11+
12+
*Jetty NPN 库是一个外部的库,而不是 Netty 的本身的一部分。它用于处理 Next Protocol Negotiation, 这是用于检测客户端是否支持 SPDY。*
13+
14+
### 集成 Next Protocol Negotiation
15+
16+
Jetty 库提供了一个接口称为 ServerProvider,确定所使用的协议和选择哪个钩子。这个的实现可能取决于不同版本的 HTTP 和 SPDY 版本的支持。下面的清单显示了将用于我们的示例应用程序的实现。
17+
18+
Listing 12.1 Implementation of ServerProvider
19+
20+
public class DefaultServerProvider implements NextProtoNego.ServerProvider {
21+
private static final List<String> PROTOCOLS =
22+
Collections.unmodifiableList(Arrays.asList("spdy/2", "spdy/3", "http/1.1")); //1
23+
24+
private String protocol;
25+
26+
@Override
27+
public void unsupported() {
28+
protocol = "http/1.1"; //2
29+
}
30+
31+
@Override
32+
public List<String> protocols() {
33+
return PROTOCOLS; //3
34+
}
35+
36+
@Override
37+
public void protocolSelected(String protocol) {
38+
this.protocol = protocol; //4
39+
}
40+
41+
public String getSelectedProtocol() {
42+
return protocol; //5
43+
}
44+
}
45+
46+
1. 定义所有的 ServerProvider 实现的协议
47+
2. 设置如果 SPDY 协议失败了就转到 http/1.1
48+
3. 返回支持的协议的列表
49+
4. 设置选择的协议
50+
5. 返回选择的协议
51+
52+
在 ServerProvider 的实现,我们支持下面的3种协议:
53+
54+
* SPDY 2
55+
* SPDY 3
56+
* HTTP 1.1
57+
58+
如果客户端不支持 SPDY ,则默认使用 HTTP 1.1
59+
60+
#### 实现各种 ChannelHandler
61+
62+
第一个 ChannelInboundHandler 是用于不支持 SPDY 的情况下处理客户端 HTTP 请求,如果不支持 SPDY 就回滚使用默认的 HTTP 协议。
63+
64+
清单12.2显示了HTTP流量的处理程序。
65+
66+
Listing 12.2 Implementation that handles HTTP
67+
68+
@ChannelHandler.Sharable
69+
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
70+
@Override
71+
public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { //1
72+
if (HttpHeaders.is100ContinueExpected(request)) {
73+
send100Continue(ctx); //2
74+
}
75+
76+
FullHttpResponse response = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK); //3
77+
response.content().writeBytes(getContent().getBytes(CharsetUtil.UTF_8)); //4
78+
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); //5
79+
80+
boolean keepAlive = HttpHeaders.isKeepAlive(request);
81+
82+
if (keepAlive) { //6
83+
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
84+
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
85+
}
86+
ChannelFuture future = ctx.writeAndFlush(response); //7
87+
88+
if (!keepAlive) {
89+
future.addListener (ChannelFutureListener.CLOSE); //8
90+
}
91+
}
92+
93+
protected String getContent() { //9
94+
return "This content is transmitted via HTTP\r\n";
95+
}
96+
97+
private static void send100Continue(ChannelHandlerContext ctx) { //10
98+
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
99+
ctx.writeAndFlush(response);
100+
}
101+
102+
@Override
103+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
104+
throws Exception { //11
105+
cause.printStackTrace();
106+
ctx.close();
107+
}
108+
}
109+
110+
1. 重写 channelRead0() ,可以被所有的接收到的 FullHttpRequest 调用
111+
2. 检查如果接下来的响应是预期的,就写入
112+
3. 新建 FullHttpResponse,用于对请求的响应
113+
4. 生成响应的内容,将它写入 payload
114+
5. 设置头文件,这样客户端就能知道如何与 响应的 payload 交互
115+
6. 检查请求设置是否启用了 keepalive;如果是这样,将标题设置为符合HTTP RFC
116+
7. 写响应给客户端,并获取到 Future 的引用,用于写完成时,获取到通知
117+
8. 如果响应不是 keepalive,在写完成时关闭连接
118+
9. 返回内容作为响应的 payload
119+
10. Helper 方法生成了100 持续的响应,并写回给客户端
120+
11. 若执行阶段抛出异常,则关闭管道
121+

‎SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ This is the summary of my book.
7878
* [SPDY](NETTY BY EXAMPLE/SPDY.md)
7979
* [SPDY 背景](NETTY BY EXAMPLE/SPDY background.md)
8080
* [示例程序](NETTY BY EXAMPLE/The sample application.md)
81+
* [实现](NETTY BY EXAMPLE/Implementation.md)
8182
* [通过 UDP 广播事件](NETTY BY EXAMPLE/Broadcasting events with UDP.md)
8283
* 高级主题
8384
* [实现自定义编解码器](ADVANCED TOPICS/Implement a custom codec.md)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /