Search in sources :

Example 26 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class ProxyUnauthorized407Interceptor method exitAfterHandling407.

public boolean exitAfterHandling407(Channel channel, NettyResponseFuture<?> future, HttpResponse response, Request request, ProxyServer proxyServer, HttpRequest httpRequest) {
    if (future.isAndSetInProxyAuth(true)) {
        LOGGER.info("Can't handle 407 as auth was already performed");
        return false;
    }
    Realm proxyRealm = future.getProxyRealm();
    if (proxyRealm == null) {
        LOGGER.debug("Can't handle 407 as there's no proxyRealm");
        return false;
    }
    List<String> proxyAuthHeaders = response.headers().getAll(PROXY_AUTHENTICATE);
    if (proxyAuthHeaders.isEmpty()) {
        LOGGER.info("Can't handle 407 as response doesn't contain Proxy-Authenticate headers");
        return false;
    }
    // FIXME what's this???
    future.setChannelState(ChannelState.NEW);
    HttpHeaders requestHeaders = new DefaultHttpHeaders().add(request.getHeaders());
    switch(proxyRealm.getScheme()) {
        case BASIC:
            if (getHeaderWithPrefix(proxyAuthHeaders, "Basic") == null) {
                LOGGER.info("Can't handle 407 with Basic realm as Proxy-Authenticate headers don't match");
                return false;
            }
            if (proxyRealm.isUsePreemptiveAuth()) {
                // FIXME do we need this, as future.getAndSetAuth
                // was tested above?
                // auth was already performed, most likely auth
                // failed
                LOGGER.info("Can't handle 407 with Basic realm as auth was preemptive and already performed");
                return false;
            }
            // FIXME do we want to update the realm, or directly
            // set the header?
            Realm newBasicRealm = realm(proxyRealm).setUsePreemptiveAuth(true).build();
            future.setProxyRealm(newBasicRealm);
            break;
        case DIGEST:
            String digestHeader = getHeaderWithPrefix(proxyAuthHeaders, "Digest");
            if (digestHeader == null) {
                LOGGER.info("Can't handle 407 with Digest realm as Proxy-Authenticate headers don't match");
                return false;
            }
            Realm newDigestRealm = realm(proxyRealm).setUri(request.getUri()).setMethodName(request.getMethod()).setUsePreemptiveAuth(true).parseProxyAuthenticateHeader(digestHeader).build();
            future.setProxyRealm(newDigestRealm);
            break;
        case NTLM:
            String ntlmHeader = getHeaderWithPrefix(proxyAuthHeaders, "NTLM");
            if (ntlmHeader == null) {
                LOGGER.info("Can't handle 407 with NTLM realm as Proxy-Authenticate headers don't match");
                return false;
            }
            ntlmProxyChallenge(ntlmHeader, requestHeaders, proxyRealm, future);
            Realm newNtlmRealm = realm(proxyRealm).setUsePreemptiveAuth(true).build();
            future.setProxyRealm(newNtlmRealm);
            break;
        case KERBEROS:
        case SPNEGO:
            if (getHeaderWithPrefix(proxyAuthHeaders, NEGOTIATE) == null) {
                LOGGER.info("Can't handle 407 with Kerberos or Spnego realm as Proxy-Authenticate headers don't match");
                return false;
            }
            try {
                kerberosProxyChallenge(proxyRealm, proxyServer, requestHeaders);
            } catch (SpnegoEngineException e) {
                // FIXME
                String ntlmHeader2 = getHeaderWithPrefix(proxyAuthHeaders, "NTLM");
                if (ntlmHeader2 != null) {
                    LOGGER.warn("Kerberos/Spnego proxy auth failed, proceeding with NTLM");
                    ntlmProxyChallenge(ntlmHeader2, requestHeaders, proxyRealm, future);
                    Realm newNtlmRealm2 = realm(proxyRealm).setScheme(AuthScheme.NTLM).setUsePreemptiveAuth(true).build();
                    future.setProxyRealm(newNtlmRealm2);
                } else {
                    requestSender.abort(channel, future, e);
                    return false;
                }
            }
            break;
        default:
            throw new IllegalStateException("Invalid Authentication scheme " + proxyRealm.getScheme());
    }
    RequestBuilder nextRequestBuilder = future.getCurrentRequest().toBuilder().setHeaders(requestHeaders);
    if (future.getCurrentRequest().getUri().isSecured()) {
        nextRequestBuilder.setMethod(CONNECT);
    }
    final Request nextRequest = nextRequestBuilder.build();
    LOGGER.debug("Sending proxy authentication to {}", request.getUri());
    if (future.isKeepAlive() && !HttpUtil.isTransferEncodingChunked(httpRequest) && !HttpUtil.isTransferEncodingChunked(response)) {
        future.setConnectAllowed(true);
        future.setReuseChannel(true);
        requestSender.drainChannelAndExecuteNextRequest(channel, future, nextRequest);
    } else {
        channelManager.closeChannel(channel);
        requestSender.sendNextRequest(nextRequest, future);
    }
    return true;
}
Also used : RequestBuilder(org.asynchttpclient.RequestBuilder) SpnegoEngineException(org.asynchttpclient.spnego.SpnegoEngineException) Request(org.asynchttpclient.Request) Realm(org.asynchttpclient.Realm)

Example 27 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class MultipartUploadTest method sendFileInputStream.

private void sendFileInputStream(boolean useContentLength, boolean disableZeroCopy) throws Exception {
    File file = getClasspathFile("textfile.txt");
    try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        InputStreamPart part;
        if (useContentLength) {
            part = new InputStreamPart("file", inputStream, file.getName(), file.length());
        } else {
            part = new InputStreamPart("file", inputStream, file.getName());
        }
        Request r = post("http://localhost" + ":" + port1 + "/upload").addBodyPart(part).build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) GZIPInputStream(java.util.zip.GZIPInputStream) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestUtils.getClasspathFile(org.asynchttpclient.test.TestUtils.getClasspathFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 28 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class MultipartUploadTest method sendEmptyFile0.

private void sendEmptyFile0(boolean disableZeroCopy) throws Exception {
    File file = getClasspathFile("empty.txt");
    try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) {
        Request r = post("http://localhost" + ":" + port1 + "/upload").addBodyPart(new FilePart("file", file, "text/plain", UTF_8)).build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestUtils.getClasspathFile(org.asynchttpclient.test.TestUtils.getClasspathFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 29 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class MultipartUploadTest method sendEmptyFileInputStream.

private void sendEmptyFileInputStream(boolean disableZeroCopy) throws Exception {
    File file = getClasspathFile("empty.txt");
    try (AsyncHttpClient c = asyncHttpClient(config().setDisableZeroCopy(disableZeroCopy))) {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        Request r = post("http://localhost" + ":" + port1 + "/upload").addBodyPart(new InputStreamPart("file", inputStream, file.getName(), file.length(), "text/plain", UTF_8)).build();
        Response res = c.executeRequest(r).get();
        assertEquals(res.getStatusCode(), 200);
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) GZIPInputStream(java.util.zip.GZIPInputStream) Request(org.asynchttpclient.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestUtils.getClasspathFile(org.asynchttpclient.test.TestUtils.getClasspathFile) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 30 with Request

use of org.asynchttpclient.Request in project async-http-client by AsyncHttpClient.

the class HttpUtilsTest method testGetHostHeaderNoVirtualHost.

@Test
public void testGetHostHeaderNoVirtualHost() {
    Request request = Dsl.get("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs").build();
    Uri uri = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs");
    String hostHeader = HttpUtils.hostHeader(request, uri);
    assertEquals(hostHeader, "stackoverflow.com", "Incorrect hostHeader returned");
}
Also used : Request(org.asynchttpclient.Request) Uri(org.asynchttpclient.uri.Uri) Test(org.testng.annotations.Test)

Aggregations

Request (org.asynchttpclient.Request)67 Test (org.testng.annotations.Test)32 Response (org.asynchttpclient.Response)31 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)18 RequestBuilder (org.asynchttpclient.RequestBuilder)17 Test (org.junit.Test)16 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 HttpRequest (io.netty.handler.codec.http.HttpRequest)8 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)8 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 DefaultAsyncHttpClientConfig (org.asynchttpclient.DefaultAsyncHttpClientConfig)6 ListenableFuture (org.asynchttpclient.ListenableFuture)6 Uri (org.asynchttpclient.uri.Uri)5 Realm (org.asynchttpclient.Realm)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 HttpResponse (io.netty.handler.codec.http.HttpResponse)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3