Search in sources :

Example 81 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpConversionUtilTest method stripTEHeadersAccountsForValueSimilarToTrailers.

@Test
public void stripTEHeadersAccountsForValueSimilarToTrailers() {
    HttpHeaders inHeaders = new DefaultHttpHeaders();
    inHeaders.add(TE, TRAILERS + "foo");
    Http2Headers out = new DefaultHttp2Headers();
    HttpConversionUtil.toHttp2Headers(inHeaders, out);
    assertFalse(out.contains(TE));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.jupiter.api.Test)

Example 82 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpConversionUtil method toHttp2Headers.

/**
 * Converts the given HTTP/1.x headers into HTTP/2 headers.
 * The following headers are only used if they can not be found in from the {@code HOST} header or the
 * {@code Request-Line} as defined by <a href="https://tools.ietf.org/html/rfc7230">rfc7230</a>
 * <ul>
 * <li>{@link ExtensionHeaderNames#SCHEME}</li>
 * </ul>
 * {@link ExtensionHeaderNames#PATH} is ignored and instead extracted from the {@code Request-Line}.
 */
public static Http2Headers toHttp2Headers(HttpMessage in, boolean validateHeaders) {
    HttpHeaders inHeaders = in.headers();
    final Http2Headers out = new DefaultHttp2Headers(validateHeaders, inHeaders.size());
    if (in instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) in;
        URI requestTargetUri = URI.create(request.uri());
        out.path(toHttp2Path(requestTargetUri));
        out.method(request.method().asciiName());
        setHttp2Scheme(inHeaders, requestTargetUri, out);
        if (!isOriginForm(requestTargetUri) && !isAsteriskForm(requestTargetUri)) {
            // Attempt to take from HOST header before taking from the request-line
            String host = inHeaders.getAsString(HttpHeaderNames.HOST);
            setHttp2Authority(host == null || host.isEmpty() ? requestTargetUri.getAuthority() : host, out);
        }
    } else if (in instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) in;
        out.status(response.status().codeAsText());
    }
    // Add the HTTP headers which have not been consumed above
    toHttp2Headers(inHeaders, out);
    return out;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsciiString(io.netty.util.AsciiString) URI(java.net.URI)

Example 83 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class WebSocketClientHandshakerTest method testDuplicateWebsocketHandshakeHeaders.

@Test
public void testDuplicateWebsocketHandshakeHeaders() {
    URI uri = URI.create("ws://localhost:9999/foo");
    HttpHeaders inputHeaders = new DefaultHttpHeaders();
    String bogusSubProtocol = "bogusSubProtocol";
    String bogusHeaderValue = "bogusHeaderValue";
    // add values for the headers that are reserved for use in the websockets handshake
    for (CharSequence header : getHandshakeRequiredHeaderNames()) {
        if (!HttpHeaderNames.HOST.equals(header)) {
            inputHeaders.add(header, bogusHeaderValue);
        }
    }
    inputHeaders.add(getProtocolHeaderName(), bogusSubProtocol);
    String realSubProtocol = "realSubProtocol";
    WebSocketClientHandshaker handshaker = newHandshaker(uri, realSubProtocol, inputHeaders, false);
    FullHttpRequest request = handshaker.newHandshakeRequest();
    HttpHeaders outputHeaders = request.headers();
    // the header values passed in originally have been replaced with values generated by the Handshaker
    for (CharSequence header : getHandshakeRequiredHeaderNames()) {
        assertEquals(1, outputHeaders.getAll(header).size());
        assertNotEquals(bogusHeaderValue, outputHeaders.get(header));
    }
    // the subprotocol header value is that of the subprotocol string passed into the Handshaker
    assertEquals(1, outputHeaders.getAll(getProtocolHeaderName()).size());
    assertEquals(realSubProtocol, outputHeaders.get(getProtocolHeaderName()));
    request.release();
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 84 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class WebSocketClientHandshakerTest method testSetOriginFromCustomHeaders.

@Test
public void testSetOriginFromCustomHeaders() {
    HttpHeaders customHeaders = new DefaultHttpHeaders().set(getOriginHeaderName(), "http://example.com");
    WebSocketClientHandshaker handshaker = newHandshaker(URI.create("ws://server.example.com/chat"), null, customHeaders, false);
    FullHttpRequest request = handshaker.newHandshakeRequest();
    try {
        assertEquals("http://example.com", request.headers().get(getOriginHeaderName()));
    } finally {
        request.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.jupiter.api.Test)

Example 85 with HttpHeaders

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project netty by netty.

the class HttpUploadClient method formget.

/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 */
private static List<Entry<String, String>> formget(Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();
    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");
    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);
    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    // connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");
    headers.set(HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));
    // send request
    channel.writeAndFlush(request);
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    // convert headers to list
    return headers.entries();
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) URI(java.net.URI) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)286 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)149 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)83 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)73 Test (org.junit.Test)68 Test (org.junit.jupiter.api.Test)57 Test (org.testng.annotations.Test)51 HttpRequest (io.netty.handler.codec.http.HttpRequest)43 HttpResponse (io.netty.handler.codec.http.HttpResponse)40 AsciiString (io.netty.util.AsciiString)29 ByteBuf (io.netty.buffer.ByteBuf)27 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)27 BStruct (org.ballerinalang.model.values.BStruct)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)22 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)20 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)19 Cookie (io.netty.handler.codec.http.cookie.Cookie)19 BValue (org.ballerinalang.model.values.BValue)19 ChannelPromise (io.netty.channel.ChannelPromise)18