Search in sources :

Example 16 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestBasicMessages method testRequestWithNoPath.

@Test
public void testRequestWithNoPath() throws Exception {
    final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("http://host"));
    Assertions.assertEquals(Method.GET.name(), request.getMethod());
    Assertions.assertEquals("/", request.getPath());
    Assertions.assertEquals(new URIAuthority("host"), request.getAuthority());
    Assertions.assertEquals("http", request.getScheme());
    Assertions.assertEquals(new URI("http://host/"), request.getUri());
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) URIAuthority(org.apache.hc.core5.net.URIAuthority) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 17 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestStandardInterceptors method testRequestTargetHostNotGenerated.

@Test
public void testRequestTargetHostNotGenerated() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.GET, "/");
    request.setAuthority(new URIAuthority("somehost", 8080));
    request.addHeader(new BasicHeader(HttpHeaders.HOST, "whatever"));
    final HttpRequestInterceptor interceptor = RequestTargetHost.INSTANCE;
    interceptor.process(request, request.getEntity(), context);
    final Header header = request.getFirstHeader(HttpHeaders.HOST);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("whatever", header.getValue());
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 18 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestStandardInterceptors method testRequestTargetHostConnectHttp11.

@Test
public void testRequestTargetHostConnectHttp11() throws Exception {
    final HttpContext context = new BasicHttpContext(null);
    final BasicClassicHttpRequest request = new BasicClassicHttpRequest(Method.CONNECT, "/");
    request.setAuthority(new URIAuthority("somehost", 8080));
    final HttpRequestInterceptor interceptor = RequestTargetHost.INSTANCE;
    interceptor.process(request, request.getEntity(), context);
    final Header header = request.getFirstHeader(HttpHeaders.HOST);
    Assertions.assertNotNull(header);
    Assertions.assertEquals("somehost:8080", header.getValue());
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HttpRequestInterceptor(org.apache.hc.core5.http.HttpRequestInterceptor) Test(org.junit.jupiter.api.Test)

Example 19 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class TestBasicMessageBuilders method testRequestBasics.

@Test
public void testRequestBasics() throws Exception {
    final BasicRequestBuilder builder = BasicRequestBuilder.get();
    Assertions.assertEquals(URI.create("/"), builder.getUri());
    Assertions.assertEquals("GET", builder.getMethod());
    Assertions.assertNull(builder.getScheme());
    Assertions.assertNull(builder.getAuthority());
    Assertions.assertNull(builder.getPath());
    Assertions.assertNull(builder.getHeaders());
    Assertions.assertNull(builder.getVersion());
    Assertions.assertNull(builder.getCharset());
    Assertions.assertNull(builder.getParameters());
    final BasicHttpRequest r1 = builder.build();
    Assertions.assertNotNull(r1);
    Assertions.assertEquals("GET", r1.getMethod());
    Assertions.assertNull(r1.getScheme());
    Assertions.assertNull(r1.getAuthority());
    Assertions.assertNull(r1.getPath());
    Assertions.assertEquals(URI.create("/"), r1.getUri());
    Assertions.assertNull(r1.getVersion());
    builder.setUri(URI.create("http://host:1234/blah?param=value"));
    builder.setVersion(HttpVersion.HTTP_1_1);
    Assertions.assertEquals("http", builder.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), builder.getAuthority());
    Assertions.assertEquals("/blah?param=value", builder.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), builder.getUri());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, builder.getVersion());
    final BasicHttpRequest r2 = builder.build();
    Assertions.assertEquals("GET", r2.getMethod());
    Assertions.assertEquals("http", r2.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), r2.getAuthority());
    Assertions.assertEquals("/blah?param=value", r2.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), r2.getUri());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, builder.getVersion());
    builder.setCharset(StandardCharsets.US_ASCII);
    builder.addParameter("param1", "value1");
    builder.addParameter("param2", null);
    builder.addParameters(new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null));
    Assertions.assertEquals(builder.getParameters(), Arrays.asList(new BasicNameValuePair("param1", "value1"), new BasicNameValuePair("param2", null), new BasicNameValuePair("param3", "value3"), new BasicNameValuePair("param4", null)));
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value"), builder.getUri());
    final BasicHttpRequest r3 = builder.build();
    Assertions.assertEquals("GET", r3.getMethod());
    Assertions.assertEquals("http", r3.getScheme());
    Assertions.assertEquals(new URIAuthority("host", 1234), r3.getAuthority());
    Assertions.assertEquals("/blah?param=value&param1=value1&param2&param3=value3&param4", r3.getPath());
    Assertions.assertEquals(URI.create("http://host:1234/blah?param=value&param1=value1&param2&param3=value3&param4"), r3.getUri());
    builder.addHeader("h1", "v1");
    builder.addHeader("h1", "v2");
    builder.addHeader("h2", "v2");
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(builder.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(builder.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    final BasicHttpRequest r4 = builder.build();
    assertThat(r4.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2"), new BasicHeader("h2", "v2")));
    assertThat(r4.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h1", "v2")));
    assertThat(r4.getFirstHeader("h1"), HeaderMatcher.same("h1", "v1"));
    assertThat(r4.getLastHeader("h1"), HeaderMatcher.same("h1", "v2"));
    builder.removeHeader(new BasicHeader("h1", "v2"));
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    final BasicHttpRequest r5 = builder.build();
    assertThat(r5.getHeaders("h1"), HeadersMatcher.same(new BasicHeader("h1", "v1")));
    assertThat(r5.getHeaders(), HeadersMatcher.same(new BasicHeader("h1", "v1"), new BasicHeader("h2", "v2")));
    builder.removeHeaders("h1");
    assertThat(builder.getHeaders("h1"), HeadersMatcher.same());
    assertThat(builder.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
    final BasicHttpRequest r6 = builder.build();
    assertThat(r6.getHeaders("h1"), HeadersMatcher.same());
    assertThat(r6.getHeaders(), HeadersMatcher.same(new BasicHeader("h2", "v2")));
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 20 with URIAuthority

use of org.apache.hc.core5.net.URIAuthority in project httpcomponents-core by apache.

the class AbstractAsyncServerAuthFilter method handle.

@Override
public final AsyncDataConsumer handle(final HttpRequest request, final EntityDetails entityDetails, final HttpContext context, final AsyncFilterChain.ResponseTrigger responseTrigger, final AsyncFilterChain chain) throws HttpException, IOException {
    final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
    final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
    final URIAuthority authority = request.getAuthority();
    final String requestUri = request.getRequestUri();
    final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
    final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
    final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
    if (authenticated) {
        if (expectContinue) {
            responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
        }
        return chain.proceed(request, entityDetails, context, responseTrigger);
    }
    final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
    unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
    final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
    if (respondImmediately || expectContinue || entityDetails == null) {
        responseTrigger.submitResponse(unauthorized, responseContentProducer);
        return null;
    }
    return new AsyncDataConsumer() {

        @Override
        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
            capacityChannel.update(Integer.MAX_VALUE);
        }

        @Override
        public void consume(final ByteBuffer src) throws IOException {
        }

        @Override
        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
            responseTrigger.submitResponse(unauthorized, responseContentProducer);
        }

        @Override
        public void releaseResources() {
            if (responseContentProducer != null) {
                responseContentProducer.releaseResources();
            }
        }
    };
}
Also used : URIAuthority(org.apache.hc.core5.net.URIAuthority) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) ByteBuffer(java.nio.ByteBuffer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) Header(org.apache.hc.core5.http.Header) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) AsyncDataConsumer(org.apache.hc.core5.http.nio.AsyncDataConsumer) List(java.util.List) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Aggregations

URIAuthority (org.apache.hc.core5.net.URIAuthority)63 Test (org.junit.jupiter.api.Test)32 HttpRequest (org.apache.hc.core5.http.HttpRequest)21 HttpHost (org.apache.hc.core5.http.HttpHost)17 Header (org.apache.hc.core5.http.Header)15 URI (java.net.URI)14 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)11 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)10 HttpException (org.apache.hc.core5.http.HttpException)9 IOException (java.io.IOException)8 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)8 ProtocolException (org.apache.hc.core5.http.ProtocolException)8 HttpResponse (org.apache.hc.core5.http.HttpResponse)7 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)7 HttpEntity (org.apache.hc.core5.http.HttpEntity)6 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)6 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)6 HttpRoute (org.apache.hc.client5.http.HttpRoute)5 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)5 BasicClassicHttpRequest (org.apache.hc.core5.http.message.BasicClassicHttpRequest)5