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
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.webkit;
import android.net.http.EventHandler;
import android.net.http.RequestHandle;
import android.util.Log;
import android.webkit.CacheManager.CacheResult;
import java.util.HashMap;
import java.util.Map;
class FrameLoader {
private final LoadListener mListener;
private final String mMethod;
private final WebSettings mSettings;
private Map<String, String> mHeaders;
private byte[] mPostData;
private Network mNetwork;
private int mCacheMode;
private String mReferrer;
private String mContentType;
private static final int URI_PROTOCOL = 0x100;
private static final String CONTENT_TYPE = "content-type";
// Contents of an about:blank page
private static final String mAboutBlank =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EB\">" +
"<html><head><title>about:blank</title></head><body></body></html>";
static final String HEADER_STR = "text/xml, text/html, " +
"application/xhtml+xml, image/png, text/plain, */*;q=0.8";
private static final String LOGTAG = "webkit";
FrameLoader(LoadListener listener, WebSettings settings,
String method) {
mListener = listener;
mHeaders = null;
mMethod = method;
mCacheMode = WebSettings.LOAD_NORMAL;
mSettings = settings;
}
public void setReferrer(String ref) {
// only set referrer for http or https
if (URLUtil.isNetworkUrl(ref)) mReferrer = ref;
}
public void setPostData(byte[] postData) {
mPostData = postData;
}
public void setContentTypeForPost(String postContentType) {
mContentType = postContentType;
}
public void setCacheMode(int cacheMode) {
mCacheMode = cacheMode;
}
public void setHeaders(HashMap headers) {
mHeaders = headers;
}
public LoadListener getLoadListener() {
return mListener;
}
/**
* Issues the load request.
*
* Return value does not indicate if the load was successful or not. It
* simply indicates that the load request is reasonable.
*
* @return true if the load is reasonable.
*/
public boolean executeLoad() {
String url = mListener.url();
if (URLUtil.isNetworkUrl(url)){
if (mSettings.getBlockNetworkLoads()) {
mListener.error(EventHandler.ERROR_BAD_URL,
mListener.getContext().getString(
com.android.internal.R.string.httpErrorBadUrl));
return false;
}
// Make sure the host part of the url is correctly
// encoded before sending the request
if (!URLUtil.verifyURLEncoding(mListener.host())) {
mListener.error(EventHandler.ERROR_BAD_URL,
mListener.getContext().getString(
com.android.internal.R.string.httpErrorBadUrl));
return false;
}
mNetwork = Network.getInstance(mListener.getContext());
if (mListener.isSynchronous()) {
return handleHTTPLoad();
}
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_HTTPLOADER, this).sendToTarget();
return true;
} else if (handleLocalFile(url, mListener, mSettings)) {
return true;
}
if (DebugFlags.FRAME_LOADER) {
Log.v(LOGTAG, "FrameLoader.executeLoad: url protocol not supported:"
+ mListener.url());
}
mListener.error(EventHandler.ERROR_UNSUPPORTED_SCHEME,
mListener.getContext().getText(
com.android.internal.R.string.httpErrorUnsupportedScheme).toString());
return false;
}
/* package */
static boolean handleLocalFile(String url, LoadListener loadListener,
WebSettings settings) {
// Attempt to decode the percent-encoded url before passing to the
// local loaders.
try {
url = new String(URLUtil.decode(url.getBytes()));
} catch (IllegalArgumentException e) {
loadListener.error(EventHandler.ERROR_BAD_URL,
loadListener.getContext().getString(
com.android.internal.R.string.httpErrorBadUrl));
// Return true here so we do not trigger an unsupported scheme
// error.
return true;
}
if (URLUtil.isAssetUrl(url)) {
if (loadListener.isSynchronous()) {
new FileLoader(url, loadListener, FileLoader.TYPE_ASSET,
true).load();
} else {
// load asset in a separate thread as it involves IO
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_STREAMLOADER,
new FileLoader(url, loadListener, FileLoader.TYPE_ASSET,
true)).sendToTarget();
}
return true;
} else if (URLUtil.isResourceUrl(url)) {
if (loadListener.isSynchronous()) {
new FileLoader(url, loadListener, FileLoader.TYPE_RES,
true).load();
} else {
// load resource in a separate thread as it involves IO
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_STREAMLOADER,
new FileLoader(url, loadListener, FileLoader.TYPE_RES,
true)).sendToTarget();
}
return true;
} else if (URLUtil.isFileUrl(url)) {
if (loadListener.isSynchronous()) {
new FileLoader(url, loadListener, FileLoader.TYPE_FILE,
settings.getAllowFileAccess()).load();
} else {
// load file in a separate thread as it involves IO
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_STREAMLOADER,
new FileLoader(url, loadListener, FileLoader.TYPE_FILE,
settings.getAllowFileAccess())).sendToTarget();
}
return true;
} else if (URLUtil.isContentUrl(url)) {
// Send the raw url to the ContentLoader because it will do a
// permission check and the url has to match.
if (loadListener.isSynchronous()) {
new ContentLoader(loadListener.url(), loadListener).load();
} else {
// load content in a separate thread as it involves IO
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_STREAMLOADER,
new ContentLoader(loadListener.url(), loadListener))
.sendToTarget();
}
return true;
} else if (URLUtil.isDataUrl(url)) {
// load data in the current thread to reduce the latency
new DataLoader(url, loadListener).load();
return true;
} else if (URLUtil.isAboutUrl(url)) {
loadListener.data(mAboutBlank.getBytes(), mAboutBlank.length());
loadListener.endData();
return true;
}
return false;
}
boolean handleHTTPLoad() {
if (mHeaders == null) {
mHeaders = new HashMap<String, String>();
}
populateStaticHeaders();
populateHeaders();
// response was handled by Cache, don't issue HTTP request
if (handleCache()) {
// push the request data down to the LoadListener
// as response from the cache could be a redirect
// and we may need to initiate a network request if the cache
// can't satisfy redirect URL
mListener.setRequestData(mMethod, mHeaders, mPostData);
return true;
}
if (DebugFlags.FRAME_LOADER) {
Log.v(LOGTAG, "FrameLoader: http " + mMethod + " load for: "
+ mListener.url());
}
boolean ret = false;
int error = EventHandler.ERROR_UNSUPPORTED_SCHEME;
try {
ret = mNetwork.requestURL(mMethod, mHeaders,
mPostData, mListener);
} catch (android.net.ParseException ex) {
error = EventHandler.ERROR_BAD_URL;
} catch (java.lang.RuntimeException ex) {
/* probably an empty header set by javascript. We want
the same result as bad URL */
error = EventHandler.ERROR_BAD_URL;
}
if (!ret) {
mListener.error(error, mListener.getContext().getText(
EventHandler.errorStringResources[Math.abs(error)]).toString());
return false;
}
return true;
}
/*
* This function is used by handleCache to
* setup a load from the byte stream in a CacheResult.
*/
private void startCacheLoad(CacheResult result) {
if (DebugFlags.FRAME_LOADER) {
Log.v(LOGTAG, "FrameLoader: loading from cache: "
+ mListener.url());
}
// Tell the Listener respond with the cache file
CacheLoader cacheLoader =
new CacheLoader(mListener, result);
mListener.setCacheLoader(cacheLoader);
if (mListener.isSynchronous()) {
cacheLoader.load();
} else {
// Load the cached file in a separate thread
WebViewWorker.getHandler().obtainMessage(
WebViewWorker.MSG_ADD_STREAMLOADER, cacheLoader).sendToTarget();
}
}
/*
* This function is used by the handleHTTPLoad to setup the cache headers
* correctly.
* Returns true if the response was handled from the cache
*/
private boolean handleCache() {
switch (mCacheMode) {
// This mode is normally used for a reload, it instructs the http
// loader to not use the cached content.
case WebSettings.LOAD_NO_CACHE:
break;
// This mode is used when the content should only be loaded from
// the cache. If it is not there, then fail the load. This is used
// to load POST content in a history navigation.
case WebSettings.LOAD_CACHE_ONLY: {
CacheResult result = CacheManager.getCacheFile(mListener.url(),
mListener.postIdentifier(), null);
if (result != null) {
startCacheLoad(result);
} else {
// This happens if WebCore was first told that the POST
// response was in the cache, then when we try to use it
// it has gone.
// Generate a file not found error
int err = EventHandler.FILE_NOT_FOUND_ERROR;
mListener.error(err, mListener.getContext().getText(
EventHandler.errorStringResources[Math.abs(err)])
.toString());
}
return true;
}
// This mode is for when the user is doing a history navigation
// in the browser and should returned cached content regardless
// of it's state. If it is not in the cache, then go to the
// network.
case WebSettings.LOAD_CACHE_ELSE_NETWORK: {
if (DebugFlags.FRAME_LOADER) {
Log.v(LOGTAG, "FrameLoader: checking cache: "
+ mListener.url());
}
// Get the cache file name for the current URL, passing null for
// the validation headers causes no validation to occur
CacheResult result = CacheManager.getCacheFile(mListener.url(),
mListener.postIdentifier(), null);
if (result != null) {
startCacheLoad(result);
return true;
}
break;
}
// This is the default case, which is to check to see if the
// content in the cache can be used. If it can be used, then
// use it. If it needs revalidation then the relevant headers
// are added to the request.
default:
case WebSettings.LOAD_NORMAL:
return mListener.checkCache(mHeaders);
}// end of switch
return false;
}
/**
* Add the static headers that don't change with each request.
*/
private void populateStaticHeaders() {
// Accept header should already be there as they are built by WebCore,
// but in the case they are missing, add some.
String accept = mHeaders.get("Accept");
if (accept == null || accept.length() == 0) {
mHeaders.put("Accept", HEADER_STR);
}
mHeaders.put("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7");
String acceptLanguage = mSettings.getAcceptLanguage();
if (acceptLanguage.length() > 0) {
mHeaders.put("Accept-Language", acceptLanguage);
}
mHeaders.put("User-Agent", mSettings.getUserAgentString());
}
/**
* Add the content related headers. These headers contain user private data
* and is not used when we are proxying an untrusted request.
*/
private void populateHeaders() {
if (mReferrer != null) mHeaders.put("Referer", mReferrer);
if (mContentType != null) mHeaders.put(CONTENT_TYPE, mContentType);
// if we have an active proxy and have proxy credentials, do pre-emptive
// authentication to avoid an extra round-trip:
if (mNetwork.isValidProxySet()) {
String username;
String password;
/* The proxy credentials can be set in the Network thread */
synchronized (mNetwork) {
username = mNetwork.getProxyUsername();
password = mNetwork.getProxyPassword();
}
if (username != null && password != null) {
// we collect credentials ONLY if the proxy scheme is BASIC!!!
String proxyHeader = RequestHandle.authorizationHeader(true);
mHeaders.put(proxyHeader,
"Basic " + RequestHandle.computeBasicAuthResponse(
username, password));
}
}
// Set cookie header
String cookie = CookieManager.getInstance().getCookie(
mListener.getWebAddress());
if (cookie != null && cookie.length() > 0) {
mHeaders.put("Cookie", cookie);
}
}
}
|