Call.java 13.3 KB
package com.squareup.okhttp;

import com.squareup.okhttp.internal.NamedRunnable;
import com.squareup.okhttp.internal.http.HttpEngine;
import com.squareup.okhttp.internal.http.RequestException;
import com.squareup.okhttp.internal.http.RouteException;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.List;

public class Call
{
  volatile boolean canceled;
  private final OkHttpClient client;
  HttpEngine engine;
  private boolean executed;
  Request originalRequest;
  
  protected Call(OkHttpClient paramOkHttpClient, Request paramRequest)
  {
    this.client = paramOkHttpClient.copyWithDefaults();
    this.originalRequest = paramRequest;
  }
  
  private Response getResponseWithInterceptorChain(boolean paramBoolean)
    throws IOException
  {
    return new ApplicationInterceptorChain(0, this.originalRequest, paramBoolean).proceed(this.originalRequest);
  }
  
  private String toLoggableString()
  {
    if (this.canceled) {}
    for (String str = "canceled call";; str = "call")
    {
      HttpUrl localHttpUrl = this.originalRequest.httpUrl().resolve("/...");
      return str + " to " + localHttpUrl;
    }
  }
  
  public void cancel()
  {
    this.canceled = true;
    if (this.engine != null) {
      this.engine.disconnect();
    }
  }
  
  public void enqueue(Callback paramCallback)
  {
    enqueue(paramCallback, false);
  }
  
  void enqueue(Callback paramCallback, boolean paramBoolean)
  {
    try
    {
      if (this.executed) {
        throw new IllegalStateException("Already Executed");
      }
    }
    finally {}
    this.executed = true;
    this.client.getDispatcher().enqueue(new AsyncCall(paramCallback, paramBoolean, null));
  }
  
  public Response execute()
    throws IOException
  {
    try
    {
      if (this.executed) {
        throw new IllegalStateException("Already Executed");
      }
    }
    finally {}
    this.executed = true;
    try
    {
      this.client.getDispatcher().executed(this);
      Response localResponse1 = getResponseWithInterceptorChain(false);
      if (localResponse1 == null) {
        throw new IOException("Canceled");
      }
    }
    finally
    {
      this.client.getDispatcher().finished(this);
    }
    this.client.getDispatcher().finished(this);
    return localResponse2;
  }
  
  Response getResponse(Request paramRequest, boolean paramBoolean)
    throws IOException
  {
    Object localObject1 = paramRequest.body();
    Object localObject2;
    if (localObject1 != null)
    {
      paramRequest = paramRequest.newBuilder();
      localObject2 = ((RequestBody)localObject1).contentType();
      if (localObject2 != null) {
        paramRequest.header("Content-Type", ((MediaType)localObject2).toString());
      }
      long l = ((RequestBody)localObject1).contentLength();
      if (l != -1L)
      {
        paramRequest.header("Content-Length", Long.toString(l));
        paramRequest.removeHeader("Transfer-Encoding");
        paramRequest = paramRequest.build();
      }
    }
    for (;;)
    {
      this.engine = new HttpEngine(this.client, paramRequest, false, false, paramBoolean, null, null, null, null);
      int i = 0;
      for (;;)
      {
        if (this.canceled)
        {
          this.engine.releaseConnection();
          throw new IOException("Canceled");
          paramRequest.header("Transfer-Encoding", "chunked");
          paramRequest.removeHeader("Content-Length");
          break;
        }
        try
        {
          this.engine.sendRequest();
          this.engine.readResponse();
          paramRequest = this.engine.getResponse();
          localObject1 = this.engine.followUpRequest();
          if (localObject1 == null)
          {
            if (!paramBoolean) {
              this.engine.releaseConnection();
            }
            return paramRequest;
          }
        }
        catch (RequestException paramRequest)
        {
          throw paramRequest.getCause();
        }
        catch (RouteException paramRequest)
        {
          localObject1 = this.engine.recover(paramRequest);
          if (localObject1 != null)
          {
            this.engine = ((HttpEngine)localObject1);
            continue;
          }
          throw paramRequest.getLastConnectException();
        }
        catch (IOException paramRequest)
        {
          localObject1 = this.engine.recover(paramRequest, null);
          if (localObject1 != null)
          {
            this.engine = ((HttpEngine)localObject1);
            continue;
          }
          throw paramRequest;
        }
        i += 1;
        if (i > 20) {
          throw new ProtocolException("Too many follow-up requests: " + i);
        }
        if (!this.engine.sameConnection(((Request)localObject1).httpUrl())) {
          this.engine.releaseConnection();
        }
        localObject2 = this.engine.close();
        this.engine = new HttpEngine(this.client, (Request)localObject1, false, false, paramBoolean, (Connection)localObject2, null, null, paramRequest);
      }
    }
  }
  
  public boolean isCanceled()
  {
    return this.canceled;
  }
  
  Object tag()
  {
    return this.originalRequest.tag();
  }
  
  class ApplicationInterceptorChain
    implements Interceptor.Chain
  {
    private final boolean forWebSocket;
    private final int index;
    private final Request request;
    
    ApplicationInterceptorChain(int paramInt, Request paramRequest, boolean paramBoolean)
    {
      this.index = paramInt;
      this.request = paramRequest;
      this.forWebSocket = paramBoolean;
    }
    
    public Connection connection()
    {
      return null;
    }
    
    public Response proceed(Request paramRequest)
      throws IOException
    {
      if (this.index < Call.this.client.interceptors().size())
      {
        Object localObject = new ApplicationInterceptorChain(Call.this, this.index + 1, paramRequest, this.forWebSocket);
        paramRequest = (Interceptor)Call.this.client.interceptors().get(this.index);
        localObject = paramRequest.intercept((Interceptor.Chain)localObject);
        if (localObject == null) {
          throw new NullPointerException("application interceptor " + paramRequest + " returned null");
        }
        return (Response)localObject;
      }
      return Call.this.getResponse(paramRequest, this.forWebSocket);
    }
    
    public Request request()
    {
      return this.request;
    }
  }
  
  final class AsyncCall
    extends NamedRunnable
  {
    private final boolean forWebSocket;
    private final Callback responseCallback;
    
    private AsyncCall(Callback paramCallback, boolean paramBoolean)
    {
      super(new Object[] { Call.this.originalRequest.urlString() });
      this.responseCallback = paramCallback;
      this.forWebSocket = paramBoolean;
    }
    
    final void cancel()
    {
      Call.this.cancel();
    }
    
    /* Error */
    protected final void execute()
    {
      // Byte code:
      //   0: iconst_1
      //   1: istore_1
      //   2: aload_0
      //   3: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   6: aload_0
      //   7: getfield 38	com/squareup/okhttp/Call$AsyncCall:forWebSocket	Z
      //   10: invokestatic 53	com/squareup/okhttp/Call:access$100	(Lcom/squareup/okhttp/Call;Z)Lcom/squareup/okhttp/Response;
      //   13: astore_3
      //   14: aload_0
      //   15: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   18: getfield 56	com/squareup/okhttp/Call:canceled	Z
      //   21: istore_2
      //   22: iload_2
      //   23: ifeq +43 -> 66
      //   26: aload_0
      //   27: getfield 36	com/squareup/okhttp/Call$AsyncCall:responseCallback	Lcom/squareup/okhttp/Callback;
      //   30: aload_0
      //   31: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   34: getfield 25	com/squareup/okhttp/Call:originalRequest	Lcom/squareup/okhttp/Request;
      //   37: new 49	java/io/IOException
      //   40: dup
      //   41: ldc 58
      //   43: invokespecial 61	java/io/IOException:<init>	(Ljava/lang/String;)V
      //   46: invokeinterface 67 3 0
      //   51: aload_0
      //   52: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   55: invokestatic 71	com/squareup/okhttp/Call:access$300	(Lcom/squareup/okhttp/Call;)Lcom/squareup/okhttp/OkHttpClient;
      //   58: invokevirtual 77	com/squareup/okhttp/OkHttpClient:getDispatcher	()Lcom/squareup/okhttp/Dispatcher;
      //   61: aload_0
      //   62: invokevirtual 83	com/squareup/okhttp/Dispatcher:finished	(Lcom/squareup/okhttp/Call$AsyncCall;)V
      //   65: return
      //   66: aload_0
      //   67: getfield 36	com/squareup/okhttp/Call$AsyncCall:responseCallback	Lcom/squareup/okhttp/Callback;
      //   70: aload_3
      //   71: invokeinterface 87 2 0
      //   76: goto -25 -> 51
      //   79: astore_3
      //   80: iload_1
      //   81: ifeq +50 -> 131
      //   84: getstatic 93	com/squareup/okhttp/internal/Internal:logger	Ljava/util/logging/Logger;
      //   87: getstatic 99	java/util/logging/Level:INFO	Ljava/util/logging/Level;
      //   90: new 101	java/lang/StringBuilder
      //   93: dup
      //   94: ldc 103
      //   96: invokespecial 104	java/lang/StringBuilder:<init>	(Ljava/lang/String;)V
      //   99: aload_0
      //   100: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   103: invokestatic 108	com/squareup/okhttp/Call:access$200	(Lcom/squareup/okhttp/Call;)Ljava/lang/String;
      //   106: invokevirtual 112	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
      //   109: invokevirtual 115	java/lang/StringBuilder:toString	()Ljava/lang/String;
      //   112: aload_3
      //   113: invokevirtual 121	java/util/logging/Logger:log	(Ljava/util/logging/Level;Ljava/lang/String;Ljava/lang/Throwable;)V
      //   116: aload_0
      //   117: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   120: invokestatic 71	com/squareup/okhttp/Call:access$300	(Lcom/squareup/okhttp/Call;)Lcom/squareup/okhttp/OkHttpClient;
      //   123: invokevirtual 77	com/squareup/okhttp/OkHttpClient:getDispatcher	()Lcom/squareup/okhttp/Dispatcher;
      //   126: aload_0
      //   127: invokevirtual 83	com/squareup/okhttp/Dispatcher:finished	(Lcom/squareup/okhttp/Call$AsyncCall;)V
      //   130: return
      //   131: aload_0
      //   132: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   135: getfield 125	com/squareup/okhttp/Call:engine	Lcom/squareup/okhttp/internal/http/HttpEngine;
      //   138: ifnonnull +44 -> 182
      //   141: aload_0
      //   142: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   145: getfield 25	com/squareup/okhttp/Call:originalRequest	Lcom/squareup/okhttp/Request;
      //   148: astore 4
      //   150: aload_0
      //   151: getfield 36	com/squareup/okhttp/Call$AsyncCall:responseCallback	Lcom/squareup/okhttp/Callback;
      //   154: aload 4
      //   156: aload_3
      //   157: invokeinterface 67 3 0
      //   162: goto -46 -> 116
      //   165: astore_3
      //   166: aload_0
      //   167: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   170: invokestatic 71	com/squareup/okhttp/Call:access$300	(Lcom/squareup/okhttp/Call;)Lcom/squareup/okhttp/OkHttpClient;
      //   173: invokevirtual 77	com/squareup/okhttp/OkHttpClient:getDispatcher	()Lcom/squareup/okhttp/Dispatcher;
      //   176: aload_0
      //   177: invokevirtual 83	com/squareup/okhttp/Dispatcher:finished	(Lcom/squareup/okhttp/Call$AsyncCall;)V
      //   180: aload_3
      //   181: athrow
      //   182: aload_0
      //   183: getfield 17	com/squareup/okhttp/Call$AsyncCall:this$0	Lcom/squareup/okhttp/Call;
      //   186: getfield 125	com/squareup/okhttp/Call:engine	Lcom/squareup/okhttp/internal/http/HttpEngine;
      //   189: invokevirtual 131	com/squareup/okhttp/internal/http/HttpEngine:getRequest	()Lcom/squareup/okhttp/Request;
      //   192: astore 4
      //   194: goto -44 -> 150
      //   197: astore_3
      //   198: iconst_0
      //   199: istore_1
      //   200: goto -120 -> 80
      // Local variable table:
      //   start	length	slot	name	signature
      //   0	203	0	this	AsyncCall
      //   1	199	1	i	int
      //   21	2	2	bool	boolean
      //   13	58	3	localResponse	Response
      //   79	78	3	localIOException1	IOException
      //   165	16	3	localObject	Object
      //   197	1	3	localIOException2	IOException
      //   148	45	4	localRequest	Request
      // Exception table:
      //   from	to	target	type
      //   26	51	79	java/io/IOException
      //   66	76	79	java/io/IOException
      //   2	22	165	finally
      //   26	51	165	finally
      //   66	76	165	finally
      //   84	116	165	finally
      //   131	150	165	finally
      //   150	162	165	finally
      //   182	194	165	finally
      //   2	22	197	java/io/IOException
    }
    
    final Call get()
    {
      return Call.this;
    }
    
    final String host()
    {
      return Call.this.originalRequest.httpUrl().host();
    }
    
    final Request request()
    {
      return Call.this.originalRequest;
    }
    
    final Object tag()
    {
      return Call.this.originalRequest.tag();
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/com/squareup/okhttp/Call.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */