CacheStrategy.java
8.48 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
package com.squareup.okhttp.internal.http;
import com.squareup.okhttp.CacheControl;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Request.Builder;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Response.Builder;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public final class CacheStrategy
{
public final Response cacheResponse;
public final Request networkRequest;
private CacheStrategy(Request paramRequest, Response paramResponse)
{
this.networkRequest = paramRequest;
this.cacheResponse = paramResponse;
}
public static boolean isCacheable(Response paramResponse, Request paramRequest)
{
switch (paramResponse.code())
{
}
do
{
return false;
} while (((paramResponse.header("Expires") == null) && (paramResponse.cacheControl().maxAgeSeconds() == -1) && (!paramResponse.cacheControl().isPublic()) && (!paramResponse.cacheControl().isPrivate())) || (paramResponse.cacheControl().noStore()) || (paramRequest.cacheControl().noStore()));
return true;
}
public static class Factory
{
private int ageSeconds = -1;
final Response cacheResponse;
private String etag;
private Date expires;
private Date lastModified;
private String lastModifiedString;
final long nowMillis;
private long receivedResponseMillis;
final Request request;
private long sentRequestMillis;
private Date servedDate;
private String servedDateString;
public Factory(long paramLong, Request paramRequest, Response paramResponse)
{
this.nowMillis = paramLong;
this.request = paramRequest;
this.cacheResponse = paramResponse;
if (paramResponse != null)
{
paramRequest = paramResponse.headers();
int i = 0;
int j = paramRequest.size();
if (i < j)
{
paramResponse = paramRequest.name(i);
String str = paramRequest.value(i);
if ("Date".equalsIgnoreCase(paramResponse))
{
this.servedDate = HttpDate.parse(str);
this.servedDateString = str;
}
for (;;)
{
i += 1;
break;
if ("Expires".equalsIgnoreCase(paramResponse))
{
this.expires = HttpDate.parse(str);
}
else if ("Last-Modified".equalsIgnoreCase(paramResponse))
{
this.lastModified = HttpDate.parse(str);
this.lastModifiedString = str;
}
else if ("ETag".equalsIgnoreCase(paramResponse))
{
this.etag = str;
}
else if ("Age".equalsIgnoreCase(paramResponse))
{
this.ageSeconds = HeaderParser.parseSeconds(str, -1);
}
else if (OkHeaders.SENT_MILLIS.equalsIgnoreCase(paramResponse))
{
this.sentRequestMillis = Long.parseLong(str);
}
else if (OkHeaders.RECEIVED_MILLIS.equalsIgnoreCase(paramResponse))
{
this.receivedResponseMillis = Long.parseLong(str);
}
}
}
}
}
private long cacheResponseAge()
{
long l1 = 0L;
if (this.servedDate != null) {
l1 = Math.max(0L, this.receivedResponseMillis - this.servedDate.getTime());
}
long l2 = l1;
if (this.ageSeconds != -1) {
l2 = Math.max(l1, TimeUnit.SECONDS.toMillis(this.ageSeconds));
}
return l2 + (this.receivedResponseMillis - this.sentRequestMillis) + (this.nowMillis - this.receivedResponseMillis);
}
private long computeFreshnessLifetime()
{
CacheControl localCacheControl = this.cacheResponse.cacheControl();
long l1;
if (localCacheControl.maxAgeSeconds() != -1)
{
l1 = TimeUnit.SECONDS.toMillis(localCacheControl.maxAgeSeconds());
return l1;
}
if (this.expires != null)
{
if (this.servedDate != null) {}
for (l1 = this.servedDate.getTime();; l1 = this.receivedResponseMillis)
{
long l2 = this.expires.getTime() - l1;
l1 = l2;
if (l2 > 0L) {
break;
}
return 0L;
}
}
if ((this.lastModified != null) && (this.cacheResponse.request().httpUrl().query() == null))
{
if (this.servedDate != null) {}
for (l1 = this.servedDate.getTime();; l1 = this.sentRequestMillis)
{
l1 -= this.lastModified.getTime();
if (l1 <= 0L) {
break;
}
return l1 / 10L;
}
return 0L;
}
return 0L;
}
private CacheStrategy getCandidate()
{
long l4 = 0L;
if (this.cacheResponse == null) {
return new CacheStrategy(this.request, null, null);
}
if ((this.request.isHttps()) && (this.cacheResponse.handshake() == null)) {
return new CacheStrategy(this.request, null, null);
}
if (!CacheStrategy.isCacheable(this.cacheResponse, this.request)) {
return new CacheStrategy(this.request, null, null);
}
Object localObject = this.request.cacheControl();
if ((((CacheControl)localObject).noCache()) || (hasConditions(this.request))) {
return new CacheStrategy(this.request, null, null);
}
long l5 = cacheResponseAge();
long l2 = computeFreshnessLifetime();
long l1 = l2;
if (((CacheControl)localObject).maxAgeSeconds() != -1) {
l1 = Math.min(l2, TimeUnit.SECONDS.toMillis(((CacheControl)localObject).maxAgeSeconds()));
}
if (((CacheControl)localObject).minFreshSeconds() != -1) {}
for (l2 = TimeUnit.SECONDS.toMillis(((CacheControl)localObject).minFreshSeconds());; l2 = 0L)
{
CacheControl localCacheControl = this.cacheResponse.cacheControl();
long l3 = l4;
if (!localCacheControl.mustRevalidate())
{
l3 = l4;
if (((CacheControl)localObject).maxStaleSeconds() != -1) {
l3 = TimeUnit.SECONDS.toMillis(((CacheControl)localObject).maxStaleSeconds());
}
}
if ((!localCacheControl.noCache()) && (l5 + l2 < l3 + l1))
{
localObject = this.cacheResponse.newBuilder();
if (l2 + l5 >= l1) {
((Response.Builder)localObject).addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
}
if ((l5 > 86400000L) && (isFreshnessLifetimeHeuristic())) {
((Response.Builder)localObject).addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
}
return new CacheStrategy(null, ((Response.Builder)localObject).build(), null);
}
localObject = this.request.newBuilder();
if (this.etag != null) {
((Request.Builder)localObject).header("If-None-Match", this.etag);
}
for (;;)
{
localObject = ((Request.Builder)localObject).build();
if (!hasConditions((Request)localObject)) {
break;
}
return new CacheStrategy((Request)localObject, this.cacheResponse, null);
if (this.lastModified != null) {
((Request.Builder)localObject).header("If-Modified-Since", this.lastModifiedString);
} else if (this.servedDate != null) {
((Request.Builder)localObject).header("If-Modified-Since", this.servedDateString);
}
}
return new CacheStrategy((Request)localObject, null, null);
}
}
private static boolean hasConditions(Request paramRequest)
{
return (paramRequest.header("If-Modified-Since") != null) || (paramRequest.header("If-None-Match") != null);
}
private boolean isFreshnessLifetimeHeuristic()
{
return (this.cacheResponse.cacheControl().maxAgeSeconds() == -1) && (this.expires == null);
}
public CacheStrategy get()
{
CacheStrategy localCacheStrategy2 = getCandidate();
CacheStrategy localCacheStrategy1 = localCacheStrategy2;
if (localCacheStrategy2.networkRequest != null)
{
localCacheStrategy1 = localCacheStrategy2;
if (this.request.cacheControl().onlyIfCached()) {
localCacheStrategy1 = new CacheStrategy(null, null, null);
}
}
return localCacheStrategy1;
}
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/com/squareup/okhttp/internal/http/CacheStrategy.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/