Java Code Examples for io.netty.handler.codec.http.HttpHeaders#add()

The following examples show how to use io.netty.handler.codec.http.HttpHeaders#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with what is expected.
 * @param blobId the blob ID of the blob to GET.
 * @param range the {@link ByteRange} for the request.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param isPrivate {@code true} if the blob is private, {@code false} if not.
 * @param expectedContent the expected content of the blob.
 * @param accountName the account name that should be in the response
 * @param containerName the container name that should be in the response
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getBlobAndVerify(String blobId, ByteRange range, GetOption getOption, HttpHeaders expectedHeaders,
    boolean isPrivate, ByteBuffer expectedContent, String accountName, String containerName)
    throws ExecutionException, InterruptedException, RestServiceException {
  HttpHeaders headers = new DefaultHttpHeaders();
  if (range != null) {
    headers.add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
  }
  if (getOption != null) {
    headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
  }
  FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, headers, null);
  ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
  verifyGetBlobResponse(responseParts, range, expectedHeaders, isPrivate, expectedContent, accountName,
      containerName);
}
 
Example 2
Source File: ComponentTestUtils.java    From riposte with Apache License 2.0 6 votes vote down vote up
public static HttpHeaders extractHeadersFromRawRequestOrResponse(String rawRequestOrResponseString) {
    int indexOfFirstCrlf = rawRequestOrResponseString.indexOf("\r\n");
    int indexOfBodySeparator = rawRequestOrResponseString.indexOf("\r\n\r\n");

    if (indexOfFirstCrlf == -1 || indexOfBodySeparator == -1) {
        throw new IllegalArgumentException("The given rawRequestOrResponseString does not appear to be a valid HTTP message");
    }

    String concatHeaders = rawRequestOrResponseString.substring(indexOfFirstCrlf + "\r\n".length(), indexOfBodySeparator);

    HttpHeaders extractedHeaders = new DefaultHttpHeaders();

    for (String concatHeader : split(concatHeaders, "\r\n")) {
        extractedHeaders.add(substringBefore(concatHeader, HEADER_SEPARATOR).trim(), substringAfter(concatHeader, HEADER_SEPARATOR).trim());
    }

    return extractedHeaders;
}
 
Example 3
Source File: RequestInfoForLoggingRiposteAdapterTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void getHeaderMapDelegatesToRequestInfoAndCachesResult() {
    Map<String, List<String>> expectedHeaderMap = new TreeMap<>(MapBuilder.<String, List<String>>builder()
                                                                          .put("header1", Arrays.asList("h1val1"))
                                                                          .put("header2", Arrays.asList("h2val1", "h2val2"))
                                                                          .build());

    HttpHeaders nettyHeaders = new DefaultHttpHeaders();
    for (Map.Entry<String, List<String>> headerEntry : expectedHeaderMap.entrySet()) {
        nettyHeaders.add(headerEntry.getKey(), headerEntry.getValue());
    }
    setFieldOnRequestInfo("headers", nettyHeaders);
    Map<String, List<String>> actualHeaderMap = adapter.getHeadersMap();
    assertThat(actualHeaderMap, is(expectedHeaderMap));
    assertThat(adapter.getHeadersMap(), sameInstance(actualHeaderMap));
}
 
Example 4
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the user metadata of the blob with blob ID {@code blobId} and verifies them against what is expected.
 * @param blobId the blob ID of the blob to HEAD.
 * @param getOption the options to use while getting the blob.
 * @param expectedHeaders the expected headers in the response.
 * @param usermetadata if non-null, this is expected to come as the body.
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void getUserMetadataAndVerify(String blobId, GetOption getOption, HttpHeaders expectedHeaders,
    byte[] usermetadata) throws ExecutionException, InterruptedException {
  HttpHeaders headers = new DefaultHttpHeaders();
  if (getOption != null) {
    headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
  }
  FullHttpRequest httpRequest =
      buildRequest(HttpMethod.GET, blobId + "/" + RestUtils.SubResource.UserMetadata, headers, null);
  ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
  HttpResponse response = getHttpResponse(responseParts);
  assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
  verifyTrackingHeaders(response);
  checkCommonGetHeadHeaders(response.headers());
  verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts.queue);
  if (usermetadata == null) {
    assertEquals("Content-Length is not 0", 0, HttpUtil.getContentLength(response));
    assertNoContent(responseParts.queue, 1);
  }
  assertTrue("Channel should be active", HttpUtil.isKeepAlive(response));
}
 
Example 5
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the blob with blob ID {@code blobId} and verifies that the blob is not returned as blob is not modified
 * @param blobId the blob ID of the blob to GET.
 * @param getOption the options to use while getting the blob.
 * @param isPrivate {@code true} if the blob is private, {@code false} if not.
 * @throws Exception
 */
private void getNotModifiedBlobAndVerify(String blobId, GetOption getOption, boolean isPrivate) throws Exception {
  HttpHeaders headers = new DefaultHttpHeaders();
  if (getOption != null) {
    headers.add(RestUtils.Headers.GET_OPTION, getOption.toString());
  }
  headers.add(RestUtils.Headers.IF_MODIFIED_SINCE, new Date());
  FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, headers, null);
  ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
  HttpResponse response = getHttpResponse(responseParts);
  assertEquals("Unexpected response status", HttpResponseStatus.NOT_MODIFIED, response.status());
  assertNotNull("Date header should be set", response.headers().get(RestUtils.Headers.DATE));
  assertNotNull("Last-Modified header should be set", response.headers().get("Last-Modified"));
  assertNull("Content-Length should not be set", response.headers().get(RestUtils.Headers.CONTENT_LENGTH));
  assertNull("Accept-Ranges should not be set", response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
  assertNull("Content-Range header should not be set", response.headers().get(RestUtils.Headers.CONTENT_RANGE));
  assertNull("Life-Version header should not be set", response.headers().get(RestUtils.Headers.LIFE_VERSION));
  assertNull(RestUtils.Headers.BLOB_SIZE + " should have been null ",
      response.headers().get(RestUtils.Headers.BLOB_SIZE));
  assertNull("Content-Type should have been null", response.headers().get(RestUtils.Headers.CONTENT_TYPE));
  verifyTrackingHeaders(response);
  verifyCacheHeaders(isPrivate, response);
  assertNoContent(responseParts.queue, 1);
}
 
Example 6
Source File: HttpObjectEncoderBenchmark.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[256];
    content = Unpooled.buffer(bytes.length);
    content.writeBytes(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());
    HttpHeaders headersWithChunked = new DefaultHttpHeaders(false);
    headersWithChunked.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    HttpHeaders headersWithContentLength = new DefaultHttpHeaders(false);
    headersWithContentLength.add(HttpHeaderNames.CONTENT_LENGTH, testContent.readableBytes());

    fullRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", testContent,
            headersWithContentLength, EmptyHttpHeaders.INSTANCE);
    contentLengthRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index",
            headersWithContentLength);
    chunkedRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/index", headersWithChunked);
    lastContent = new DefaultLastHttpContent(testContent, false);

    encoder = new HttpRequestEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT :
            UnpooledByteBufAllocator.DEFAULT, encoder) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
 
Example 7
Source File: GatewayServiceConfigurationReaderTest.java    From azure-cosmosdb-java with MIT License 5 votes vote down vote up
private HttpClientResponse<ByteBuf> getMockResponse(String databaseAccountJson) {
    HttpClientResponse<ByteBuf> resp = Mockito.mock(HttpClientResponse.class);
    Mockito.doReturn(HttpResponseStatus.valueOf(200)).when(resp).getStatus();
    ByteBuf byteBuffer = ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, databaseAccountJson);

    Mockito.doReturn(Observable.just(byteBuffer))
            .when(resp).getContent();

    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders = httpHeaders.add(HttpConstants.HttpHeaders.CONTENT_LENGTH, byteBuffer.writerIndex());

    DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.valueOf(200), httpHeaders);

    try {
        Constructor<HttpResponseHeaders> constructor = HttpResponseHeaders.class
                .getDeclaredConstructor(HttpResponse.class);
        constructor.setAccessible(true);
        HttpResponseHeaders httpResponseHeaders = constructor.newInstance(httpResponse);
        Mockito.doReturn(httpResponseHeaders).when(resp).getHeaders();

    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException e) {
        throw new IllegalStateException("Failed to instantiate class object.", e);
    }
    return resp;
}
 
Example 8
Source File: ODataNettyHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNettyReqResp_GetMethod() {
  MetadataProcessor processor = mock(MetadataProcessor.class);
  final ODataNetty odata = ODataNetty.newInstance();
  final ServiceMetadata metadata = odata.createServiceMetadata(
      new EdmTechProvider(), Collections.<EdmxReference> emptyList());

  ODataNettyHandler handler = odata.createNettyHandler(metadata);

  handler.register(processor);
  DefaultFullHttpRequest nettyRequest = mock(DefaultFullHttpRequest.class);
  io.netty.handler.codec.http.HttpMethod httpMethod = mock(io.netty.handler.codec.http.HttpMethod.class);
  when(httpMethod.name()).thenReturn("GET");
  when(nettyRequest.method()).thenReturn(httpMethod);
  HttpVersion httpVersion = mock(HttpVersion.class);
  when(httpVersion.text()).thenReturn("HTTP/1.0");
  when(nettyRequest.protocolVersion()).thenReturn(httpVersion);
  when(nettyRequest.uri()).thenReturn("/odata.svc/$metadata");
  HttpHeaders headers = mock(HttpHeaders.class);
  headers.add("Accept", "application/atom+xml");
  Set<String> set = new HashSet<String>();
  set.add("Accept");
  when(headers.names()).thenReturn(set);
  when(nettyRequest.headers()).thenReturn(headers);
  when(nettyRequest.content()).thenReturn(Unpooled.buffer());
  
  DefaultFullHttpResponse nettyResponse = mock(DefaultFullHttpResponse.class);
  when(nettyResponse.status()).thenReturn(HttpResponseStatus.OK);
  when(nettyResponse.headers()).thenReturn(headers);
  
  when(nettyResponse.content()).thenReturn(Unpooled.buffer());

  Map<String, String> requestParams = new HashMap<String, String>();
  requestParams.put("contextPath", "/odata.svc");
  handler.processNettyRequest(nettyRequest, nettyResponse, requestParams);
  
  nettyResponse.status();
  assertEquals(HttpStatusCode.OK.getStatusCode(), HttpResponseStatus.OK.code());
}
 
Example 9
Source File: HttpConversionUtilTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void stripConnectionHeadersAndNominees() {
    HttpHeaders inHeaders = new DefaultHttpHeaders();
    inHeaders.add(CONNECTION, "foo");
    inHeaders.add("foo", "bar");
    Http2Headers out = new DefaultHttp2Headers();
    HttpConversionUtil.toHttp2Headers(inHeaders, out);
    assertTrue(out.isEmpty());
}
 
Example 10
Source File: HttpConversionUtilTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void stripTEHeadersAccountsForOWS() {
    HttpHeaders inHeaders = new DefaultHttpHeaders();
    inHeaders.add(TE, " " + TRAILERS + " ");
    Http2Headers out = new DefaultHttp2Headers();
    HttpConversionUtil.toHttp2Headers(inHeaders, out);
    assertSame(TRAILERS, out.get(TE));
}
 
Example 11
Source File: Headers.java    From xio with Apache License 2.0 5 votes vote down vote up
/**
 * @param isTrailer this Headers object will be used for trailers.
 * @param isRequest this Headers object will be used in a request header.
 * @return an Http1 Headers object based on the values in this Headers object.
 */
default HttpHeaders http1Headers(boolean isTrailer, boolean isRequest) {
  // TODO(CK): filter out headers that can't be in a trailer
  // TODO(CK): filter out headers that can't be in a request
  HttpHeaders result = new DefaultHttpHeaders();
  for (Entry<CharSequence, CharSequence> entry : this) {
    result.add(entry.getKey(), entry.getValue());
  }
  return result;
}
 
Example 12
Source File: HttpConversionUtilTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void stripTEHeadersCsvSeparatedAccountsForValueSimilarToTrailers() {
    HttpHeaders inHeaders = new DefaultHttpHeaders();
    inHeaders.add(TE, GZIP + "," + TRAILERS + "foo");
    Http2Headers out = new DefaultHttp2Headers();
    HttpConversionUtil.toHttp2Headers(inHeaders, out);
    assertFalse(out.contains(TE));
}
 
Example 13
Source File: ClientCodecFunctionalTest.java    From xio with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodePost() {
  ByteBuf content = Unpooled.wrappedBuffer("this is the content".getBytes());
  HttpHeaders headers = new DefaultHttpHeaders();
  headers.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);

  Request request =
      new Request(
          UUID.fromString("70b3e594-2387-412c-9050-f6f7f46b64d5"),
          SettableFuture.create(),
          SettableFuture.create());
  HttpRequest payload =
      new DefaultFullHttpRequest(
          HttpVersion.HTTP_1_1,
          HttpMethod.POST,
          "/path",
          content,
          headers,
          new DefaultHttpHeaders());

  Message message = new Message(request, payload);
  channel.writeOutbound(message);
  channel.runPendingTasks();

  ByteBuf expectedLength = Unpooled.copiedBuffer(hexToBytes("007d"));
  ByteBuf expectedEncoded =
      Unpooled.copiedBuffer(
          hexToBytes(
              "37306233653539342d323338372d343132632d393035302d6636663766343662363464350000000100000051504f5354202f7061746820485454502f312e310d0a7472616e736665722d656e636f64696e673a206368756e6b65640d0a0d0a31330d0a746869732069732074686520636f6e74656e740d0a300d0a0d0a"));

  ByteBuf length = (ByteBuf) channel.outboundMessages().poll();
  ByteBuf encoded = (ByteBuf) channel.outboundMessages().poll();

  assertTrue(
      "Expected: " + ByteBufUtil.hexDump(expectedLength),
      ByteBufUtil.equals(expectedLength, length));
  assertTrue(
      "Expected: " + ByteBufUtil.hexDump(expectedEncoded),
      ByteBufUtil.equals(expectedEncoded, encoded));
}
 
Example 14
Source File: WebSocketExtensionUtilTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsWebsocketUpgrade() {
    HttpHeaders headers = new DefaultHttpHeaders();
    assertFalse(WebSocketExtensionUtil.isWebsocketUpgrade(headers));

    headers.add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    assertFalse(WebSocketExtensionUtil.isWebsocketUpgrade(headers));

    headers.add(HttpHeaderNames.CONNECTION, "Keep-Alive, Upgrade");
    assertTrue(WebSocketExtensionUtil.isWebsocketUpgrade(headers));
}
 
Example 15
Source File: NettyToStyxRequestDecoderTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void decodesNettyInternalRequestToStyxRequest() throws Exception {
    FullHttpRequest originalRequest = newHttpRequest("/uri");
    HttpHeaders originalRequestHeaders = originalRequest.headers();
    originalRequestHeaders.add("Foo", "Bar");
    originalRequestHeaders.add("Bar", "Bar");
    originalRequestHeaders.add("Host", "foo.com");

    LiveHttpRequest styxRequest = decode(originalRequest);

    assertThat(styxRequest.id().toString(), is("1"));
    assertThat(styxRequest.url().encodedUri(), is(originalRequest.getUri()));
    assertThat(styxRequest.method(), is(HttpMethod.GET));
    assertThatHttpHeadersAreSame(styxRequest.headers(), originalRequestHeaders);
}
 
Example 16
Source File: AbstractHttp2ClientTransport.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
private void addToHeader(HttpHeaders headers, CharSequence key, CharSequence value) {
    if (StringUtils.isNotEmpty(value)) {
        headers.add(key, value);
    }
}
 
Example 17
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Upload data chunks using chunk upload signed URL.
 * @param account the {@link Account} to upload into.
 * @param container the {@link Container} to upload into.
 * @param chunkSizes The sizes for each data chunk to upload.
 * @return the list of signed chunk IDs for the uploaded chunks and an array containing the concatenated content of
 *         the data chunks.
 * @throws Exception
 */
private Pair<List<String>, byte[]> uploadDataChunksAndVerify(Account account, Container container, int... chunkSizes)
    throws Exception {
  IdSigningService idSigningService = new AmbryIdSigningService();
  HttpHeaders chunkUploadHeaders = new DefaultHttpHeaders();
  chunkUploadHeaders.add(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
  chunkUploadHeaders.add(RestUtils.Headers.CHUNK_UPLOAD, "true");
  setAmbryHeadersForPut(chunkUploadHeaders, TTL_SECS, !container.isCacheable(), "chunkUploader",
      "application/octet-stream", "stitchedUploadTest", account.getName(), container.getName());

  // POST
  // Get signed URL
  FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, Operations.GET_SIGNED_URL, chunkUploadHeaders, null);
  ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
  HttpResponse response = getHttpResponse(responseParts);
  assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
  verifyTrackingHeaders(response);
  String signedPostUrl = response.headers().get(RestUtils.Headers.SIGNED_URL);
  assertNotNull("Did not get a signed POST URL", signedPostUrl);
  assertNoContent(responseParts.queue, 1);

  List<String> signedChunkIds = new ArrayList<>();
  ByteArrayOutputStream fullContentStream = new ByteArrayOutputStream();
  URI uri = new URI(signedPostUrl);
  for (int chunkSize : chunkSizes) {
    byte[] contentArray = TestUtils.getRandomBytes(chunkSize);
    ByteBuffer content = ByteBuffer.wrap(contentArray);
    // Use signed URL to POST
    httpRequest = buildRequest(HttpMethod.POST, uri.getPath() + "?" + uri.getQuery(), null, content);
    responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
    String signedId = verifyPostAndReturnBlobId(responseParts, chunkSize);
    assertTrue("Blob ID for chunk upload must be signed", idSigningService.isIdSigned(signedId.substring(1)));
    Pair<String, Map<String, String>> idAndMetadata = idSigningService.parseSignedId(signedId.substring(1));
    // Inspect metadata fields
    String chunkUploadSession = idAndMetadata.getSecond().get(RestUtils.Headers.SESSION);
    assertNotNull("x-ambry-chunk-upload-session should be present in signed ID", chunkUploadSession);
    String blobSize = idAndMetadata.getSecond().get(RestUtils.Headers.BLOB_SIZE);
    assertNotNull("x-ambry-blob-size should be present in signed ID", blobSize);
    assertEquals("wrong size value in signed id", content.capacity(), Long.parseLong(blobSize));
    HttpHeaders expectedGetHeaders = new DefaultHttpHeaders().add(chunkUploadHeaders);
    // Use signed ID and blob ID for GET request
    expectedGetHeaders.add(RestUtils.Headers.BLOB_SIZE, content.capacity());
    // Blob TTL for chunk upload is fixed
    expectedGetHeaders.set(RestUtils.Headers.TTL, FRONTEND_CONFIG.chunkUploadInitialChunkTtlSecs);
    expectedGetHeaders.set(RestUtils.Headers.LIFE_VERSION, "0");
    for (String id : new String[]{signedId, idAndMetadata.getFirst()}) {
      getBlobAndVerify(id, null, GetOption.None, expectedGetHeaders, !container.isCacheable(), content,
          account.getName(), container.getName());
      getBlobInfoAndVerify(id, GetOption.None, expectedGetHeaders, !container.isCacheable(), account.getName(),
          container.getName(), null);
    }
    signedChunkIds.add(addClusterPrefix ? "/" + CLUSTER_NAME + signedId : signedId);
    fullContentStream.write(contentArray);
  }
  return new Pair<>(signedChunkIds, fullContentStream.toByteArray());
}
 
Example 18
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 4 votes vote down vote up
/**
 * Utility to test blob POST, GET, HEAD and DELETE operations for a specified size
 * @param contentSize the size of the blob to be tested
 * @param toPostAccount the {@link Account} to use in post headers. Can be {@code null} if only using service ID.
 * @param toPostContainer the {@link Container} to use in post headers. Can be {@code null} if only using service ID.
 * @param serviceId the serviceId to use for the POST
 * @param isPrivate the isPrivate flag to pass as part of the POST
 * @param expectedAccountName the expected account name in some response.
 * @param expectedContainerName the expected container name in some responses.
 * @param multipartPost {@code true} if multipart POST is desired, {@code false} otherwise.
 * @throws Exception
 */
private void doPostGetHeadUpdateDeleteUndeleteTest(int contentSize, Account toPostAccount, Container toPostContainer,
    String serviceId, boolean isPrivate, String expectedAccountName, String expectedContainerName,
    boolean multipartPost) throws Exception {
  ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(contentSize));
  String contentType = "application/octet-stream";
  String ownerId = "postGetHeadDeleteOwnerID";
  String accountNameInPost = toPostAccount != null ? toPostAccount.getName() : null;
  String containerNameInPost = toPostContainer != null ? toPostContainer.getName() : null;
  HttpHeaders headers = new DefaultHttpHeaders();
  setAmbryHeadersForPut(headers, TTL_SECS, isPrivate, serviceId, contentType, ownerId, accountNameInPost,
      containerNameInPost);
  String blobId;
  byte[] usermetadata = null;
  if (multipartPost) {
    usermetadata = TestUtils.getRandomString(32).getBytes();
    blobId = multipartPostBlobAndVerify(headers, content, ByteBuffer.wrap(usermetadata));
  } else {
    headers.add(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
    headers.add(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key2", "value2");
    blobId = postBlobAndVerify(headers, content, contentSize);
  }
  headers.add(RestUtils.Headers.BLOB_SIZE, content.capacity());
  headers.add(RestUtils.Headers.LIFE_VERSION, "0");
  getBlobAndVerify(blobId, null, null, headers, isPrivate, content, expectedAccountName, expectedContainerName);
  getHeadAndVerify(blobId, null, null, headers, isPrivate, expectedAccountName, expectedContainerName);
  getBlobAndVerify(blobId, null, GetOption.None, headers, isPrivate, content, expectedAccountName,
      expectedContainerName);
  getHeadAndVerify(blobId, null, GetOption.None, headers, isPrivate, expectedAccountName, expectedContainerName);
  ByteRange range = ByteRanges.fromLastNBytes(ThreadLocalRandom.current().nextLong(content.capacity() + 1));
  getBlobAndVerify(blobId, range, null, headers, isPrivate, content, expectedAccountName, expectedContainerName);
  getHeadAndVerify(blobId, range, null, headers, isPrivate, expectedAccountName, expectedContainerName);
  if (contentSize > 0) {
    range = ByteRanges.fromStartOffset(ThreadLocalRandom.current().nextLong(content.capacity()));
    getBlobAndVerify(blobId, range, null, headers, isPrivate, content, expectedAccountName, expectedContainerName);
    getHeadAndVerify(blobId, range, null, headers, isPrivate, expectedAccountName, expectedContainerName);
    long random1 = ThreadLocalRandom.current().nextLong(content.capacity());
    long random2 = ThreadLocalRandom.current().nextLong(content.capacity());
    range = ByteRanges.fromOffsetRange(Math.min(random1, random2), Math.max(random1, random2));
    getBlobAndVerify(blobId, range, null, headers, isPrivate, content, expectedAccountName, expectedContainerName);
    getHeadAndVerify(blobId, range, null, headers, isPrivate, expectedAccountName, expectedContainerName);
  }
  getNotModifiedBlobAndVerify(blobId, null, isPrivate);
  getUserMetadataAndVerify(blobId, null, headers, usermetadata);
  getBlobInfoAndVerify(blobId, null, headers, isPrivate, expectedAccountName, expectedContainerName, usermetadata);
  updateBlobTtlAndVerify(blobId, headers, isPrivate, expectedAccountName, expectedContainerName, usermetadata);
  deleteBlobAndVerify(blobId);

  // check GET, HEAD, TTL update and DELETE after delete.
  verifyOperationsAfterDelete(blobId, headers, isPrivate, expectedAccountName, expectedContainerName, content,
      usermetadata);
  // Undelete it
  headers.add(RestUtils.Headers.LIFE_VERSION, "1");
  undeleteBlobAndVerify(blobId, headers, isPrivate, expectedAccountName, expectedContainerName, usermetadata);
}
 
Example 19
Source File: HttpToHttp2ConnectionHandlerTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Test
public void testRequestWithBody() throws Exception {
    final String text = "foooooogoooo";
    final List<String> receivedBuffers = Collections.synchronizedList(new ArrayList<String>());
    doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock in) throws Throwable {
            receivedBuffers.add(((ByteBuf) in.getArguments()[2]).toString(UTF_8));
            return null;
        }
    }).when(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3),
            any(ByteBuf.class), eq(0), eq(true));
    bootstrapEnv(3, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST,
            "http://[email protected]:5555/example",
            Unpooled.copiedBuffer(text, UTF_8));
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.set(HttpHeaderNames.HOST, "www.example-origin.org:5555");
    httpHeaders.add(of("foo"), of("goo"));
    httpHeaders.add(of("foo"), of("goo2"));
    httpHeaders.add(of("foo2"), of("goo2"));
    final Http2Headers http2Headers =
            new DefaultHttp2Headers().method(new AsciiString("POST")).path(new AsciiString("/example"))
            .authority(new AsciiString("www.example-origin.org:5555")).scheme(new AsciiString("http"))
            .add(new AsciiString("foo"), new AsciiString("goo"))
            .add(new AsciiString("foo"), new AsciiString("goo2"))
            .add(new AsciiString("foo2"), new AsciiString("goo2"));
    ChannelPromise writePromise = newPromise();
    ChannelFuture writeFuture = clientChannel.writeAndFlush(request, writePromise);

    assertTrue(writePromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writePromise.isSuccess());
    assertTrue(writeFuture.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writeFuture.isSuccess());
    awaitRequests();
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(http2Headers), eq(0),
            anyShort(), anyBoolean(), eq(0), eq(false));
    verify(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3), any(ByteBuf.class), eq(0),
            eq(true));
    assertEquals(1, receivedBuffers.size());
    assertEquals(text, receivedBuffers.get(0));
}
 
Example 20
Source File: ClientResponseWriter.java    From zuul with Apache License 2.0 4 votes vote down vote up
private HttpResponse buildHttpResponse(final HttpResponseMessage zuulResp) {
    final HttpRequestInfo zuulRequest = zuulResp.getInboundRequest();
    HttpVersion responseHttpVersion;
    final String inboundProtocol = zuulRequest.getProtocol();
    if (inboundProtocol.startsWith("HTTP/1")) {
        responseHttpVersion = HttpVersion.valueOf(inboundProtocol);
    }
    else {
        // Default to 1.1. We do this to cope with HTTP/2 inbound requests.
        responseHttpVersion = HttpVersion.HTTP_1_1;
    }

    // Create the main http response to send, with body.
    final DefaultHttpResponse nativeResponse = new DefaultHttpResponse(responseHttpVersion,
            HttpResponseStatus.valueOf(zuulResp.getStatus()), false, false);

    // Now set all of the response headers - note this is a multi-set in keeping with HTTP semantics
    final HttpHeaders nativeHeaders = nativeResponse.headers();
    for (Header entry : zuulResp.getHeaders().entries()) {
        nativeHeaders.add(entry.getKey(), entry.getValue());
    }

    // Netty does not automatically add Content-Length or Transfer-Encoding: chunked. So we add here if missing.
    if (! HttpUtil.isContentLengthSet(nativeResponse) && ! HttpUtil.isTransferEncodingChunked(nativeResponse)) {
        nativeResponse.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }

    final HttpRequest nativeReq = (HttpRequest) zuulResp.getContext().get(CommonContextKeys.NETTY_HTTP_REQUEST);
    if (!closeConnection && HttpUtil.isKeepAlive(nativeReq)) {
        HttpUtil.setKeepAlive(nativeResponse, true);
    } else {
        // Send a Connection: close response header (only needed for HTTP/1.0 but no harm in doing for 1.1 too).
        nativeResponse.headers().set("Connection", "close");
    }

    // TODO - temp hack for http/2 handling.
    if (nativeReq.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
        String streamId = nativeReq.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
        nativeResponse.headers().set(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
    }

    return nativeResponse;
}