private void readHeaders(Handler handler, int flags, int length, int streamId)
        throws IOException {
      if (streamId == 0) throw ioException("TYPE_HEADERS streamId == 0");

      boolean inFinished = (flags & FLAG_END_STREAM) != 0;

      while (true) {
        hpackReader.readHeaders(length);

        if ((flags & FLAG_END_HEADERS) != 0) {
          hpackReader.emitReferenceSet();
          List<String> namesAndValues = hpackReader.getAndReset();
          int priority = -1; // TODO: priority
          handler.headers(
              false,
              inFinished,
              streamId,
              -1,
              priority,
              namesAndValues,
              HeadersMode.HTTP_20_HEADERS);
          return;
        }

        // Read another continuation frame.
        int w1 = in.readInt();
        int w2 = in.readInt();

        length = (w1 & 0xffff0000) >> 16;
        int newType = (w1 & 0xff00) >> 8;
        flags = w1 & 0xff;

        // TODO: remove in draft 8: CONTINUATION no longer sets END_STREAM
        inFinished = (flags & FLAG_END_STREAM) != 0;

        // boolean u = (w2 & 0x80000000) != 0; // Unused.
        int newStreamId = (w2 & 0x7fffffff);

        if (newType != TYPE_CONTINUATION) {
          throw ioException("TYPE_CONTINUATION didn't have FLAG_END_HEADERS");
        }
        if (newStreamId != streamId) throw ioException("TYPE_CONTINUATION streamId changed");
      }
    }
示例#2
0
文件: Http2.java 项目: ihrthk/okhttp
    private void readHeaders(Handler handler, int length, byte flags, int streamId)
        throws IOException {
      if (streamId == 0) throw ioException("PROTOCOL_ERROR: TYPE_HEADERS streamId == 0");

      boolean endStream = (flags & FLAG_END_STREAM) != 0;

      short padding = (flags & FLAG_PADDED) != 0 ? (short) (source.readByte() & 0xff) : 0;

      if ((flags & FLAG_PRIORITY) != 0) {
        readPriority(handler, streamId);
        length -= 5; // account for above read.
      }

      length = lengthWithoutPadding(length, flags, padding);

      List<Header> headerBlock = readHeaderBlock(length, padding, flags, streamId);

      handler.headers(false, endStream, streamId, -1, headerBlock, HeadersMode.HTTP_20_HEADERS);
    }