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++;
}
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);
}
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"));
}
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"));
}
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++;
}
Aggregations