use of com.squareup.okhttp.Connection in project robovm by robovm.
the class HttpTransport method discardStream.
/**
* Discards the response body so that the connection can be reused. This
* needs to be done judiciously, since it delays the current request in
* order to speed up a potential future request that may never occur.
*/
private static boolean discardStream(HttpEngine httpEngine, InputStream responseBodyIn) {
Connection connection = httpEngine.connection;
if (connection == null)
return false;
Socket socket = connection.getSocket();
if (socket == null)
return false;
try {
int socketTimeout = socket.getSoTimeout();
socket.setSoTimeout(DISCARD_STREAM_TIMEOUT_MILLIS);
try {
Util.skipAll(responseBodyIn);
return true;
} finally {
socket.setSoTimeout(socketTimeout);
}
} catch (IOException e) {
return false;
}
}
use of com.squareup.okhttp.Connection in project phonegap-facebook-plugin by Wizcorp.
the class RouteSelector method next.
/**
* Returns the next route address to attempt.
*
* @throws NoSuchElementException if there are no more routes to attempt.
*/
public Connection next() throws IOException {
// Always prefer pooled connections over new connections.
Connection pooled = pool.get(address);
if (pooled != null) {
return pooled;
}
// Compute the next route to attempt.
if (!hasNextTlsMode()) {
if (!hasNextInetSocketAddress()) {
if (!hasNextProxy()) {
if (!hasNextPostponed()) {
throw new NoSuchElementException();
}
return new Connection(nextPostponed());
}
lastProxy = nextProxy();
resetNextInetSocketAddress(lastProxy);
}
lastInetSocketAddress = nextInetSocketAddress();
resetNextTlsMode();
}
boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
if (failedRoutes.contains(route)) {
postponedRoutes.add(route);
// tried last.
return next();
}
return new Connection(route);
}
use of com.squareup.okhttp.Connection in project stetho by facebook.
the class StethoInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
String requestId = mEventReporter.nextRequestId();
Request request = chain.request();
RequestBodyHelper requestBodyHelper = null;
if (mEventReporter.isEnabled()) {
requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
mEventReporter.requestWillBeSent(inspectorRequest);
}
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
if (mEventReporter.isEnabled()) {
mEventReporter.httpExchangeFailed(requestId, e.toString());
}
throw e;
}
if (mEventReporter.isEnabled()) {
if (requestBodyHelper != null && requestBodyHelper.hasBody()) {
requestBodyHelper.reportDataSent();
}
Connection connection = chain.connection();
if (connection == null) {
throw new IllegalStateException("No connection associated with this request; " + "did you use addInterceptor instead of addNetworkInterceptor?");
}
mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response, connection));
ResponseBody body = response.body();
MediaType contentType = null;
InputStream responseStream = null;
if (body != null) {
contentType = body.contentType();
responseStream = body.byteStream();
}
responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
if (responseStream != null) {
response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
}
}
return response;
}
use of com.squareup.okhttp.Connection in project robovm by robovm.
the class RouteSelector method next.
/**
* Returns the next route address to attempt.
*
* @throws NoSuchElementException if there are no more routes to attempt.
*/
public Connection next(String method) throws IOException {
// Always prefer pooled connections over new connections.
for (Connection pooled; (pooled = pool.get(address)) != null; ) {
if (method.equals("GET") || pooled.isReadable())
return pooled;
pooled.close();
}
// Compute the next route to attempt.
if (!hasNextTlsMode()) {
if (!hasNextInetSocketAddress()) {
if (!hasNextProxy()) {
if (!hasNextPostponed()) {
throw new NoSuchElementException();
}
return new Connection(nextPostponed());
}
lastProxy = nextProxy();
resetNextInetSocketAddress(lastProxy);
}
lastInetSocketAddress = nextInetSocketAddress();
resetNextTlsMode();
}
boolean modernTls = nextTlsMode() == TLS_MODE_MODERN;
Route route = new Route(address, lastProxy, lastInetSocketAddress, modernTls);
if (routeDatabase.shouldPostpone(route)) {
postponedRoutes.add(route);
// tried last.
return next(method);
}
return new Connection(route);
}
use of com.squareup.okhttp.Connection in project pinpoint by naver.
the class HttpEngineConnectMethodInterceptor method doInAfterTrace.
@Override
protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
if (target instanceof ConnectionGetter) {
Connection connection = ((ConnectionGetter) target)._$PINPOINT$_getConnection();
if (connection != null) {
final StringBuilder sb = new StringBuilder();
sb.append(connection.getRoute().getAddress().getUriHost()).append(":");
sb.append(connection.getRoute().getAddress().getUriPort());
recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, sb.toString());
}
}
recorder.recordApi(methodDescriptor);
recorder.recordServiceType(OkHttpConstants.OK_HTTP_CLIENT_INTERNAL);
recorder.recordException(throwable);
}
Aggregations