1 /* 2 * Copyright 2010, The Android Open Source Project 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "WebRequest.h" 28 29 #include "JNIUtility.h" 30 #include "MainThread.h" 31 #include "UrlInterceptResponse.h" 32 #include "WebCoreFrameBridge.h" 33 #include "WebCoreJni.h" 34 #include "WebRequestContext.h" 35 #include "WebResourceRequest.h" 36 #include "WebUrlLoaderClient.h" 37 #include "jni.h" 38 39 #include <cutils/log.h> 40 #include <openssl/x509.h> 41 #include <string> 42 #include <androidfw/AssetManager.h> 43 44 extern android::AssetManager* globalAssetManager(); 45 46 // TODO: 47 // - Finish the file upload. Testcase is mobile buzz 48 // - Add network throttle needed by Android plugins 49 50 // TODO: Turn off asserts crashing before release 51 // http://b/issue?id=2951985 52 #undef ASSERT 53 #define ASSERT(assertion, ...) do \ 54 if (!(assertion)) { \ 55 android_printLog(ANDROID_LOG_ERROR, __FILE__, __VA_ARGS__); \ 56 } \ 57 while (0) 58 59 namespace android { 60 61 namespace { 62 const int kInitialReadBufSize = 32768; 63 const char* kXRequestedWithHeader = "X-Requested-With"; 64 65 struct RequestPackageName { 66 std::string value; 67 RequestPackageName(); 68 }; 69 70 RequestPackageName::RequestPackageName() { 71 JNIEnv* env = JSC::Bindings::getJNIEnv(); 72 jclass bridgeClass = env->FindClass("android/webkit/JniUtil"); 73 jmethodID method = env->GetStaticMethodID(bridgeClass, "getPackageName", "()Ljava/lang/String;"); 74 value = jstringToStdString(env, static_cast<jstring>(env->CallStaticObjectMethod(bridgeClass, method))); 75 env->DeleteLocalRef(bridgeClass); 76 } 77 78 base::LazyInstance<RequestPackageName> s_packageName(base::LINKER_INITIALIZED); 79 80 } 81 82 WebRequest::WebRequest(WebUrlLoaderClient* loader, const WebResourceRequest& webResourceRequest) 83 : m_urlLoader(loader) 84 , m_url(webResourceRequest.url()) 85 , m_userAgent(webResourceRequest.userAgent()) 86 , m_loadState(Created) 87 , m_authRequestCount(0) 88 , m_cacheMode(0) 89 , m_runnableFactory(this) 90 , m_wantToPause(false) 91 , m_isPaused(false) 92 , m_isSync(false) 93 { 94 GURL gurl(m_url); 95 96 m_request = new net::URLRequest(gurl, this); 97 98 m_request->SetExtraRequestHeaders(webResourceRequest.requestHeaders()); 99 m_request->SetExtraRequestHeaderByName(kXRequestedWithHeader, s_packageName.Get().value, false); 100 m_request->set_referrer(webResourceRequest.referrer()); 101 m_request->set_method(webResourceRequest.method()); 102 m_request->set_load_flags(webResourceRequest.loadFlags()); 103 } 104 105 // This is a special URL for Android. Query the Java InputStream 106 // for data and send to WebCore 107 WebRequest::WebRequest(WebUrlLoaderClient* loader, const WebResourceRequest& webResourceRequest, UrlInterceptResponse* intercept) 108 : m_urlLoader(loader) 109 , m_interceptResponse(intercept) 110 , m_url(webResourceRequest.url()) 111 , m_userAgent(webResourceRequest.userAgent()) 112 , m_loadState(Created) 113 , m_authRequestCount(0) 114 , m_cacheMode(0) 115 , m_runnableFactory(this) 116 , m_wantToPause(false) 117 , m_isPaused(false) 118 , m_isSync(false) 119 { 120 } 121 122 WebRequest::~WebRequest() 123 { 124 ASSERT(m_loadState == Finished, "dtor called on a WebRequest in a different state than finished (%d)", m_loadState); 125 126 m_loadState = Deleted; 127 } 128 129 const std::string& WebRequest::getUrl() const 130 { 131 return m_url; 132 } 133 134 const std::string& WebRequest::getUserAgent() const 135 { 136 return m_userAgent; 137 } 138 139 #ifdef LOG_REQUESTS 140 namespace { 141 int remaining = 0; 142 } 143 #endif 144 145 void WebRequest::finish(bool success) 146 { 147 m_runnableFactory.RevokeAll(); 148 ASSERT(m_loadState < Finished, "(%p) called finish on an already finished WebRequest (%d) (%s)", this, m_loadState, m_url.c_str()); 149 if (m_loadState >= Finished) 150 return; 151 #ifdef LOG_REQUESTS 152 time_t finish; 153 time(&finish); 154 finish = finish - m_startTime; 155 struct tm * timeinfo; 156 char buffer[80]; 157 timeinfo = localtime(&finish); 158 strftime(buffer, 80, "Time: %M:%S",timeinfo); 159 android_printLog(ANDROID_LOG_DEBUG, "KM", "(%p) finish (%d) (%s) (%d) (%s)", this, --remaining, buffer, success, m_url.c_str()); 160 #endif 161 162 // Make sure WebUrlLoaderClient doesn't delete us in the middle of this method. 163 scoped_refptr<WebRequest> guard(this); 164 165 m_loadState = Finished; 166 if (success) { 167 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 168 m_urlLoader.get(), &WebUrlLoaderClient::didFinishLoading)); 169 } else { 170 if (m_interceptResponse == NULL) { 171 OwnPtr<WebResponse> webResponse(new WebResponse(m_request.get())); 172 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 173 m_urlLoader.get(), &WebUrlLoaderClient::didFail, webResponse.release())); 174 } else { 175 OwnPtr<WebResponse> webResponse(new WebResponse(m_url, m_interceptResponse->mimeType(), 0, 176 m_interceptResponse->encoding(), m_interceptResponse->status())); 177 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 178 m_urlLoader.get(), &WebUrlLoaderClient::didFail, webResponse.release())); 179 } 180 } 181 m_networkBuffer = 0; 182 m_request = 0; 183 m_urlLoader = 0; 184 } 185 186 void WebRequest::appendFileToUpload(const std::string& filename) 187 { 188 // AppendFileToUpload is only valid before calling start 189 ASSERT(m_loadState == Created, "appendFileToUpload called on a WebRequest not in CREATED state: (%s)", m_url.c_str()); 190 FilePath filePath(filename); 191 m_request->AppendFileToUpload(filePath); 192 } 193 194 void WebRequest::appendBytesToUpload(WTF::Vector<char>* data) 195 { 196 // AppendBytesToUpload is only valid before calling start 197 ASSERT(m_loadState == Created, "appendBytesToUpload called on a WebRequest not in CREATED state: (%s)", m_url.c_str()); 198 m_request->AppendBytesToUpload(data->data(), data->size()); 199 delete data; 200 } 201 202 void WebRequest::setRequestContext(WebRequestContext* context) 203 { 204 m_cacheMode = context->getCacheMode(); 205 if (m_request) 206 m_request->set_context(context); 207 } 208 209 void WebRequest::updateLoadFlags(int& loadFlags) 210 { 211 if (m_cacheMode == 1) { // LOAD_CACHE_ELSE_NETWORK 212 loadFlags |= net::LOAD_PREFERRING_CACHE; 213 loadFlags &= ~net::LOAD_VALIDATE_CACHE; 214 } 215 if (m_cacheMode == 2) // LOAD_NO_CACHE 216 loadFlags |= net::LOAD_BYPASS_CACHE; 217 if (m_cacheMode == 3) // LOAD_CACHE_ONLY 218 loadFlags |= net::LOAD_ONLY_FROM_CACHE; 219 220 if (m_isSync) 221 loadFlags |= net::LOAD_IGNORE_LIMITS; 222 } 223 224 void WebRequest::start() 225 { 226 ASSERT(m_loadState == Created, "Start called on a WebRequest not in CREATED state: (%s)", m_url.c_str()); 227 #ifdef LOG_REQUESTS 228 android_printLog(ANDROID_LOG_DEBUG, "KM", "(%p) start (%d) (%s)", this, ++remaining, m_url.c_str()); 229 time(&m_startTime); 230 #endif 231 232 m_loadState = Started; 233 234 if (m_interceptResponse != NULL) 235 return handleInterceptedURL(); 236 237 // Handle data urls before we send it off to the http stack 238 if (m_request->url().SchemeIs("data")) 239 return handleDataURL(m_request->url()); 240 241 if (m_request->url().SchemeIs("browser")) 242 return handleBrowserURL(m_request->url()); 243 244 // Update load flags with settings from WebSettings 245 int loadFlags = m_request->load_flags(); 246 updateLoadFlags(loadFlags); 247 m_request->set_load_flags(loadFlags); 248 249 m_request->Start(); 250 } 251 252 void WebRequest::cancel() 253 { 254 ASSERT(m_loadState >= Started, "Cancel called on a not started WebRequest: (%s)", m_url.c_str()); 255 ASSERT(m_loadState != Cancelled, "Cancel called on an already cancelled WebRequest: (%s)", m_url.c_str()); 256 257 // There is a possible race condition between the IO thread finishing the request and 258 // the WebCore thread cancelling it. If the request has already finished, do 259 // nothing to avoid sending duplicate finish messages to WebCore. 260 if (m_loadState > Cancelled) { 261 return; 262 } 263 ASSERT(m_request, "Request set to 0 before it is finished"); 264 265 m_loadState = Cancelled; 266 267 m_request->Cancel(); 268 finish(true); 269 } 270 271 void WebRequest::pauseLoad(bool pause) 272 { 273 ASSERT(m_loadState >= GotData, "PauseLoad in state other than RESPONSE and GOTDATA"); 274 if (pause) { 275 if (!m_isPaused) 276 m_wantToPause = true; 277 } else { 278 m_wantToPause = false; 279 if (m_isPaused) { 280 m_isPaused = false; 281 MessageLoop::current()->PostTask(FROM_HERE, m_runnableFactory.NewRunnableMethod(&WebRequest::startReading)); 282 } 283 } 284 } 285 286 void WebRequest::handleInterceptedURL() 287 { 288 m_loadState = Response; 289 290 const std::string& mime = m_interceptResponse->mimeType(); 291 // Get the MIME type from the URL. "text/html" is a last resort, hopefully overridden. 292 std::string mimeType("text/html"); 293 if (mime == "") { 294 // Get the MIME type from the file extension, if any. 295 FilePath path(m_url); 296 net::GetMimeTypeFromFile(path, &mimeType); 297 } else { 298 // Set from the intercept response. 299 mimeType = mime; 300 } 301 302 303 OwnPtr<WebResponse> webResponse(new WebResponse(m_url, mimeType, 0, m_interceptResponse->encoding(), m_interceptResponse->status())); 304 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 305 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveResponse, webResponse.release())); 306 307 do { 308 // data is deleted in WebUrlLoaderClient::didReceiveAndroidFileData 309 // data is sent to the webcore thread 310 OwnPtr<std::vector<char> > data(new std::vector<char>); 311 data->reserve(kInitialReadBufSize); 312 313 // Read returns false on error and size of 0 on eof. 314 if (!m_interceptResponse->readStream(data.get()) || data->size() == 0) 315 break; 316 317 m_loadState = GotData; 318 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 319 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveAndroidFileData, data.release())); 320 } while (true); 321 322 finish(m_interceptResponse->status() == 200); 323 } 324 325 void WebRequest::handleDataURL(GURL url) 326 { 327 OwnPtr<std::string> data(new std::string); 328 std::string mimeType; 329 std::string charset; 330 331 if (net::DataURL::Parse(url, &mimeType, &charset, data.get())) { 332 // PopulateURLResponse from chrome implementation 333 // weburlloader_impl.cc 334 m_loadState = Response; 335 OwnPtr<WebResponse> webResponse(new WebResponse(url.spec(), mimeType, data->size(), charset, 200)); 336 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 337 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveResponse, webResponse.release())); 338 339 if (!data->empty()) { 340 m_loadState = GotData; 341 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 342 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveDataUrl, data.release())); 343 } 344 } else { 345 // handle the failed case 346 } 347 348 finish(true); 349 } 350 351 void WebRequest::handleBrowserURL(GURL url) 352 { 353 std::string data("data:text/html;charset=utf-8,"); 354 if (url.spec() == "browser:incognito") { 355 AssetManager* assetManager = globalAssetManager(); 356 Asset* asset = assetManager->open("webkit/incognito_mode_start_page.html", Asset::ACCESS_BUFFER); 357 if (asset) { 358 data.append((const char*)asset->getBuffer(false), asset->getLength()); 359 delete asset; 360 } 361 } 362 GURL dataURL(data.c_str()); 363 handleDataURL(dataURL); 364 } 365 366 // Called upon a server-initiated redirect. The delegate may call the 367 // request's Cancel method to prevent the redirect from being followed. 368 // Since there may be multiple chained redirects, there may also be more 369 // than one redirect call. 370 // 371 // When this function is called, the request will still contain the 372 // original URL, the destination of the redirect is provided in 'new_url'. 373 // If the delegate does not cancel the request and |*defer_redirect| is 374 // false, then the redirect will be followed, and the request's URL will be 375 // changed to the new URL. Otherwise if the delegate does not cancel the 376 // request and |*defer_redirect| is true, then the redirect will be 377 // followed once FollowDeferredRedirect is called on the URLRequest. 378 // 379 // The caller must set |*defer_redirect| to false, so that delegates do not 380 // need to set it if they are happy with the default behavior of not 381 // deferring redirect. 382 void WebRequest::OnReceivedRedirect(net::URLRequest* newRequest, const GURL& newUrl, bool* deferRedirect) 383 { 384 ASSERT(m_loadState < Response, "Redirect after receiving response"); 385 ASSERT(newRequest && newRequest->status().is_success(), "Invalid redirect"); 386 387 OwnPtr<WebResponse> webResponse(new WebResponse(newRequest)); 388 webResponse->setUrl(newUrl.spec()); 389 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 390 m_urlLoader.get(), &WebUrlLoaderClient::willSendRequest, webResponse.release())); 391 392 // Defer the redirect until followDeferredRedirect() is called. 393 *deferRedirect = true; 394 } 395 396 // Called when we receive an authentication failure. The delegate should 397 // call request->SetAuth() with the user's credentials once it obtains them, 398 // or request->CancelAuth() to cancel the login and display the error page. 399 // When it does so, the request will be reissued, restarting the sequence 400 // of On* callbacks. 401 void WebRequest::OnAuthRequired(net::URLRequest* request, net::AuthChallengeInfo* authInfo) 402 { 403 ASSERT(m_loadState == Started, "OnAuthRequired called on a WebRequest not in STARTED state (state=%d)", m_loadState); 404 405 scoped_refptr<net::AuthChallengeInfo> authInfoPtr(authInfo); 406 bool firstTime = (m_authRequestCount == 0); 407 ++m_authRequestCount; 408 409 bool suppressDialog = (request->load_flags() & net::LOAD_DO_NOT_PROMPT_FOR_LOGIN); 410 411 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 412 m_urlLoader.get(), &WebUrlLoaderClient::authRequired, authInfoPtr, firstTime, suppressDialog)); 413 } 414 415 // Called when we received an SSL certificate error. The delegate will provide 416 // the user the options to proceed, cancel, or view certificates. 417 void WebRequest::OnSSLCertificateError(net::URLRequest* request, int cert_error, net::X509Certificate* cert) 418 { 419 scoped_refptr<net::X509Certificate> scoped_cert = cert; 420 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 421 m_urlLoader.get(), &WebUrlLoaderClient::reportSslCertError, cert_error, scoped_cert)); 422 } 423 424 void WebRequest::OnCertificateRequested(net::URLRequest* request, net::SSLCertRequestInfo* cert_request_info) 425 { 426 scoped_refptr<net::SSLCertRequestInfo> scoped_cert_request_info = cert_request_info; 427 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 428 m_urlLoader.get(), &WebUrlLoaderClient::requestClientCert, scoped_cert_request_info)); 429 } 430 431 432 // After calling Start(), the delegate will receive an OnResponseStarted 433 // callback when the request has completed. If an error occurred, the 434 // request->status() will be set. On success, all redirects have been 435 // followed and the final response is beginning to arrive. At this point, 436 // meta data about the response is available, including for example HTTP 437 // response headers if this is a request for a HTTP resource. 438 void WebRequest::OnResponseStarted(net::URLRequest* request) 439 { 440 ASSERT(m_loadState == Started, "Got response after receiving response"); 441 442 m_loadState = Response; 443 if (request && request->status().is_success()) { 444 OwnPtr<WebResponse> webResponse(new WebResponse(request)); 445 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 446 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveResponse, webResponse.release())); 447 448 // Start reading the response 449 startReading(); 450 } else { 451 finish(false); 452 } 453 } 454 455 void WebRequest::setAuth(const string16& username, const string16& password) 456 { 457 ASSERT(m_loadState == Started, "setAuth called on a WebRequest not in STARTED state (state=%d)", m_loadState); 458 459 m_request->SetAuth(username, password); 460 } 461 462 void WebRequest::cancelAuth() 463 { 464 ASSERT(m_loadState == Started, "cancelAuth called on a WebRequest not in STARTED state (state=%d)", m_loadState); 465 466 m_request->CancelAuth(); 467 } 468 469 void WebRequest::followDeferredRedirect() 470 { 471 ASSERT(m_loadState < Response, "Redirect after receiving response"); 472 473 m_request->FollowDeferredRedirect(); 474 } 475 476 void WebRequest::proceedSslCertError() 477 { 478 m_request->ContinueDespiteLastError(); 479 } 480 481 void WebRequest::cancelSslCertError(int cert_error) 482 { 483 m_request->SimulateError(cert_error); 484 } 485 486 void WebRequest::sslClientCert(EVP_PKEY* pkey, scoped_refptr<net::X509Certificate> chain) 487 { 488 base::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> privateKey(pkey); 489 if (privateKey.get() == NULL || chain.get() == NULL) { 490 m_request->ContinueWithCertificate(NULL); 491 return; 492 } 493 GURL gurl(m_url); 494 net::OpenSSLPrivateKeyStore::GetInstance()->StorePrivateKey(gurl, privateKey.release()); 495 m_request->ContinueWithCertificate(chain.release()); 496 } 497 498 void WebRequest::startReading() 499 { 500 ASSERT(m_networkBuffer == 0, "startReading called with a nonzero buffer"); 501 ASSERT(m_isPaused == 0, "startReading called in paused state"); 502 ASSERT(m_loadState == Response || m_loadState == GotData, "StartReading in state other than RESPONSE and GOTDATA"); 503 if (m_loadState > GotData) // We have been cancelled between reads 504 return; 505 506 if (m_wantToPause) { 507 m_isPaused = true; 508 return; 509 } 510 511 int bytesRead = 0; 512 513 if (!read(&bytesRead)) { 514 if (m_request && m_request->status().is_io_pending()) 515 return; // Wait for OnReadCompleted() 516 return finish(false); 517 } 518 519 // bytesRead == 0 indicates finished 520 if (!bytesRead) 521 return finish(true); 522 523 m_loadState = GotData; 524 // Read ok, forward buffer to webcore 525 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod(m_urlLoader.get(), &WebUrlLoaderClient::didReceiveData, m_networkBuffer, bytesRead)); 526 m_networkBuffer = 0; 527 MessageLoop::current()->PostTask(FROM_HERE, m_runnableFactory.NewRunnableMethod(&WebRequest::startReading)); 528 } 529 530 bool WebRequest::read(int* bytesRead) 531 { 532 ASSERT(m_loadState == Response || m_loadState == GotData, "read in state other than RESPONSE and GOTDATA"); 533 ASSERT(m_networkBuffer == 0, "Read called with a nonzero buffer"); 534 535 // TODO: when asserts work, check that the buffer is 0 here 536 m_networkBuffer = new net::IOBuffer(kInitialReadBufSize); 537 return m_request->Read(m_networkBuffer, kInitialReadBufSize, bytesRead); 538 } 539 540 // This is called when there is data available 541 542 // Called when the a Read of the response body is completed after an 543 // IO_PENDING status from a Read() call. 544 // The data read is filled into the buffer which the caller passed 545 // to Read() previously. 546 // 547 // If an error occurred, request->status() will contain the error, 548 // and bytes read will be -1. 549 void WebRequest::OnReadCompleted(net::URLRequest* request, int bytesRead) 550 { 551 ASSERT(m_loadState == Response || m_loadState == GotData, "OnReadCompleted in state other than RESPONSE and GOTDATA"); 552 553 if (request->status().is_success()) { 554 m_loadState = GotData; 555 m_urlLoader->maybeCallOnMainThread(NewRunnableMethod( 556 m_urlLoader.get(), &WebUrlLoaderClient::didReceiveData, m_networkBuffer, bytesRead)); 557 m_networkBuffer = 0; 558 559 // Get the rest of the data 560 startReading(); 561 } else { 562 finish(false); 563 } 564 } 565 566 } // namespace android 567