Java Code Examples for io.netty.handler.codec.http.FullHttpRequest#getProtocolVersion()

The following examples show how to use io.netty.handler.codec.http.FullHttpRequest#getProtocolVersion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Router.java    From minnal with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: EchoServerHttpRequestHandler.java    From examples-javafx-repos1 with Apache License 2.0 5 votes vote down vote up
@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);
			}
		}
	}
}
 
Example 3
Source File: HttpRootController.java    From happor with MIT License 5 votes vote down vote up
@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;
			}
		}
	}
}
 
Example 4
Source File: HttpRequestHandler.java    From termd with Apache License 2.0 4 votes vote down vote up
@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);
    }
  }
}
 
Example 5
Source File: HttpRequestHandler.java    From aesh-readline with Apache License 2.0 4 votes vote down vote up
@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);
        }
    }
}
 
Example 6
Source File: HttpRequestHandler.java    From termd with Apache License 2.0 4 votes vote down vote up
@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);
    }
  }
}