Search in sources :

Example 1 with Headers

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

the class ResponseCacheMiddleware method onBodyDecoder.

// step 2) if this is a conditional cache request, serve it from the cache if necessary
// otherwise, see if it is cacheable
@Override
public void onBodyDecoder(OnBodyDecoderData data) {
    CachedSocket cached = com.koushikdutta.async.Util.getWrappedSocket(data.socket, CachedSocket.class);
    if (cached != null) {
        data.response.headers().set(SERVED_FROM, CACHE);
        return;
    }
    CacheData cacheData = data.state.get("cache-data");
    RawHeaders rh = RawHeaders.fromMultimap(data.response.headers().getMultiMap());
    rh.removeAll("Content-Length");
    rh.setStatusLine(String.format(Locale.ENGLISH, "%s %s %s", data.response.protocol(), data.response.code(), data.response.message()));
    ResponseHeaders networkResponse = new ResponseHeaders(data.request.getUri(), rh);
    data.state.put("response-headers", networkResponse);
    if (cacheData != null) {
        if (cacheData.cachedResponseHeaders.validate(networkResponse)) {
            data.request.logi("Serving response from conditional cache");
            ResponseHeaders combined = cacheData.cachedResponseHeaders.combine(networkResponse);
            data.response.headers(new Headers(combined.getHeaders().toMultimap()));
            data.response.code(combined.getHeaders().getResponseCode());
            data.response.message(combined.getHeaders().getResponseMessage());
            data.response.headers().set(SERVED_FROM, CONDITIONAL_CACHE);
            conditionalCacheHitCount++;
            CachedBodyEmitter bodySpewer = new CachedBodyEmitter(cacheData.candidate, cacheData.contentLength);
            bodySpewer.setDataEmitter(data.bodyEmitter);
            data.bodyEmitter = bodySpewer;
            bodySpewer.sendCachedData();
            return;
        }
        // did not validate, so fall through and cache the response
        data.state.remove("cache-data");
        StreamUtility.closeQuietly(cacheData.snapshot);
    }
    if (!caching)
        return;
    RequestHeaders requestHeaders = data.state.get("request-headers");
    if (requestHeaders == null || !networkResponse.isCacheable(requestHeaders) || !data.request.getMethod().equals(AsyncHttpGet.METHOD)) {
        /*
             * Don't cache non-GET responses. We're technically allowed to cache
             * HEAD requests and some POST requests, but the complexity of doing
             * so is high and the benefit is low.
             */
        networkCount++;
        data.request.logd("Response is not cacheable");
        return;
    }
    String key = FileCache.toKeyString(data.request.getUri());
    RawHeaders varyHeaders = requestHeaders.getHeaders().getAll(networkResponse.getVaryFields());
    Entry entry = new Entry(data.request.getUri(), varyHeaders, data.request, networkResponse.getHeaders());
    BodyCacher cacher = new BodyCacher();
    EntryEditor editor = new EntryEditor(key);
    try {
        entry.writeTo(editor);
        // create the file
        editor.newOutputStream(ENTRY_BODY);
    } catch (Exception e) {
        // Log.e(LOGTAG, "error", e);
        editor.abort();
        networkCount++;
        return;
    }
    cacher.editor = editor;
    cacher.setDataEmitter(data.bodyEmitter);
    data.bodyEmitter = cacher;
    data.state.put("body-cacher", cacher);
    data.request.logd("Caching response");
    cacheStoreCount++;
}
Also used : Headers(com.koushikdutta.async.http.Headers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Example 2 with Headers

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

the class MultipartFormDataBody method onBoundaryStart.

@Override
protected void onBoundaryStart() {
    final Headers headers = new Headers();
    liner = new LineEmitter();
    liner.setLineCallback(new StringCallback() {

        @Override
        public void onStringAvailable(String s) {
            if (!"\r".equals(s)) {
                headers.addLine(s);
            } else {
                handleLast();
                liner = null;
                setDataCallback(null);
                Part part = new Part(headers);
                if (mCallback != null)
                    mCallback.onPart(part);
                if (getDataCallback() == null) {
                    // if (part.isFile()) {
                    // setDataCallback(new NullDataCallback());
                    // return;
                    // }
                    lastPart = part;
                    lastData = new ByteBufferList();
                    setDataCallback(new DataCallback() {

                        @Override
                        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                            bb.get(lastData);
                        }
                    });
                }
            }
        }
    });
    setDataCallback(liner);
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) LineEmitter(com.koushikdutta.async.LineEmitter) Headers(com.koushikdutta.async.http.Headers) DataEmitter(com.koushikdutta.async.DataEmitter) StringCallback(com.koushikdutta.async.LineEmitter.StringCallback) DataCallback(com.koushikdutta.async.callback.DataCallback)

Example 3 with Headers

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

the class CookieTests method testCookies.

public void testCookies() throws Exception {
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    CookieManager manager = new CookieManager(null, null);
    Headers headers = new Headers();
    headers.set("Set-Cookie", "foo=bar");
    URI uri = URI.create("http://example.com");
    manager.put(uri, headers.getMultiMap());
    headers.set("Set-Cookie", "poop=scoop");
    manager.put(uri, headers.getMultiMap());
    headers.set("Set-Cookie", "foo=goop");
    manager.put(uri, headers.getMultiMap());
    Headers newHeaders = new Headers();
    Map<String, List<String>> cookies = manager.get(uri, newHeaders.getMultiMap());
    manager.get(uri, cookies);
    CookieMiddleware.addCookies(cookies, newHeaders);
    assertTrue(newHeaders.get("Cookie").contains("foo=goop"));
    assertTrue(newHeaders.get("Cookie").contains("poop=scoop"));
    assertFalse(newHeaders.get("Cookie").contains("bar"));
}
Also used : Headers(com.koushikdutta.async.http.Headers) Ion(com.koushikdutta.ion.Ion) List(java.util.List) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 4 with Headers

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

the class HeadersTests method testHeadCasing.

public void testHeadCasing() throws Exception {
    Headers headers = new Headers();
    headers.set("Foo", "bar");
    assertTrue(headers.toString().contains("Foo"));
}
Also used : Headers(com.koushikdutta.async.http.Headers)

Example 5 with Headers

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

the class ResponseCacheMiddleware method onBodyDecoder.

// step 2) if this is a conditional cache request, serve it from the cache if necessary
// otherwise, see if it is cacheable
@Override
public void onBodyDecoder(OnBodyDataOnRequestSentData data) {
    CachedSocket cached = com.koushikdutta.async.Util.getWrappedSocket(data.socket, CachedSocket.class);
    if (cached != null) {
        data.response.headers().set(SERVED_FROM, CACHE);
        return;
    }
    CacheData cacheData = data.state.get("cache-data");
    RawHeaders rh = RawHeaders.fromMultimap(data.response.headers().getMultiMap());
    rh.removeAll("Content-Length");
    rh.setStatusLine(String.format(Locale.ENGLISH, "%s %s %s", data.response.protocol(), data.response.code(), data.response.message()));
    ResponseHeaders networkResponse = new ResponseHeaders(data.request.getUri(), rh);
    data.state.put("response-headers", networkResponse);
    if (cacheData != null) {
        if (cacheData.cachedResponseHeaders.validate(networkResponse)) {
            data.request.logi("Serving response from conditional cache");
            ResponseHeaders combined = cacheData.cachedResponseHeaders.combine(networkResponse);
            data.response.headers(new Headers(combined.getHeaders().toMultimap()));
            data.response.code(combined.getHeaders().getResponseCode());
            data.response.message(combined.getHeaders().getResponseMessage());
            data.response.headers().set(SERVED_FROM, CONDITIONAL_CACHE);
            conditionalCacheHitCount++;
            CachedBodyEmitter bodySpewer = new CachedBodyEmitter(cacheData.candidate, cacheData.contentLength);
            bodySpewer.setDataEmitter(data.bodyEmitter);
            data.bodyEmitter = bodySpewer;
            bodySpewer.sendCachedData();
            return;
        }
        // did not validate, so fall through and cache the response
        data.state.remove("cache-data");
        StreamUtility.closeQuietly(cacheData.snapshot);
    }
    if (!caching)
        return;
    RequestHeaders requestHeaders = data.state.get("request-headers");
    if (requestHeaders == null || !networkResponse.isCacheable(requestHeaders) || !data.request.getMethod().equals(AsyncHttpGet.METHOD)) {
        /*
             * Don't cache non-GET responses. We're technically allowed to cache
             * HEAD requests and some POST requests, but the complexity of doing
             * so is high and the benefit is low.
             */
        networkCount++;
        data.request.logd("Response is not cacheable");
        return;
    }
    String key = FileCache.toKeyString(data.request.getUri());
    RawHeaders varyHeaders = requestHeaders.getHeaders().getAll(networkResponse.getVaryFields());
    Entry entry = new Entry(data.request.getUri(), varyHeaders, data.request, networkResponse.getHeaders());
    BodyCacher cacher = new BodyCacher();
    EntryEditor editor = new EntryEditor(key);
    try {
        entry.writeTo(editor);
        // create the file
        editor.newOutputStream(ENTRY_BODY);
    } catch (Exception e) {
        // Log.e(LOGTAG, "error", e);
        editor.abort();
        networkCount++;
        return;
    }
    cacher.editor = editor;
    cacher.setDataEmitter(data.bodyEmitter);
    data.bodyEmitter = cacher;
    data.state.put("body-cacher", cacher);
    data.request.logd("Caching response");
    cacheStoreCount++;
}
Also used : Headers(com.koushikdutta.async.http.Headers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException)

Aggregations

Headers (com.koushikdutta.async.http.Headers)11 IOException (java.io.IOException)3 CookieManager (java.net.CookieManager)3 List (java.util.List)3 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 DataEmitter (com.koushikdutta.async.DataEmitter)2 Ion (com.koushikdutta.ion.Ion)2 URI (java.net.URI)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 CertificateException (java.security.cert.CertificateException)2 LineEmitter (com.koushikdutta.async.LineEmitter)1 StringCallback (com.koushikdutta.async.LineEmitter.StringCallback)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 TransformFuture (com.koushikdutta.async.future.TransformFuture)1 AsyncHttpRequestBody (com.koushikdutta.async.http.body.AsyncHttpRequestBody)1 CookieMiddleware (com.koushikdutta.ion.cookie.CookieMiddleware)1 HttpCookie (java.net.HttpCookie)1 ArrayList (java.util.ArrayList)1