Ejemplo n.º 1
0
    /**
     * Decodes the client connection preface string from the input buffer.
     *
     * @return {@code true} if processing of the client preface string is complete. Since client
     *     preface strings can only be received by servers, returns true immediately for client
     *     endpoints.
     */
    private boolean readClientPrefaceString(ByteBuf in) throws Http2Exception {
      if (clientPrefaceString == null) {
        return true;
      }

      int prefaceRemaining = clientPrefaceString.readableBytes();
      int bytesRead = min(in.readableBytes(), prefaceRemaining);

      // If the input so far doesn't match the preface, break the connection.
      if (bytesRead == 0
          || !ByteBufUtil.equals(
              in,
              in.readerIndex(),
              clientPrefaceString,
              clientPrefaceString.readerIndex(),
              bytesRead)) {
        String receivedBytes =
            hexDump(
                in, in.readerIndex(), min(in.readableBytes(), clientPrefaceString.readableBytes()));
        throw connectionError(
            PROTOCOL_ERROR,
            "HTTP/2 client preface string missing or corrupt. " + "Hex dump for received bytes: %s",
            receivedBytes);
      }
      in.skipBytes(bytesRead);
      clientPrefaceString.skipBytes(bytesRead);

      if (!clientPrefaceString.isReadable()) {
        // Entire preface has been read.
        clientPrefaceString.release();
        clientPrefaceString = null;
        return true;
      }
      return false;
    }
Ejemplo n.º 2
0
  public boolean decodeHeader(
      final ByteBuf buf, InetSocketAddress recipient, final InetSocketAddress sender) {
    if (message == null) {
      if (buf.readableBytes() < MessageHeaderCodec.HEADER_SIZE) {
        // we don't have the header yet, we need the full header first
        // wait for more data
        return false;
      }
      message = MessageHeaderCodec.decodeHeader(buf, recipient, sender);
    }

    if (message.sender().isNet4Private()
        && buf.readableBytes() < MessageHeaderCodec.HEADER_PRIVATE_ADDRESS_SIZE) {
      return false;
    } else if (message.sender().isNet4Private()
        && buf.readableBytes() >= MessageHeaderCodec.HEADER_PRIVATE_ADDRESS_SIZE) {
      PeerSocketAddress internalPeerSocketAddress = PeerSocketAddress.create(buf, true);
      message.sender(message.sender().changeInternalPeerSocketAddress(internalPeerSocketAddress));
    }
    // we have set the content types already
    message.presetContentTypes(true);
    for (Content content : message.contentTypes()) {
      if (content == Content.EMPTY) {
        break;
      }
      if (content == Content.PUBLIC_KEY_SIGNATURE) {
        message.setHintSign();
      }
      contentTypes.offer(content);
    }
    LOG.debug("Parsed message {}.", message);
    return true;
  }
  @Override
  public int onDataRead(
      ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream)
      throws Http2Exception {
    FullHttpMessage msg = messageMap.get(streamId);
    if (msg == null) {
      throw connectionError(
          PROTOCOL_ERROR, "Data Frame received for unknown stream id %d", streamId);
    }

    ByteBuf content = msg.content();
    final int dataReadableBytes = data.readableBytes();
    if (content.readableBytes() > maxContentLength - dataReadableBytes) {
      throw connectionError(
          INTERNAL_ERROR,
          "Content length exceeded max of %d for stream id %d",
          maxContentLength,
          streamId);
    }

    content.writeBytes(data, data.readerIndex(), dataReadableBytes);

    if (endOfStream) {
      fireChannelRead(ctx, msg, streamId);
    }

    // All bytes have been processed.
    return dataReadableBytes + padding;
  }
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    long frameLength = decodeLength(in, 0);
    if (frameLength > maxFrameLength) {
      long discard = frameLength - in.readableBytes();
      tooLongFrameLength = frameLength;

      if (discard < 0) {
        // buffer contains more bytes then the frameLength so we can discard all now
        in.skipBytes((int) frameLength);
      } else {
        // Enter the discard mode and discard everything received so far.
        discardingTooLongFrame = true;
        bytesToDiscard = discard;
        in.skipBytes(in.readableBytes());
      }
      failIfNecessary(true);
    } else {
      int readerIndex = in.readerIndex();
      int actualFrameLength = (int) frameLength;
      ByteBuf buffer = extractFrame(ctx, in, readerIndex, actualFrameLength);
      in.readerIndex(readerIndex + actualFrameLength);
      out.add(buffer);
    }
  }
Ejemplo n.º 5
0
 protected void internalReceive(HttpResponseStatus status, HttpHeaders headers, ByteBuf content) {
   try {
     if (status.code() > 399) {
       byte[] b = new byte[content.readableBytes()];
       content.readBytes(b);
       onErrorResponse(status, headers, new String(b, CharsetUtil.UTF_8));
       return;
     }
     if (type == ByteBuf.class) {
       _doReceive(status, headers, type.cast(content));
     } else if (type == String.class || type == CharSequence.class) {
       byte[] b = new byte[content.readableBytes()];
       content.readBytes(b);
       _doReceive(status, headers, type.cast(new String(b, CharsetUtil.UTF_8)));
     } else if (type == byte[].class) {
       byte[] b = new byte[content.readableBytes()];
       content.readBytes(b);
       _doReceive(status, headers, type.cast(b));
     } else {
       ObjectMapper mapper = new ObjectMapper();
       byte[] b = new byte[content.readableBytes()];
       content.readBytes(b);
       try {
         Object o = mapper.readValue(b, type);
         _doReceive(status, headers, type.cast(o));
       } catch (Exception ex) {
         Exceptions.chuck(ex);
       }
     }
   } finally {
     latch.countDown();
   }
 }
Ejemplo n.º 6
0
 @Override
 public void addContent(ByteBuf buffer, boolean last) throws IOException {
   if (buffer != null) {
     long localsize = buffer.readableBytes();
     if (definedSize > 0 && definedSize < size + localsize) {
       throw new IOException("Out of size: " + (size + localsize) + " > " + definedSize);
     }
     size += localsize;
     if (byteBuf == null) {
       byteBuf = buffer;
     } else if (byteBuf instanceof CompositeByteBuf) {
       CompositeByteBuf cbb = (CompositeByteBuf) byteBuf;
       cbb.addComponent(buffer);
       cbb.writerIndex(cbb.writerIndex() + buffer.readableBytes());
     } else {
       CompositeByteBuf cbb = compositeBuffer();
       cbb.addComponents(byteBuf, buffer);
       cbb.writerIndex(byteBuf.readableBytes() + buffer.readableBytes());
       byteBuf = cbb;
     }
   }
   if (last) {
     completed = true;
   } else {
     if (buffer == null) {
       throw new NullPointerException("buffer");
     }
   }
 }
  @Override
  public Subobject parseSubobject(final ByteBuf buffer, final boolean loose)
      throws PCEPDeserializerException {
    Preconditions.checkArgument(
        buffer != null && buffer.isReadable(),
        "Array of bytes is mandatory. Can't be null or empty.");
    if (buffer.readableBytes() < HEADER_LENGTH) {
      throw new PCEPDeserializerException(
          "Wrong length of array of bytes. Passed: "
              + buffer.readableBytes()
              + "; Expected: >"
              + HEADER_LENGTH
              + ".");
    }
    final BitArray reserved = BitArray.valueOf(buffer, FLAGS_SIZE);
    final short cType = buffer.readUnsignedByte();

    final LabelType labelType = this.registry.parseLabel(cType, buffer.slice());
    if (labelType == null) {
      throw new PCEPDeserializerException(
          "Unknown C-TYPE for ero label subobject. Passed: " + cType);
    }
    final LabelBuilder builder = new LabelBuilder();
    builder.setUniDirectional(reserved.get(U_FLAG_OFFSET));
    builder.setLabelType(labelType);
    return new SubobjectBuilder()
        .setLoose(loose)
        .setSubobjectType(new LabelCaseBuilder().setLabel(builder.build()).build())
        .build();
  }
Ejemplo n.º 8
0
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // System.out.println("decode:" + in.readableBytes());
    if (in.readableBytes() > 4) {
      in.markReaderIndex();
      int needBytes = in.readInt();
      // System.out.println("needBytes:" + needBytes);
      if (in.readableBytes() >= needBytes) {
        byte[] content = new byte[in.readableBytes()];
        in.readBytes(content);
        // byte[] data= ZLipHelper.decompress(content);
        // System.out.println("data:" + new String(data));

        Amf3Input amf3Input = new Amf3Input(SerializationContext.getSerializationContext());
        //				amf3Input = new Amf3Input(SerializationContext.getSerializationContext());
        InputStream bais = new ByteArrayInputStream(content);
        amf3Input.setInputStream(bais);
        try {
          Object decoded = amf3Input.readObject();
          if (decoded != null) {
            out.add(decoded);
            // System.out.println("decoded:" + decoded);
          }
        } catch (Exception e) {
        }
        //				amf3Input.close();
      } else {
        in.resetReaderIndex();
      }
    }
  }
 @Override
 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
   Protocol.DirectionData prot = this.server ? this.protocol.TO_SERVER : this.protocol.TO_CLIENT;
   ByteBuf copy = in.copy();
   try {
     int packetId = DefinedPacket.readVarInt(in);
     DefinedPacket packet = null;
     if (prot.hasPacket(packetId)) {
       packet = prot.createPacket(packetId);
       packet.read(in, prot.getDirection(), this.protocolVersion);
       if (in.readableBytes() != 0) {
         throw new BadPacketException(
             "Did not read all bytes from packet "
                 + packet.getClass()
                 + " "
                 + packetId
                 + " Protocol "
                 + this.protocol
                 + " Direction "
                 + prot);
       }
     } else {
       in.skipBytes(in.readableBytes());
     }
     out.add(new PacketWrapper(packet, copy));
     copy = null;
   } finally {
     if (copy != null) {
       copy.release();
     }
   }
 }
Ejemplo n.º 10
0
  private void write(ByteBuf buff, boolean end) {
    int readableBytes = buff.readableBytes();
    if (readableBytes == 0 && !end) {
      // nothing to write to the connection just return
      return;
    }

    if (end) {
      completed = true;
    }
    if (!end && !chunked && !contentLengthSet()) {
      throw new IllegalStateException(
          "You must set the Content-Length header to be the total size of the message "
              + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
    }

    written += buff.readableBytes();
    if (conn == null) {
      if (pendingChunks == null) {
        pendingChunks = buff;
      } else {
        CompositeByteBuf pending;
        if (pendingChunks instanceof CompositeByteBuf) {
          pending = (CompositeByteBuf) pendingChunks;
        } else {
          pending = Unpooled.compositeBuffer();
          pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
          pendingChunks = pending;
        }
        pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
      }
      connect();
    } else {
      if (!headWritten) {
        writeHeadWithContent(buff, end);
      } else {
        if (end) {
          if (buff.isReadable()) {
            conn.writeToChannel(new DefaultLastHttpContent(buff, false));
          } else {
            conn.writeToChannel(LastHttpContent.EMPTY_LAST_CONTENT);
          }
        } else {
          conn.writeToChannel(new DefaultHttpContent(buff));
        }
      }
      if (end) {
        conn.reportBytesWritten(written);

        if (respHandler != null) {
          conn.endRequest();
        }
      }
    }
  }
Ejemplo n.º 11
0
  private SpdyHeadersFrame readHeaderBlockFrame(ByteBuf buffer) {
    int streamId;
    switch (type) {
      case SPDY_SYN_STREAM_FRAME:
        if (buffer.readableBytes() < 10) {
          return null;
        }

        int offset = buffer.readerIndex();
        streamId = getUnsignedInt(buffer, offset);
        int associatedToStreamId = getUnsignedInt(buffer, offset + 4);
        byte priority = (byte) (buffer.getByte(offset + 8) >> 5 & 0x07);
        buffer.skipBytes(10);
        length -= 10;

        SpdySynStreamFrame spdySynStreamFrame =
            new DefaultSpdySynStreamFrame(streamId, associatedToStreamId, priority);
        spdySynStreamFrame.setLast((flags & SPDY_FLAG_FIN) != 0);
        spdySynStreamFrame.setUnidirectional((flags & SPDY_FLAG_UNIDIRECTIONAL) != 0);

        return spdySynStreamFrame;

      case SPDY_SYN_REPLY_FRAME:
        if (buffer.readableBytes() < 4) {
          return null;
        }

        streamId = getUnsignedInt(buffer, buffer.readerIndex());
        buffer.skipBytes(4);
        length -= 4;

        SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamId);
        spdySynReplyFrame.setLast((flags & SPDY_FLAG_FIN) != 0);

        return spdySynReplyFrame;

      case SPDY_HEADERS_FRAME:
        if (buffer.readableBytes() < 4) {
          return null;
        }

        streamId = getUnsignedInt(buffer, buffer.readerIndex());
        buffer.skipBytes(4);
        length -= 4;

        SpdyHeadersFrame spdyHeadersFrame = new DefaultSpdyHeadersFrame(streamId);
        spdyHeadersFrame.setLast((flags & SPDY_FLAG_FIN) != 0);

        return spdyHeadersFrame;

      default:
        throw new Error("Shouldn't reach here.");
    }
  }
Ejemplo n.º 12
0
    /**
     * Transfers data from the original message to the channel, encrypting it in the process.
     *
     * <p>This method also breaks down the original message into smaller chunks when needed. This is
     * done to keep memory usage under control. This avoids having to copy the whole message data
     * into memory at once, and can avoid ballooning memory usage when transferring large messages
     * such as shuffle blocks.
     *
     * <p>The {@link #transfered()} counter also behaves a little funny, in that it won't go forward
     * until a whole chunk has been written. This is done because the code can't use the actual
     * number of bytes written to the channel as the transferred count (see {@link #count()}).
     * Instead, once an encrypted chunk is written to the output (including its header), the size of
     * the original block will be added to the {@link #transfered()} amount.
     */
    @Override
    public long transferTo(final WritableByteChannel target, final long position)
        throws IOException {

      Preconditions.checkArgument(position == transfered(), "Invalid position.");

      long reportedWritten = 0L;
      long actuallyWritten = 0L;
      do {
        if (currentChunk == null) {
          nextChunk();
        }

        if (currentHeader.readableBytes() > 0) {
          int bytesWritten = target.write(currentHeader.nioBuffer());
          currentHeader.skipBytes(bytesWritten);
          actuallyWritten += bytesWritten;
          if (currentHeader.readableBytes() > 0) {
            // Break out of loop if there are still header bytes left to write.
            break;
          }
        }

        actuallyWritten += target.write(currentChunk);
        if (!currentChunk.hasRemaining()) {
          // Only update the count of written bytes once a full chunk has been written.
          // See method javadoc.
          long chunkBytesRemaining = unencryptedChunkSize - currentReportedBytes;
          reportedWritten += chunkBytesRemaining;
          transferred += chunkBytesRemaining;
          currentHeader.release();
          currentHeader = null;
          currentChunk = null;
          currentChunkSize = 0;
          currentReportedBytes = 0;
        }
      } while (currentChunk == null && transfered() + reportedWritten < count());

      // Returning 0 triggers a backoff mechanism in netty which may harm performance. Instead,
      // we return 1 until we can (i.e. until the reported count would actually match the size
      // of the current chunk), at which point we resort to returning 0 so that the counts still
      // match, at the cost of some performance. That situation should be rare, though.
      if (reportedWritten != 0L) {
        return reportedWritten;
      }

      if (actuallyWritten > 0 && currentReportedBytes < currentChunkSize - 1) {
        transferred += 1L;
        currentReportedBytes += 1L;
        return 1L;
      }

      return 0L;
    }
Ejemplo n.º 13
0
 @Override
 public ByteBuf translateFrame(ByteBuf readBuffer)
     throws LimitExedeedException, InvalidDataException {
   while (readBuffer.isReadable()) {
     switch (status) {
       case STATUS_H:
         h = readBuffer.readByte();
         status = STATUS_L;
         break;
       case STATUS_L:
         l = readBuffer.readByte();
         final int blen =
             Protocol.order == ByteOrder.BIG_ENDIAN
                 ? (0x0000ff00 & (h << 8)) | (0x000000ff & l)
                 : (0x0000ff00 & (l << 8)) | (0x000000ff & h);
         if (context != null) {
           if (blen <= 0 || blen > maxFrameSize) {
             throw new LimitExedeedException("帧长度非法:" + h + "/" + l + ":" + blen);
           }
         }
         incompleteframe = PooledByteBufAllocator.DEFAULT.buffer(blen + 16 + 2);
         incompleteframe = incompleteframe.order(Protocol.order);
         incompleteframe.writeShort(blen);
         status = STATUS_C;
         break;
       case STATUS_C:
         int len = incompleteframe.writableBytes() - 16;
         len = len < readBuffer.readableBytes() ? len : readBuffer.readableBytes();
         // incompleteframe.writeBytes(readBuffer, len);
         if (readBuffer.hasMemoryAddress()) {
           PlatformDependent.copyMemory(
               readBuffer.memoryAddress() + readBuffer.readerIndex(),
               incompleteframe.memoryAddress() + incompleteframe.writerIndex(),
               len);
         } else if (readBuffer.hasArray()) {
           PlatformDependent.copyMemory(
               readBuffer.array(),
               readBuffer.arrayOffset() + readBuffer.readerIndex(),
               incompleteframe.memoryAddress() + incompleteframe.writerIndex(),
               len);
         }
         incompleteframe.writerIndex(incompleteframe.writerIndex() + len);
         readBuffer.readerIndex(readBuffer.readerIndex() + len);
         if ((incompleteframe.writableBytes() - 16) <= 0) {
           status = STATUS_H;
           return incompleteframe;
         }
         break;
     }
   }
   return null;
 }
Ejemplo n.º 14
0
    @Override
    public final void flushNow() {
      if (inFlushNow || flushTaskInProgress != null) {
        return;
      }

      inFlushNow = true;
      ChannelHandlerContext ctx = directOutboundContext();
      Throwable cause = null;
      try {
        if (ctx.hasOutboundByteBuffer()) {
          ByteBuf out = ctx.outboundByteBuffer();
          int oldSize = out.readableBytes();
          try {
            doFlushByteBuffer(out);
          } catch (Throwable t) {
            cause = t;
          } finally {
            final int newSize = out.readableBytes();
            final int writtenBytes = oldSize - newSize;
            if (writtenBytes > 0) {
              flushFutureNotifier.increaseWriteCounter(writtenBytes);
              if (newSize == 0) {
                out.discardReadBytes();
              }
            }
          }
        } else {
          MessageBuf<Object> out = ctx.outboundMessageBuffer();
          int oldSize = out.size();
          try {
            doFlushMessageBuffer(out);
          } catch (Throwable t) {
            cause = t;
          } finally {
            flushFutureNotifier.increaseWriteCounter(oldSize - out.size());
          }
        }

        if (cause == null) {
          flushFutureNotifier.notifyFlushFutures();
        } else {
          flushFutureNotifier.notifyFlushFutures(cause);
          if (cause instanceof IOException) {
            close(voidFuture());
          }
        }
      } finally {
        inFlushNow = false;
      }
    }
Ejemplo n.º 15
0
  private Object readControlFrame(ByteBuf buffer) {
    int streamId;
    int statusCode;
    switch (type) {
      case SPDY_RST_STREAM_FRAME:
        if (buffer.readableBytes() < 8) {
          return null;
        }

        streamId = getUnsignedInt(buffer, buffer.readerIndex());
        statusCode = getSignedInt(buffer, buffer.readerIndex() + 4);
        buffer.skipBytes(8);

        return new DefaultSpdyRstStreamFrame(streamId, statusCode);

      case SPDY_PING_FRAME:
        if (buffer.readableBytes() < 4) {
          return null;
        }

        int ID = getSignedInt(buffer, buffer.readerIndex());
        buffer.skipBytes(4);

        return new DefaultSpdyPingFrame(ID);

      case SPDY_GOAWAY_FRAME:
        if (buffer.readableBytes() < 8) {
          return null;
        }

        int lastGoodStreamId = getUnsignedInt(buffer, buffer.readerIndex());
        statusCode = getSignedInt(buffer, buffer.readerIndex() + 4);
        buffer.skipBytes(8);

        return new DefaultSpdyGoAwayFrame(lastGoodStreamId, statusCode);

      case SPDY_WINDOW_UPDATE_FRAME:
        if (buffer.readableBytes() < 8) {
          return null;
        }

        streamId = getUnsignedInt(buffer, buffer.readerIndex());
        int deltaWindowSize = getUnsignedInt(buffer, buffer.readerIndex() + 4);
        buffer.skipBytes(8);

        return new DefaultSpdyWindowUpdateFrame(streamId, deltaWindowSize);

      default:
        throw new Error("Shouldn't reach here.");
    }
  }
Ejemplo n.º 16
0
  /**
   * Load a string from the given buffer, reading first the two bytes of len and then the UTF-8
   * bytes of the string.
   *
   * @return the decoded string or null if NEED_DATA
   */
  static String decodeString(ByteBuf in) throws UnsupportedEncodingException {
    if (in.readableBytes() < 2) {
      return null;
    }
    // int strLen = Utils.readWord(in);
    int strLen = in.readUnsignedShort();
    if (in.readableBytes() < strLen) {
      return null;
    }
    byte[] strRaw = new byte[strLen];
    in.readBytes(strRaw);

    return new String(strRaw, "UTF-8");
  }
Ejemplo n.º 17
0
  @Override
  public ByteBuf readPacket() throws IOException {
    Preconditions.checkState(!mClosed, "PacketReader is closed while reading packets.");
    ByteBuf buf = null;
    mLock.lock();
    try {
      while (true) {
        if (mDone) {
          return null;
        }
        if (mPacketReaderException != null) {
          throw new IOException(mPacketReaderException);
        }
        buf = mPackets.poll();

        // TODO(peis): Have a better criteria to resume so that we can have fewer state changes.
        if (!tooManyPacketsPending()) {
          resume();
        }
        // Queue is empty.
        if (buf == null) {
          try {
            if (!mNotEmptyOrFailed.await(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
              throw new IOException(
                  String.format("Timeout while reading packet from block %d @ %s.", mId, mAddress));
            }
          } catch (InterruptedException e) {
            throw Throwables.propagate(e);
          }
        } else {
          if (buf.readableBytes() == 0) {
            buf.release();
            mDone = true;
            return null;
          }
          mPosToRead += buf.readableBytes();
          Preconditions.checkState(mPosToRead - mStart <= mBytesToRead);
          return buf;
        }
      }
    } catch (Throwable e) {
      if (buf != null) {
        buf.release();
      }
      throw e;
    } finally {
      mLock.unlock();
    }
  }
Ejemplo n.º 18
0
  @Override
  public void writeData(ByteBuf data) {
    super.writeData(data);

    ByteBuf tmpState = Unpooled.buffer();

    tmpState.writeByte(stateList.size());
    for (StateWithId stateWithId : stateList) {
      tmpState.writeByte(stateWithId.stateId);
      stateWithId.state.writeData(tmpState);
    }

    data.writeInt(tmpState.readableBytes());
    data.writeBytes(tmpState.readBytes(tmpState.readableBytes()));
  }
Ejemplo n.º 19
0
  private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data)
      throws Exception {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));

    chEncoder.writeOutbound(data.copy());
    chEncoder.flush();

    EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
    for (; ; ) {
      ByteBuf deflatedData = chEncoder.readOutbound();
      if (deflatedData == null) {
        break;
      }
      chDecoderZlib.writeInbound(deflatedData);
    }

    byte[] decompressed = new byte[data.readableBytes()];
    int offset = 0;
    for (; ; ) {
      ByteBuf buf = chDecoderZlib.readInbound();
      if (buf == null) {
        break;
      }
      int length = buf.readableBytes();
      buf.readBytes(decompressed, offset, length);
      offset += length;
      buf.release();
      if (offset == decompressed.length) {
        break;
      }
    }
    assertEquals(data, Unpooled.wrappedBuffer(decompressed));
    assertNull(chDecoderZlib.readInbound());

    // Closing an encoder channel will generate a footer.
    assertTrue(chEncoder.finish());
    for (; ; ) {
      Object msg = chEncoder.readOutbound();
      if (msg == null) {
        break;
      }
      ReferenceCountUtil.release(msg);
    }
    // But, the footer will be decoded into nothing. It's only for validation.
    assertFalse(chDecoderZlib.finish());

    data.release();
  }
Ejemplo n.º 20
0
  @Override
  public void fatalError(String message, String stack) {
    if (log.isDebugEnabled()) {
      log.debug("Sending HTTP error due to script error {}", message);
    }

    StringBuilder msg = new StringBuilder(message);
    if (stack != null) {
      msg.append('\n');
      msg.append(stack);
    }
    ByteBuf data = Unpooled.copiedBuffer(msg, Charsets.UTF8);

    response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    response.headers().add("Content-Type", "text/plain");
    response.headers().add("Content-Length", data.readableBytes());
    calculateKeepAlive(true);
    channel.write(response);

    DefaultHttpContent chunk = new DefaultHttpContent(data);
    channel.write(chunk);

    sendLastChunk();
    channel.flush();
    if (!keepAlive) {
      shutDown();
    }
  }
Ejemplo n.º 21
0
  public NettyMessage decode(ByteBuf in) throws Exception {
    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(in.readInt());
    header.setLength(in.readInt());
    header.setSessionID(in.readLong());
    header.setType(in.readByte());
    header.setPriority(in.readByte());

    int size = in.readInt();
    if (size > 0) {
      Map<String, Object> attch = new HashMap<String, Object>(size);
      int keySize = 0;
      byte[] keyArray = null;
      String key = null;
      for (int i = 0; i < size; i++) {
        keySize = in.readInt();
        keyArray = new byte[keySize];
        in.readBytes(keyArray);
        key = new String(keyArray, "UTF-8");
        attch.put(key, marshallingDecoder.decode(in));
      }
      keyArray = null;
      key = null;
      header.setAttachment(attch);
    }
    if (in.readableBytes() > 4) {
      message.setBody(marshallingDecoder.decode(in));
    }
    message.setHeader(header);
    return message;
  }
Ejemplo n.º 22
0
  public ByteBuf encode(NettyMessage msg) throws Exception {
    ByteBuf sendBuf = Unpooled.buffer();
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;
    byte[] keyArray = null;
    Object value = null;

    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
      key = param.getKey();
      keyArray = key.getBytes("UTF-8");
      sendBuf.writeInt(keyArray.length);
      sendBuf.writeBytes(keyArray);
      value = param.getValue();
      marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
      marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes());
    return sendBuf;
  }
Ejemplo n.º 23
0
  private void handleChunk(
      HttpContent chunk, //
      final Channel channel, //
      final NettyResponseFuture<?> future, //
      AsyncHandler<?> handler)
      throws IOException, Exception {

    boolean interrupt = false;
    boolean last = chunk instanceof LastHttpContent;

    // Netty 4: the last chunk is not empty
    if (last) {
      LastHttpContent lastChunk = (LastHttpContent) chunk;
      HttpHeaders trailingHeaders = lastChunk.trailingHeaders();
      if (!trailingHeaders.isEmpty()) {
        interrupt =
            handler.onHeadersReceived(new HttpResponseHeaders(trailingHeaders, true))
                != State.CONTINUE;
      }
    }

    ByteBuf buf = chunk.content();
    if (!interrupt
        && !(handler instanceof StreamedAsyncHandler)
        && (buf.readableBytes() > 0 || last)) {
      HttpResponseBodyPart part =
          config.getResponseBodyPartFactory().newResponseBodyPart(buf, last);
      interrupt = updateBodyAndInterrupt(future, handler, part);
    }

    if (interrupt || last) finishUpdate(future, channel, !last);
  }
Ejemplo n.º 24
0
  @Override
  public boolean renameTo(File dest) throws IOException {
    if (dest == null) {
      throw new NullPointerException("dest");
    }
    if (byteBuf == null) {
      // empty file
      dest.createNewFile();
      isRenamed = true;
      return true;
    }
    int length = byteBuf.readableBytes();
    FileOutputStream outputStream = new FileOutputStream(dest);
    FileChannel fileChannel = outputStream.getChannel();
    int written = 0;
    if (byteBuf.nioBufferCount() == 1) {
      ByteBuffer byteBuffer = byteBuf.nioBuffer();
      while (written < length) {
        written += fileChannel.write(byteBuffer);
      }
    } else {
      ByteBuffer[] byteBuffers = byteBuf.nioBuffers();
      while (written < length) {
        written += fileChannel.write(byteBuffers);
      }
    }

    fileChannel.force(false);
    fileChannel.close();
    outputStream.close();
    isRenamed = true;
    return written == length;
  }
Ejemplo n.º 25
0
  @Override
  protected void encode(ChannelHandlerContext chc, SubscribeMessage message, ByteBuf out) {
    if (message.subscriptions().isEmpty()) {
      throw new IllegalArgumentException("Found a subscribe message with empty topics");
    }

    if (message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
      throw new IllegalArgumentException(
          "Expected a message with QOS 1, found " + message.getQos());
    }

    ByteBuf variableHeaderBuff = chc.alloc().buffer(4);
    ByteBuf buff = null;
    try {
      variableHeaderBuff.writeShort(message.getMessageID());
      for (SubscribeMessage.Couple c : message.subscriptions()) {
        variableHeaderBuff.writeBytes(Utils.encodeString(c.topicFilter));
        variableHeaderBuff.writeByte(c.qos);
      }

      int variableHeaderSize = variableHeaderBuff.readableBytes();
      byte flags = Utils.encodeFlags(message);
      buff = chc.alloc().buffer(2 + variableHeaderSize);

      buff.writeByte(AbstractMessage.SUBSCRIBE << 4 | flags);
      buff.writeBytes(Utils.encodeRemainingLength(variableHeaderSize));
      buff.writeBytes(variableHeaderBuff);

      out.writeBytes(buff);
    } finally {
      variableHeaderBuff.release();
      buff.release();
    }
  }
Ejemplo n.º 26
0
 public void consumePacket(ByteBuf data) {
   int cnt = data.readableBytes() / 4;
   dimensions = new int[cnt];
   for (int i = 0; i < cnt; i++) {
     dimensions[i] = data.readInt();
   }
 }
Ejemplo n.º 27
0
 public Response(int id, int recipientID, String recipient, ByteBuf payload) {
   this.id = id;
   this.size = 20 + payload.readableBytes();
   this.recipientID = recipientID;
   this.recipient = recipient;
   this.payload = payload;
 }
    @Override
    public OFMeterFeaturesStatsReply readFrom(ByteBuf bb) throws OFParseError {
      int start = bb.readerIndex();
      // fixed value property version == 5
      byte version = bb.readByte();
      if (version != (byte) 0x5)
        throw new OFParseError("Wrong version: Expected=OFVersion.OF_14(5), got=" + version);
      // fixed value property type == 19
      byte type = bb.readByte();
      if (type != (byte) 0x13)
        throw new OFParseError("Wrong type: Expected=OFType.STATS_REPLY(19), got=" + type);
      int length = U16.f(bb.readShort());
      if (length != 32) throw new OFParseError("Wrong length: Expected=32(32), got=" + length);
      if (bb.readableBytes() + (bb.readerIndex() - start) < length) {
        // Buffer does not have all data yet
        bb.readerIndex(start);
        return null;
      }
      if (logger.isTraceEnabled()) logger.trace("readFrom - length={}", length);
      long xid = U32.f(bb.readInt());
      // fixed value property statsType == 11
      short statsType = bb.readShort();
      if (statsType != (short) 0xb)
        throw new OFParseError(
            "Wrong statsType: Expected=OFStatsType.METER_FEATURES(11), got=" + statsType);
      Set<OFStatsReplyFlags> flags = OFStatsReplyFlagsSerializerVer14.readFrom(bb);
      // pad: 4 bytes
      bb.skipBytes(4);
      OFMeterFeatures features = OFMeterFeaturesVer14.READER.readFrom(bb);

      OFMeterFeaturesStatsReplyVer14 meterFeaturesStatsReplyVer14 =
          new OFMeterFeaturesStatsReplyVer14(xid, flags, features);
      if (logger.isTraceEnabled()) logger.trace("readFrom - read={}", meterFeaturesStatsReplyVer14);
      return meterFeaturesStatsReplyVer14;
    }
    @Override
    public OFInstructionIdBsnAutoNegotiation readFrom(ByteBuf bb) throws OFParseError {
      int start = bb.readerIndex();
      // fixed value property type == 65535
      short type = bb.readShort();
      if (type != (short) 0xffff)
        throw new OFParseError(
            "Wrong type: Expected=OFInstructionType.EXPERIMENTER(65535), got=" + type);
      int length = U16.f(bb.readShort());
      if (length != 12) throw new OFParseError("Wrong length: Expected=12(12), got=" + length);
      if (bb.readableBytes() + (bb.readerIndex() - start) < length) {
        // Buffer does not have all data yet
        bb.readerIndex(start);
        return null;
      }
      if (logger.isTraceEnabled()) logger.trace("readFrom - length={}", length);
      // fixed value property experimenter == 0x5c16c7L
      int experimenter = bb.readInt();
      if (experimenter != 0x5c16c7)
        throw new OFParseError(
            "Wrong experimenter: Expected=0x5c16c7L(0x5c16c7L), got=" + experimenter);
      // fixed value property subtype == 0xbL
      int subtype = bb.readInt();
      if (subtype != 0xb)
        throw new OFParseError("Wrong subtype: Expected=0xbL(0xbL), got=" + subtype);

      if (logger.isTraceEnabled())
        logger.trace("readFrom - returning shared instance={}", INSTANCE);
      return INSTANCE;
    }
  @Override
  public void appendMessages(Tpp tpp, Collection<MessageBatchWithRawData> batches)
      throws Exception {
    ByteBuf bodyBuf = Unpooled.buffer();
    KafkaMessageBrokerSender sender = getSender(tpp.getTopic());
    try {
      for (MessageBatchWithRawData batch : batches) {
        List<PartialDecodedMessage> pdmsgs = batch.getMessages();
        for (PartialDecodedMessage pdmsg : pdmsgs) {
          m_messageCodec.encodePartial(pdmsg, bodyBuf);
          byte[] bytes = new byte[bodyBuf.readableBytes()];
          bodyBuf.readBytes(bytes);
          bodyBuf.clear();

          ByteBuf propertiesBuf = pdmsg.getDurableProperties();
          HermesPrimitiveCodec codec = new HermesPrimitiveCodec(propertiesBuf);
          Map<String, String> propertiesMap = codec.readStringStringMap();
          sender.send(tpp.getTopic(), propertiesMap.get("pK"), bytes);
          BrokerStatusMonitor.INSTANCE.kafkaSend(tpp.getTopic());
        }
      }
    } finally {
      bodyBuf.release();
    }
  }