Search in sources :

Example 6 with Headers

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

the class CookieTests method testReinit.

public void testReinit() throws Exception {
    CookieMiddleware middleware = Ion.getDefault(getContext()).getCookieMiddleware();
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    Headers headers = new Headers();
    headers.set("Set-Cookie", "foo=bar");
    URI uri = URI.create("http://example.com");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "poop=scoop");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "foo=goop");
    middleware.put(uri, headers);
    middleware.reinit();
    CookieManager manager = middleware.getCookieManager();
    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 : CookieMiddleware(com.koushikdutta.ion.cookie.CookieMiddleware) Headers(com.koushikdutta.async.http.Headers) Ion(com.koushikdutta.ion.Ion) List(java.util.List) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 7 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)

Example 8 with Headers

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

the class SpdyMiddleware method exchangeHeaders.

@Override
public boolean exchangeHeaders(final OnExchangeHeaderData data) {
    if (!(data.socket instanceof AsyncSpdyConnection.SpdySocket))
        return super.exchangeHeaders(data);
    AsyncHttpRequestBody requestBody = data.request.getBody();
    if (requestBody != null) {
        data.response.sink(data.socket);
    }
    // headers were already sent as part of the socket being opened.
    data.sendHeadersCallback.onCompleted(null);
    final AsyncSpdyConnection.SpdySocket spdySocket = (AsyncSpdyConnection.SpdySocket) data.socket;
    spdySocket.headers().then(new TransformFuture<Headers, List<Header>>() {

        @Override
        protected void transform(List<Header> result) throws Exception {
            Headers headers = new Headers();
            for (Header header : result) {
                String key = header.name.utf8();
                String value = header.value.utf8();
                headers.add(key, value);
            }
            String status = headers.remove(Header.RESPONSE_STATUS.utf8());
            String[] statusParts = status.split(" ", 2);
            data.response.code(Integer.parseInt(statusParts[0]));
            if (statusParts.length == 2)
                data.response.message(statusParts[1]);
            data.response.protocol(headers.remove(Header.VERSION.utf8()));
            data.response.headers(headers);
            setComplete(headers);
        }
    }).setCallback(new FutureCallback<Headers>() {

        @Override
        public void onCompleted(Exception e, Headers result) {
            data.receiveHeadersCallback.onCompleted(e);
            DataEmitter emitter = HttpUtil.getBodyDecoder(spdySocket, spdySocket.getConnection().protocol, result, false);
            data.response.emitter(emitter);
        }
    });
    return true;
}
Also used : Headers(com.koushikdutta.async.http.Headers) IOException(java.io.IOException) DataEmitter(com.koushikdutta.async.DataEmitter) ArrayList(java.util.ArrayList) ByteBufferList(com.koushikdutta.async.ByteBufferList) List(java.util.List) AsyncHttpRequestBody(com.koushikdutta.async.http.body.AsyncHttpRequestBody) TransformFuture(com.koushikdutta.async.future.TransformFuture)

Example 9 with Headers

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

the class IonRequestBuilder method getHeaders.

private Headers getHeaders() {
    if (headers == null) {
        headers = new Headers();
        AsyncHttpRequest.setDefaultHeaders(headers, uri == null ? null : Uri.parse(uri));
    }
    return headers;
}
Also used : Headers(com.koushikdutta.async.http.Headers)

Aggregations

Headers (com.koushikdutta.async.http.Headers)9 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 IOException (java.io.IOException)2 URI (java.net.URI)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 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1