HttpConnection.java
19 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
package com.squareup.okhttp.internal.http;
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.Protocol;
import com.squareup.okhttp.Response.Builder;
import com.squareup.okhttp.internal.Internal;
import com.squareup.okhttp.internal.Util;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeUnit;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.ForwardingTimeout;
import okio.Okio;
import okio.Sink;
import okio.Source;
import okio.Timeout;
public final class HttpConnection
{
private static final int ON_IDLE_CLOSE = 2;
private static final int ON_IDLE_HOLD = 0;
private static final int ON_IDLE_POOL = 1;
private static final int STATE_CLOSED = 6;
private static final int STATE_IDLE = 0;
private static final int STATE_OPEN_REQUEST_BODY = 1;
private static final int STATE_OPEN_RESPONSE_BODY = 4;
private static final int STATE_READING_RESPONSE_BODY = 5;
private static final int STATE_READ_RESPONSE_HEADERS = 3;
private static final int STATE_WRITING_REQUEST_BODY = 2;
private final Connection connection;
private int onIdle = 0;
private final ConnectionPool pool;
private final BufferedSink sink;
private final Socket socket;
private final BufferedSource source;
private int state = 0;
public HttpConnection(ConnectionPool paramConnectionPool, Connection paramConnection, Socket paramSocket)
throws IOException
{
this.pool = paramConnectionPool;
this.connection = paramConnection;
this.socket = paramSocket;
this.source = Okio.buffer(Okio.source(paramSocket));
this.sink = Okio.buffer(Okio.sink(paramSocket));
}
private void detachTimeout(ForwardingTimeout paramForwardingTimeout)
{
Timeout localTimeout = paramForwardingTimeout.delegate();
paramForwardingTimeout.setDelegate(Timeout.NONE);
localTimeout.clearDeadline();
localTimeout.clearTimeout();
}
public final long bufferSize()
{
return this.source.buffer().size();
}
public final void closeIfOwnedBy(Object paramObject)
throws IOException
{
Internal.instance.closeIfOwnedBy(this.connection, paramObject);
}
public final void closeOnIdle()
throws IOException
{
this.onIdle = 2;
if (this.state == 0)
{
this.state = 6;
this.connection.getSocket().close();
}
}
public final void flush()
throws IOException
{
this.sink.flush();
}
public final boolean isClosed()
{
return this.state == 6;
}
public final boolean isReadable()
{
try
{
int i = this.socket.getSoTimeout();
try
{
this.socket.setSoTimeout(1);
boolean bool = this.source.exhausted();
return !bool;
}
finally
{
this.socket.setSoTimeout(i);
}
return false;
}
catch (SocketTimeoutException localSocketTimeoutException)
{
return true;
}
catch (IOException localIOException) {}
}
public final Sink newChunkedSink()
{
if (this.state != 1) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 2;
return new ChunkedSink(null);
}
public final Source newChunkedSource(HttpEngine paramHttpEngine)
throws IOException
{
if (this.state != 4) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 5;
return new ChunkedSource(paramHttpEngine);
}
public final Sink newFixedLengthSink(long paramLong)
{
if (this.state != 1) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 2;
return new FixedLengthSink(paramLong, null);
}
public final Source newFixedLengthSource(long paramLong)
throws IOException
{
if (this.state != 4) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 5;
return new FixedLengthSource(paramLong);
}
public final Source newUnknownLengthSource()
throws IOException
{
if (this.state != 4) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 5;
return new UnknownLengthSource(null);
}
public final void poolOnIdle()
{
this.onIdle = 1;
if (this.state == 0)
{
this.onIdle = 0;
Internal.instance.recycle(this.pool, this.connection);
}
}
public final BufferedSink rawSink()
{
return this.sink;
}
public final BufferedSource rawSource()
{
return this.source;
}
public final void readHeaders(Headers.Builder paramBuilder)
throws IOException
{
for (;;)
{
String str = this.source.readUtf8LineStrict();
if (str.length() == 0) {
break;
}
Internal.instance.addLenient(paramBuilder, str);
}
}
public final Response.Builder readResponse()
throws IOException
{
if ((this.state != 1) && (this.state != 3)) {
throw new IllegalStateException("state: " + this.state);
}
try
{
StatusLine localStatusLine;
do
{
localStatusLine = StatusLine.parse(this.source.readUtf8LineStrict());
localObject = new Response.Builder().protocol(localStatusLine.protocol).code(localStatusLine.code).message(localStatusLine.message);
Headers.Builder localBuilder = new Headers.Builder();
readHeaders(localBuilder);
localBuilder.add(OkHeaders.SELECTED_PROTOCOL, localStatusLine.protocol.toString());
((Response.Builder)localObject).headers(localBuilder.build());
} while (localStatusLine.code == 100);
this.state = 4;
return (Response.Builder)localObject;
}
catch (EOFException localEOFException)
{
Object localObject = new IOException("unexpected end of stream on " + this.connection + " (recycle count=" + Internal.instance.recycleCount(this.connection) + ")");
((IOException)localObject).initCause(localEOFException);
throw ((Throwable)localObject);
}
}
public final void setTimeouts(int paramInt1, int paramInt2)
{
if (paramInt1 != 0) {
this.source.timeout().timeout(paramInt1, TimeUnit.MILLISECONDS);
}
if (paramInt2 != 0) {
this.sink.timeout().timeout(paramInt2, TimeUnit.MILLISECONDS);
}
}
public final void writeRequest(Headers paramHeaders, String paramString)
throws IOException
{
if (this.state != 0) {
throw new IllegalStateException("state: " + this.state);
}
this.sink.writeUtf8(paramString).writeUtf8("\r\n");
int i = 0;
int j = paramHeaders.size();
while (i < j)
{
this.sink.writeUtf8(paramHeaders.name(i)).writeUtf8(": ").writeUtf8(paramHeaders.value(i)).writeUtf8("\r\n");
i += 1;
}
this.sink.writeUtf8("\r\n");
this.state = 1;
}
public final void writeRequestBody(RetryableSink paramRetryableSink)
throws IOException
{
if (this.state != 1) {
throw new IllegalStateException("state: " + this.state);
}
this.state = 3;
paramRetryableSink.writeToSocket(this.sink);
}
abstract class AbstractSource
implements Source
{
protected boolean closed;
protected final ForwardingTimeout timeout = new ForwardingTimeout(HttpConnection.this.source.timeout());
private AbstractSource() {}
protected final void endOfInput(boolean paramBoolean)
throws IOException
{
if (HttpConnection.this.state != 5) {
throw new IllegalStateException("state: " + HttpConnection.this.state);
}
HttpConnection.this.detachTimeout(this.timeout);
HttpConnection.access$502(HttpConnection.this, 0);
if ((paramBoolean) && (HttpConnection.this.onIdle == 1))
{
HttpConnection.access$702(HttpConnection.this, 0);
Internal.instance.recycle(HttpConnection.this.pool, HttpConnection.this.connection);
}
while (HttpConnection.this.onIdle != 2) {
return;
}
HttpConnection.access$502(HttpConnection.this, 6);
HttpConnection.this.connection.getSocket().close();
}
public Timeout timeout()
{
return this.timeout;
}
protected final void unexpectedEndOfInput()
{
Util.closeQuietly(HttpConnection.this.connection.getSocket());
HttpConnection.access$502(HttpConnection.this, 6);
}
}
final class ChunkedSink
implements Sink
{
private boolean closed;
private final ForwardingTimeout timeout = new ForwardingTimeout(HttpConnection.this.sink.timeout());
private ChunkedSink() {}
/* Error */
public final void close()
throws IOException
{
// Byte code:
// 0: aload_0
// 1: monitorenter
// 2: aload_0
// 3: getfield 47 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:closed Z
// 6: istore_1
// 7: iload_1
// 8: ifeq +6 -> 14
// 11: aload_0
// 12: monitorexit
// 13: return
// 14: aload_0
// 15: iconst_1
// 16: putfield 47 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:closed Z
// 19: aload_0
// 20: getfield 19 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:this$0 Lcom/squareup/okhttp/internal/http/HttpConnection;
// 23: invokestatic 28 com/squareup/okhttp/internal/http/HttpConnection:access$300 (Lcom/squareup/okhttp/internal/http/HttpConnection;)Lokio/BufferedSink;
// 26: ldc 49
// 28: invokeinterface 53 2 0
// 33: pop
// 34: aload_0
// 35: getfield 19 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:this$0 Lcom/squareup/okhttp/internal/http/HttpConnection;
// 38: aload_0
// 39: getfield 38 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:timeout Lokio/ForwardingTimeout;
// 42: invokestatic 57 com/squareup/okhttp/internal/http/HttpConnection:access$400 (Lcom/squareup/okhttp/internal/http/HttpConnection;Lokio/ForwardingTimeout;)V
// 45: aload_0
// 46: getfield 19 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:this$0 Lcom/squareup/okhttp/internal/http/HttpConnection;
// 49: iconst_3
// 50: invokestatic 61 com/squareup/okhttp/internal/http/HttpConnection:access$502 (Lcom/squareup/okhttp/internal/http/HttpConnection;I)I
// 53: pop
// 54: goto -43 -> 11
// 57: astore_2
// 58: aload_0
// 59: monitorexit
// 60: aload_2
// 61: athrow
// Local variable table:
// start length slot name signature
// 0 62 0 this ChunkedSink
// 6 2 1 bool boolean
// 57 4 2 localObject Object
// Exception table:
// from to target type
// 2 7 57 finally
// 14 54 57 finally
}
/* Error */
public final void flush()
throws IOException
{
// Byte code:
// 0: aload_0
// 1: monitorenter
// 2: aload_0
// 3: getfield 47 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:closed Z
// 6: istore_1
// 7: iload_1
// 8: ifeq +6 -> 14
// 11: aload_0
// 12: monitorexit
// 13: return
// 14: aload_0
// 15: getfield 19 com/squareup/okhttp/internal/http/HttpConnection$ChunkedSink:this$0 Lcom/squareup/okhttp/internal/http/HttpConnection;
// 18: invokestatic 28 com/squareup/okhttp/internal/http/HttpConnection:access$300 (Lcom/squareup/okhttp/internal/http/HttpConnection;)Lokio/BufferedSink;
// 21: invokeinterface 65 1 0
// 26: goto -15 -> 11
// 29: astore_2
// 30: aload_0
// 31: monitorexit
// 32: aload_2
// 33: athrow
// Local variable table:
// start length slot name signature
// 0 34 0 this ChunkedSink
// 6 2 1 bool boolean
// 29 4 2 localObject Object
// Exception table:
// from to target type
// 2 7 29 finally
// 14 26 29 finally
}
public final Timeout timeout()
{
return this.timeout;
}
public final void write(Buffer paramBuffer, long paramLong)
throws IOException
{
if (this.closed) {
throw new IllegalStateException("closed");
}
if (paramLong == 0L) {
return;
}
HttpConnection.this.sink.writeHexadecimalUnsignedLong(paramLong);
HttpConnection.this.sink.writeUtf8("\r\n");
HttpConnection.this.sink.write(paramBuffer, paramLong);
HttpConnection.this.sink.writeUtf8("\r\n");
}
}
class ChunkedSource
extends HttpConnection.AbstractSource
{
private static final long NO_CHUNK_YET = -1L;
private long bytesRemainingInChunk = -1L;
private boolean hasMoreChunks = true;
private final HttpEngine httpEngine;
ChunkedSource(HttpEngine paramHttpEngine)
throws IOException
{
super(null);
this.httpEngine = paramHttpEngine;
}
private void readChunkSize()
throws IOException
{
if (this.bytesRemainingInChunk != -1L) {
HttpConnection.this.source.readUtf8LineStrict();
}
try
{
this.bytesRemainingInChunk = HttpConnection.this.source.readHexadecimalUnsignedLong();
String str = HttpConnection.this.source.readUtf8LineStrict().trim();
if ((this.bytesRemainingInChunk < 0L) || ((!str.isEmpty()) && (!str.startsWith(";")))) {
throw new ProtocolException("expected chunk size and optional extensions but was \"" + this.bytesRemainingInChunk + str + "\"");
}
}
catch (NumberFormatException localNumberFormatException)
{
throw new ProtocolException(localNumberFormatException.getMessage());
}
if (this.bytesRemainingInChunk == 0L)
{
this.hasMoreChunks = false;
Headers.Builder localBuilder = new Headers.Builder();
HttpConnection.this.readHeaders(localBuilder);
this.httpEngine.receiveHeaders(localBuilder.build());
endOfInput(true);
}
}
public void close()
throws IOException
{
if (this.closed) {
return;
}
if ((this.hasMoreChunks) && (!Util.discard(this, 100, TimeUnit.MILLISECONDS))) {
unexpectedEndOfInput();
}
this.closed = true;
}
public long read(Buffer paramBuffer, long paramLong)
throws IOException
{
if (paramLong < 0L) {
throw new IllegalArgumentException("byteCount < 0: " + paramLong);
}
if (this.closed) {
throw new IllegalStateException("closed");
}
if (!this.hasMoreChunks) {}
do
{
return -1L;
if ((this.bytesRemainingInChunk != 0L) && (this.bytesRemainingInChunk != -1L)) {
break;
}
readChunkSize();
} while (!this.hasMoreChunks);
paramLong = HttpConnection.this.source.read(paramBuffer, Math.min(paramLong, this.bytesRemainingInChunk));
if (paramLong == -1L)
{
unexpectedEndOfInput();
throw new ProtocolException("unexpected end of stream");
}
this.bytesRemainingInChunk -= paramLong;
return paramLong;
}
}
final class FixedLengthSink
implements Sink
{
private long bytesRemaining;
private boolean closed;
private final ForwardingTimeout timeout = new ForwardingTimeout(HttpConnection.this.sink.timeout());
private FixedLengthSink(long paramLong)
{
this.bytesRemaining = paramLong;
}
public final void close()
throws IOException
{
if (this.closed) {
return;
}
this.closed = true;
if (this.bytesRemaining > 0L) {
throw new ProtocolException("unexpected end of stream");
}
HttpConnection.this.detachTimeout(this.timeout);
HttpConnection.access$502(HttpConnection.this, 3);
}
public final void flush()
throws IOException
{
if (this.closed) {
return;
}
HttpConnection.this.sink.flush();
}
public final Timeout timeout()
{
return this.timeout;
}
public final void write(Buffer paramBuffer, long paramLong)
throws IOException
{
if (this.closed) {
throw new IllegalStateException("closed");
}
Util.checkOffsetAndCount(paramBuffer.size(), 0L, paramLong);
if (paramLong > this.bytesRemaining) {
throw new ProtocolException("expected " + this.bytesRemaining + " bytes but received " + paramLong);
}
HttpConnection.this.sink.write(paramBuffer, paramLong);
this.bytesRemaining -= paramLong;
}
}
class FixedLengthSource
extends HttpConnection.AbstractSource
{
private long bytesRemaining;
public FixedLengthSource(long paramLong)
throws IOException
{
super(null);
this.bytesRemaining = paramLong;
if (this.bytesRemaining == 0L) {
endOfInput(true);
}
}
public void close()
throws IOException
{
if (this.closed) {
return;
}
if ((this.bytesRemaining != 0L) && (!Util.discard(this, 100, TimeUnit.MILLISECONDS))) {
unexpectedEndOfInput();
}
this.closed = true;
}
public long read(Buffer paramBuffer, long paramLong)
throws IOException
{
if (paramLong < 0L) {
throw new IllegalArgumentException("byteCount < 0: " + paramLong);
}
if (this.closed) {
throw new IllegalStateException("closed");
}
if (this.bytesRemaining == 0L) {
return -1L;
}
paramLong = HttpConnection.this.source.read(paramBuffer, Math.min(this.bytesRemaining, paramLong));
if (paramLong == -1L)
{
unexpectedEndOfInput();
throw new ProtocolException("unexpected end of stream");
}
this.bytesRemaining -= paramLong;
if (this.bytesRemaining == 0L) {
endOfInput(true);
}
return paramLong;
}
}
class UnknownLengthSource
extends HttpConnection.AbstractSource
{
private boolean inputExhausted;
private UnknownLengthSource()
{
super(null);
}
public void close()
throws IOException
{
if (this.closed) {
return;
}
if (!this.inputExhausted) {
unexpectedEndOfInput();
}
this.closed = true;
}
public long read(Buffer paramBuffer, long paramLong)
throws IOException
{
if (paramLong < 0L) {
throw new IllegalArgumentException("byteCount < 0: " + paramLong);
}
if (this.closed) {
throw new IllegalStateException("closed");
}
if (this.inputExhausted) {
return -1L;
}
paramLong = HttpConnection.this.source.read(paramBuffer, paramLong);
if (paramLong == -1L)
{
this.inputExhausted = true;
endOfInput(false);
return -1L;
}
return paramLong;
}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/squareup/okhttp/internal/http/HttpConnection.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/