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

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#getAll() . 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: ServerWebSocketContainer.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void afterRequest(final HttpHeaders headers) {
    ClientEndpointConfig.Configurator configurator = config.getConfigurator();
    if (configurator != null) {
        final Map<String, List<String>> newHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        for (Map.Entry<String, String> entry : headers) {
            ArrayList<String> arrayList = new ArrayList<>(headers.getAll(entry.getKey()));
            newHeaders.put(entry.getKey(), arrayList);
        }
        configurator.afterResponse(new HandshakeResponse() {
            @Override
            public Map<String, List<String>> getHeaders() {
                return newHeaders;
            }
        });
    }
    headers.remove(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
    super.afterRequest(headers);
}
 
Example 2
Source File: ProxyUtils.java    From g4proxy with Apache License 2.0 6 votes vote down vote up
/**
 * Given an HttpHeaders instance, removes 'sdch' from the 'Accept-Encoding'
 * header list (if it exists) and returns the modified instance.
 * <p>
 * Removes all occurrences of 'sdch' from the 'Accept-Encoding' header.
 *
 * @param headers The headers to modify.
 */
public static void removeSdchEncoding(HttpHeaders headers) {
    List<String> encodings = headers.getAll(HttpHeaders.Names.ACCEPT_ENCODING);
    headers.remove(HttpHeaders.Names.ACCEPT_ENCODING);

    for (String encoding : encodings) {
        if (encoding != null) {
            // The former regex should remove occurrences of 'sdch' while the
            // latter regex should take care of the dangling comma case when
            // 'sdch' was the first element in the list and there are other
            // encodings.
            encoding = encoding.replaceAll(",? *(sdch|SDCH)", "").replaceFirst("^ *, *", "");

            if (StringUtils.isNotBlank(encoding)) {
                headers.add(HttpHeaders.Names.ACCEPT_ENCODING, encoding);
            }
        }
    }
}
 
Example 3
Source File: HttpHeaderCompressionTest.java    From netty-http2 with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    assertTrue(msg instanceof HttpHeaderBlockFrame);
    HttpHeaders actual = ((HttpHeaderBlockFrame) msg).headers();
    HttpHeaders expected = frame.headers();
    for (String name : expected.names()) {
        List<String> expectedValues = new ArrayList<String>(expected.getAll(name));
        List<String> actualValues = new ArrayList<String>(actual.getAll(name));
        assertTrue(actualValues.containsAll(expectedValues));
        actualValues.removeAll(expectedValues);
        assertTrue(actualValues.isEmpty());
        actual.remove(name);
    }
    assertTrue(actual.isEmpty());
    success = true;
}
 
Example 4
Source File: ClientToProxyConnection.java    From g4proxy with Apache License 2.0 5 votes vote down vote up
/**
 * RFC2616 Section 14.10
 * 
 * HTTP/1.1 proxies MUST parse the Connection header field before a message
 * is forwarded and, for each connection-token in this field, remove any
 * header field(s) from the message with the same name as the
 * connection-token.
 * 
 * @param headers
 *            The headers to modify
 */
private void stripConnectionTokens(HttpHeaders headers) {
    if (headers.contains(HttpHeaders.Names.CONNECTION)) {
        for (String headerValue : headers.getAll(HttpHeaders.Names.CONNECTION)) {
            for (String connectionToken : ProxyUtils.splitCommaSeparatedHeaderValues(headerValue)) {
                // do not strip out the Transfer-Encoding header if it is specified in the Connection header, since LittleProxy does not
                // normally modify the Transfer-Encoding of the message.
                if (!LOWERCASE_TRANSFER_ENCODING_HEADER.equals(connectionToken.toLowerCase(Locale.US))) {
                    headers.remove(connectionToken);
                }
            }
        }
    }
}
 
Example 5
Source File: RiposteWingtipsNettyClientTagAdapter.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public List<String> getHeaderMultipleValue(@Nullable HttpRequest request, @NotNull String headerKey) {
    if (request == null) {
        return null;
    }

    HttpHeaders headers = request.headers();
    if (headers == null) {
        return null;
    }

    return headers.getAll(headerKey);
}
 
Example 6
Source File: AsyncHttpResponse.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
public Collection<HttpHeader> getHeaders() {
	HttpHeaders headers = response.getHeaders();

	List<HttpHeader> results = new ArrayList<>(headers.size());
	for (Map.Entry<String, String> entry : headers) {
		String name = entry.getKey();
		List<String> values = headers.getAll(name);
		results.add(HttpHeader.header(name, values));
	}

	return results;
}
 
Example 7
Source File: HttpPostRequestEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Finalize the request by preparing the Header in the request and returns the request ready to be sent.<br>
 * Once finalized, no data must be added.<br>
 * If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote
 * server.通过准备请求中的标头完成请求,并返回准备发送的请求。一旦确定,就不需要添加任何数据。如果请求不需要chunk (isChunked() == false),此请求是发送到远程服务器的唯一对象。
 *
 * @return the request object (chunked or not according to size of body)
 * @throws ErrorDataEncoderException
 *             if the encoding is in error or if the finalize were already done
 */
public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }

    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaderNames.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaderNames.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaderNames.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            String lowercased = contentType.toLowerCase();
            if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()) ||
                    lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
                // ignore
            } else {
                headers.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaderValues.MULTIPART_FORM_DATA + "; " + HttpHeaderValues.BOUNDARY + '='
                + multipartDataBoundary;
        headers.add(HttpHeaderNames.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        realSize -= 1; // last '&' removed
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
            for (CharSequence v : transferEncoding) {
                if (HttpHeaderValues.CHUNKED.contentEqualsIgnoreCase(v)) {
                    // ignore
                } else {
                    headers.add(HttpHeaderNames.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpUtil.setTransferEncodingChunked(request, true);

        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}
 
Example 8
Source File: HttpPostRequestEncoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * Finalize the request by preparing the Header in the request and returns the request ready to be sent.<br>
 * Once finalized, no data must be added.<br>
 * If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote
 * server.
 *
 * @return the request object (chunked or not according to size of body)
 * @throws ErrorDataEncoderException
 *             if the encoding is in error or if the finalize were already done
 */
public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }

    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaders.Names.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaders.Names.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaders.Names.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            String lowercased = contentType.toLowerCase();
            if (lowercased.startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA) ||
                lowercased.startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
                // ignore
            } else {
                headers.add(HttpHeaders.Names.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaders.Values.MULTIPART_FORM_DATA + "; " + HttpHeaders.Values.BOUNDARY + '='
                + multipartDataBoundary;
        headers.add(HttpHeaders.Names.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        realSize -= 1; // last '&' removed
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaders.Names.TRANSFER_ENCODING);
            for (String v : transferEncoding) {
                if (v.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
                    // ignore
                } else {
                    headers.add(HttpHeaders.Names.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpHeaders.setTransferEncodingChunked(request);

        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}
 
Example 9
Source File: AccessLogPublisher.java    From zuul with Apache License 2.0 4 votes vote down vote up
String headerAsString(HttpHeaders headers, String headerName)
{
    List<String> values = headers.getAll(headerName);
    return (values.size() == 0) ? "-" : String.join(",", values);
}