use of com.feeyo.net.http.handler.IRequestHandler in project feeyo-hlsserver by variflight.
the class HttpServerRequestHandler method getHandler.
private IRequestHandler getHandler(HttpRequest request) {
IRequestHandler handler = null;
// 解析QueryString
String uriString = request.getUri();
// 获取Path
String path = null;
int pathEndPos = uriString.indexOf('?');
if (pathEndPos < 0) {
path = uriString;
} else {
path = uriString.substring(0, pathEndPos);
}
// 获取参数
Map<String, String> parameters = new HashMap<String, String>();
if (uriString.startsWith("?")) {
uriString = uriString.substring(1, uriString.length());
}
String[] querys = uriString.split("&");
for (String query : querys) {
String[] pair = query.split("=");
if (pair.length == 2) {
try {
parameters.put(URLDecoder.decode(pair[0], "UTF8"), URLDecoder.decode(pair[1], "UTF8"));
} catch (UnsupportedEncodingException e) {
parameters.put(pair[0], pair[1]);
}
}
}
HttpMethod method = request.getMethod();
if (method == HttpMethod.GET) {
handler = getHandlers.retrieve(path, parameters);
} else if (method == HttpMethod.POST) {
handler = postHandlers.retrieve(path, parameters);
}
return handler;
}
use of com.feeyo.net.http.handler.IRequestHandler in project feeyo-hlsserver by variflight.
the class HttpServerRequestHandler method processHttpRequest.
private void processHttpRequest(ChannelHandlerContext ctx, MessageEvent e) {
HttpRequest request = (DefaultHttpRequest) e.getMessage();
String uri = request.getUri();
IRequestHandler requestHandler = getHandler(request);
if (requestHandler != null) {
boolean isFilted = requestHandler.isFilted();
if (isFilted && !processFilter(ctx, e, requestHandler)) {
IRequestHandler.Type type = requestHandler.getType();
if (type == IRequestHandler.Type.VM) {
sendRedirect(ctx, "/v1/view/login");
} else {
HttpResponse response = buildDefaultResponse("", HttpResponseStatus.UNAUTHORIZED);
sendResponse(ctx, response);
}
return;
}
} else {
// path 路由不成功, 检测是否是静态文件下载
int lastDot = uri.lastIndexOf('.');
if (lastDot != -1) {
String extension = uri.substring(lastDot + 1).toLowerCase();
if (extension != null && extension.length() < 6) {
requestHandler = new ResourceFileDownloadGetHandler();
}
}
}
if (requestHandler != null) {
try {
requestHandler.execute(ctx, e);
} catch (Exception e1) {
LOGGER.error("http handler err:", e1);
HttpResponse response = buildErrorResponse("internal error");
sendResponse(ctx, response);
}
} else {
HttpResponse response = buildDefaultResponse("", HttpResponseStatus.NOT_FOUND);
sendResponse(ctx, response);
}
}
Aggregations