com.linecorp.armeria.common.HttpRequest Java Examples

The following examples show how to use com.linecorp.armeria.common.HttpRequest. 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: FirebaseAuthorizer.java    From curiostack with MIT License 6 votes vote down vote up
@Override
public HttpResponse authFailed(
    HttpService delegate, ServiceRequestContext ctx, HttpRequest req, @Nullable Throwable cause)
    throws Exception {
  if (cause != null) {
    logger.warn("Unexpected exception during authorization.", cause);
    return HttpResponse.of(HttpStatus.UNAUTHORIZED);
  }
  if (!config.getIncludedPaths().isEmpty()) {
    if (config.getIncludedPaths().contains(ctx.path())) {
      return HttpResponse.of(HttpStatus.UNAUTHORIZED);
    } else {
      return delegate.serve(ctx, req);
    }
  }
  if (config.getExcludedPaths().contains(ctx.path())) {
    return delegate.serve(ctx, req);
  }
  return HttpResponse.of(HttpStatus.UNAUTHORIZED);
}
 
Example #2
Source File: HttpServerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ArgumentsSource(ClientAndProtocolProvider.class)
void testStreamRequestLongerThanTimeout(WebClient client) throws Exception {
    // Disable timeouts and length limits so that test does not fail due to slow transfer.
    clientWriteTimeoutMillis = 0;
    clientResponseTimeoutMillis = 0;
    clientMaxResponseLength = 0;
    serverRequestTimeoutMillis = 0;

    final HttpRequestWriter request = HttpRequest.streaming(HttpMethod.POST, "/echo");
    final HttpResponse response = client.execute(request);
    request.write(HttpData.ofUtf8("a"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("b"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("c"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("d"));
    Thread.sleep(2000);
    request.write(HttpData.ofUtf8("e"));
    request.close();
    assertThat(response.aggregate().get().contentUtf8()).isEqualTo("abcde");
}
 
Example #3
Source File: FileService.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    return HttpResponse.from(
            first.findFile(ctx, req)
                 .readAttributes(ctx.blockingTaskExecutor())
                 .thenApply(firstAttrs -> {
                     try {
                         if (firstAttrs != null) {
                             return first.serve(ctx, req);
                         }

                         return second.serve(ctx, req);
                     } catch (Exception e) {
                         return Exceptions.throwUnsafely(e);
                     }
                 }));
}
 
Example #4
Source File: AbstractHttpService.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Serves the specified {@link HttpRequest} by delegating it to the matching {@code 'doMETHOD()'} method.
 */
@Override
public final HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    switch (req.method()) {
        case OPTIONS:
            return doOptions(ctx, req);
        case GET:
            return doGet(ctx, req);
        case HEAD:
            return doHead(ctx, req);
        case POST:
            return doPost(ctx, req);
        case PUT:
            return doPut(ctx, req);
        case PATCH:
            return doPatch(ctx, req);
        case DELETE:
            return doDelete(ctx, req);
        case TRACE:
            return doTrace(ctx, req);
        default:
            return HttpResponse.of(HttpStatus.METHOD_NOT_ALLOWED);
    }
}
 
Example #5
Source File: DefaultClientRequestContextTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void setResponseTimeoutAfter() throws InterruptedException {
    final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/");
    final DefaultClientRequestContext ctx = (DefaultClientRequestContext) ClientRequestContext.of(req);
    final long tolerance = 500;

    ctx.eventLoop().execute(() -> {
        ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_NOW, 1000);
        final long oldResponseTimeoutMillis = ctx.responseTimeoutMillis();
        ctx.setResponseTimeoutMillis(TimeoutMode.SET_FROM_NOW, 2000);
        assertThat(ctx.responseTimeoutMillis()).isBetween(oldResponseTimeoutMillis + 1000 - tolerance,
                                                          oldResponseTimeoutMillis + 1000 + tolerance);
        finished.set(true);
    });

    await().untilTrue(finished);
}
 
Example #6
Source File: RetryingClientWithLoggingTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.service("/hello", new AbstractHttpService() {
        final AtomicInteger reqCount = new AtomicInteger();

        @Override
        protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) {
            ctx.mutateAdditionalResponseTrailers(
                    mutator -> mutator.add(HttpHeaderNames.of("foo"), "bar"));
            if (reqCount.getAndIncrement() < 1) {
                return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
            } else {
                return HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, "hello");
            }
        }
    });
}
 
Example #7
Source File: HttpServerStreamingTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@ArgumentsSource(ClientProvider.class)
void testTooLargeContent(WebClient client) throws Exception {
    final int maxContentLength = 65536;
    serverMaxRequestLength = maxContentLength;

    final HttpRequestWriter req = HttpRequest.streaming(HttpMethod.POST, "/count");
    final CompletableFuture<AggregatedHttpResponse> f = client.execute(req).aggregate();

    stream(req, maxContentLength + 1, 1024);

    final AggregatedHttpResponse res = f.get();

    assertThat(res.status()).isEqualTo(HttpStatus.REQUEST_ENTITY_TOO_LARGE);
    assertThat(res.contentType()).isEqualTo(MediaType.PLAIN_TEXT_UTF_8);
    assertThat(res.contentUtf8()).isEqualTo("413 Request Entity Too Large");
}
 
Example #8
Source File: RequestContextExportingAppenderTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static ClientRequestContext newClientContext(
        String path, @Nullable String query) throws Exception {

    final InetSocketAddress remoteAddress = new InetSocketAddress(
            InetAddress.getByAddress("server.com", new byte[] { 1, 2, 3, 4 }), 8080);
    final InetSocketAddress localAddress = new InetSocketAddress(
            InetAddress.getByAddress("client.com", new byte[] { 5, 6, 7, 8 }), 5678);

    final String pathAndQuery = path + (query != null ? '?' + query : "");
    final HttpRequest req = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, pathAndQuery,
                                                             HttpHeaderNames.AUTHORITY, "server.com:8080",
                                                             HttpHeaderNames.USER_AGENT, "some-client"));

    final ClientRequestContext ctx =
            ClientRequestContext.builder(req)
                                .remoteAddress(remoteAddress)
                                .localAddress(localAddress)
                                .endpoint(Endpoint.of("server.com", 8080))
                                .sslSession(newSslSession())
                                .build();

    ctx.setAttr(MY_ATTR, new CustomObject("some-name", "some-value"));
    return ctx;
}
 
Example #9
Source File: ServerRequestContextAdapterTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void parseClientIpAndPort() throws Exception {
    final ServiceRequestContext ctx =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/"))
                                 .remoteAddress(new InetSocketAddress("127.0.0.1", 1234))
                                 .build();

    final HttpServerRequest req =
            ServiceRequestContextAdapter.asHttpServerRequest(ctx);
    final Span span = mock(Span.class);
    when(span.remoteIpAndPort("127.0.0.1", 1234)).thenReturn(true);
    assertThat(req.parseClientIpAndPort(span)).isTrue();
    verify(span, times(1)).remoteIpAndPort("127.0.0.1", 1234);
    verifyNoMoreInteractions(span);
}
 
Example #10
Source File: MatchesHeaderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void multiple() {
    assertThat(client.execute(HttpRequest.of(RequestHeaders.of(
            HttpMethod.GET, "/multiple", "my-header", "my-value")))
                     .aggregate().join().contentUtf8()).isEqualTo("multiple");
    assertThat(client.execute(HttpRequest.of(RequestHeaders.of(
            HttpMethod.GET, "/multiple", "my-header", "my-value", "your-header", "your-value")))
                     .aggregate().join().contentUtf8()).isEqualTo("fallback");
}
 
Example #11
Source File: ConcurrencyLimitingClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the request pattern that exceeds maxConcurrency.
 */
@Test
void testLimitedRequest() throws Exception {
    final ClientRequestContext ctx1 = newContext();
    final ClientRequestContext ctx2 = newContext();
    final HttpRequest req1 = newReq();
    final HttpRequest req2 = newReq();
    final HttpResponseWriter actualRes1 = HttpResponse.streaming();
    final HttpResponseWriter actualRes2 = HttpResponse.streaming();

    when(delegate.execute(ctx1, req1)).thenReturn(actualRes1);
    when(delegate.execute(ctx2, req2)).thenReturn(actualRes2);

    final ConcurrencyLimitingClient client =
            newDecorator(1).apply(delegate);

    // The first request should be delegated immediately.
    final HttpResponse res1 = client.execute(ctx1, req1);
    verify(delegate).execute(ctx1, req1);
    assertThat(res1.isOpen()).isTrue();

    // The second request should never be delegated until the first response is closed.
    final HttpResponse res2 = client.execute(ctx2, req2);
    verify(delegate, never()).execute(ctx2, req2);
    assertThat(res2.isOpen()).isTrue();
    assertThat(client.numActiveRequests()).isEqualTo(1); // Only req1 is active.

    // Complete res1.
    closeAndDrain(actualRes1, res1);

    // Once res1 is complete, req2 should be delegated.
    await().untilAsserted(() -> verify(delegate).execute(ctx2, req2));
    assertThat(client.numActiveRequests()).isEqualTo(1); // Only req2 is active.

    // Complete res2, leaving no active requests.
    closeAndDrain(actualRes2, res2);
    await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
 
Example #12
Source File: ClientRequestContextAdapterTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void path() {
    final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/foo");
    final HttpClientRequest braveReq = ClientRequestContextAdapter.asHttpClientRequest(
            ClientRequestContext.of(req),
            req.headers().toBuilder());

    assertThat(braveReq.path()).isEqualTo("/foo");
}
 
Example #13
Source File: AbstractEndpointSelectorTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void delayedSelection() {
    final DynamicEndpointGroup group = new DynamicEndpointGroup();
    final ClientRequestContext ctx = ClientRequestContext.of(HttpRequest.of(HttpMethod.GET, "/"));
    final CompletableFuture<Endpoint> future = newSelector(group).select(ctx, ctx.eventLoop(),
                                                                         Long.MAX_VALUE);
    assertThat(future).isNotDone();

    final Endpoint endpoint = Endpoint.of("foo");
    group.addEndpoint(endpoint);
    assertThat(future.join()).isSameAs(endpoint);
}
 
Example #14
Source File: HttpClientDelegate.java    From armeria with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static String extractHost(ClientRequestContext ctx, HttpRequest req, Endpoint endpoint) {
    String host = extractHost(ctx.additionalRequestHeaders().get(HttpHeaderNames.AUTHORITY));
    if (host != null) {
        return host;
    }

    host = extractHost(req.authority());
    if (host != null) {
        return host;
    }

    return endpoint.host();
}
 
Example #15
Source File: AnnotatedServiceFactoryTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public Function<? super HttpService, ? extends HttpService> newDecorator(
        UserDefinedRepeatableDecorator parameter) {
    return service -> new SimpleDecoratingHttpService(service) {
        @Override
        public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
            return service.serve(ctx, req);
        }
    };
}
 
Example #16
Source File: ServerRequestContextAdapterTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void statusCode() {
    final HttpRequest req = HttpRequest.of(HttpMethod.GET, "/");
    final ServiceRequestContext ctx = ServiceRequestContext.of(req);
    ctx.logBuilder().endRequest();
    ctx.logBuilder().responseHeaders(ResponseHeaders.of(HttpStatus.OK));
    ctx.logBuilder().endResponse();

    final HttpServerResponse res =
            ServiceRequestContextAdapter.asHttpServerResponse(ctx.log().ensureComplete(), null);

    assertThat(res.statusCode()).isEqualTo(200);
}
 
Example #17
Source File: DefaultClientRequestContext.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a derived context.
 */
private DefaultClientRequestContext(DefaultClientRequestContext ctx,
                                    RequestId id,
                                    @Nullable HttpRequest req,
                                    @Nullable RpcRequest rpcReq,
                                    @Nullable Endpoint endpoint) {
    super(ctx.meterRegistry(), ctx.sessionProtocol(), id, ctx.method(), ctx.path(), ctx.query(),
          req, rpcReq, ctx.root());

    // The new requests cannot be null if it was previously non-null.
    if (ctx.request() != null) {
        requireNonNull(req, "req");
    }
    if (ctx.rpcRequest() != null) {
        requireNonNull(rpcReq, "rpcReq");
    }

    eventLoop = ctx.eventLoop();
    options = ctx.options();
    endpointGroup = ctx.endpointGroup();
    updateEndpoint(endpoint);
    fragment = ctx.fragment();
    root = ctx.root();

    log = RequestLog.builder(this);
    timeoutScheduler = new TimeoutScheduler(ctx.responseTimeoutMillis());

    writeTimeoutMillis = ctx.writeTimeoutMillis();
    maxResponseLength = ctx.maxResponseLength();
    additionalRequestHeaders = ctx.additionalRequestHeaders();

    for (final Iterator<Entry<AttributeKey<?>, Object>> i = ctx.ownAttrs(); i.hasNext();) {
        addAttr(i.next());
    }
}
 
Example #18
Source File: MeterIdPrefixFunctionTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ServiceRequestContext newContext(HttpMethod method, String path,
                                                @Nullable Object requestContent) {
    final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(method, path));
    ctx.logBuilder().requestContent(requestContent, null);
    ctx.logBuilder().endRequest();
    return ctx;
}
 
Example #19
Source File: MatchesParamTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void percentEncoded() {
    assertThat(client.execute(HttpRequest.of(HttpMethod.GET, "/matches/percentEncoded?my-param=/"))
                     .aggregate().join().contentUtf8()).isEqualTo("my-param=/");
    assertThat(client.execute(HttpRequest.of(HttpMethod.GET, "/matches/percentEncoded?my-param=%2F"))
                     .aggregate().join().contentUtf8()).isEqualTo("my-param=/");
    assertThat(client.execute(HttpRequest.of(HttpMethod.GET, "/matches/percentEncoded?my-param=%252F"))
                     .aggregate().join().contentUtf8()).isEqualTo("my-param=%2F");
}
 
Example #20
Source File: ArmeriaChannel.java    From armeria with Apache License 2.0 5 votes vote down vote up
private DefaultClientRequestContext newContext(HttpMethod method, HttpRequest req) {
    return new DefaultClientRequestContext(
            meterRegistry,
            sessionProtocol,
            options().requestIdGenerator().get(),
            method,
            req.path(),
            null,
            null,
            options(),
            req,
            null,
            System.nanoTime(),
            SystemInfo.currentTimeMicros());
}
 
Example #21
Source File: AnnotatedServiceExceptionHandlerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public HttpResponse handleException(ServiceRequestContext ctx, HttpRequest req, Throwable cause) {
    final HttpResponseWriter response = HttpResponse.streaming();
    response.write(ResponseHeaders.of(HttpStatus.OK));
    // Timeout may occur before responding.
    ctx.eventLoop().schedule((Runnable) response::close, 10, TimeUnit.SECONDS);
    return response;
}
 
Example #22
Source File: HttpClientDelegate.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void handleEarlyRequestException(ClientRequestContext ctx,
                                                HttpRequest req, Throwable cause) {
    try (SafeCloseable ignored = RequestContextUtil.pop()) {
        req.abort(cause);
        final RequestLogBuilder logBuilder = ctx.logBuilder();
        logBuilder.endRequest(cause);
        logBuilder.endResponse(cause);
    }
}
 
Example #23
Source File: ArmeriaClientHttpRequest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private Supplier<Mono<Void>> execute(Supplier<HttpRequest> supplier) {
    return () -> Mono.defer(() -> {
        assert request == null : request;
        request = supplier.get();
        future.complete(client.execute(request));
        return Mono.fromFuture(request.whenComplete());
    });
}
 
Example #24
Source File: AuthServiceBuilder.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Adds an {@link Authorizer}.
 */
public AuthServiceBuilder add(Authorizer<HttpRequest> authorizer) {
    requireNonNull(authorizer, "authorizer");
    if (this.authorizer == null) {
        this.authorizer = authorizer;
    } else {
        this.authorizer = this.authorizer.orElse(authorizer);
    }
    return this;
}
 
Example #25
Source File: TokenBucketThrottlingStrategyTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    final TokenBucket tokenBucket = TokenBucket.builder()
                                               .limit(1L, Duration.ofSeconds(10L))
                                               .build();
    sb.service("/http-serve",
               SERVICE.decorate(
                       ThrottlingService.newDecorator(
                               TokenBucketThrottlingStrategy.<HttpRequest>builder(tokenBucket)
                                       .build())));
    sb.service("/http-throttle1",
               SERVICE.decorate(
                       ThrottlingService.newDecorator(
                               TokenBucketThrottlingStrategy.<HttpRequest>builder(tokenBucket)
                                       .withHeadersScheme(ThrottlingHeaders.X_RATELIMIT)
                                       .build())));
    sb.service("/http-throttle2",
               SERVICE.decorate(
                       ThrottlingService.newDecorator(
                               TokenBucketThrottlingStrategy.<HttpRequest>builder(tokenBucket)
                                       .withMinimumBackoff(Duration.ofSeconds(15L))
                                       .withHeadersScheme(ThrottlingHeaders.X_RATELIMIT, true)
                                       .build())));
    sb.service("/http-throttle3",
               SERVICE.decorate(
                       ThrottlingService.newDecorator(
                               TokenBucketThrottlingStrategy.<HttpRequest>builder(tokenBucket)
                                       .build())));
    sb.service("/http-throttle4",
               SERVICE.decorate(
                       ThrottlingService.newDecorator(
                               TokenBucketThrottlingStrategy.<HttpRequest>builder(tokenBucket)
                                       .withMinimumBackoff(Duration.ofSeconds(5L))
                                       .build(),
                               (delegate, ctx, req, cause) ->
                                       HttpResponse.of(HttpStatus.SERVICE_UNAVAILABLE))));
}
 
Example #26
Source File: AWSSignatureVersion4.java    From zipkin-aws with Apache License 2.0 5 votes vote down vote up
@Override public HttpResponse execute(ClientRequestContext ctx, HttpRequest req) {
  // We aggregate the request body with pooled objects because signing implies reading it before
  // sending it to Elasticsearch.
  return HttpResponse.from(
      req.aggregateWithPooledObjects(ctx.contextAwareEventLoop(), ctx.alloc())
          .thenApply(aggReg -> {
            try {
              AggregatedHttpRequest signed = sign(ctx, aggReg);
              return delegate().execute(ctx, signed.toHttpRequest());
            } catch (Exception e) {
              return HttpResponse.ofFailure(e);
            }
          }));
}
 
Example #27
Source File: StaticSiteService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
  if (ctx.mappedPath().indexOf('.', ctx.mappedPath().lastIndexOf('/') + 1) != -1
      || ctx.mappedPath().charAt(ctx.mappedPath().length() - 1) == '/') {
    // A path that ends with '/' will be handled by HttpFileService correctly, and otherwise if
    // it has a '.' in the last path segment, assume it is a filename.
    return delegate().serve(ctx, req);
  }
  return delegate().serve(new ContextWrapper(ctx), req);
}
 
Example #28
Source File: AccessLogFormatsTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void requestLogWithEmptyCause() {
    final ServiceRequestContext ctx = ServiceRequestContext.of(HttpRequest.of(HttpMethod.GET, "/"));

    final RequestLogBuilder logBuilder = ctx.logBuilder();

    final List<AccessLogComponent> format =
            AccessLogFormats.parseCustom("%{requestCause}L %{responseCause}L");

    logBuilder.endRequest();
    logBuilder.endResponse();

    assertThat(AccessLogger.format(format, ctx.log().ensureComplete())).isEqualTo("- -");
}
 
Example #29
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void responseAbortWithException() throws InterruptedException {
    final WebClient client = WebClient.of(server.httpUri());
    final HttpRequest request = HttpRequest.streaming(HttpMethod.GET, "/client-aborted");
    final HttpResponse response = client.execute(request);

    await().until(() -> completed);
    final IllegalStateException badState = new IllegalStateException("bad state");
    response.abort(badState);
    assertThatThrownBy(() -> response.aggregate().join())
            .isInstanceOf(CompletionException.class)
            .hasCause(badState);
}
 
Example #30
Source File: CircuitBreakerClientTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static void invoke(
        Function<? super HttpClient, CircuitBreakerClient> decorator,
        HttpRequest req) throws Exception {
    final HttpClient client = mock(HttpClient.class);
    final HttpClient decorated = decorator.apply(client);

    decorated.execute(ctx, req);
}