NettyHttpServerHandler.java
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.genersoft.iot.vmp.jt1078.http;
import com.genersoft.iot.vmp.jt1078.app.VideoServerApp;
import com.genersoft.iot.vmp.jt1078.entity.Media.Type;
import com.genersoft.iot.vmp.jt1078.publisher.PublishManager;
import com.genersoft.iot.vmp.jt1078.server.Jtt1078Handler;
import com.genersoft.iot.vmp.jt1078.server.Session;
import com.genersoft.iot.vmp.jt1078.util.FileUtils;
import com.genersoft.iot.vmp.jt1078.util.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
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.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Objects;
public class NettyHttpServerHandler extends ChannelInboundHandlerAdapter {
private String tagMapping;
private Integer httpPort;
static Logger logger = LoggerFactory.getLogger(NettyHttpServerHandler.class);
static final byte[] HTTP_403_DATA = "<h1>403 Forbidden</h1><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding--><!--padding-->".getBytes();
static final byte[] SUCCESS = "{code:0}".getBytes();
static final String HEADER_ENCODING = "ISO-8859-1";
private static final AttributeKey<Session> SESSION_KEY = AttributeKey.valueOf("session");
public NettyHttpServerHandler(String tagMapping, Integer httpPort) {
this.tagMapping = tagMapping;
this.httpPort = httpPort;
}
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
FullHttpRequest fhr = (FullHttpRequest) msg;
String uri = fhr.uri();
Packet resp = Packet.create(1024);
String tagMapping;
long wid;
if (uri.startsWith("/video/")) {
tagMapping = uri.substring("/video/".length());
resp.addBytes("HTTP/1.1 200 OK\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Connection: keep-alive\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Content-Type: video/x-flv\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Transfer-Encoding: chunked\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Cache-Control: no-cache\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Origin: *\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Credentials: true\r\n".getBytes("ISO-8859-1"));
resp.addBytes("\r\n".getBytes("ISO-8859-1"));
ctx.writeAndFlush(resp.getBytes()).await();
logger.info("Thread id:[{}]", Thread.currentThread().getId());
if (StringUtils.isEmpty(this.tagMapping)) {
this.tagMapping = tagMapping;
}
wid = PublishManager.getInstance().subscribe(this.tagMapping, Type.Video, ctx, this.httpPort).getId();
this.setSession(ctx, (new Session()).set("subscriber-id", wid).set("tag", this.tagMapping));
// if (wid == 0) {
try {
Jtt1078Handler.createStreamProxy(this.tagMapping, httpPort);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
// }
} else if (uri.equals("/test/multimedia")) {
this.responseHTMLFile("/multimedia.html", ctx);
} else {
String httpPort;
if (uri.startsWith("/stop/channel/")) {
resp.addBytes("HTTP/1.1 200 OK\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Connection: keep-alive\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Content-Type: video/x-flv\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Transfer-Encoding: chunked\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Cache-Control: no-cache\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Origin: *\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Credentials: true\r\n".getBytes("ISO-8859-1"));
tagMapping = uri.substring("/stop/channel/".length());
String str = uri.substring("/stop/channel/".length());
int endIndex = StringUtils.indexOf(str, "/");
StringUtils.substring(str, 0, endIndex);
Integer startIndex = endIndex + 1;
endIndex = StringUtils.indexOf(str, "/", startIndex);
httpPort = StringUtils.substring(str, startIndex, endIndex);
startIndex = endIndex + 1;
httpPort = StringUtils.substring(str, startIndex, str.length());
PublishManager publishManager = PublishManager.getInstance();
publishManager.unsubscribeAndClose(tagMapping);
VideoServerApp.stopServer(Integer.parseInt(httpPort), Integer.parseInt(httpPort));
logger.info("{}停流", tagMapping);
ByteBuf body = Unpooled.buffer(SUCCESS.length);
body.writeBytes(SUCCESS);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(200), body);
response.headers().add("Content-Length", SUCCESS.length);
ctx.writeAndFlush(response).await();
ctx.flush();
} else if (uri.startsWith("/new/server/")) {
resp.addBytes("HTTP/1.1 200 OK\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Connection: keep-alive\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Content-Type: video/x-flv\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Transfer-Encoding: chunked\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Cache-Control: no-cache\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Origin: *\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Credentials: true\r\n".getBytes("ISO-8859-1"));
tagMapping = uri.substring("/new/server/".length());
int endIndex = StringUtils.indexOf(tagMapping, "/");
String key = StringUtils.substring(tagMapping, 0, endIndex);
Integer startIndex = endIndex + 1;
endIndex = StringUtils.indexOf(tagMapping, "/", startIndex);
String port = StringUtils.substring(tagMapping, startIndex, endIndex);
startIndex = endIndex + 1;
httpPort = StringUtils.substring(tagMapping, startIndex, tagMapping.length());
ByteBuf body = Unpooled.buffer(SUCCESS.length);
body.writeBytes(SUCCESS);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(200), body);
response.headers().add("Content-Length", SUCCESS.length);
ctx.writeAndFlush(response).await();
ctx.flush();
} else if (uri.startsWith("/play/history/")) {
tagMapping = uri.substring("/play/history/".length());
resp.addBytes("HTTP/1.1 200 OK\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Connection: keep-alive\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Content-Type: video/x-flv\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Transfer-Encoding: chunked\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Cache-Control: no-cache\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Origin: *\r\n".getBytes("ISO-8859-1"));
resp.addBytes("Access-Control-Allow-Credentials: true\r\n".getBytes("ISO-8859-1"));
resp.addBytes("\r\n".getBytes("ISO-8859-1"));
ctx.writeAndFlush(resp.getBytes()).await();
logger.info("Thread id:[{}]", Thread.currentThread().getId());
wid = PublishManager.getInstance().subscribe(tagMapping, Type.Video, ctx, this.httpPort).getId();
this.setSession(ctx, (new Session()).set("subscriber-id", wid).set("tag", tagMapping));
} else {
ByteBuf body = Unpooled.buffer(HTTP_403_DATA.length);
body.writeBytes(HTTP_403_DATA);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(403), body);
response.headers().add("Content-Length", HTTP_403_DATA.length);
ctx.writeAndFlush(response).await();
ctx.flush();
}
}
}
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
Session session = this.getSession(ctx);
if (session != null && session.has("subscriber-id") && session.has("tag")) {
String tag = (String) session.get("tag");
Long wid = (Long) session.get("subscriber-id");
PublishManager.getInstance().unsubscribe(tag, wid);
}
}
private void responseHTMLFile(String htmlFilePath, ChannelHandlerContext ctx) {
byte[] fileData = FileUtils.read(NettyHttpServerHandler.class.getResourceAsStream(htmlFilePath));
ByteBuf body = Unpooled.buffer(fileData.length);
body.writeBytes(fileData);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(200), body);
response.headers().add("Content-Length", fileData.length);
ctx.write(response);
ctx.flush();
}
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
cause.printStackTrace();
}
public final void setSession(ChannelHandlerContext context, Session session) {
context.channel().attr(SESSION_KEY).set(session);
}
public final Session getSession(ChannelHandlerContext context) {
Attribute<Session> attr = context.channel().attr(SESSION_KEY);
return null == attr ? null : (Session) attr.get();
}
}