@Override public String get(AsciiString name) { return delegate.get(name); }
@Override public String get(AsciiString name, String defaultValue) { return delegate.get(name, defaultValue); }
@Override public Object getMessage0(RequestLog log) { return httpHeaders.apply(log).get(headerName); } }
void setCorsAllowCredentials(HttpHeaders headers) { // The string "*" cannot be used for a resource that supports credentials. // https://www.w3.org/TR/cors/#resource-requests if (credentialsAllowed && !ANY_ORIGIN.equals(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN))) { headers.set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); } }
private static Decompressor clientDecompressor(HttpHeaders headers, DecompressorRegistry registry) { final String encoding = headers.get(GrpcHeaderNames.GRPC_ENCODING); if (encoding == null) { return Identity.NONE; } final Decompressor decompressor = registry.lookupDecompressor(encoding); return firstNonNull(decompressor, Identity.NONE); }
private static void exportHttpHeaders(Map<String, String> out, HttpHeaders headers, ExportEntry<AsciiString>[] requiredHeaderNames) { for (ExportEntry<AsciiString> e : requiredHeaderNames) { final String value = headers.get(e.key); final String mdcKey = e.mdcKey; if (value != null) { out.put(mdcKey, e.stringify(value)); } } }
@Nullable @Override public OAuth2Token apply(HttpHeaders headers) { final String authorization = headers.get(header); if (Strings.isNullOrEmpty(authorization)) { return null; } final Matcher matcher = AUTHORIZATION_HEADER_PATTERN.matcher(authorization); if (!matcher.matches()) { logger.warn("Invalid authorization header: " + authorization); return null; } return OAuth2Token.of(matcher.group("accessToken")); } }
@Nullable static HttpEncodingType getWrapperForRequest(HttpRequest request) { final String acceptEncoding = request.headers().get(HttpHeaderNames.ACCEPT_ENCODING); if (acceptEncoding == null) { return null; } return determineEncoding(acceptEncoding); }
/** * Checks whether the request has right permission to access the service. In this example, a {@code Cookie} * header is used to hold the information. If a {@code username} key exists in the {@code Cookie} header, * the request is treated as authenticated. */ @Override public CompletionStage<Boolean> authorize(ServiceRequestContext ctx, HttpRequest data) { final String cookie = data.headers().get(HttpHeaderNames.COOKIE); if (cookie == null) { return CompletableFuture.completedFuture(false); } final boolean authenticated = ServerCookieDecoder.LAX.decode(cookie).stream().anyMatch( c -> "username".equals(c.name()) && !Strings.isNullOrEmpty(c.value())); return CompletableFuture.completedFuture(authenticated); }
private static void echoCorsRequestOrigin(HttpRequest request, HttpHeaders headers) { setCorsOrigin(headers, request.headers().get(HttpHeaderNames.ORIGIN)); }
@Override public CompletionStage<Boolean> authorize(ServiceRequestContext ctx, HttpRequest req) { final String value = req.headers().get(HttpHeaderNames.COOKIE); if (value == null) { return CompletableFuture.completedFuture(false); } // Authentication will be succeeded only if both the specified cookie name and value are matched. final Set<Cookie> cookies = ServerCookieDecoder.STRICT.decode(value); final boolean result = cookies.stream().anyMatch( cookie -> cookieName.equals(cookie.name()) && cookieValue.equals(cookie.value())); return CompletableFuture.completedFuture(result); } }
@Test public void shouldRespondAuthnRequest_HttpRedirect() throws Exception { final AggregatedHttpMessage resp = client.get("/redirect").aggregate().join(); assertThat(resp.status()).isEqualTo(HttpStatus.FOUND); // Check the order of the parameters in the quest string. final String location = resp.headers().get(HttpHeaderNames.LOCATION); final Pattern p = Pattern.compile( "http://idp\\.example\\.com/saml/sso/redirect\\?" + "SAMLRequest=([^&]+)&RelayState=([^&]+)&SigAlg=([^&]+)&Signature=(.+)$"); assertThat(p.matcher(location).matches()).isTrue(); final QueryStringDecoder decoder = new QueryStringDecoder(location, true); assertThat(decoder.parameters().get(SIGNATURE_ALGORITHM).get(0)).isEqualTo(signatureAlgorithm); }
@Test public void sendsTimeoutHeader() { final long configuredTimeoutMinutes = 100; final TestServiceBlockingStub stub = Clients.newDerivedClient( blockingStub, ClientOption.DEFAULT_RESPONSE_TIMEOUT_MILLIS.newValue( TimeUnit.MINUTES.toMillis(configuredTimeoutMinutes))); stub.emptyCall(EMPTY); final long transferredTimeoutMinutes = TimeUnit.NANOSECONDS.toMinutes( TimeoutHeaderUtil.fromHeaderValue( CLIENT_HEADERS_CAPTURE.get().get(GrpcHeaderNames.GRPC_TIMEOUT))); assertThat(transferredTimeoutMinutes).isEqualTo(configuredTimeoutMinutes); }
@Test public void shouldConsumeLogoutRequest_HttpRedirect() throws Exception { final LogoutRequest logoutRequest = getLogoutRequest("http://" + spHostname + ':' + rule.httpPort() + "/saml/slo/redirect", "http://idp.example.com/redirect"); final AggregatedHttpMessage msg = sendViaHttpRedirectBindingProtocol("/saml/slo/redirect", SAML_REQUEST, logoutRequest); assertThat(msg.status()).isEqualTo(HttpStatus.FOUND); // Check the order of the parameters in the quest string. final String location = msg.headers().get(HttpHeaderNames.LOCATION); final Pattern p = Pattern.compile( "http://idp\\.example\\.com/saml/slo/redirect\\?" + "SAMLResponse=([^&]+)&SigAlg=([^&]+)&Signature=(.+)$"); assertThat(p.matcher(location).matches()).isTrue(); }
@Test public void shouldConsumeAssertion_HttpPost() throws Exception { final Response response = getAuthResponse("http://" + spHostname + ':' + rule.httpPort() + "/saml/acs/post"); final AggregatedHttpMessage msg = sendViaHttpPostBindingProtocol("/saml/acs/post", SAML_RESPONSE, response); assertThat(msg.status()).isEqualTo(HttpStatus.FOUND); assertThat(msg.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/"); }
@Test public void shouldConsumeAssertion_HttpRedirect() throws Exception { final Response response = getAuthResponse("http://" + spHostname + ':' + rule.httpPort() + "/saml/acs/redirect"); final AggregatedHttpMessage msg = sendViaHttpRedirectBindingProtocol("/saml/acs/redirect", SAML_RESPONSE, response); assertThat(msg.status()).isEqualTo(HttpStatus.FOUND); assertThat(msg.headers().get(HttpHeaderNames.LOCATION)).isEqualTo("/"); }
/** Sends an rpc to an unimplemented method within TestService. */ @Test public void unimplementedMethod() throws Exception { final Throwable t = catchThrowable(() -> blockingStub.unimplementedCall(Empty.getDefaultInstance())); assertThat(t).isInstanceOf(StatusRuntimeException.class); assertThat(((StatusRuntimeException) t).getStatus().getCode()) .isEqualTo(Status.UNIMPLEMENTED.getCode()); checkRequestLogError((headers, rpcReq, cause) -> { assertThat(rpcReq).isNotNull(); assertThat(rpcReq.params()).containsExactly(Empty.getDefaultInstance()); assertThat(headers.get(GrpcHeaderNames.GRPC_STATUS)).isEqualTo( String.valueOf(Status.UNIMPLEMENTED.getCode().value())); }); }
@Test public void shouldReturnCompressedResponse() { final ArmeriaReactiveWebServerFactory factory = factory(); final Compression compression = new Compression(); compression.setEnabled(true); compression.setMinResponseSize(DataSize.ofBytes(1)); compression.setMimeTypes(new String[] { "text/plain" }); compression.setExcludedUserAgents(new String[] { "unknown-agent/[0-9]+\\.[0-9]+\\.[0-9]+$" }); factory.setCompression(compression); runEchoServer(factory, server -> { final AggregatedHttpMessage res = sendPostRequest(httpClient(server)); assertThat(res.status()).isEqualTo(com.linecorp.armeria.common.HttpStatus.OK); assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isEqualTo("gzip"); assertThat(res.content().toStringUtf8()).isNotEqualTo("hello"); }); }
@Test public void shouldReturnNonCompressedResponse_dueToContentType() { final ArmeriaReactiveWebServerFactory factory = factory(); final Compression compression = new Compression(); compression.setEnabled(true); compression.setMinResponseSize(DataSize.ofBytes(1)); compression.setMimeTypes(new String[] { "text/html" }); factory.setCompression(compression); runEchoServer(factory, server -> { final AggregatedHttpMessage res = sendPostRequest(httpClient(server)); validateEchoResponse(res); assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isNull(); }); }
@Test public void shouldReturnNonCompressedResponse_dueToUserAgent() { final ArmeriaReactiveWebServerFactory factory = factory(); final Compression compression = new Compression(); compression.setEnabled(true); compression.setMinResponseSize(DataSize.ofBytes(1)); compression.setExcludedUserAgents(new String[] { "test-agent/[0-9]+\\.[0-9]+\\.[0-9]+$" }); factory.setCompression(compression); runEchoServer(factory, server -> { final AggregatedHttpMessage res = sendPostRequest(httpClient(server)); validateEchoResponse(res); assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING)).isNull(); }); }