Beispiel #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;
  }
Beispiel #2
0
 /**
  * Sets the value of the {@code "Connection"} header depending on the protocol version of the
  * specified message. This getMethod sets or removes the {@code "Connection"} header depending on
  * what the default keep alive mode of the message's protocol version is, as specified by {@link
  * HttpVersion#isKeepAliveDefault()}.
  *
  * <ul>
  *   <li>If the connection is kept alive by default:
  *       <ul>
  *         <li>set to {@code "close"} if {@code keepAlive} is {@code false}.
  *         <li>remove otherwise.
  *       </ul>
  *   <li>If the connection is closed by default:
  *       <ul>
  *         <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.
  *         <li>remove otherwise.
  *       </ul>
  * </ul>
  */
 public static void setKeepAlive(HttpMessage message, boolean keepAlive) {
   HttpHeaders h = message.headers();
   if (message.getProtocolVersion().isKeepAliveDefault()) {
     if (keepAlive) {
       h.remove(Names.CONNECTION);
     } else {
       h.set(Names.CONNECTION, Values.CLOSE);
     }
   } else {
     if (keepAlive) {
       h.set(Names.CONNECTION, Values.KEEP_ALIVE);
     } else {
       h.remove(Names.CONNECTION);
     }
   }
 }
Beispiel #3
0
  private void send(final ByteBuf buffer) throws Exception {
    DefaultFullHttpResponse rsp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buffer);

    if (!headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
      headers
          .remove(HttpHeaderNames.TRANSFER_ENCODING)
          .set(HttpHeaderNames.CONTENT_LENGTH, buffer.readableBytes());
    }

    if (keepAlive) {
      headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // dump headers
    rsp.headers().set(headers);

    Attribute<Boolean> async = ctx.attr(NettyRequest.ASYNC);
    boolean isAsync = async != null && async.get() == Boolean.TRUE;
    if (isAsync) {
      // we need flush, from async
      keepAlive(ctx.writeAndFlush(rsp));
    } else {
      keepAlive(ctx.write(rsp));
    }

    committed = true;
  }
Beispiel #4
0
  @Override
  public void send(final FileChannel channel) throws Exception {
    long len = channel.size();

    DefaultHttpResponse rsp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
    if (!headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
      headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
      headers.set(HttpHeaderNames.CONTENT_LENGTH, len);
    }

    if (keepAlive) {
      headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // dump headers
    rsp.headers().set(headers);
    ChannelHandlerContext ctx = this.ctx;
    ctx.attr(NettyRequest.NEED_FLUSH).set(false);
    ctx.channel()
        .eventLoop()
        .execute(
            () -> {
              // send headers
              ctx.write(rsp);
              ctx.write(new DefaultFileRegion(channel, 0, len));
              keepAlive(ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT));
            });

    committed = true;
  }
Beispiel #5
0
 private void prepareHeaders() {
   HttpHeaders headers = request.headers();
   headers.remove(TRANSFER_ENCODING);
   if (!headers.contains(HOST)) {
     request.headers().set(HOST, conn.hostHeader());
   }
   if (chunked) {
     HttpHeaders.setTransferEncodingChunked(request);
   }
   if (client.getOptions().isTryUseCompression()
       && request.headers().get(ACCEPT_ENCODING) == null) {
     // if compression should be used but nothing is specified by the user support deflate and
     // gzip.
     request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP);
   }
   if (!client.getOptions().isKeepAlive()
       && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) {
     request.headers().set(CONNECTION, CLOSE);
   } else if (client.getOptions().isKeepAlive()
       && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) {
     request.headers().set(CONNECTION, KEEP_ALIVE);
   }
 }
  @Test
  public void serverResponseHeaderInformational() throws Exception {
    final FullHttpMessage request =
        new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/info/test", true);
    HttpHeaders httpHeaders = request.headers();
    httpHeaders.set(HttpUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
    httpHeaders.set(HttpHeaders.Names.EXPECT, HttpHeaders.Values.CONTINUE);
    httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 0);
    final Http2Headers http2Headers =
        new DefaultHttp2Headers()
            .method(as("PUT"))
            .path(as("/info/test"))
            .set(
                as(HttpHeaders.Names.EXPECT.toString()),
                as(HttpHeaders.Values.CONTINUE.toString()));
    final FullHttpMessage response =
        new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE);
    final String text = "a big payload";
    final ByteBuf payload = Unpooled.copiedBuffer(text.getBytes());
    final FullHttpMessage request2 = request.copy(payload);
    final FullHttpMessage response2 =
        new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);

    try {
      runInChannel(
          clientChannel,
          new Http2Runnable() {
            @Override
            public void run() {
              frameWriter.writeHeaders(ctxClient(), 3, http2Headers, 0, false, newPromiseClient());
              ctxClient().flush();
            }
          });
      awaitRequests();
      ArgumentCaptor<FullHttpMessage> requestCaptor =
          ArgumentCaptor.forClass(FullHttpMessage.class);
      verify(serverListener).messageReceived(requestCaptor.capture());
      capturedRequests = requestCaptor.getAllValues();
      assertEquals(request, capturedRequests.get(0));
      cleanupCapturedRequests();
      reset(serverListener);

      httpHeaders = response.headers();
      httpHeaders.set(HttpUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
      httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 0);
      final Http2Headers http2HeadersResponse = new DefaultHttp2Headers().status(as("100"));
      runInChannel(
          serverConnectedChannel,
          new Http2Runnable() {
            @Override
            public void run() {
              frameWriter.writeHeaders(
                  ctxServer(), 3, http2HeadersResponse, 0, false, newPromiseServer());
              ctxServer().flush();
            }
          });
      awaitResponses();
      ArgumentCaptor<FullHttpMessage> responseCaptor =
          ArgumentCaptor.forClass(FullHttpMessage.class);
      verify(clientListener).messageReceived(responseCaptor.capture());
      capturedResponses = responseCaptor.getAllValues();
      assertEquals(response, capturedResponses.get(0));
      cleanupCapturedResponses();
      reset(clientListener);

      setServerLatch(1);
      httpHeaders = request2.headers();
      httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, text.length());
      httpHeaders.remove(HttpHeaders.Names.EXPECT);
      runInChannel(
          clientChannel,
          new Http2Runnable() {
            @Override
            public void run() {
              frameWriter.writeData(ctxClient(), 3, payload.retain(), 0, true, newPromiseClient());
              ctxClient().flush();
            }
          });
      awaitRequests();
      requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
      verify(serverListener).messageReceived(requestCaptor.capture());
      capturedRequests = requestCaptor.getAllValues();
      assertEquals(request2, capturedRequests.get(0));

      setClientLatch(1);
      httpHeaders = response2.headers();
      httpHeaders.set(HttpUtil.ExtensionHeaderNames.STREAM_ID.text(), 3);
      httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, 0);
      final Http2Headers http2HeadersResponse2 = new DefaultHttp2Headers().status(as("200"));
      runInChannel(
          serverConnectedChannel,
          new Http2Runnable() {
            @Override
            public void run() {
              frameWriter.writeHeaders(
                  ctxServer(), 3, http2HeadersResponse2, 0, true, newPromiseServer());
              ctxServer().flush();
            }
          });
      awaitResponses();
      responseCaptor = ArgumentCaptor.forClass(FullHttpMessage.class);
      verify(clientListener).messageReceived(responseCaptor.capture());
      capturedResponses = responseCaptor.getAllValues();
      assertEquals(response2, capturedResponses.get(0));
    } finally {
      request.release();
      request2.release();
      response.release();
      response2.release();
    }
  }
Beispiel #7
0
 @Override
 public void header(final String name, final Iterable<String> values) {
   headers.remove(name).add(name, values);
 }
Beispiel #8
0
  @SuppressWarnings("deprecation")
  private SpdySynStreamFrame createSynStreamFrame(HttpMessage httpMessage) throws Exception {
    // Get the Stream-ID, Associated-To-Stream-ID, Priority, URL, and scheme from the headers
    final HttpHeaders httpHeaders = httpMessage.headers();
    int streamID = httpHeaders.getInt(Names.STREAM_ID);
    int associatedToStreamId = httpHeaders.getInt(Names.ASSOCIATED_TO_STREAM_ID, 0);
    byte priority = (byte) httpHeaders.getInt(Names.PRIORITY, 0);
    String URL = httpHeaders.get(Names.URL);
    String scheme = httpHeaders.get(Names.SCHEME);
    httpHeaders.remove(Names.STREAM_ID);
    httpHeaders.remove(Names.ASSOCIATED_TO_STREAM_ID);
    httpHeaders.remove(Names.PRIORITY);
    httpHeaders.remove(Names.URL);
    httpHeaders.remove(Names.SCHEME);

    // 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);

    SpdySynStreamFrame spdySynStreamFrame =
        new DefaultSpdySynStreamFrame(streamID, associatedToStreamId, priority);

    // Unfold the first line of the message into name/value pairs
    SpdyHeaders frameHeaders = spdySynStreamFrame.headers();
    if (httpMessage instanceof FullHttpRequest) {
      HttpRequest httpRequest = (HttpRequest) httpMessage;
      frameHeaders.set(METHOD, httpRequest.method());
      frameHeaders.set(PATH, httpRequest.uri());
      frameHeaders.set(VERSION, httpMessage.protocolVersion());
    }
    if (httpMessage instanceof HttpResponse) {
      HttpResponse httpResponse = (HttpResponse) httpMessage;
      frameHeaders.set(STATUS, httpResponse.status());
      frameHeaders.set(PATH, URL);
      frameHeaders.set(VERSION, httpMessage.protocolVersion());
      spdySynStreamFrame.setUnidirectional(true);
    }

    // Replace the HTTP host header with the SPDY host header
    if (spdyVersion >= 3) {
      CharSequence host = httpHeaders.getUnconverted(HttpHeaders.Names.HOST);
      httpHeaders.remove(HttpHeaders.Names.HOST);
      frameHeaders.set(HOST, host);
    }

    // Set the SPDY scheme header
    if (scheme == null) {
      scheme = "https";
    }
    frameHeaders.set(SCHEME, scheme);

    // Transfer the remaining HTTP headers
    for (Map.Entry<String, String> entry : httpHeaders) {
      frameHeaders.add(entry.getKey(), entry.getValue());
    }
    currentStreamId = spdySynStreamFrame.streamId();
    spdySynStreamFrame.setLast(isLast(httpMessage));

    return spdySynStreamFrame;
  }