Java Code Examples for io.netty.handler.codec.http.HttpHeaders#isEmpty()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#isEmpty() . 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: ServerRequestBuilder.java    From datamill with ISC License 6 votes vote down vote up
public static Multimap<String, String> buildHeadersMap(HttpHeaders headers) {
    Multimap<String, String> headersMap;

    HttpHeaders requestHeaders = headers;
    if (!requestHeaders.isEmpty()) {
        ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();

        for (Map.Entry<String, String> header : requestHeaders) {
            String key = header.getKey();
            String value = header.getValue();

            if (key != null && value != null) {
                builder.put(key.toLowerCase(), value);
            }
        }

        headersMap = builder.build();
    } else {
        headersMap = null;
    }

    return headersMap;
}
 
Example 2
Source File: HttpConversionUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public static Http2Headers toHttp2Headers(HttpHeaders inHeaders, boolean validateHeaders) {
    if (inHeaders.isEmpty()) {
        return EmptyHttp2Headers.INSTANCE;
    }

    final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size());
    toHttp2Headers(inHeaders, out);
    return out;
}
 
Example 3
Source File: HttpObjectUtil.java    From nitmproxy with MIT License 5 votes vote down vote up
private static String headersString(HttpHeaders headers) {
    if (headers.isEmpty()) {
        return "";
    }

    List<String> headerStrings = headers.entries().stream()
                                        .map(entry -> String.format("%s: %s", entry.getKey(), entry.getValue()))
                                        .collect(Collectors.toList());
    return Joiner.on("\r\n").join(headerStrings) + "\r\n";
}
 
Example 4
Source File: HttpClientHandler.java    From protools with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) {
    if (msg instanceof FullHttpResponse) {
        final FullHttpResponse response = (FullHttpResponse) msg;

        httpReceive.setStatusCode(response.status().code())
                .setStatusText(response.status().codeAsText().toString());

        HttpHeaders headers = response.headers();
        if (httpSend.getNeedReceiveHeaders() && !headers.isEmpty()) {

            final Map<String, String> responseHeaderMap = Maps.newHashMapWithExpectedSize(headers.size());

            headers.forEach(one -> {
                responseHeaderMap.put(one.getKey(), one.getValue());
            });

            httpReceive.setResponseHeader(responseHeaderMap);
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            if (log.isDebugEnabled()) {
                log.debug("#HTTP 内容开始{");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("#HTTP 内容开始{");
            }
        }

        final String responseBody = response.content().toString(httpSend.getCharset());

        httpReceive.setResponseBody(responseBody);

        if (log.isDebugEnabled()) {
            log.debug(responseBody);
        }

        if (log.isDebugEnabled()) {
            log.debug("}EOF#");
        }

        final DecoderResult decoderResult = response.decoderResult();
        if (decoderResult.isFailure()) {
            Throwable cause = decoderResult.cause();
            if (log.isErrorEnabled()) {
                log.error(ToolFormat.toException(cause), cause);
            }
            httpReceive.setHaveError(true)
                    .setErrMsg(cause.getMessage())
                    .setThrowable(cause);
        } else if (response.status().code() != 200) {
            httpReceive.setHaveError(true)
                    .setErrMsg("本次请求响应码不是200,是" + response.status().code());
        }

        httpReceive.setIsDone(true);
        ctx.close();
    }
}
 
Example 5
Source File: ApiRequestParser.java    From netty.book.kor with MIT License 4 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpMessage msg) {
    // Request header 처리.
    if (msg instanceof HttpRequest) {
        this.request = (HttpRequest) msg;

        if (HttpHeaders.is100ContinueExpected(request)) {
            send100Continue(ctx);
        }

        HttpHeaders headers = request.headers();
        if (!headers.isEmpty()) {
            for (Map.Entry<String, String> h : headers) {
                String key = h.getKey();
                if (usingHeader.contains(key)) {
                    reqData.put(key, h.getValue());
                }
            }
        }

        reqData.put("REQUEST_URI", request.getUri());
        reqData.put("REQUEST_METHOD", request.getMethod().name());
    }

    // Request content 처리.
    if (msg instanceof HttpContent) {
        if (msg instanceof LastHttpContent) {
            logger.debug("LastHttpContent message received!!" + request.getUri());

            LastHttpContent trailer = (LastHttpContent) msg;

            readPostData();

            ApiRequest service = ServiceDispatcher.dispatch(reqData);

            try {
                service.executeService();

                apiResult = service.getApiResult();
            }
            finally {
                reqData.clear();
            }

            if (!writeResponse(trailer, ctx)) {
                // If keep-alive is off, close the connection once the
                // content is fully written.
                ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
            }
            reset();
        }
    }
}