Example usage for io.netty.handler.codec.http HttpVersion valueOf

List of usage examples for io.netty.handler.codec.http HttpVersion valueOf

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpVersion valueOf.

Prototype

public static HttpVersion valueOf(String text) 

Source Link

Document

Returns an existing or new HttpVersion instance which matches to the specified protocol version string.

Usage

From source file:cn.wantedonline.puppy.httpserver.component.HttpRequestDecoder.java

License:Apache License

@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
    HttpMethod method = HttpMethod.valueOf(initialLine[0]);
    try {//from   w ww . j a  v  a 2  s .  c o m
        return new HttpRequest(HttpVersion.valueOf(initialLine[2]), method, initialLine[1]);
    } catch (Exception e) {
        String fix = initialLine[1] + " " + initialLine[2];
        int result = 0;
        for (result = fix.length(); result > 0; --result) {
            if (Character.isWhitespace(fix.charAt(result - 1))) {
                break;
            }
        }
        String version = fix.substring(result);
        for (; result > 0; --result) {
            if (!Character.isWhitespace(fix.charAt(result - 1))) {
                break;
            }
        }
        String uri = fix.substring(0, result);
        uri = uri.replaceAll("\t", "%09").replaceAll("\n", "%0D").replaceAll("\r", "%0A").replaceAll(" ", "+");
        log.error(
                "parse httpRequest initialLine fail!\n\tori:{}\n\t      fix:{}\n\t      uri:{}\n\t  version:{}\n\t{}",
                new Object[] { Arrays.toString(initialLine), fix, uri, version, e.getMessage() });
        return new HttpRequest(HttpVersion.valueOf(version), method, uri);
    }
}

From source file:com.creamsugardonut.CustomHttpRequestDecoder.java

License:Apache License

@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
    return new DefaultHttpRequest(HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]),
            initialLine[1], validateHeaders);
}

From source file:io.higgs.http.server.HttpRequestDecoder.java

License:Apache License

@Override
protected HttpMessage createMessage(String[] initialLine) throws Exception {
    return new HttpRequest(HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]),
            initialLine[1]);/*  w  w w .  j  a v a  2 s  .  co m*/
}

From source file:io.vertx.core.http.impl.VertxHttpRequestDecoder.java

License:Open Source License

@Override
protected HttpMessage createMessage(String[] initialLine) {
    return new DefaultHttpRequest(HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]),
            initialLine[1], new VertxHttpHeaders());
}

From source file:io.werval.server.netty.WervalHttpHandler.java

License:Apache License

private ChannelFuture writeOutcome(ChannelHandlerContext nettyContext, Outcome outcome) {
    // == Build the Netty Response
    ResponseHeader responseHeader = outcome.responseHeader();

    // Netty Version & Status
    HttpVersion responseVersion = HttpVersion.valueOf(responseHeader.version().toString());
    HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(responseHeader.status().code());

    // Netty Headers & Body output
    final HttpResponse nettyResponse;
    final ChannelFuture writeFuture;
    if (outcome instanceof ChunkedInputOutcome) {
        ChunkedInputOutcome chunkedOutcome = (ChunkedInputOutcome) outcome;
        nettyResponse = new DefaultHttpResponse(responseVersion, responseStatus);
        // Headers
        applyResponseHeader(responseHeader, nettyResponse);
        nettyResponse.headers().set(TRANSFER_ENCODING, CHUNKED);
        nettyResponse.headers().set(TRAILER, X_WERVAL_CONTENT_LENGTH);
        // Body/*  w  w w  .  j ava2s  .  c om*/
        nettyContext.write(nettyResponse);
        writeFuture = nettyContext.writeAndFlush(new HttpChunkedBodyEncoder(
                new ChunkedStream(chunkedOutcome.inputStream(), chunkedOutcome.chunkSize())));
    } else if (outcome instanceof InputStreamOutcome) {
        InputStreamOutcome streamOutcome = (InputStreamOutcome) outcome;
        nettyResponse = new DefaultFullHttpResponse(responseVersion, responseStatus);
        // Headers
        applyResponseHeader(responseHeader, nettyResponse);
        nettyResponse.headers().set(CONTENT_LENGTH, streamOutcome.contentLength());
        // Body
        try (InputStream bodyInputStream = streamOutcome.bodyInputStream()) {
            ((ByteBufHolder) nettyResponse).content().writeBytes(bodyInputStream,
                    new BigDecimal(streamOutcome.contentLength()).intValueExact());
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
        writeFuture = nettyContext.writeAndFlush(nettyResponse);
    } else if (outcome instanceof SimpleOutcome) {
        SimpleOutcome simpleOutcome = (SimpleOutcome) outcome;
        byte[] body = simpleOutcome.body().asBytes();
        nettyResponse = new DefaultFullHttpResponse(responseVersion, responseStatus);
        // Headers
        applyResponseHeader(responseHeader, nettyResponse);
        nettyResponse.headers().set(CONTENT_LENGTH, body.length);
        // Body
        ((ByteBufHolder) nettyResponse).content().writeBytes(body);
        writeFuture = nettyContext.writeAndFlush(nettyResponse);
    } else {
        LOG.warn("{} Unhandled Outcome type '{}', no response body.", requestIdentity, outcome.getClass());
        nettyResponse = new DefaultFullHttpResponse(responseVersion, responseStatus);
        applyResponseHeader(responseHeader, nettyResponse);
        writeFuture = nettyContext.writeAndFlush(nettyResponse);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("{} Sent a HttpResponse:\n{}", requestIdentity, nettyResponse.toString());
    }

    // Close the connection as soon as the response is sent if not keep alive
    if (!outcome.responseHeader().isKeepAlive() || nettyContext.executor().isShuttingDown()) {
        writeFuture.addListener(ChannelFutureListener.CLOSE);
    }

    // Done!
    return writeFuture;
}

From source file:org.ebayopensource.scc.cache.NettyResponseDeserializer.java

License:Apache License

@Override
public FullHttpResponse deserialize(CacheResponse cacheResp) {
    CompositeByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.compositeBuffer();
    if (cacheResp.getContent() != null) {
        byteBuf.capacity(cacheResp.getContent().length);
        byteBuf.setBytes(0, cacheResp.getContent());
        byteBuf.writerIndex(cacheResp.getContent().length);
    }//  w ww  . j a v a  2  s  . co  m
    DefaultFullHttpResponse response = new DefaultFullHttpResponse(
            HttpVersion.valueOf(cacheResp.getProtocalVersion()),
            new HttpResponseStatus(cacheResp.getCode(), cacheResp.getReasonPhrase()), byteBuf, true);
    HttpHeaders headers = response.headers();
    List<CacheEntry<String, String>> cacheHeaders = cacheResp.getHeaders();
    for (Entry<String, String> entry : cacheHeaders) {
        headers.add(entry.getKey(), entry.getValue());
    }

    HttpHeaders trailingHeaders = response.trailingHeaders();
    List<CacheEntry<String, String>> cacheTrailingHeaders = cacheResp.getTrailingHeaders();
    for (Entry<String, String> entry : cacheTrailingHeaders) {
        trailingHeaders.add(entry.getKey(), entry.getValue());
    }

    return response;
}