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

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

Introduction

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

Prototype

AsciiString PROXY_AUTHORIZATION

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

Click Source Link

Document

"proxy-authorization"

Usage

From source file:com.github.sinsinpub.pero.manual.proxyhandler.HttpProxyServer.java

License:Apache License

private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
    assertThat(req.method(), Matchers.is(HttpMethod.CONNECT));

    if (testMode != TestMode.INTERMEDIARY) {
        ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
    }// w w  w .j  a v  a 2 s. c  o  m

    ctx.pipeline().remove(HttpObjectAggregator.class);
    ctx.pipeline().remove(HttpRequestDecoder.class);

    boolean authzSuccess = false;
    if (username != null) {
        CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
        if (authz != null) {
            String[] authzParts = StringUtil.split(authz.toString(), ' ', 2);
            ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
            ByteBuf authzBuf = Base64.decode(authzBuf64);

            String expectedAuthz = username + ':' + password;
            authzSuccess = "Basic".equals(authzParts[0])
                    && expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));

            authzBuf64.release();
            authzBuf.release();
        }
    } else {
        authzSuccess = true;
    }

    return authzSuccess;
}

From source file:com.otcdlink.chiron.downend.Http11ProxyHandler.java

License:Apache License

@Override
protected Object newInitialMessage(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    String rhost;//w  w w  .  j  av a 2s  .  c o  m
    if (raddr.isUnresolved()) {
        rhost = raddr.getHostString();
    } else {
        rhost = raddr.getAddress().getHostAddress();
    }

    FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT,
            rhost + ':' + raddr.getPort(), Unpooled.EMPTY_BUFFER,
            new DefaultHttpHeaders().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE).add(
                    HttpHeaderNames.PROXY_CONNECTION, HttpHeaderValues.KEEP_ALIVE),
            EmptyHttpHeaders.INSTANCE);

    SocketAddress proxyAddress = proxyAddress();
    if (proxyAddress instanceof InetSocketAddress) {
        InetSocketAddress hostAddr = (InetSocketAddress) proxyAddress;
        //            req.headers().set(HttpHeaderNames.HOST, hostAddr.getHostString() + ':' + hostAddr.getPort());
        req.headers().set(HttpHeaderNames.HOST, rhost + ':' + raddr.getPort());
    }

    if (authorization != null) {
        req.headers().set(HttpHeaderNames.PROXY_AUTHORIZATION, authorization);
    }

    return req;
}