Search in sources :

Example 1 with IRequestHandler

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;
}
Also used : IRequestHandler(com.feeyo.net.http.handler.IRequestHandler) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpMethod(org.jboss.netty.handler.codec.http.HttpMethod)

Example 2 with IRequestHandler

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);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) IRequestHandler(com.feeyo.net.http.handler.IRequestHandler) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Type(com.feeyo.net.http.handler.IRequestHandler.Type) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) ResourceFileDownloadGetHandler(com.feeyo.net.http.handler.ResourceFileDownloadGetHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IRequestHandler (com.feeyo.net.http.handler.IRequestHandler)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Type (com.feeyo.net.http.handler.IRequestHandler.Type)1 ResourceFileDownloadGetHandler (com.feeyo.net.http.handler.ResourceFileDownloadGetHandler)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1 HttpMethod (org.jboss.netty.handler.codec.http.HttpMethod)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)1