Search in sources :

Example 6 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project AndroidAsync by koush.

the class SpdyMiddleware method newSocket.

private void newSocket(GetSocketData data, final AsyncSpdyConnection connection, final ConnectCallback callback) {
    final AsyncHttpRequest request = data.request;
    data.protocol = connection.protocol.toString();
    final AsyncHttpRequestBody requestBody = data.request.getBody();
    // this causes app engine to shit a brick, but if it is missing,
    // drive shits the bed
    //        if (requestBody != null) {
    //            if (requestBody.length() >= 0) {
    //                request.getHeaders().set("Content-Length", String.valueOf(requestBody.length()));
    //            }
    //        }
    final ArrayList<Header> headers = new ArrayList<Header>();
    headers.add(new Header(Header.TARGET_METHOD, request.getMethod()));
    headers.add(new Header(Header.TARGET_PATH, requestPath(request.getUri())));
    String host = request.getHeaders().get("Host");
    if (Protocol.SPDY_3 == connection.protocol) {
        headers.add(new Header(Header.VERSION, "HTTP/1.1"));
        headers.add(new Header(Header.TARGET_HOST, host));
    } else if (Protocol.HTTP_2 == connection.protocol) {
        // Optional in HTTP/2
        headers.add(new Header(Header.TARGET_AUTHORITY, host));
    } else {
        throw new AssertionError();
    }
    headers.add(new Header(Header.TARGET_SCHEME, request.getUri().getScheme()));
    final Multimap mm = request.getHeaders().getMultiMap();
    for (String key : mm.keySet()) {
        if (SpdyTransport.isProhibitedHeader(connection.protocol, key))
            continue;
        for (String value : mm.get(key)) {
            headers.add(new Header(key.toLowerCase(Locale.US), value));
        }
    }
    request.logv("\n" + request);
    final AsyncSpdyConnection.SpdySocket spdy = connection.newStream(headers, requestBody != null, true);
    callback.onConnectCompleted(null, spdy);
}
Also used : Multimap(com.koushikdutta.async.http.Multimap) ArrayList(java.util.ArrayList) AsyncHttpRequestBody(com.koushikdutta.async.http.body.AsyncHttpRequestBody) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 7 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project AndroidAsync by koush.

the class XHRPollingTransport method postMessage.

private void postMessage(String message) {
    if (!message.startsWith("5"))
        return;
    AsyncHttpRequest request = new AsyncHttpPost(computedRequestUrl());
    request.setBody(new StringBody(message));
    client.executeString(request, null);
}
Also used : StringBody(com.koushikdutta.async.http.body.StringBody) AsyncHttpPost(com.koushikdutta.async.http.AsyncHttpPost) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 8 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project AndroidAsync by koush.

the class AsyncProxyServer method onRequest.

@Override
protected void onRequest(HttpServerRequestCallback callback, AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
    super.onRequest(callback, request, response);
    if (callback != null)
        return;
    try {
        Uri uri;
        try {
            uri = Uri.parse(request.getPath());
            if (uri.getScheme() == null)
                throw new Exception("no host or full uri provided");
        } catch (Exception e) {
            String host = request.getHeaders().get("Host");
            int port = 80;
            if (host != null) {
                String[] splits = host.split(":", 2);
                if (splits.length == 2) {
                    host = splits[0];
                    port = Integer.parseInt(splits[1]);
                }
            }
            uri = Uri.parse("http://" + host + ":" + port + request.getPath());
        }
        proxyClient.execute(new AsyncHttpRequest(uri, request.getMethod(), request.getHeaders()), new HttpConnectCallback() {

            @Override
            public void onConnectCompleted(Exception ex, AsyncHttpResponse remoteResponse) {
                if (ex != null) {
                    response.code(500);
                    response.send(ex.getMessage());
                    return;
                }
                response.proxy(remoteResponse);
            }
        });
    } catch (Exception e) {
        response.code(500);
        response.send(e.getMessage());
    }
}
Also used : AsyncHttpResponse(com.koushikdutta.async.http.AsyncHttpResponse) HttpConnectCallback(com.koushikdutta.async.http.callback.HttpConnectCallback) Uri(android.net.Uri) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 9 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project ion by koush.

the class IonRequestBuilder method getLoaderEmitter.

private <T> void getLoaderEmitter(final EmitterTransform<T> ret) {
    Uri uri = prepareURI();
    if (uri == null) {
        ret.setComplete(new Exception("Invalid URI"));
        return;
    }
    AsyncHttpRequest request = prepareRequest(uri);
    ret.initialRequest = request;
    getLoaderEmitter(ret, request);
}
Also used : Uri(android.net.Uri) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Example 10 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest in project ion by koush.

the class IonRequestBuilder method prepareRequest.

private AsyncHttpRequest prepareRequest(Uri uri) {
    AsyncHttpRequest request = ion.configure().getAsyncHttpRequestFactory().createAsyncHttpRequest(uri, method, headers);
    request.setFollowRedirect(followRedirect);
    request.setBody(body);
    request.setLogging(ion.logtag, ion.logLevel);
    if (logTag != null)
        request.setLogging(logTag, logLevel);
    request.enableProxy(proxyHost, proxyPort);
    request.setTimeout(timeoutMilliseconds);
    request.logd("preparing request");
    return request;
}
Also used : AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)11 Uri (android.net.Uri)4 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 StringBody (com.koushikdutta.async.http.body.StringBody)2 TimeoutException (java.util.concurrent.TimeoutException)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 Multimap (com.koushikdutta.async.http.Multimap)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 AsyncHttpRequestBody (com.koushikdutta.async.http.body.AsyncHttpRequestBody)1 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)1 LoaderEmitter (com.koushikdutta.ion.Loader.LoaderEmitter)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1