io.netty.handler.codec.http.FullHttpRequest # getProtocolVersion ( ) 源码实例Demo

下面列出了 io.netty.handler.codec.http.FullHttpRequest # getProtocolVersion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: minnal   文件: Router.java

/**
 * Creates the http response from container response</p>
 * 
 * <li> Sets the content length to the http response from the container response. If content length is not available, computes from the response entity
 * <li> 
 * 
 * @param context
 * @param containerResponse
 * @param buffer
 * @return
 */
protected FullHttpResponse createHttpResponse(MessageContext context, ContainerResponse containerResponse, ByteBuf buffer) {
	FullHttpRequest httpRequest = context.getRequest();
	DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.valueOf(containerResponse.getStatus()), buffer);
	int length = containerResponse.getLength();
	
	// FIXME Hack. is there a better way?
	if (length == -1 && containerResponse.getEntity() instanceof String) {
		final String entity = (String) containerResponse.getEntity();
		final byte[] encodedBytes = entity.getBytes(Charset.forName("UTF-8"));
		length = encodedBytes.length;
	}
	if (! containerResponse.getHeaders().containsKey(HttpHeaders.Names.CONTENT_LENGTH)) {
		HttpHeaders.setContentLength(httpResponse, length);
		logger.trace("Writing response status and headers {}, length {}", containerResponse, containerResponse.getLength());
	}

	for (Map.Entry<String, List<Object>> headerEntry : containerResponse.getHeaders().entrySet()) {
		HttpHeaders.addHeader(httpResponse, headerEntry.getKey(), Joiner.on(", ").join(headerEntry.getValue()));
	}
	return httpResponse;
}
 

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
	
	if( wsURI.equalsIgnoreCase(request.getUri()) ) {
		
		ctx.fireChannelRead(request.retain());
	
	} else {
		
		if( HttpHeaders.is100ContinueExpected(request) ) {
			send100Continue(ctx);
		}
		
		try (
			RandomAccessFile rFile = new RandomAccessFile(indexHTML, "r")
		) {
			HttpResponse response = new DefaultHttpResponse( request.getProtocolVersion(), HttpResponseStatus.OK );
			response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8");
			boolean keepAlive = HttpHeaders.isKeepAlive(request);
			if( keepAlive ) {
				response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, rFile.length());
				response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
			}
			
			ctx.write(response);
			
			if( ctx.pipeline().get(SslHandler.class) == null ) {
				ctx.write(new DefaultFileRegion(rFile.getChannel(), 0, rFile.length()));
			} else {
				ctx.write(new ChunkedNioFile(rFile.getChannel()));
			}
			
			ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
			
			if( !keepAlive ) {
				future.addListener(ChannelFutureListener.CLOSE);
			}
		}
	}
}
 
源代码3 项目: happor   文件: HttpRootController.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
		throws Exception {
	// TODO Auto-generated method stub
	if (msg instanceof FullHttpRequest) {
		request = (FullHttpRequest) msg;
		response = new DefaultFullHttpResponse(
				request.getProtocolVersion(), HttpResponseStatus.OK);
		
		HapporContext happorContext = server.getContext(request);
		if (happorContext == null) {
			logger.error("cannot find path for URI: " + request.getUri());
			response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
			ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
			return;
		}
		
		HttpController lastController = null;
		
		Map<String, ControllerRegistry> controllers = happorContext.getControllers();
		for (Map.Entry<String, ControllerRegistry> entry : controllers.entrySet()) {
			ControllerRegistry registry = entry.getValue();
			String method = registry.getMethod();
			String uriPattern = registry.getUriPattern();
			UriParser uriParser = new UriParser(request.getUri());
			
			if (isMethodMatch(method) && isUriMatch(uriParser, uriPattern)) {
				HttpController controller = happorContext.getController(registry.getClazz());
				controller.setPrev(lastController);
				controller.setServer(server);
				controller.setUriParser(uriParser);
				boolean isEnd = controller.input(ctx, request, response);
				if (isEnd) {
					break;
				}
				lastController = controller;
			}
		}
	}
}
 
源代码4 项目: termd   文件: HttpRequestHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
  if (wsUri.equalsIgnoreCase(request.getUri())) {
    ctx.fireChannelRead(request.retain());
  } else {
    if (HttpHeaders.is100ContinueExpected(request)) {
      send100Continue(ctx);
    }

    HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

    String path = new URI(request.getUri()).getPath();

    if ("/".equals(path)) {
      path = "/index.html";
    }
    URL res = HttpTtyConnection.class.getResource(httpResourcePath + path);
    try {
      if (res != null) {
        DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        InputStream in = res.openStream();
        byte[] tmp = new byte[256];
        for (int l = 0; l != -1; l = in.read(tmp)) {
          fullResp.content().writeBytes(tmp, 0, l);
        }
        int li = path.lastIndexOf('.');
        if (li != -1 && li != path.length() - 1) {
          String ext = path.substring(li + 1, path.length());
          String contentType;
          if ("html".equals(ext)) {
            contentType = "text/html";
          } else if ("js".equals(ext)) {
            contentType = "application/javascript";
          } else if ("css".equals(ext)) {
              contentType = "text/css";
          } else {
            contentType = null;
          }

          if (contentType != null) {
            fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
          }
        }
        response = fullResp;
      } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ctx.write(response);
      ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
      future.addListener(ChannelFutureListener.CLOSE);
    }
  }
}
 

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    if (wsUri.equalsIgnoreCase(request.getUri())) {
        ctx.fireChannelRead(request.retain());
    } else {
        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }

        HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

        String path = request.getUri();
        if ("/".equals(path)) {
            path = "/index.html";
        }
        URL res = HttpTtyConnection.class.getResource("/org/aesh/terminal/http" + path);
        try {
            if (res != null) {
                DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
                InputStream in = res.openStream();
                byte[] tmp = new byte[256];
                for (int l = 0; l != -1; l = in.read(tmp)) {
                    fullResp.content().writeBytes(tmp, 0, l);
                }
                int li = path.lastIndexOf('.');
                if (li != -1 && li != path.length() - 1) {
                    String ext = path.substring(li + 1, path.length());
                    String contentType;
                    switch (ext) {
                        case "html":
                            contentType = "text/html";
                            break;
                        case "js":
                            contentType = "application/javascript";
                            break;
                        default:
                            contentType = null;
                            break;
                    }
                    if (contentType != null) {
                        fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
                    }
                }
                response = fullResp;
            } else {
                response.setStatus(HttpResponseStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ctx.write(response);
            ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }
}
 
源代码6 项目: termd   文件: HttpRequestHandler.java

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
  if (wsUri.equalsIgnoreCase(request.getUri())) {
    ctx.fireChannelRead(request.retain());
  } else {
    if (HttpHeaders.is100ContinueExpected(request)) {
      send100Continue(ctx);
    }

    HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR);

    String path = request.getUri();
    if ("/".equals(path)) {
      path = "/index.html";
    }
    URL res = HttpTtyConnection.class.getResource("/io/termd/core/http" + path);
    try {
      if (res != null) {
        DefaultFullHttpResponse fullResp = new DefaultFullHttpResponse(request.getProtocolVersion(), HttpResponseStatus.OK);
        InputStream in = res.openStream();
        byte[] tmp = new byte[256];
        for (int l = 0; l != -1; l = in.read(tmp)) {
          fullResp.content().writeBytes(tmp, 0, l);
        }
        int li = path.lastIndexOf('.');
        if (li != -1 && li != path.length() - 1) {
          String ext = path.substring(li + 1, path.length());
          String contentType;
          switch (ext) {
            case "html":
              contentType = "text/html";
              break;
            case "js":
              contentType = "application/javascript";
              break;
            default:
              contentType = null;
              break;
          }
          if (contentType != null) {
            fullResp.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType);
          }
        }
        response = fullResp;
      } else {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ctx.write(response);
      ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
      future.addListener(ChannelFutureListener.CLOSE);
    }
  }
}