@Override public String decodeAddress(Socks5AddressType addrType, ByteBuf in) throws Exception { if (addrType == Socks5AddressType.IPv4) { return NetUtil.intToIpAddress(in.readInt()); } if (addrType == Socks5AddressType.DOMAIN) { final int length = in.readUnsignedByte(); final String domain = in.toString(in.readerIndex(), length, CharsetUtil.US_ASCII); in.skipBytes(length); return domain; } if (addrType == Socks5AddressType.IPv6) { if (in.hasArray()) { final int readerIdx = in.readerIndex(); in.readerIndex(readerIdx + IPv6_LEN); return NetUtil.bytesToIpAddress(in.array(), in.arrayOffset() + readerIdx, IPv6_LEN); } else { byte[] tmp = new byte[IPv6_LEN]; in.readBytes(tmp); return NetUtil.bytesToIpAddress(tmp); } } else { throw new DecoderException("unsupported address type: " + (addrType.byteValue() & 0xFF)); } } };
while (in.isReadable()) { int outSize = out.size(); if (ctx.isRemoved()) { break; int oldInputLength = in.readableBytes(); decodeRemovalReentryProtection(ctx, in, out); if (ctx.isRemoved()) { break; if (oldInputLength == in.readableBytes()) { break; } else { if (oldInputLength == in.readableBytes()) { throw new DecoderException( StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message."); throw e; } catch (Exception cause) { throw new DecoderException(cause);
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof ByteBuf) { CodecOutputList out = CodecOutputList.newInstance(); try { ByteBuf data = (ByteBuf) msg; cumulation = data; } else { cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data); throw e; } catch (Exception e) { throw new DecoderException(e); } finally { if (cumulation != null && !cumulation.isReadable()) { numReads = 0; cumulation.release(); cumulation = null; } else if (++ numReads >= discardAfterReads) { int size = out.size(); decodeWasNull = !out.insertSinceRecycled(); fireChannelRead(ctx, out, size); out.recycle(); ctx.fireChannelRead(msg);
switch (state()) { case START: { final int version = in.readUnsignedByte(); if (version != 0) { throw new DecoderException("unsupported reply version: " + version + " (expected: 0)"); final Socks4CommandStatus status = Socks4CommandStatus.valueOf(in.readByte()); final int dstPort = in.readUnsignedShort(); final String dstAddr = NetUtil.intToIpAddress(in.readInt());
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { int index = msg.readerIndex(); int uncompressedSize = ByteBufUtils.readVarInt(msg); if (uncompressedSize == 0) { int length = msg.readableBytes(); if (length >= threshold) { throw new DecoderException( "Received uncompressed message of size " + length + " greater than threshold " + threshold); ByteBuf buf = ctx.alloc().buffer(length); msg.readBytes(buf, length); out.add(buf); } else { out.add(msg); } else if (resultLength != uncompressedSize) { throw new DecoderException( "Received compressed message claiming to be of size " + uncompressedSize + " but actually " + resultLength);
static Function<DnsRecord, String> address(int octets) { return record -> { ByteBuf data = ((DnsRawRecord)record).content(); int size = data.readableBytes(); if (size != octets) { throw new DecoderException("Invalid content length, or reader index when decoding address [index: " + data.readerIndex() + ", expected length: " + octets + ", actual: " + size + "]."); } byte[] address = new byte[octets]; data.getBytes(data.readerIndex(), address); try { return InetAddress.getByAddress(address).getHostAddress(); } catch (UnknownHostException e) { throw new DecoderException("Could not convert address " + data.toString(data.readerIndex(), size, CharsetUtil.UTF_8) + " to InetAddress."); } }; }
private void channelInputClosed(ChannelHandlerContext ctx, boolean callChannelInactive) throws Exception { CodecOutputList out = CodecOutputList.newInstance(); try { channelInputClosed(ctx, out); } catch (DecoderException e) { throw e; } catch (Exception e) { throw new DecoderException(e); } finally { try { if (cumulation != null) { cumulation.release(); cumulation = null; } int size = out.size(); fireChannelRead(ctx, out, size); if (size > 0) { // Something was read, call fireChannelReadComplete() ctx.fireChannelReadComplete(); } if (callChannelInactive) { ctx.fireChannelInactive(); } } finally { // Recycle in all cases out.recycle(); } } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { // uuid byte[] uuidBytes = new byte[36]; in.readBytes(uuidBytes); UUID id = UUID.fromString(new String(uuidBytes)); // op byte[] opBytes = new byte[4]; in.readBytes(opBytes); Message.Op op = Message.Op.fromBytes(opBytes); // payloadSize byte[] payloadSizeBytes = new byte[4]; in.readBytes(payloadSizeBytes); int payloadSize = Ints.fromByteArray(payloadSizeBytes); if (in.readableBytes() < payloadSize) { ctx.fireExceptionCaught(new DecoderException("Not enough bytes available to decode payload")); } out.add(in.readRetainedSlice(payloadSize)); out.add(new Message(id, op)); } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { try { switch (state()) { case INIT: { final byte version = in.readByte(); if (version != 1) { throw new DecoderException("unsupported subnegotiation version: " + version + " (expected: 1)"); } out.add(new DefaultSocks5PasswordAuthResponse(Socks5PasswordAuthStatus.valueOf(in.readByte()))); checkpoint(State.SUCCESS); } case SUCCESS: { int readableBytes = actualReadableBytes(); if (readableBytes > 0) { out.add(in.readRetainedSlice(readableBytes)); } break; } case FAILURE: { in.skipBytes(actualReadableBytes()); break; } } } catch (Exception e) { fail(out, e); } }
cumulation = data; } else { cumulation = cumulator.cumulate(ctx.alloc(), cumulation, data); throw e; } catch (Throwable t) { throw new DecoderException(t); } finally { if (cumulation != null && !cumulation.isReadable()) { numReads = 0; cumulation.release(); cumulation = null; } else if (++numReads >= discardAfterReads) { decodeWasNull = true; } else if (size == 1) { ctx.fireChannelRead(out.get(0)); } else { ArrayList<Object> ret = new ArrayList<Object>(size); ret.add(out.get(i)); ctx.fireChannelRead(ret);
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf input, List<Object> list) throws IllegalAccessException, InstantiationException { if (!input.isReadable()) { return; } EnumProtocol protocol = ctx.channel().attr(NetworkManager.c).get(); wrapper.setBuf(input); int packetId = VarNumberSerializer.readVarInt(wrapper); Packet<?> packet = protocol.a(EnumProtocolDirection.SERVERBOUND, packetId); if (packet == null) { throw new DecoderException("Bad packet id " + packetId); } try { packet.a(nativeSerializer); } catch (IOException e) { throw new DecoderException(e); } if (nativeSerializer.isReadable()) { throw new DecoderException("Did not read all data from packet " + packet.getClass().getName() + ", bytes left: " + nativeSerializer.readableBytes()); } list.add(packet); }
/** * Reads a variable-length NUL-terminated string as defined in SOCKS4. */ private static String readString(String fieldName, ByteBuf in) { int length = in.bytesBefore(MAX_FIELD_LENGTH + 1, (byte) 0); if (length < 0) { throw new DecoderException("field '" + fieldName + "' longer than " + MAX_FIELD_LENGTH + " chars"); } String value = in.readSlice(length).toString(CharsetUtil.US_ASCII); in.skipBytes(1); // Skip the NUL. return value; } }
throw e; } catch (Exception e) { throw new DecoderException(e); } finally { try { if (cumulation != null) { cumulation.release(); cumulation = null; ctx.fireChannelRead(out.get(i)); ctx.fireChannelReadComplete(); ctx.fireChannelInactive(); } finally {
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { CodecOutputList out = CodecOutputList.newInstance(); try { if (acceptInboundMessage(msg)) { @SuppressWarnings("unchecked") I cast = (I) msg; try { decode(ctx, cast, out); } finally { ReferenceCountUtil.release(cast); } } else { out.add(msg); } } catch (DecoderException e) { throw e; } catch (Exception e) { throw new DecoderException(e); } finally { int size = out.size(); for (int i = 0; i < size; i ++) { ctx.fireChannelRead(out.getUnsafe(i)); } out.recycle(); } }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf from, List<Object> list) throws Exception { if (!from.isReadable()) { return; } int uncompressedlength = VarNumberSerializer.readVarInt(from); if (uncompressedlength == 0) { list.add(from.retain()); } else { if (uncompressedlength > maxPacketLength) { throw new DecoderException(MessageFormat.format("Badly compressed packet - size of {0} is larger than protocol maximum of {1}", uncompressedlength, maxPacketLength)); } ByteBuf out = ctx.alloc().heapBuffer(uncompressedlength); readBuffer.readFrom(from, (larray, loffset, llength) -> decompressor.decompressTo(out, larray, loffset, llength, uncompressedlength)); list.add(out); } }
private static Result<MqttConnectVariableHeader> decodeConnectionVariableHeader(ByteBuf buffer) { final Result<String> protoString = decodeString(buffer); int numberOfBytesConsumed = protoString.numberOfBytesConsumed; final byte protocolLevel = buffer.readByte(); numberOfBytesConsumed += 1; final int b1 = buffer.readUnsignedByte(); numberOfBytesConsumed += 1; final Result<Integer> keepAlive = decodeMsbLsb(buffer); numberOfBytesConsumed += keepAlive.numberOfBytesConsumed; throw new DecoderException("non-zero reserved flag");
logger.error("post request validation failed", ex); responseContent.append(ex.getMessage()); writeResponse(ctx.channel(), HttpResponseStatus.BAD_REQUEST); requestProcessed = true; return; logger.error("Unable to create upload entity. An exception occurred."); responseContent.append("Internal Server Error"); writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR); requestProcessed = true; return; } catch (ErrorDataDecoderException | IncompatibleDataDecoderException e) { logger.error("exception while initialising the decoder", e); responseContent.append(e.getMessage()); writeResponse(ctx.channel(), HttpResponseStatus.INTERNAL_SERVER_ERROR); requestProcessed = true; return;
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { switch (state()) { case READ_FIXED_HEADER: try { mqttFixedHeader = decodeFixedHeader(buffer); bytesRemainingInVariablePart = mqttFixedHeader.remainingLength(); checkpoint(DecoderState.READ_VARIABLE_HEADER); throw new DecoderException("too large message: " + bytesRemainingInVariablePart + " bytes"); final Result<?> decodedVariableHeader = decodeVariableHeader(buffer, mqttFixedHeader); decodePayload( buffer, mqttFixedHeader.messageType(), bytesRemainingInVariablePart, variableHeader); bytesRemainingInVariablePart -= decodedPayload.numberOfBytesConsumed; if (bytesRemainingInVariablePart != 0) { throw new DecoderException( "non-zero remaining payload bytes: " + bytesRemainingInVariablePart + " (" + mqttFixedHeader.messageType() + ')'); checkpoint(DecoderState.READ_FIXED_HEADER); buffer.skipBytes(actualReadableBytes()); break;
private static DecoderException newDecoderException(ByteBuf buffer, int readerIndex, int readable) { return new DecoderException( "Received invalid line: '" + buffer.toString(readerIndex, readable, CharsetUtil.US_ASCII) + '\''); }
buf = buf.order(order); long frameLength; switch (length) { case 1: frameLength = buf.getUnsignedByte(offset); break; case 2: frameLength = buf.getUnsignedShort(offset); break; case 3: break; default: throw new DecoderException( "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");