Search in sources :

Example 1 with AsyncSSLException

use of com.koushikdutta.async.AsyncSSLException in project AndroidAsync by koush.

the class AsyncHttpClient method executeSocket.

private void executeSocket(final AsyncHttpRequest request, final int redirectCount, final FutureAsyncHttpResponse cancel, final HttpConnectCallback callback, final AsyncHttpClientMiddleware.OnResponseCompleteDataOnRequestSentData data) {
    // 4) wait for request to be sent fully
    // and
    // 6) wait for headers
    final AsyncHttpResponseImpl ret = new AsyncHttpResponseImpl(request) {

        @Override
        protected void onRequestCompleted(Exception ex) {
            if (ex != null) {
                reportConnectedCompleted(cancel, ex, null, request, callback);
                return;
            }
            request.logv("request completed");
            if (cancel.isCancelled())
                return;
            // 5) after request is sent, set a header timeout
            if (cancel.timeoutRunnable != null && mHeaders == null) {
                mServer.removeAllCallbacks(cancel.scheduled);
                cancel.scheduled = mServer.postDelayed(cancel.timeoutRunnable, getTimeoutRemaining(request));
            }
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onRequestSent(data);
            }
        }

        @Override
        public void setDataEmitter(DataEmitter emitter) {
            data.bodyEmitter = emitter;
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onBodyDecoder(data);
            }
            super.setDataEmitter(data.bodyEmitter);
            Headers headers = mHeaders;
            int responseCode = code();
            if ((responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == 307) && request.getFollowRedirect()) {
                String location = headers.get("Location");
                Uri redirect;
                try {
                    redirect = Uri.parse(location);
                    if (redirect.getScheme() == null) {
                        redirect = Uri.parse(new URL(new URL(request.getUri().toString()), location).toString());
                    }
                } catch (Exception e) {
                    reportConnectedCompleted(cancel, e, this, request, callback);
                    return;
                }
                final String method = request.getMethod().equals(AsyncHttpHead.METHOD) ? AsyncHttpHead.METHOD : AsyncHttpGet.METHOD;
                AsyncHttpRequest newReq = new AsyncHttpRequest(redirect, method);
                newReq.executionTime = request.executionTime;
                newReq.logLevel = request.logLevel;
                newReq.LOGTAG = request.LOGTAG;
                newReq.proxyHost = request.proxyHost;
                newReq.proxyPort = request.proxyPort;
                setupAndroidProxy(newReq);
                copyHeader(request, newReq, "User-Agent");
                copyHeader(request, newReq, "Range");
                request.logi("Redirecting");
                newReq.logi("Redirected");
                execute(newReq, redirectCount + 1, cancel, callback);
                setDataCallback(new NullDataCallback());
                return;
            }
            request.logv("Final (post cache response) headers:\n" + toString());
            // at this point the headers are done being modified
            reportConnectedCompleted(cancel, null, this, request, callback);
        }

        protected void onHeadersReceived() {
            super.onHeadersReceived();
            if (cancel.isCancelled())
                return;
            // 7) on headers, cancel timeout
            if (cancel.timeoutRunnable != null)
                mServer.removeAllCallbacks(cancel.scheduled);
            // allow the middleware to massage the headers before the body is decoded
            request.logv("Received headers:\n" + toString());
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onHeadersReceived(data);
            }
        // drop through, and setDataEmitter will be called for the body decoder.
        // headers will be further massaged in there.
        }

        @Override
        protected void report(Exception ex) {
            if (ex != null)
                request.loge("exception during response", ex);
            if (cancel.isCancelled())
                return;
            if (ex instanceof AsyncSSLException) {
                request.loge("SSL Exception", ex);
                AsyncSSLException ase = (AsyncSSLException) ex;
                request.onHandshakeException(ase);
                if (ase.getIgnore())
                    return;
            }
            final AsyncSocket socket = socket();
            if (socket == null)
                return;
            super.report(ex);
            if (!socket.isOpen() || ex != null) {
                if (headers() == null && ex != null)
                    reportConnectedCompleted(cancel, ex, null, request, callback);
            }
            data.exception = ex;
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onResponseComplete(data);
            }
        }

        @Override
        public AsyncSocket detachSocket() {
            request.logd("Detaching socket");
            AsyncSocket socket = socket();
            if (socket == null)
                return null;
            socket.setWriteableCallback(null);
            socket.setClosedCallback(null);
            socket.setEndCallback(null);
            socket.setDataCallback(null);
            setSocket(null);
            return socket;
        }
    };
    data.sendHeadersCallback = new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null)
                ret.report(ex);
            else
                ret.onHeadersSent();
        }
    };
    data.receiveHeadersCallback = new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null)
                ret.report(ex);
            else
                ret.onHeadersReceived();
        }
    };
    data.response = ret;
    ret.setSocket(data.socket);
    for (AsyncHttpClientMiddleware middleware : mMiddleware) {
        if (middleware.exchangeHeaders(data))
            break;
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) Uri(android.net.Uri) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SuppressLint(android.annotation.SuppressLint) URL(java.net.URL) AsyncSocket(com.koushikdutta.async.AsyncSocket) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncSSLException(com.koushikdutta.async.AsyncSSLException)

Example 2 with AsyncSSLException

use of com.koushikdutta.async.AsyncSSLException in project AndroidAsync by koush.

the class AsyncHttpClient method executeSocket.

private void executeSocket(final AsyncHttpRequest request, final int redirectCount, final FutureAsyncHttpResponse cancel, final HttpConnectCallback callback, final AsyncHttpClientMiddleware.OnResponseCompleteData data) {
    // 4) wait for request to be sent fully
    // and
    // 6) wait for headers
    final AsyncHttpResponseImpl ret = new AsyncHttpResponseImpl(request) {

        @Override
        protected void onRequestCompleted(Exception ex) {
            if (ex != null) {
                reportConnectedCompleted(cancel, ex, null, request, callback);
                return;
            }
            request.logv("request completed");
            if (cancel.isCancelled())
                return;
            // 5) after request is sent, set a header timeout
            if (cancel.timeoutRunnable != null && mHeaders == null) {
                cancel.scheduled.cancel();
                cancel.scheduled = mServer.postDelayed(cancel.timeoutRunnable, getTimeoutRemaining(request));
            }
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onRequestSent(data);
            }
        }

        @Override
        public void setDataEmitter(DataEmitter emitter) {
            data.bodyEmitter = emitter;
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onBodyDecoder(data);
            }
            super.setDataEmitter(data.bodyEmitter);
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                AsyncHttpRequest newReq = middleware.onResponseReady(data);
                if (newReq != null) {
                    newReq.executionTime = request.executionTime;
                    newReq.logLevel = request.logLevel;
                    newReq.LOGTAG = request.LOGTAG;
                    newReq.proxyHost = request.proxyHost;
                    newReq.proxyPort = request.proxyPort;
                    setupAndroidProxy(newReq);
                    request.logi("Response intercepted by middleware");
                    newReq.logi("Request initiated by middleware intercept by middleware");
                    // post to allow reuse of socket.
                    mServer.post(() -> execute(newReq, redirectCount, cancel, callback));
                    setDataCallback(new NullDataCallback());
                    return;
                }
            }
            Headers headers = mHeaders;
            int responseCode = code();
            if ((responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == 307) && request.getFollowRedirect()) {
                String location = headers.get("Location");
                Uri redirect;
                try {
                    redirect = Uri.parse(location);
                    if (redirect.getScheme() == null) {
                        redirect = Uri.parse(new URL(new URL(request.getUri().toString()), location).toString());
                    }
                } catch (Exception e) {
                    reportConnectedCompleted(cancel, e, this, request, callback);
                    return;
                }
                final String method = request.getMethod().equals(AsyncHttpHead.METHOD) ? AsyncHttpHead.METHOD : AsyncHttpGet.METHOD;
                AsyncHttpRequest newReq = new AsyncHttpRequest(redirect, method);
                newReq.executionTime = request.executionTime;
                newReq.logLevel = request.logLevel;
                newReq.LOGTAG = request.LOGTAG;
                newReq.proxyHost = request.proxyHost;
                newReq.proxyPort = request.proxyPort;
                setupAndroidProxy(newReq);
                copyHeader(request, newReq, "User-Agent");
                copyHeader(request, newReq, "Range");
                request.logi("Redirecting");
                newReq.logi("Redirected");
                mServer.post(() -> execute(newReq, redirectCount + 1, cancel, callback));
                setDataCallback(new NullDataCallback());
                return;
            }
            request.logv("Final (post cache response) headers:\n" + toString());
            // at this point the headers are done being modified
            reportConnectedCompleted(cancel, null, this, request, callback);
        }

        protected void onHeadersReceived() {
            super.onHeadersReceived();
            if (cancel.isCancelled())
                return;
            // 7) on headers, cancel timeout
            if (cancel.timeoutRunnable != null)
                cancel.scheduled.cancel();
            // allow the middleware to massage the headers before the body is decoded
            request.logv("Received headers:\n" + toString());
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onHeadersReceived(data);
            }
        // drop through, and setDataEmitter will be called for the body decoder.
        // headers will be further massaged in there.
        }

        @Override
        protected void report(Exception ex) {
            if (ex != null)
                request.loge("exception during response", ex);
            if (cancel.isCancelled())
                return;
            if (ex instanceof AsyncSSLException) {
                request.loge("SSL Exception", ex);
                AsyncSSLException ase = (AsyncSSLException) ex;
                request.onHandshakeException(ase);
                if (ase.getIgnore())
                    return;
            }
            final AsyncSocket socket = socket();
            if (socket == null)
                return;
            super.report(ex);
            if (!socket.isOpen() || ex != null) {
                if (headers() == null && ex != null)
                    reportConnectedCompleted(cancel, ex, null, request, callback);
            }
            data.exception = ex;
            for (AsyncHttpClientMiddleware middleware : mMiddleware) {
                middleware.onResponseComplete(data);
            }
        }

        @Override
        public AsyncSocket detachSocket() {
            request.logd("Detaching socket");
            AsyncSocket socket = socket();
            if (socket == null)
                return null;
            socket.setWriteableCallback(null);
            socket.setClosedCallback(null);
            socket.setEndCallback(null);
            socket.setDataCallback(null);
            setSocket(null);
            return socket;
        }
    };
    data.sendHeadersCallback = new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null)
                ret.report(ex);
            else
                ret.onHeadersSent();
        }
    };
    data.receiveHeadersCallback = new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null)
                ret.report(ex);
            else
                ret.onHeadersReceived();
        }
    };
    data.response = ret;
    ret.setSocket(data.socket);
    for (AsyncHttpClientMiddleware middleware : mMiddleware) {
        if (middleware.exchangeHeaders(data))
            break;
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) Uri(android.net.Uri) TimeoutException(java.util.concurrent.TimeoutException) AsyncSSLException(com.koushikdutta.async.AsyncSSLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SuppressLint(android.annotation.SuppressLint) URL(java.net.URL) AsyncSocket(com.koushikdutta.async.AsyncSocket) DataEmitter(com.koushikdutta.async.DataEmitter) AsyncSSLException(com.koushikdutta.async.AsyncSSLException)

Aggregations

SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 AsyncSSLException (com.koushikdutta.async.AsyncSSLException)2 AsyncSocket (com.koushikdutta.async.AsyncSocket)2 DataEmitter (com.koushikdutta.async.DataEmitter)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 URL (java.net.URL)2 TimeoutException (java.util.concurrent.TimeoutException)2