Search in sources :

Example 56 with HttpHeaders

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

the class HttpClientStreamInterceptor method before.

@Override
public void before(Object target, Object[] args) {
    if (isDebug) {
        logger.beforeInterceptor(target, args);
    }
    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    try {
        final SpanEventRecorder recorder = trace.traceBlockBegin();
        if (!validate(args)) {
            return;
        }
        final HttpRequest request = (HttpRequest) args[0];
        final HttpHeaders headers = request.headers();
        if (headers == null) {
            // defense code.
            return;
        }
        final String host = (String) args[1];
        // generate next trace id.
        final TraceId nextId = trace.getTraceId().getNextTraceId();
        recorder.recordNextSpanId(nextId.getSpanId());
        requestTraceWriter.write(request, nextId, host);
    } catch (Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn("BEFORE. Caused:{}", t.getMessage(), t);
        }
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders)

Example 57 with HttpHeaders

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

the class HttpClientStreamInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }
    final Trace trace = traceContext.currentTraceObject();
    if (trace == null) {
        return;
    }
    try {
        final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
        recorder.recordApi(descriptor);
        recorder.recordException(throwable);
        recorder.recordServiceType(VertxConstants.VERTX_HTTP_CLIENT);
        if (!validate(args)) {
            return;
        }
        final HttpRequest request = (HttpRequest) args[0];
        final HttpHeaders headers = request.headers();
        if (headers == null) {
            return;
        }
        final String host = (String) args[1];
        ClientRequestWrapper clientRequest = new VertxHttpClientRequestWrapper(request, host);
        this.clientRequestRecorder.record(recorder, clientRequest, throwable);
        this.cookieRecorder.record(recorder, request, throwable);
    } catch (Throwable t) {
        if (logger.isWarnEnabled()) {
            logger.warn("AFTER. Caused:{}", t.getMessage(), t);
        }
    } finally {
        trace.traceBlockEnd();
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) ClientRequestWrapper(com.navercorp.pinpoint.bootstrap.plugin.request.ClientRequestWrapper) VertxHttpClientRequestWrapper(com.navercorp.pinpoint.plugin.vertx.VertxHttpClientRequestWrapper) VertxHttpClientRequestWrapper(com.navercorp.pinpoint.plugin.vertx.VertxHttpClientRequestWrapper)

Example 58 with HttpHeaders

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

the class HttpProxyHandlerTest method testInitialMessage.

private static void testInitialMessage(InetSocketAddress socketAddress, String expectedUrl, String expectedHostHeader, HttpHeaders headers, boolean ignoreDefaultPortsInConnectHostHeader) throws Exception {
    InetSocketAddress proxyAddress = new InetSocketAddress(NetUtil.LOCALHOST, 8080);
    ChannelPromise promise = mock(ChannelPromise.class);
    verifyNoMoreInteractions(promise);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.connect(same(proxyAddress), isNull(InetSocketAddress.class), same(promise))).thenReturn(promise);
    HttpProxyHandler handler = new HttpProxyHandler(new InetSocketAddress(NetUtil.LOCALHOST, 8080), headers, ignoreDefaultPortsInConnectHostHeader);
    handler.connect(ctx, socketAddress, null, promise);
    FullHttpRequest request = (FullHttpRequest) handler.newInitialMessage(ctx);
    try {
        assertEquals(HttpVersion.HTTP_1_1, request.protocolVersion());
        assertEquals(expectedUrl, request.uri());
        HttpHeaders actualHeaders = request.headers();
        assertEquals(expectedHostHeader, actualHeaders.get(HttpHeaderNames.HOST));
        if (headers != null) {
            // The actual request header is a strict superset of the custom header
            for (String name : headers.names()) {
                assertEquals(headers.getAll(name), actualHeaders.getAll(name));
            }
        }
    } finally {
        request.release();
    }
    verify(ctx).connect(proxyAddress, null, promise);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) InetSocketAddress(java.net.InetSocketAddress) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 59 with HttpHeaders

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

the class WebSocketClientHandshaker07 method newHandshakeRequest.

/**
 * /**
 * <p>
 * Sends the opening request to the server:
 * </p>
 *
 * <pre>
 * GET /chat HTTP/1.1
 * Host: server.example.com
 * Upgrade: websocket
 * Connection: Upgrade
 * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
 * Sec-WebSocket-Origin: http://example.com
 * Sec-WebSocket-Protocol: chat, superchat
 * Sec-WebSocket-Version: 7
 * </pre>
 */
@Override
protected FullHttpRequest newHandshakeRequest() {
    URI wsURL = uri();
    // Get 16 bit nonce and base 64 encode it
    byte[] nonce = WebSocketUtil.randomBytes(16);
    String key = WebSocketUtil.base64(nonce);
    String acceptSeed = key + MAGIC_GUID;
    byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
    expectedChallengeResponseString = WebSocketUtil.base64(sha1);
    if (logger.isDebugEnabled()) {
        logger.debug("WebSocket version 07 client handshake key: {}, expected response: {}", key, expectedChallengeResponseString);
    }
    // Format request
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, upgradeUrl(wsURL), Unpooled.EMPTY_BUFFER);
    HttpHeaders headers = request.headers();
    if (customHeaders != null) {
        headers.add(customHeaders);
        if (!headers.contains(HttpHeaderNames.HOST)) {
            // Only add HOST header if customHeaders did not contain it.
            // 
            // See https://github.com/netty/netty/issues/10101
            headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
        }
    } else {
        headers.set(HttpHeaderNames.HOST, websocketHostValue(wsURL));
    }
    headers.set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE).set(HttpHeaderNames.SEC_WEBSOCKET_KEY, key);
    if (!headers.contains(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN)) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_ORIGIN, websocketOriginValue(wsURL));
    }
    String expectedSubprotocol = expectedSubprotocol();
    if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) {
        headers.set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol);
    }
    headers.set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, version().toAsciiString());
    return request;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) URI(java.net.URI)

Example 60 with HttpHeaders

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

the class WebSocketClientHandshaker07 method verify.

/**
 * <p>
 * Process server response:
 * </p>
 *
 * <pre>
 * HTTP/1.1 101 Switching Protocols
 * Upgrade: websocket
 * Connection: Upgrade
 * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
 * Sec-WebSocket-Protocol: chat
 * </pre>
 *
 * @param response
 *            HTTP response returned from the server for the request sent by beginOpeningHandshake00().
 * @throws WebSocketHandshakeException
 */
@Override
protected void verify(FullHttpResponse response) {
    HttpResponseStatus status = response.status();
    if (!HttpResponseStatus.SWITCHING_PROTOCOLS.equals(status)) {
        throw new WebSocketClientHandshakeException("Invalid handshake response getStatus: " + status, response);
    }
    HttpHeaders headers = response.headers();
    CharSequence upgrade = headers.get(HttpHeaderNames.UPGRADE);
    if (!HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(upgrade)) {
        throw new WebSocketClientHandshakeException("Invalid handshake response upgrade: " + upgrade, response);
    }
    if (!headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true)) {
        throw new WebSocketClientHandshakeException("Invalid handshake response connection: " + headers.get(HttpHeaderNames.CONNECTION), response);
    }
    CharSequence accept = headers.get(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT);
    if (accept == null || !accept.equals(expectedChallengeResponseString)) {
        throw new WebSocketClientHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString), response);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

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