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