Search in sources :

Example 16 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class InputStreamTest method testInvalidInputStream.

@Test(groups = "standalone")
public void testInvalidInputStream() throws IOException, ExecutionException, TimeoutException, InterruptedException {
    try (AsyncHttpClient c = asyncHttpClient()) {
        HttpHeaders h = new DefaultHttpHeaders().add(CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
        InputStream is = new InputStream() {

            public int readAllowed;

            @Override
            public int available() {
                // Fake
                return 1;
            }

            @Override
            public int read() throws IOException {
                int fakeCount = readAllowed++;
                if (fakeCount == 0) {
                    return (int) 'a';
                } else if (fakeCount == 1) {
                    return (int) 'b';
                } else if (fakeCount == 2) {
                    return (int) 'c';
                } else {
                    return -1;
                }
            }
        };
        Response resp = c.preparePost(getTargetUrl()).setHeaders(h).setBody(is).execute().get();
        assertNotNull(resp);
        assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(resp.getHeader("X-Param"), "abc");
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.asynchttpclient.Response) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) InputStream(java.io.InputStream) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 17 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class MultipartPartTest method transferToShouldWriteStringPart.

@Test
public void transferToShouldWriteStringPart() throws IOException, URISyntaxException {
    String text = FileUtils.readFileToString(TestUtils.resourceAsFile("test_sample_message.eml"));
    List<Part> parts = new ArrayList<>();
    parts.add(new StringPart("test_sample_message.eml", text));
    HttpHeaders headers = new DefaultHttpHeaders();
    headers.set("Cookie", "open-xchange-public-session-d41d8cd98f00b204e9800998ecf8427e=bfb98150b24f42bd844fc9ef2a9eaad5; open-xchange-secret-TSlq4Cm4nCBnDpBL1Px2A=9a49b76083e34c5ba2ef5c47362313fd; JSESSIONID=6883138728830405130.OX2");
    headers.set("Content-Length", "9241");
    headers.set("Content-Type", "multipart/form-data; boundary=5gigAKQyqDCVdlZ1fCkeLlEDDauTNoOOEhRnFg");
    headers.set("Host", "appsuite.qa.open-xchange.com");
    headers.set("Accept", "*/*");
    String boundary = "uwyqQolZaSmme019O2kFKvAeHoC14Npp";
    List<MultipartPart<? extends Part>> multipartParts = MultipartUtils.generateMultipartParts(parts, boundary.getBytes());
    MultipartBody multipartBody = new MultipartBody(multipartParts, "multipart/form-data; boundary=" + boundary, boundary.getBytes());
    ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(8 * 1024);
    try {
        multipartBody.transferTo(byteBuf);
        byteBuf.toString(StandardCharsets.UTF_8);
    } finally {
        multipartBody.close();
        byteBuf.release();
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) FileLikePart(org.asynchttpclient.request.body.multipart.FileLikePart) Part(org.asynchttpclient.request.body.multipart.Part) StringPart(org.asynchttpclient.request.body.multipart.StringPart) MultipartBody(org.asynchttpclient.request.body.multipart.MultipartBody) ArrayList(java.util.ArrayList) StringPart(org.asynchttpclient.request.body.multipart.StringPart) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 18 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method configureTransferAdapter.

private void configureTransferAdapter(AsyncHandler<?> handler, HttpRequest httpRequest) {
    HttpHeaders h = new DefaultHttpHeaders(false).set(httpRequest.headers());
    TransferCompletionHandler.class.cast(handler).headers(h);
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) TransferCompletionHandler(org.asynchttpclient.handler.TransferCompletionHandler)

Example 19 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class ProxyUnauthorized407Interceptor method exitAfterHandling407.

public //
boolean exitAfterHandling407(//
Channel channel, //
NettyResponseFuture<?> future, //
HttpResponse response, //
Request request, //
int statusCode, //
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(false).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, request, 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(channel, proxyAuthHeaders, request, proxyServer, proxyRealm, requestHeaders, future);
            } catch (SpnegoEngineException e) {
                // FIXME
                String ntlmHeader2 = getHeaderWithPrefix(proxyAuthHeaders, "NTLM");
                if (ntlmHeader2 != null) {
                    LOGGER.warn("Kerberos/Spnego proxy auth failed, proceeding with NTLM");
                    ntlmProxyChallenge(ntlmHeader2, request, 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 = new RequestBuilder(future.getCurrentRequest()).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 : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RequestBuilder(org.asynchttpclient.RequestBuilder) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SpnegoEngineException(org.asynchttpclient.spnego.SpnegoEngineException) Request(org.asynchttpclient.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) Realm(org.asynchttpclient.Realm)

Example 20 with DefaultHttpHeaders

use of io.netty.handler.codec.http.DefaultHttpHeaders in project async-http-client by AsyncHttpClient.

the class Unauthorized401Interceptor method exitAfterHandling401.

public //
boolean exitAfterHandling401(//
final Channel channel, //
final NettyResponseFuture<?> future, //
HttpResponse response, //
final Request request, //
int statusCode, //
Realm realm, //
ProxyServer proxyServer, HttpRequest httpRequest) {
    if (realm == null) {
        LOGGER.debug("Can't handle 401 as there's no realm");
        return false;
    }
    if (future.isAndSetInAuth(true)) {
        LOGGER.info("Can't handle 401 as auth was already performed");
        return false;
    }
    List<String> wwwAuthHeaders = response.headers().getAll(WWW_AUTHENTICATE);
    if (wwwAuthHeaders.isEmpty()) {
        LOGGER.info("Can't handle 401 as response doesn't contain WWW-Authenticate headers");
        return false;
    }
    // FIXME what's this???
    future.setChannelState(ChannelState.NEW);
    HttpHeaders requestHeaders = new DefaultHttpHeaders(false).add(request.getHeaders());
    switch(realm.getScheme()) {
        case BASIC:
            if (getHeaderWithPrefix(wwwAuthHeaders, "Basic") == null) {
                LOGGER.info("Can't handle 401 with Basic realm as WWW-Authenticate headers don't match");
                return false;
            }
            if (realm.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 401 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(realm).setUsePreemptiveAuth(//
            true).build();
            future.setRealm(newBasicRealm);
            break;
        case DIGEST:
            String digestHeader = getHeaderWithPrefix(wwwAuthHeaders, "Digest");
            if (digestHeader == null) {
                LOGGER.info("Can't handle 401 with Digest realm as WWW-Authenticate headers don't match");
                return false;
            }
            Realm newDigestRealm = //
            realm(realm).setUri(//
            request.getUri()).setMethodName(//
            request.getMethod()).setUsePreemptiveAuth(//
            true).parseWWWAuthenticateHeader(//
            digestHeader).build();
            future.setRealm(newDigestRealm);
            break;
        case NTLM:
            String ntlmHeader = getHeaderWithPrefix(wwwAuthHeaders, "NTLM");
            if (ntlmHeader == null) {
                LOGGER.info("Can't handle 401 with NTLM realm as WWW-Authenticate headers don't match");
                return false;
            }
            ntlmChallenge(ntlmHeader, request, requestHeaders, realm, future);
            Realm newNtlmRealm = //
            realm(realm).setUsePreemptiveAuth(//
            true).build();
            future.setRealm(newNtlmRealm);
            break;
        case KERBEROS:
        case SPNEGO:
            if (getHeaderWithPrefix(wwwAuthHeaders, NEGOTIATE) == null) {
                LOGGER.info("Can't handle 401 with Kerberos or Spnego realm as WWW-Authenticate headers don't match");
                return false;
            }
            try {
                kerberosChallenge(channel, wwwAuthHeaders, request, requestHeaders, realm, future);
            } catch (SpnegoEngineException e) {
                // FIXME
                String ntlmHeader2 = getHeaderWithPrefix(wwwAuthHeaders, "NTLM");
                if (ntlmHeader2 != null) {
                    LOGGER.warn("Kerberos/Spnego auth failed, proceeding with NTLM");
                    ntlmChallenge(ntlmHeader2, request, requestHeaders, realm, future);
                    Realm newNtlmRealm2 = //
                    realm(realm).setScheme(//
                    AuthScheme.NTLM).setUsePreemptiveAuth(//
                    true).build();
                    future.setRealm(newNtlmRealm2);
                } else {
                    requestSender.abort(channel, future, e);
                    return false;
                }
            }
            break;
        default:
            throw new IllegalStateException("Invalid Authentication scheme " + realm.getScheme());
    }
    final Request nextRequest = new RequestBuilder(future.getCurrentRequest()).setHeaders(requestHeaders).build();
    LOGGER.debug("Sending authentication to {}", request.getUri());
    if (//
    future.isKeepAlive() && //
    !HttpUtil.isTransferEncodingChunked(httpRequest) && !HttpUtil.isTransferEncodingChunked(response)) {
        future.setReuseChannel(true);
        requestSender.drainChannelAndExecuteNextRequest(channel, future, nextRequest);
    } else {
        channelManager.closeChannel(channel);
        requestSender.sendNextRequest(nextRequest, future);
    }
    return true;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) RequestBuilder(org.asynchttpclient.RequestBuilder) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) SpnegoEngineException(org.asynchttpclient.spnego.SpnegoEngineException) Request(org.asynchttpclient.Request) HttpRequest(io.netty.handler.codec.http.HttpRequest) Realm(org.asynchttpclient.Realm)

Aggregations

DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)36 Test (org.testng.annotations.Test)20 Test (org.junit.Test)18 Cookie (io.netty.handler.codec.http.cookie.Cookie)14 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 AsyncCompletionHandlerAdapter (org.asynchttpclient.test.TestUtils.AsyncCompletionHandlerAdapter)11 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)9 Charset (java.nio.charset.Charset)8 HttpResponseHeaders (org.asynchttpclient.HttpResponseHeaders)6 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3