HttpEngine.java 30 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
package com.squareup.okhttp.internal.http;

import com.squareup.okhttp.Address;
import com.squareup.okhttp.CertificatePinner;
import com.squareup.okhttp.Connection;
import com.squareup.okhttp.ConnectionPool;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Headers.Builder;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Interceptor.Chain;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Request.Builder;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Response.Builder;
import com.squareup.okhttp.ResponseBody;
import com.squareup.okhttp.Route;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.InternalCache;
import com.squareup.okhttp.internal.Util;
import com.squareup.okhttp.internal.Version;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.CookieHandler;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketTimeoutException;
import java.security.cert.CertificateException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.GzipSource;
import okio.Okio;
import okio.Sink;
import okio.Source;
import okio.Timeout;

public final class HttpEngine
{
  private static final ResponseBody EMPTY_BODY = new ResponseBody()
  {
    public final long contentLength()
    {
      return 0L;
    }
    
    public final MediaType contentType()
    {
      return null;
    }
    
    public final BufferedSource source()
    {
      return new Buffer();
    }
  };
  public static final int MAX_FOLLOW_UPS = 20;
  private Address address;
  public final boolean bufferRequestBody;
  private BufferedSink bufferedRequestBody;
  private Response cacheResponse;
  private CacheStrategy cacheStrategy;
  private final boolean callerWritesRequestBody;
  final OkHttpClient client;
  private Connection connection;
  private final boolean forWebSocket;
  private Request networkRequest;
  private final Response priorResponse;
  private Sink requestBodyOut;
  private Route route;
  private RouteSelector routeSelector;
  long sentRequestMillis = -1L;
  private CacheRequest storeRequest;
  private boolean transparentGzip;
  private Transport transport;
  private final Request userRequest;
  private Response userResponse;
  
  public HttpEngine(OkHttpClient paramOkHttpClient, Request paramRequest, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, Connection paramConnection, RouteSelector paramRouteSelector, RetryableSink paramRetryableSink, Response paramResponse)
  {
    this.client = paramOkHttpClient;
    this.userRequest = paramRequest;
    this.bufferRequestBody = paramBoolean1;
    this.callerWritesRequestBody = paramBoolean2;
    this.forWebSocket = paramBoolean3;
    this.connection = paramConnection;
    this.routeSelector = paramRouteSelector;
    this.requestBodyOut = paramRetryableSink;
    this.priorResponse = paramResponse;
    if (paramConnection != null)
    {
      Internal.instance.setOwner(paramConnection, this);
      this.route = paramConnection.getRoute();
      return;
    }
    this.route = null;
  }
  
  private Response cacheWritingResponse(final CacheRequest paramCacheRequest, Response paramResponse)
    throws IOException
  {
    if (paramCacheRequest == null) {}
    Sink localSink;
    do
    {
      return paramResponse;
      localSink = paramCacheRequest.body();
    } while (localSink == null);
    paramCacheRequest = new Source()
    {
      boolean cacheRequestClosed;
      
      public void close()
        throws IOException
      {
        if ((!this.cacheRequestClosed) && (!Util.discard(this, 100, TimeUnit.MILLISECONDS)))
        {
          this.cacheRequestClosed = true;
          paramCacheRequest.abort();
        }
        this.val$source.close();
      }
      
      public long read(Buffer paramAnonymousBuffer, long paramAnonymousLong)
        throws IOException
      {
        try
        {
          paramAnonymousLong = this.val$source.read(paramAnonymousBuffer, paramAnonymousLong);
          if (paramAnonymousLong == -1L)
          {
            if (!this.cacheRequestClosed)
            {
              this.cacheRequestClosed = true;
              this.val$cacheBody.close();
            }
            return -1L;
          }
        }
        catch (IOException paramAnonymousBuffer)
        {
          if (!this.cacheRequestClosed)
          {
            this.cacheRequestClosed = true;
            paramCacheRequest.abort();
          }
          throw paramAnonymousBuffer;
        }
        paramAnonymousBuffer.copyTo(this.val$cacheBody.buffer(), paramAnonymousBuffer.size() - paramAnonymousLong, paramAnonymousLong);
        this.val$cacheBody.emitCompleteSegments();
        return paramAnonymousLong;
      }
      
      public Timeout timeout()
      {
        return this.val$source.timeout();
      }
    };
    return paramResponse.newBuilder().body(new RealResponseBody(paramResponse.headers(), Okio.buffer(paramCacheRequest))).build();
  }
  
  private static Headers combine(Headers paramHeaders1, Headers paramHeaders2)
    throws IOException
  {
    int j = 0;
    Headers.Builder localBuilder = new Headers.Builder();
    int k = paramHeaders1.size();
    int i = 0;
    while (i < k)
    {
      String str1 = paramHeaders1.name(i);
      String str2 = paramHeaders1.value(i);
      if (((!"Warning".equalsIgnoreCase(str1)) || (!str2.startsWith("1"))) && ((!OkHeaders.isEndToEnd(str1)) || (paramHeaders2.get(str1) == null))) {
        localBuilder.add(str1, str2);
      }
      i += 1;
    }
    k = paramHeaders2.size();
    i = j;
    while (i < k)
    {
      paramHeaders1 = paramHeaders2.name(i);
      if ((!"Content-Length".equalsIgnoreCase(paramHeaders1)) && (OkHeaders.isEndToEnd(paramHeaders1))) {
        localBuilder.add(paramHeaders1, paramHeaders2.value(i));
      }
      i += 1;
    }
    return localBuilder.build();
  }
  
  private void connect()
    throws RequestException, RouteException
  {
    if (this.connection != null) {
      throw new IllegalStateException();
    }
    if (this.routeSelector == null) {
      this.address = createAddress(this.client, this.networkRequest);
    }
    try
    {
      this.routeSelector = RouteSelector.get(this.address, this.networkRequest, this.client);
      this.connection = createNextConnection();
      Internal.instance.connectAndSetOwner(this.client, this.connection, this);
      this.route = this.connection.getRoute();
      return;
    }
    catch (IOException localIOException)
    {
      throw new RequestException(localIOException);
    }
  }
  
  private void connectFailed(RouteSelector paramRouteSelector, IOException paramIOException)
  {
    if (Internal.instance.recycleCount(this.connection) > 0) {
      return;
    }
    paramRouteSelector.connectFailed(this.connection.getRoute(), paramIOException);
  }
  
  private static Address createAddress(OkHttpClient paramOkHttpClient, Request paramRequest)
  {
    CertificatePinner localCertificatePinner = null;
    SSLSocketFactory localSSLSocketFactory;
    HostnameVerifier localHostnameVerifier;
    if (paramRequest.isHttps())
    {
      localSSLSocketFactory = paramOkHttpClient.getSslSocketFactory();
      localHostnameVerifier = paramOkHttpClient.getHostnameVerifier();
      localCertificatePinner = paramOkHttpClient.getCertificatePinner();
    }
    for (;;)
    {
      return new Address(paramRequest.httpUrl().host(), paramRequest.httpUrl().port(), paramOkHttpClient.getDns(), paramOkHttpClient.getSocketFactory(), localSSLSocketFactory, localHostnameVerifier, localCertificatePinner, paramOkHttpClient.getAuthenticator(), paramOkHttpClient.getProxy(), paramOkHttpClient.getProtocols(), paramOkHttpClient.getConnectionSpecs(), paramOkHttpClient.getProxySelector());
      localHostnameVerifier = null;
      localSSLSocketFactory = null;
    }
  }
  
  private Connection createNextConnection()
    throws RouteException
  {
    Object localObject = this.client.getConnectionPool();
    for (;;)
    {
      Connection localConnection = ((ConnectionPool)localObject).get(this.address);
      if (localConnection == null) {
        break;
      }
      if ((this.networkRequest.method().equals("GET")) || (Internal.instance.isReadable(localConnection))) {
        return localConnection;
      }
      Util.closeQuietly(localConnection.getSocket());
    }
    try
    {
      localObject = new Connection((ConnectionPool)localObject, this.routeSelector.next());
      return (Connection)localObject;
    }
    catch (IOException localIOException)
    {
      throw new RouteException(localIOException);
    }
  }
  
  public static boolean hasBody(Response paramResponse)
  {
    if (paramResponse.request().method().equals("HEAD")) {}
    do
    {
      return false;
      int i = paramResponse.code();
      if (((i < 100) || (i >= 200)) && (i != 204) && (i != 304)) {
        return true;
      }
    } while ((OkHeaders.contentLength(paramResponse) == -1L) && (!"chunked".equalsIgnoreCase(paramResponse.header("Transfer-Encoding"))));
    return true;
  }
  
  private boolean isRecoverable(RouteException paramRouteException)
  {
    if (!this.client.getRetryOnConnectionFailure()) {}
    do
    {
      do
      {
        return false;
        paramRouteException = paramRouteException.getLastConnectException();
      } while ((paramRouteException instanceof ProtocolException));
      if ((paramRouteException instanceof InterruptedIOException)) {
        return paramRouteException instanceof SocketTimeoutException;
      }
    } while ((((paramRouteException instanceof SSLHandshakeException)) && ((paramRouteException.getCause() instanceof CertificateException))) || ((paramRouteException instanceof SSLPeerUnverifiedException)));
    return true;
  }
  
  private boolean isRecoverable(IOException paramIOException)
  {
    if (!this.client.getRetryOnConnectionFailure()) {}
    while (((paramIOException instanceof ProtocolException)) || ((paramIOException instanceof InterruptedIOException))) {
      return false;
    }
    return true;
  }
  
  private void maybeCache()
    throws IOException
  {
    InternalCache localInternalCache = Internal.instance.internalCache(this.client);
    if (localInternalCache == null) {}
    do
    {
      return;
      if (CacheStrategy.isCacheable(this.userResponse, this.networkRequest)) {
        break;
      }
    } while (!HttpMethod.invalidatesCache(this.networkRequest.method()));
    try
    {
      localInternalCache.remove(this.networkRequest);
      return;
    }
    catch (IOException localIOException)
    {
      return;
    }
    this.storeRequest = localIOException.put(stripBody(this.userResponse));
  }
  
  private Request networkRequest(Request paramRequest)
    throws IOException
  {
    Request.Builder localBuilder = paramRequest.newBuilder();
    if (paramRequest.header("Host") == null) {
      localBuilder.header("Host", Util.hostHeader(paramRequest.httpUrl()));
    }
    if (paramRequest.header("Connection") == null) {
      localBuilder.header("Connection", "Keep-Alive");
    }
    if (paramRequest.header("Accept-Encoding") == null)
    {
      this.transparentGzip = true;
      localBuilder.header("Accept-Encoding", "gzip");
    }
    CookieHandler localCookieHandler = this.client.getCookieHandler();
    if (localCookieHandler != null)
    {
      Map localMap = OkHeaders.toMultimap(localBuilder.build().headers(), null);
      OkHeaders.addCookies(localBuilder, localCookieHandler.get(paramRequest.uri(), localMap));
    }
    if (paramRequest.header("User-Agent") == null) {
      localBuilder.header("User-Agent", Version.userAgent());
    }
    return localBuilder.build();
  }
  
  private Response readNetworkResponse()
    throws IOException
  {
    this.transport.finishRequest();
    Response localResponse2 = this.transport.readResponseHeaders().request(this.networkRequest).handshake(this.connection.getHandshake()).header(OkHeaders.SENT_MILLIS, Long.toString(this.sentRequestMillis)).header(OkHeaders.RECEIVED_MILLIS, Long.toString(System.currentTimeMillis())).build();
    Response localResponse1 = localResponse2;
    if (!this.forWebSocket) {
      localResponse1 = localResponse2.newBuilder().body(this.transport.openResponseBody(localResponse2)).build();
    }
    return localResponse1;
  }
  
  private static Response stripBody(Response paramResponse)
  {
    Response localResponse = paramResponse;
    if (paramResponse != null)
    {
      localResponse = paramResponse;
      if (paramResponse.body() != null) {
        localResponse = paramResponse.newBuilder().body(null).build();
      }
    }
    return localResponse;
  }
  
  private Response unzip(Response paramResponse)
    throws IOException
  {
    if ((!this.transparentGzip) || (!"gzip".equalsIgnoreCase(this.userResponse.header("Content-Encoding")))) {}
    while (paramResponse.body() == null) {
      return paramResponse;
    }
    GzipSource localGzipSource = new GzipSource(paramResponse.body().source());
    Headers localHeaders = paramResponse.headers().newBuilder().removeAll("Content-Encoding").removeAll("Content-Length").build();
    return paramResponse.newBuilder().headers(localHeaders).body(new RealResponseBody(localHeaders, Okio.buffer(localGzipSource))).build();
  }
  
  private static boolean validate(Response paramResponse1, Response paramResponse2)
  {
    if (paramResponse2.code() == 304) {}
    do
    {
      return true;
      paramResponse1 = paramResponse1.headers().getDate("Last-Modified");
      if (paramResponse1 == null) {
        break;
      }
      paramResponse2 = paramResponse2.headers().getDate("Last-Modified");
    } while ((paramResponse2 != null) && (paramResponse2.getTime() < paramResponse1.getTime()));
    return false;
  }
  
  public final Connection close()
  {
    if (this.bufferedRequestBody != null) {
      Util.closeQuietly(this.bufferedRequestBody);
    }
    while (this.userResponse == null)
    {
      if (this.connection != null) {
        Util.closeQuietly(this.connection.getSocket());
      }
      this.connection = null;
      return null;
      if (this.requestBodyOut != null) {
        Util.closeQuietly(this.requestBodyOut);
      }
    }
    Util.closeQuietly(this.userResponse.body());
    if ((this.transport != null) && (this.connection != null) && (!this.transport.canReuseConnection()))
    {
      Util.closeQuietly(this.connection.getSocket());
      this.connection = null;
      return null;
    }
    if ((this.connection != null) && (!Internal.instance.clearOwner(this.connection))) {
      this.connection = null;
    }
    Connection localConnection = this.connection;
    this.connection = null;
    return localConnection;
  }
  
  public final void disconnect()
  {
    try
    {
      if (this.transport != null)
      {
        this.transport.disconnect(this);
        return;
      }
      Connection localConnection = this.connection;
      if (localConnection != null)
      {
        Internal.instance.closeIfOwnedBy(localConnection, this);
        return;
      }
    }
    catch (IOException localIOException) {}
  }
  
  public final Request followUpRequest()
    throws IOException
  {
    if (this.userResponse == null) {
      throw new IllegalStateException();
    }
    if (getRoute() != null) {}
    for (Object localObject = getRoute().getProxy();; localObject = this.client.getProxy()) {
      switch (this.userResponse.code())
      {
      default: 
        return null;
      }
    }
    if (((Proxy)localObject).type() != Proxy.Type.HTTP) {
      throw new ProtocolException("Received HTTP_PROXY_AUTH (407) code while not using proxy");
    }
    return OkHeaders.processAuthHeader(this.client.getAuthenticator(), this.userResponse, (Proxy)localObject);
    if ((!this.userRequest.method().equals("GET")) && (!this.userRequest.method().equals("HEAD"))) {
      return null;
    }
    if (!this.client.getFollowRedirects()) {
      return null;
    }
    localObject = this.userResponse.header("Location");
    if (localObject == null) {
      return null;
    }
    localObject = this.userRequest.httpUrl().resolve((String)localObject);
    if (localObject == null) {
      return null;
    }
    if ((!((HttpUrl)localObject).scheme().equals(this.userRequest.httpUrl().scheme())) && (!this.client.getFollowSslRedirects())) {
      return null;
    }
    Request.Builder localBuilder = this.userRequest.newBuilder();
    if (HttpMethod.permitsRequestBody(this.userRequest.method()))
    {
      localBuilder.method("GET", null);
      localBuilder.removeHeader("Transfer-Encoding");
      localBuilder.removeHeader("Content-Length");
      localBuilder.removeHeader("Content-Type");
    }
    if (!sameConnection((HttpUrl)localObject)) {
      localBuilder.removeHeader("Authorization");
    }
    return localBuilder.url((HttpUrl)localObject).build();
  }
  
  public final BufferedSink getBufferedRequestBody()
  {
    Object localObject = this.bufferedRequestBody;
    if (localObject != null) {
      return (BufferedSink)localObject;
    }
    localObject = getRequestBody();
    if (localObject != null)
    {
      localObject = Okio.buffer((Sink)localObject);
      this.bufferedRequestBody = ((BufferedSink)localObject);
      return (BufferedSink)localObject;
    }
    return null;
  }
  
  public final Connection getConnection()
  {
    return this.connection;
  }
  
  public final Request getRequest()
  {
    return this.userRequest;
  }
  
  public final Sink getRequestBody()
  {
    if (this.cacheStrategy == null) {
      throw new IllegalStateException();
    }
    return this.requestBodyOut;
  }
  
  public final Response getResponse()
  {
    if (this.userResponse == null) {
      throw new IllegalStateException();
    }
    return this.userResponse;
  }
  
  public final Route getRoute()
  {
    return this.route;
  }
  
  public final boolean hasResponse()
  {
    return this.userResponse != null;
  }
  
  final boolean permitsRequestBody(Request paramRequest)
  {
    return HttpMethod.permitsRequestBody(paramRequest.method());
  }
  
  public final void readResponse()
    throws IOException
  {
    if (this.userResponse != null) {}
    label423:
    label425:
    label435:
    do
    {
      do
      {
        return;
        if ((this.networkRequest == null) && (this.cacheResponse == null)) {
          throw new IllegalStateException("call sendRequest() first!");
        }
      } while (this.networkRequest == null);
      if (this.forWebSocket) {
        this.transport.writeRequestHeaders(this.networkRequest);
      }
      Object localObject;
      for (;;)
      {
        for (localObject = readNetworkResponse();; localObject = new NetworkInterceptorChain(0, this.networkRequest).proceed(this.networkRequest))
        {
          receiveHeaders(((Response)localObject).headers());
          if (this.cacheResponse == null) {
            break label435;
          }
          if (!validate(this.cacheResponse, (Response)localObject)) {
            break label425;
          }
          this.userResponse = this.cacheResponse.newBuilder().request(this.userRequest).priorResponse(stripBody(this.priorResponse)).headers(combine(this.cacheResponse.headers(), ((Response)localObject).headers())).cacheResponse(stripBody(this.cacheResponse)).networkResponse(stripBody((Response)localObject)).build();
          ((Response)localObject).body().close();
          releaseConnection();
          localObject = Internal.instance.internalCache(this.client);
          ((InternalCache)localObject).trackConditionalCacheHit();
          ((InternalCache)localObject).update(this.cacheResponse, stripBody(this.userResponse));
          this.userResponse = unzip(this.userResponse);
          return;
          if (this.callerWritesRequestBody) {
            break;
          }
        }
        if ((this.bufferedRequestBody != null) && (this.bufferedRequestBody.buffer().size() > 0L)) {
          this.bufferedRequestBody.emit();
        }
        if (this.sentRequestMillis == -1L)
        {
          if ((OkHeaders.contentLength(this.networkRequest) == -1L) && ((this.requestBodyOut instanceof RetryableSink)))
          {
            long l = ((RetryableSink)this.requestBodyOut).contentLength();
            this.networkRequest = this.networkRequest.newBuilder().header("Content-Length", Long.toString(l)).build();
          }
          this.transport.writeRequestHeaders(this.networkRequest);
        }
        if (this.requestBodyOut != null)
        {
          if (this.bufferedRequestBody != null) {
            this.bufferedRequestBody.close();
          }
          for (;;)
          {
            if (!(this.requestBodyOut instanceof RetryableSink)) {
              break label423;
            }
            this.transport.writeRequestBody((RetryableSink)this.requestBodyOut);
            break;
            this.requestBodyOut.close();
          }
        }
      }
      Util.closeQuietly(this.cacheResponse.body());
      this.userResponse = ((Response)localObject).newBuilder().request(this.userRequest).priorResponse(stripBody(this.priorResponse)).cacheResponse(stripBody(this.cacheResponse)).networkResponse(stripBody((Response)localObject)).build();
    } while (!hasBody(this.userResponse));
    maybeCache();
    this.userResponse = unzip(cacheWritingResponse(this.storeRequest, this.userResponse));
  }
  
  public final void receiveHeaders(Headers paramHeaders)
    throws IOException
  {
    CookieHandler localCookieHandler = this.client.getCookieHandler();
    if (localCookieHandler != null) {
      localCookieHandler.put(this.userRequest.uri(), OkHeaders.toMultimap(paramHeaders, null));
    }
  }
  
  public final HttpEngine recover(RouteException paramRouteException)
  {
    if ((this.routeSelector != null) && (this.connection != null)) {
      connectFailed(this.routeSelector, paramRouteException.getLastConnectException());
    }
    if (((this.routeSelector == null) && (this.connection == null)) || ((this.routeSelector != null) && (!this.routeSelector.hasNext())) || (!isRecoverable(paramRouteException))) {
      return null;
    }
    paramRouteException = close();
    return new HttpEngine(this.client, this.userRequest, this.bufferRequestBody, this.callerWritesRequestBody, this.forWebSocket, paramRouteException, this.routeSelector, (RetryableSink)this.requestBodyOut, this.priorResponse);
  }
  
  public final HttpEngine recover(IOException paramIOException)
  {
    return recover(paramIOException, this.requestBodyOut);
  }
  
  public final HttpEngine recover(IOException paramIOException, Sink paramSink)
  {
    if ((this.routeSelector != null) && (this.connection != null)) {
      connectFailed(this.routeSelector, paramIOException);
    }
    if ((paramSink == null) || ((paramSink instanceof RetryableSink))) {}
    for (int i = 1; ((this.routeSelector == null) && (this.connection == null)) || ((this.routeSelector != null) && (!this.routeSelector.hasNext())) || (!isRecoverable(paramIOException)) || (i == 0); i = 0) {
      return null;
    }
    paramIOException = close();
    return new HttpEngine(this.client, this.userRequest, this.bufferRequestBody, this.callerWritesRequestBody, this.forWebSocket, paramIOException, this.routeSelector, (RetryableSink)paramSink, this.priorResponse);
  }
  
  public final void releaseConnection()
    throws IOException
  {
    if ((this.transport != null) && (this.connection != null)) {
      this.transport.releaseConnectionOnIdle();
    }
    this.connection = null;
  }
  
  public final boolean sameConnection(HttpUrl paramHttpUrl)
  {
    HttpUrl localHttpUrl = this.userRequest.httpUrl();
    return (localHttpUrl.host().equals(paramHttpUrl.host())) && (localHttpUrl.port() == paramHttpUrl.port()) && (localHttpUrl.scheme().equals(paramHttpUrl.scheme()));
  }
  
  public final void sendRequest()
    throws RequestException, RouteException, IOException
  {
    if (this.cacheStrategy != null) {
      return;
    }
    if (this.transport != null) {
      throw new IllegalStateException();
    }
    Request localRequest = networkRequest(this.userRequest);
    InternalCache localInternalCache = Internal.instance.internalCache(this.client);
    if (localInternalCache != null) {}
    long l;
    for (Response localResponse = localInternalCache.get(localRequest);; localResponse = null)
    {
      this.cacheStrategy = new CacheStrategy.Factory(System.currentTimeMillis(), localRequest, localResponse).get();
      this.networkRequest = this.cacheStrategy.networkRequest;
      this.cacheResponse = this.cacheStrategy.cacheResponse;
      if (localInternalCache != null) {
        localInternalCache.trackResponse(this.cacheStrategy);
      }
      if ((localResponse != null) && (this.cacheResponse == null)) {
        Util.closeQuietly(localResponse.body());
      }
      if (this.networkRequest == null) {
        break label310;
      }
      if (this.connection == null) {
        connect();
      }
      this.transport = Internal.instance.newTransport(this.connection, this);
      if ((!this.callerWritesRequestBody) || (!permitsRequestBody(this.networkRequest)) || (this.requestBodyOut != null)) {
        break;
      }
      l = OkHeaders.contentLength(localRequest);
      if (!this.bufferRequestBody) {
        break label278;
      }
      if (l <= 2147483647L) {
        break label231;
      }
      throw new IllegalStateException("Use setFixedLengthStreamingMode() or setChunkedStreamingMode() for requests larger than 2 GiB.");
    }
    label231:
    if (l != -1L)
    {
      this.transport.writeRequestHeaders(this.networkRequest);
      this.requestBodyOut = new RetryableSink((int)l);
      return;
    }
    this.requestBodyOut = new RetryableSink();
    return;
    label278:
    this.transport.writeRequestHeaders(this.networkRequest);
    this.requestBodyOut = this.transport.createRequestBody(this.networkRequest, l);
    return;
    label310:
    if (this.connection != null)
    {
      Internal.instance.recycle(this.client.getConnectionPool(), this.connection);
      this.connection = null;
    }
    if (this.cacheResponse != null) {}
    for (this.userResponse = this.cacheResponse.newBuilder().request(this.userRequest).priorResponse(stripBody(this.priorResponse)).cacheResponse(stripBody(this.cacheResponse)).build();; this.userResponse = new Response.Builder().request(this.userRequest).priorResponse(stripBody(this.priorResponse)).protocol(Protocol.HTTP_1_1).code(504).message("Unsatisfiable Request (only-if-cached)").body(EMPTY_BODY).build())
    {
      this.userResponse = unzip(this.userResponse);
      return;
    }
  }
  
  public final void writingRequestHeaders()
  {
    if (this.sentRequestMillis != -1L) {
      throw new IllegalStateException();
    }
    this.sentRequestMillis = System.currentTimeMillis();
  }
  
  class NetworkInterceptorChain
    implements Interceptor.Chain
  {
    private int calls;
    private final int index;
    private final Request request;
    
    NetworkInterceptorChain(int paramInt, Request paramRequest)
    {
      this.index = paramInt;
      this.request = paramRequest;
    }
    
    public Connection connection()
    {
      return HttpEngine.this.connection;
    }
    
    public Response proceed(Request paramRequest)
      throws IOException
    {
      this.calls += 1;
      Object localObject1;
      Object localObject2;
      if (this.index > 0)
      {
        localObject1 = (Interceptor)HttpEngine.this.client.networkInterceptors().get(this.index - 1);
        localObject2 = connection().getRoute().getAddress();
        if ((!paramRequest.httpUrl().host().equals(((Address)localObject2).getUriHost())) || (paramRequest.httpUrl().port() != ((Address)localObject2).getUriPort())) {
          throw new IllegalStateException("network interceptor " + localObject1 + " must retain the same host and port");
        }
        if (this.calls > 1) {
          throw new IllegalStateException("network interceptor " + localObject1 + " must call proceed() exactly once");
        }
      }
      if (this.index < HttpEngine.this.client.networkInterceptors().size())
      {
        localObject1 = new NetworkInterceptorChain(HttpEngine.this, this.index + 1, paramRequest);
        localObject2 = (Interceptor)HttpEngine.this.client.networkInterceptors().get(this.index);
        paramRequest = ((Interceptor)localObject2).intercept((Interceptor.Chain)localObject1);
        if (((NetworkInterceptorChain)localObject1).calls != 1) {
          throw new IllegalStateException("network interceptor " + localObject2 + " must call proceed() exactly once");
        }
        if (paramRequest == null) {
          throw new NullPointerException("network interceptor " + localObject2 + " returned null");
        }
      }
      int i;
      do
      {
        do
        {
          return paramRequest;
          HttpEngine.this.transport.writeRequestHeaders(paramRequest);
          HttpEngine.access$202(HttpEngine.this, paramRequest);
          if ((HttpEngine.this.permitsRequestBody(paramRequest)) && (paramRequest.body() != null))
          {
            localObject1 = Okio.buffer(HttpEngine.this.transport.createRequestBody(paramRequest, paramRequest.body().contentLength()));
            paramRequest.body().writeTo((BufferedSink)localObject1);
            ((BufferedSink)localObject1).close();
          }
          localObject1 = HttpEngine.this.readNetworkResponse();
          i = ((Response)localObject1).code();
          if (i == 204) {
            break;
          }
          paramRequest = (Request)localObject1;
        } while (i != 205);
        paramRequest = (Request)localObject1;
      } while (((Response)localObject1).body().contentLength() <= 0L);
      throw new ProtocolException("HTTP " + i + " had non-zero Content-Length: " + ((Response)localObject1).body().contentLength());
    }
    
    public Request request()
    {
      return this.request;
    }
  }
}


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