示例#1
0
  @SuppressWarnings("deprecation")
  private SpdySynReplyFrame createSynReplyFrame(HttpResponse httpResponse) throws Exception {
    // Get the Stream-ID from the headers
    final HttpHeaders httpHeaders = httpResponse.headers();
    int streamID = httpHeaders.getInt(Names.STREAM_ID);
    httpHeaders.remove(Names.STREAM_ID);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaders.Names.CONNECTION);
    httpHeaders.remove(HttpHeaders.Names.KEEP_ALIVE);
    httpHeaders.remove(HttpHeaders.Names.PROXY_CONNECTION);
    httpHeaders.remove(HttpHeaders.Names.TRANSFER_ENCODING);

    SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamID);
    SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
    // Unfold the first line of the response into name/value pairs
    frameHeaders.set(STATUS, httpResponse.status());
    frameHeaders.set(VERSION, httpResponse.protocolVersion());

    // Transfer the remaining HTTP headers
    for (Map.Entry<String, String> entry : httpHeaders) {
      spdySynReplyFrame.headers().add(entry.getKey(), entry.getValue());
    }

    currentStreamId = streamID;
    spdySynReplyFrame.setLast(isLast(httpResponse));

    return spdySynReplyFrame;
  }
示例#2
0
  @Override
  protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out)
      throws Exception {

    boolean valid = false;
    boolean last = false;

    if (msg instanceof HttpRequest) {

      HttpRequest httpRequest = (HttpRequest) msg;
      SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpRequest);
      out.add(spdySynStreamFrame);

      last = spdySynStreamFrame.isLast();
      valid = true;
    }
    if (msg instanceof HttpResponse) {

      HttpResponse httpResponse = (HttpResponse) msg;
      if (httpResponse.headers().contains(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID)) {
        SpdySynStreamFrame spdySynStreamFrame = createSynStreamFrame(httpResponse);
        last = spdySynStreamFrame.isLast();
        out.add(spdySynStreamFrame);
      } else {
        SpdySynReplyFrame spdySynReplyFrame = createSynReplyFrame(httpResponse);
        last = spdySynReplyFrame.isLast();
        out.add(spdySynReplyFrame);
      }

      valid = true;
    }
    if (msg instanceof HttpContent && !last) {

      HttpContent chunk = (HttpContent) msg;

      chunk.content().retain();
      SpdyDataFrame spdyDataFrame = new DefaultSpdyDataFrame(currentStreamId, chunk.content());
      spdyDataFrame.setLast(chunk instanceof LastHttpContent);
      if (chunk instanceof LastHttpContent) {
        LastHttpContent trailer = (LastHttpContent) chunk;
        HttpHeaders trailers = trailer.trailingHeaders();
        if (trailers.isEmpty()) {
          out.add(spdyDataFrame);
        } else {
          // Create SPDY HEADERS frame out of trailers
          SpdyHeadersFrame spdyHeadersFrame = new DefaultSpdyHeadersFrame(currentStreamId);
          for (Map.Entry<String, String> entry : trailers) {
            spdyHeadersFrame.headers().add(entry.getKey(), entry.getValue());
          }

          // Write HEADERS frame and append Data Frame
          out.add(spdyHeadersFrame);
          out.add(spdyDataFrame);
        }
      } else {
        out.add(spdyDataFrame);
      }

      valid = true;
    }

    if (!valid) {
      throw new UnsupportedMessageTypeException(msg);
    }
  }