Search in sources :

Example 1 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project netty by netty.

the class DnsNameResolverContext method parseAddress.

private InetAddress parseAddress(DnsRecord r, String name) {
    if (!(r instanceof DnsRawRecord)) {
        return null;
    }
    final ByteBuf content = ((ByteBufHolder) r).content();
    final int contentLen = content.readableBytes();
    if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
        return null;
    }
    final byte[] addrBytes = new byte[contentLen];
    content.getBytes(content.readerIndex(), addrBytes);
    try {
        return InetAddress.getByAddress(parent.isDecodeIdn() ? IDN.toUnicode(name) : name, addrBytes);
    } catch (UnknownHostException e) {
        // Should never reach here.
        throw new Error(e);
    }
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) UnknownHostException(java.net.UnknownHostException) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project reactor-netty by reactor.

the class HttpClientOperations method onInboundNext.

@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;
        if (response.decoderResult().isFailure()) {
            onInboundError(response.decoderResult().cause());
            return;
        }
        if (started) {
            if (log.isDebugEnabled()) {
                log.debug("{} An HttpClientOperations cannot proceed more than one " + "Response", channel(), response.headers().toString());
            }
            return;
        }
        started = true;
        setNettyResponse(response);
        if (!isKeepAlive()) {
            markPersistent(false);
        }
        if (isInboundCancelled()) {
            ReferenceCountUtil.release(msg);
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("{} Received response (auto-read:{}) : {}", channel(), channel().config().isAutoRead(), responseHeaders().entries().toString());
        }
        if (checkResponseCode(response)) {
            prefetchMore(ctx);
            parentContext().fireContextActive(this);
        }
        if (msg instanceof FullHttpResponse) {
            super.onInboundNext(ctx, msg);
            onHandlerTerminate();
        }
        return;
    }
    if (msg instanceof LastHttpContent) {
        if (!started) {
            if (log.isDebugEnabled()) {
                log.debug("{} HttpClientOperations received an incorrect end " + "delimiter" + "(previously used connection?)", channel());
            }
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("{} Received last HTTP packet", channel());
        }
        if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
            super.onInboundNext(ctx, msg);
        }
        // force auto read to enable more accurate close selection now inbound is done
        channel().config().setAutoRead(true);
        onHandlerTerminate();
        return;
    }
    if (!started) {
        if (log.isDebugEnabled()) {
            if (msg instanceof ByteBufHolder) {
                msg = ((ByteBufHolder) msg).content();
            }
            log.debug("{} HttpClientOperations received an incorrect chunk " + "" + "(previously used connection?)", channel(), msg);
        }
        return;
    }
    super.onInboundNext(ctx, msg);
    prefetchMore(ctx);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBufHolder(io.netty.buffer.ByteBufHolder) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 3 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project netty by netty.

the class DnsNameResolverTest method testResolveAllMx.

@Test
public void testResolveAllMx() {
    final DnsNameResolver resolver = newResolver().build();
    try {
        assertThat(resolver.isRecursionDesired(), is(true));
        final Map<String, Future<List<DnsRecord>>> futures = new LinkedHashMap<String, Future<List<DnsRecord>>>();
        for (String name : DOMAINS) {
            if (EXCLUSIONS_QUERY_MX.contains(name)) {
                continue;
            }
            futures.put(name, resolver.resolveAll(new DefaultDnsQuestion(name, DnsRecordType.MX)));
        }
        for (Entry<String, Future<List<DnsRecord>>> e : futures.entrySet()) {
            String hostname = e.getKey();
            Future<List<DnsRecord>> f = e.getValue().awaitUninterruptibly();
            final List<DnsRecord> mxList = f.getNow();
            assertThat(mxList.size(), is(greaterThan(0)));
            StringBuilder buf = new StringBuilder();
            for (DnsRecord r : mxList) {
                ByteBuf recordContent = ((ByteBufHolder) r).content();
                buf.append(StringUtil.NEWLINE);
                buf.append('\t');
                buf.append(r.name());
                buf.append(' ');
                buf.append(r.type().name());
                buf.append(' ');
                buf.append(recordContent.readUnsignedShort());
                buf.append(' ');
                buf.append(DnsResolveContext.decodeDomainName(recordContent));
                ReferenceCountUtil.release(r);
            }
            logger.info("{} has the following MX records:{}", hostname, buf);
        }
    } finally {
        resolver.close();
    }
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) Future(io.netty.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ByteBufHolder(io.netty.buffer.ByteBufHolder) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DnsRecord(io.netty.handler.codec.dns.DnsRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) Test(org.junit.jupiter.api.Test)

Example 4 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project netty by netty.

the class DnsNameResolverTest method testQueryMx.

@Test
public void testQueryMx() {
    DnsNameResolver resolver = newResolver().build();
    try {
        assertThat(resolver.isRecursionDesired(), is(true));
        Map<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> futures = new LinkedHashMap<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>>();
        for (String name : DOMAINS) {
            if (EXCLUSIONS_QUERY_MX.contains(name)) {
                continue;
            }
            queryMx(resolver, futures, name);
        }
        for (Entry<String, Future<AddressedEnvelope<DnsResponse, InetSocketAddress>>> e : futures.entrySet()) {
            String hostname = e.getKey();
            Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> f = e.getValue().awaitUninterruptibly();
            DnsResponse response = f.getNow().content();
            assertThat(response.code(), is(DnsResponseCode.NOERROR));
            final int answerCount = response.count(DnsSection.ANSWER);
            final List<DnsRecord> mxList = new ArrayList<DnsRecord>(answerCount);
            for (int i = 0; i < answerCount; i++) {
                final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
                if (r.type() == DnsRecordType.MX) {
                    mxList.add(r);
                }
            }
            assertThat(mxList.size(), is(greaterThan(0)));
            StringBuilder buf = new StringBuilder();
            for (DnsRecord r : mxList) {
                ByteBuf recordContent = ((ByteBufHolder) r).content();
                buf.append(StringUtil.NEWLINE);
                buf.append('\t');
                buf.append(r.name());
                buf.append(' ');
                buf.append(r.type().name());
                buf.append(' ');
                buf.append(recordContent.readUnsignedShort());
                buf.append(' ');
                buf.append(DnsResolveContext.decodeDomainName(recordContent));
            }
            logger.info("{} has the following MX records:{}", hostname, buf);
            response.release();
            // We only track query lifecycle if it is managed by the DnsNameResolverContext, and not direct calls
            // to query.
            assertNoQueriesMade(resolver);
        }
    } finally {
        resolver.close();
    }
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) LinkedHashMap(java.util.LinkedHashMap) DnsResponse(io.netty.handler.codec.dns.DnsResponse) Future(io.netty.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ByteBufHolder(io.netty.buffer.ByteBufHolder) DnsRecord(io.netty.handler.codec.dns.DnsRecord) Test(org.junit.jupiter.api.Test)

Example 5 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project netty by netty.

the class DnsResolveContext method buildAliasMap.

private static Map<String, String> buildAliasMap(DnsResponse response, DnsCnameCache cache, EventLoop loop) {
    final int answerCount = response.count(DnsSection.ANSWER);
    Map<String, String> cnames = null;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.CNAME) {
            continue;
        }
        if (!(r instanceof DnsRawRecord)) {
            continue;
        }
        final ByteBuf recordContent = ((ByteBufHolder) r).content();
        final String domainName = decodeDomainName(recordContent);
        if (domainName == null) {
            continue;
        }
        if (cnames == null) {
            cnames = new HashMap<String, String>(min(8, answerCount));
        }
        String name = r.name().toLowerCase(Locale.US);
        String mapping = domainName.toLowerCase(Locale.US);
        // Cache the CNAME as well.
        String nameWithDot = hostnameWithDot(name);
        String mappingWithDot = hostnameWithDot(mapping);
        if (!nameWithDot.equalsIgnoreCase(mappingWithDot)) {
            cache.cache(nameWithDot, mappingWithDot, r.timeToLive(), loop);
            cnames.put(name, mapping);
        }
    }
    return cnames != null ? cnames : Collections.<String, String>emptyMap();
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) ByteBufHolder(io.netty.buffer.ByteBufHolder) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) DnsRecord(io.netty.handler.codec.dns.DnsRecord) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBufHolder (io.netty.buffer.ByteBufHolder)15 ByteBuf (io.netty.buffer.ByteBuf)9 ChannelFuture (io.netty.channel.ChannelFuture)4 DnsRawRecord (io.netty.handler.codec.dns.DnsRawRecord)4 DnsRecord (io.netty.handler.codec.dns.DnsRecord)4 Test (org.junit.jupiter.api.Test)3 DefaultByteBufHolder (io.netty.buffer.DefaultByteBufHolder)2 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 DnsRecordType (io.netty.handler.codec.dns.DnsRecordType)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)2 Future (io.netty.util.concurrent.Future)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ClientHead (com.corundumstudio.socketio.handler.ClientHead)1 PacketsMessage (com.corundumstudio.socketio.messages.PacketsMessage)1 InvalidResponseException (com.linecorp.armeria.client.InvalidResponseException)1