private void readPushPromiseFrame(
      final ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
      throws Http2Exception {
    final int pushPromiseStreamId = streamId;
    int padding = flags.readPaddingLength(payload);
    final int promisedStreamId = readUnsignedInt(payload);

    // Create a handler that invokes the observer when the header block is complete.
    headersContinuation =
        new HeadersContinuation() {
          @Override
          public int getStreamId() {
            return pushPromiseStreamId;
          }

          @Override
          public void processFragment(
              boolean endOfHeaders, ByteBuf fragment, int padding, Http2FrameObserver observer)
              throws Http2Exception {
            builder().addFragment(fragment, ctx.alloc(), endOfHeaders);
            if (endOfHeaders) {
              Http2Headers headers = builder().buildHeaders();
              observer.onPushPromiseRead(
                  ctx, pushPromiseStreamId, promisedStreamId, headers, padding);
              close();
            }
          }
        };

    // Process the initial fragment, invoking the observer's callback if end of headers.
    final ByteBuf fragment = payload.readSlice(payload.readableBytes() - padding);
    headersContinuation.processFragment(flags.endOfHeaders(), fragment, padding, observer);
  }
 private static void readGoAwayFrame(
     ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
     throws Http2Exception {
   int lastStreamId = readUnsignedInt(payload);
   long errorCode = payload.readUnsignedInt();
   ByteBuf debugData = payload.readSlice(payload.readableBytes());
   observer.onGoAwayRead(ctx, lastStreamId, errorCode, debugData);
 }
  private void readContinuationFrame(ByteBuf payload, Http2FrameObserver observer)
      throws Http2Exception {
    int padding = flags.readPaddingLength(payload);

    // Process the initial fragment, invoking the observer's callback if end of headers.
    final ByteBuf continuationFragment = payload.readSlice(payload.readableBytes() - padding);
    headersContinuation.processFragment(
        flags.endOfHeaders(), continuationFragment, padding, observer);
  }
  private void processPayloadState(
      ChannelHandlerContext ctx, ByteBuf in, Http2FrameObserver observer) throws Http2Exception {
    if (in.readableBytes() < payloadLength) {
      // Wait until the entire payload has been read.
      return;
    }

    // Get a view of the buffer for the size of the payload.
    ByteBuf payload = in.readSlice(payloadLength);

    // Read the payload and fire the frame event to the observer.
    switch (frameType) {
      case DATA:
        readDataFrame(ctx, payload, observer);
        break;
      case HEADERS:
        readHeadersFrame(ctx, payload, observer);
        break;
      case PRIORITY:
        readPriorityFrame(ctx, payload, observer);
        break;
      case RST_STREAM:
        readRstStreamFrame(ctx, payload, observer);
        break;
      case SETTINGS:
        readSettingsFrame(ctx, payload, observer);
        break;
      case PUSH_PROMISE:
        readPushPromiseFrame(ctx, payload, observer);
        break;
      case PING:
        readPingFrame(ctx, payload, observer);
        break;
      case GO_AWAY:
        readGoAwayFrame(ctx, payload, observer);
        break;
      case WINDOW_UPDATE:
        readWindowUpdateFrame(ctx, payload, observer);
        break;
      case CONTINUATION:
        readContinuationFrame(payload, observer);
        break;
      case ALT_SVC:
        readAltSvcFrame(ctx, payload, observer);
        break;
      case BLOCKED:
        observer.onBlockedRead(ctx, streamId);
        break;
      default:
        // Should never happen.
        throw protocolError("Unsupported frame type: %s", frameType);
    }

    // Go back to reading the next frame header.
    state = State.FRAME_HEADER;
  }
 private void readPingFrame(
     ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
     throws Http2Exception {
   ByteBuf data = payload.readSlice(payload.readableBytes());
   if (flags.ack()) {
     observer.onPingAckRead(ctx, data);
   } else {
     observer.onPingRead(ctx, data);
   }
 }
Beispiel #6
0
 /**
  * Read a string from a channel buffer with the specified length. It resets the reader index of
  * the buffer to the end of the string. Defaults to UTF-8 encoding in case charset passed in is
  * null
  *
  * @param buffer The Netty buffer containing the String.
  * @param length The number of bytes in the String.
  * @param charset The Charset say 'UTF-8' in which the decoding needs to be done.
  * @return Returns the read string.
  */
 public static String readString(ByteBuf buffer, int length, Charset charset) {
   String str = null;
   if (null == charset) {
     charset = CharsetUtil.UTF_8;
   }
   try {
     ByteBuf stringBuffer = buffer.readSlice(length);
     str = stringBuffer.toString(charset);
   } catch (Exception e) {
     LOG.error("Error occurred while trying to read string from buffer: {}", e);
   }
   return str;
 }
  private void processPayloadState(
      ChannelHandlerContext ctx, ByteBuf in, Http2FrameListener listener) throws Http2Exception {
    if (in.readableBytes() < payloadLength) {
      // Wait until the entire payload has been read.
      return;
    }

    // Get a view of the buffer for the size of the payload.
    ByteBuf payload = in.readSlice(payloadLength);

    // Read the payload and fire the frame event to the listener.
    switch (frameType) {
      case DATA:
        readDataFrame(ctx, payload, listener);
        break;
      case HEADERS:
        readHeadersFrame(ctx, payload, listener);
        break;
      case PRIORITY:
        readPriorityFrame(ctx, payload, listener);
        break;
      case RST_STREAM:
        readRstStreamFrame(ctx, payload, listener);
        break;
      case SETTINGS:
        readSettingsFrame(ctx, payload, listener);
        break;
      case PUSH_PROMISE:
        readPushPromiseFrame(ctx, payload, listener);
        break;
      case PING:
        readPingFrame(ctx, payload, listener);
        break;
      case GO_AWAY:
        readGoAwayFrame(ctx, payload, listener);
        break;
      case WINDOW_UPDATE:
        readWindowUpdateFrame(ctx, payload, listener);
        break;
      case CONTINUATION:
        readContinuationFrame(payload, listener);
        break;
      default:
        readUnknownFrame(ctx, payload, listener);
        break;
    }

    // Go back to reading the next frame header.
    state = State.FRAME_HEADER;
  }
  private void readDataFrame(
      ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener)
      throws Http2Exception {
    short padding = readPadding(payload);

    // Determine how much data there is to read by removing the trailing
    // padding.
    int dataLength = payload.readableBytes() - padding;
    if (dataLength < 0) {
      throw streamError(streamId, FRAME_SIZE_ERROR, "Frame payload too small for padding.");
    }

    ByteBuf data = payload.readSlice(dataLength);
    listener.onDataRead(ctx, streamId, data, padding, flags.endOfStream());
    payload.skipBytes(payload.readableBytes());
  }
Beispiel #9
0
 public static <T, V> V readObject(ByteBuf buffer, Transform<ByteBuf, V> decoder) {
   int length = 0;
   if (null != buffer && buffer.readableBytes() > 2) {
     length = buffer.readUnsignedShort();
   } else {
     return null;
   }
   ByteBuf objBuffer = buffer.readSlice(length);
   V obj = null;
   try {
     obj = decoder.convert(objBuffer);
   } catch (Exception e) {
     LOG.error("Error occurred while trying to read object from buffer: {}", e);
   }
   return obj;
 }
 private void readAltSvcFrame(
     ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
     throws Http2Exception {
   long maxAge = payload.readUnsignedInt();
   int port = payload.readUnsignedShort();
   payload.skipBytes(1);
   short protocolIdLength = payload.readUnsignedByte();
   ByteBuf protocolId = payload.readSlice(protocolIdLength);
   short hostLength = payload.readUnsignedByte();
   String host = payload.toString(payload.readerIndex(), hostLength, UTF_8);
   payload.skipBytes(hostLength);
   String origin = null;
   if (payload.isReadable()) {
     origin = payload.toString(UTF_8);
     payload.skipBytes(payload.readableBytes());
   }
   observer.onAltSvcRead(ctx, streamId, maxAge, port, protocolId, host, origin);
 }
  private void readDataFrame(
      ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
      throws Http2Exception {
    int dataPadding = flags.readPaddingLength(payload);

    // Determine how much data there is to read by removing the trailing
    // padding.
    int dataLength = payload.readableBytes() - dataPadding;
    if (dataLength < 0) {
      throw protocolError("Frame payload too small for padding.");
    }

    ByteBuf data = payload.readSlice(dataLength);
    observer.onDataRead(
        ctx,
        streamId,
        data,
        dataPadding,
        flags.endOfStream(),
        flags.endOfSegment(),
        flags.compressed());
    payload.skipBytes(payload.readableBytes());
  }
 @Override
 public ByteBuf readSlice(int length) {
   return buf.readSlice(length);
 }
 private void readContinuationFrame(ByteBuf payload, Http2FrameListener listener)
     throws Http2Exception {
   // Process the initial fragment, invoking the listener's callback if end of headers.
   final ByteBuf continuationFragment = payload.readSlice(payload.readableBytes());
   headersContinuation.processFragment(flags.endOfHeaders(), continuationFragment, listener);
 }
 @Override
 public ByteBuf readSlice(int var1) {
   return a.readSlice(var1);
 }
 private void readUnknownFrame(
     ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener)
     throws Http2Exception {
   payload = payload.readSlice(payload.readableBytes());
   listener.onUnknownFrame(ctx, frameType, streamId, flags, payload);
 }
 @Override
 public DataReader read(int n) {
   return new NettyDataReader(buf.readSlice(n));
 }
  private void readHeadersFrame(
      final ChannelHandlerContext ctx, ByteBuf payload, Http2FrameObserver observer)
      throws Http2Exception {
    final int headersStreamId = streamId;
    final Http2Flags headersFlags = flags;
    int padding = flags.readPaddingLength(payload);

    // The callback that is invoked is different depending on whether priority information
    // is present in the headers frame.
    if (flags.priorityPresent()) {
      long word1 = payload.readUnsignedInt();
      final boolean exclusive = (word1 & 0x80000000L) > 0;
      final int streamDependency = (int) (word1 & 0x7FFFFFFFL);
      final short weight = (short) (payload.readUnsignedByte() + 1);
      final ByteBuf fragment = payload.readSlice(payload.readableBytes() - padding);

      // Create a handler that invokes the observer when the header block is complete.
      headersContinuation =
          new HeadersContinuation() {
            @Override
            public int getStreamId() {
              return headersStreamId;
            }

            @Override
            public void processFragment(
                boolean endOfHeaders, ByteBuf fragment, int padding, Http2FrameObserver observer)
                throws Http2Exception {
              builder().addFragment(fragment, ctx.alloc(), endOfHeaders);
              if (endOfHeaders) {
                Http2Headers headers = builder().buildHeaders();
                observer.onHeadersRead(
                    ctx,
                    headersStreamId,
                    headers,
                    streamDependency,
                    weight,
                    exclusive,
                    padding,
                    headersFlags.endOfStream(),
                    headersFlags.endOfSegment());
                close();
              }
            }
          };

      // Process the initial fragment, invoking the observer's callback if end of headers.
      headersContinuation.processFragment(flags.endOfHeaders(), fragment, padding, observer);
      return;
    }

    // The priority fields are not present in the frame. Prepare a continuation that invokes
    // the observer callback without priority information.
    headersContinuation =
        new HeadersContinuation() {
          @Override
          public int getStreamId() {
            return headersStreamId;
          }

          @Override
          public void processFragment(
              boolean endOfHeaders, ByteBuf fragment, int padding, Http2FrameObserver observer)
              throws Http2Exception {
            builder().addFragment(fragment, ctx.alloc(), endOfHeaders);
            if (endOfHeaders) {
              Http2Headers headers = builder().buildHeaders();
              observer.onHeadersRead(
                  ctx,
                  headersStreamId,
                  headers,
                  padding,
                  headersFlags.endOfStream(),
                  headersFlags.endOfSegment());
              close();
            }
          }
        };

    // Process the initial fragment, invoking the observer's callback if end of headers.
    final ByteBuf fragment = payload.readSlice(payload.readableBytes() - padding);
    headersContinuation.processFragment(flags.endOfHeaders(), fragment, padding, observer);
  }