Example usage for io.netty.handler.codec.http HttpHeaderNames TRANSFER_ENCODING

List of usage examples for io.netty.handler.codec.http HttpHeaderNames TRANSFER_ENCODING

Introduction

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

Prototype

AsciiString TRANSFER_ENCODING

To view the source code for io.netty.handler.codec.http HttpHeaderNames TRANSFER_ENCODING.

Click Source Link

Document

"transfer-encoding"

Usage

From source file:com.bunjlabs.fuga.network.netty.NettyHttpServerHandler.java

License:Apache License

private void writeResponse(ChannelHandlerContext ctx, Request request, Response response) {
    HttpResponse httpresponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.valueOf(response.status()));

    httpresponse.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    httpresponse.headers().set(HttpHeaderNames.CONTENT_TYPE, response.contentType());

    // Disable cache by default
    httpresponse.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, no-store, must-revalidate, max-age=0");
    httpresponse.headers().set(HttpHeaderNames.PRAGMA, "no-cache");
    httpresponse.headers().set(HttpHeaderNames.EXPIRES, "0");

    response.headers().entrySet().stream().forEach((e) -> httpresponse.headers().set(e.getKey(), e.getValue()));

    httpresponse.headers().set(HttpHeaderNames.SERVER, "Fuga Netty Web Server/" + serverVersion);

    // Set cookies
    httpresponse.headers().set(HttpHeaderNames.SET_COOKIE,
            ServerCookieEncoder.STRICT.encode(NettyCookieConverter.convertListToNetty(response.cookies())));

    if (response.length() >= 0) {
        httpresponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.length());
    }/*  ww w  .  ja  v a 2s . c  o  m*/

    if (HttpUtil.isKeepAlive(httprequest)) {
        httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    } else {
        httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    }

    ctx.write(httpresponse);

    if (response.stream() != null) {
        ctx.write(new HttpChunkedInput(new ChunkedStream(response.stream())));
    }

    LastHttpContent fs = new DefaultLastHttpContent();
    ChannelFuture sendContentFuture = ctx.writeAndFlush(fs);
    if (!HttpUtil.isKeepAlive(httprequest)) {
        sendContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

private HttpObject convertClientHeaders(int streamId, HttpHeaders headers) throws Http2Exception {

    // Leading headers will always have :method, trailers will never have it.
    final HttpMethod method = headers.method();
    if (method == null) {
        return convertTrailingHeaders(streamId, headers);
    }//w  ww .  j av a 2  s. com

    // Convert leading headers.
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method.toNettyMethod(), headers.path(),
            false);

    convert(streamId, headers, req.headers(), false);
    if (HttpUtil.getContentLength(req, -1L) >= 0) {
        // Avoid the case where both 'content-length' and 'transfer-encoding' are set.
        req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
    } else {
        req.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }

    return req;
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

private static void setTransferEncoding(HttpMessage out) {
    final io.netty.handler.codec.http.HttpHeaders outHeaders = out.headers();
    final long contentLength = HttpUtil.getContentLength(out, -1L);
    if (contentLength < 0) {
        // Use chunked encoding.
        outHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        outHeaders.remove(HttpHeaderNames.CONTENT_LENGTH);
    }//from   w  w  w . j a v  a2s  . com
}

From source file:com.linecorp.armeria.internal.Http1ObjectEncoder.java

License:Apache License

private HttpObject convertClientHeaders(int streamId, HttpHeaders headers, boolean endStream)
        throws Http2Exception {

    // Leading headers will always have :method, trailers will never have it.
    final HttpMethod method = headers.method();
    if (method == null) {
        return convertTrailingHeaders(streamId, headers);
    }//from   w  ww .  j  a  v  a2s . co  m

    // Convert leading headers.
    final String path = headers.path();
    assert path != null;
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
            io.netty.handler.codec.http.HttpMethod.valueOf(method.name()), path, false);

    convert(streamId, headers, req.headers(), false);

    if (endStream) {
        req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
        req.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
    } else if (HttpUtil.getContentLength(req, -1L) >= 0) {
        // Avoid the case where both 'content-length' and 'transfer-encoding' are set.
        req.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
    } else {
        req.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }

    return req;
}

From source file:com.linkedin.r2.transport.http.client.NettyRequestAdapter.java

License:Apache License

/**
 * Adapts a StreamRequest to Netty's HttpRequest
 * @param request  R2 stream request/*from   ww  w  . j a  va  2s  .co  m*/
 * @return Adapted HttpRequest.
 */
static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
    HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
    URL url = new URL(request.getURI().toString());
    String path = url.getFile();
    // RFC 2616, section 5.1.2:
    //   Note that the absolute path cannot be empty; if none is present in the original URI,
    //   it MUST be given as "/" (the server root).
    if (path.isEmpty()) {
        path = "/";
    }

    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
    nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);

    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        // RFC 7230, section 3.3.2
        //   A sender MUST NOT send a Content-Length header field in any message
        //   that contains a Transfer-Encoding header field.
        if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
            continue;
        }

        nettyRequest.headers().set(entry.getKey(), entry.getValue());
    }
    nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
    nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, request.getCookies());

    return nettyRequest;
}

From source file:com.ociweb.pronghorn.adapter.netty.impl.HttpStaticFileServerHandler.java

License:Apache License

public static void beginHTTPResponse(ChannelHandlerContext ctx, FullHttpRequest request, long lastModified,
        String path, long fileLength) {
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    HttpUtil.setContentLength(response, fileLength);
    setContentTypeHeader(response, path);

    response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);

    setDateAndCacheHeaders(response, lastModified);
    if (HttpUtil.isKeepAlive(request)) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }/*from www. j ava 2  s  . c  o  m*/

    // Write the initial line and the header.
    ctx.write(response);
}

From source file:org.apache.hyracks.http.server.ChunkedResponse.java

License:Apache License

public ChunkedResponse(ChannelHandlerContext ctx, FullHttpRequest request, int chunkSize) {
    this.ctx = ctx;
    outputStream = new ChunkedNettyOutputStream(ctx, chunkSize, this);
    writer = new PrintWriter(outputStream);
    response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    keepAlive = HttpUtil.isKeepAlive(request);
    if (keepAlive) {
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }//from  ww w  .  j  a va2 s .  c o  m
}

From source file:org.apache.hyracks.http.server.ChunkedResponse.java

License:Apache License

private void fullResponse(HttpVersion version, HttpResponseStatus status, ByteBuf buffer, HttpHeaders headers) {
    DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(version, status, buffer);
    fullResponse.headers().set(headers);
    // for a full response remove chunked transfer-encoding and set the content length instead
    fullResponse.headers().remove(HttpHeaderNames.TRANSFER_ENCODING);
    fullResponse.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes());
    future = ctx.writeAndFlush(fullResponse);
    headerSent = true;//from www .  jav  a2  s  .c om
    done = true;
}

From source file:org.ballerinalang.test.util.client.HttpClient.java

License:Open Source License

public FullHttpResponse sendChunkRequest(FullHttpRequest httpRequest) {
    httpRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, Constants.CHUNKED);
    return send(httpRequest);
}

From source file:org.cloudfoundry.reactor._TestRequest.java

License:Apache License

public void assertEquals(RecordedRequest request) {
    assertThat(getMethod()).hasToString(request.getMethod());
    assertThat(extractPath(request)).isEqualTo(getPath());

    assertThat(request.getHeader(HttpHeaderNames.TRANSFER_ENCODING.toString()))
            .as("Does not have Transfer-Encoding header").isNull();

    if (!HttpMethod.GET.toString().equals(request.getMethod())) {
        assertThat(request.getHeader(HttpHeaderNames.CONTENT_LENGTH.toString())).as("Has Content-Length header")
                .isNotNull();/* w ww . j  av a 2s.co m*/
    }

    getHeaders().forEach((key, value) -> {
        if (value == null) {
            assertThat(request.getHeader(key)).as("Does not have %s header", key).isNull();
        } else {
            assertThat(request.getHeader(key)).as("Header %s value", key).isEqualTo(value);
        }
    });

    if (getPayload().isPresent()) {
        assertBodyEquals(request.getBody(), getPayload().map(_TestRequest::getBuffer).get());
    } else if (getContents().isPresent()) {
        getContents().get().accept(Tuples.of(request.getHeaders(), request.getBody()));
    } else {
        assertThat(request.getBodySize()).as("Invalid request body: %s", request.getBody().readUtf8())
                .isEqualTo(0);
    }
}