| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <time.h> | 11 #include <time.h> |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <memory> |
| 13 #include "webrtc/base/asyncsocket.h" | 14 #include "webrtc/base/asyncsocket.h" |
| 14 #include "webrtc/base/common.h" | 15 #include "webrtc/base/common.h" |
| 15 #include "webrtc/base/diskcache.h" | 16 #include "webrtc/base/diskcache.h" |
| 16 #include "webrtc/base/httpclient.h" | 17 #include "webrtc/base/httpclient.h" |
| 17 #include "webrtc/base/httpcommon-inl.h" | 18 #include "webrtc/base/httpcommon-inl.h" |
| 18 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 19 #include "webrtc/base/pathutils.h" | 20 #include "webrtc/base/pathutils.h" |
| 20 #include "webrtc/base/scoped_ptr.h" | |
| 21 #include "webrtc/base/socketstream.h" | 21 #include "webrtc/base/socketstream.h" |
| 22 #include "webrtc/base/stringencode.h" | 22 #include "webrtc/base/stringencode.h" |
| 23 #include "webrtc/base/stringutils.h" | 23 #include "webrtc/base/stringutils.h" |
| 24 #include "webrtc/base/thread.h" | 24 #include "webrtc/base/thread.h" |
| 25 | 25 |
| 26 namespace rtc { | 26 namespace rtc { |
| 27 | 27 |
| 28 ////////////////////////////////////////////////////////////////////// | 28 ////////////////////////////////////////////////////////////////////// |
| 29 // Helpers | 29 // Helpers |
| 30 ////////////////////////////////////////////////////////////////////// | 30 ////////////////////////////////////////////////////////////////////// |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 CacheLock lock(cache_, id, true); | 459 CacheLock lock(cache_, id, true); |
| 460 if (!lock.IsLocked()) { | 460 if (!lock.IsLocked()) { |
| 461 LOG_F(LS_WARNING) << "Couldn't lock cache"; | 461 LOG_F(LS_WARNING) << "Couldn't lock cache"; |
| 462 return false; | 462 return false; |
| 463 } | 463 } |
| 464 | 464 |
| 465 if (HE_NONE != WriteCacheHeaders(id)) { | 465 if (HE_NONE != WriteCacheHeaders(id)) { |
| 466 return false; | 466 return false; |
| 467 } | 467 } |
| 468 | 468 |
| 469 scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheBody)); | 469 std::unique_ptr<StreamInterface> stream( |
| 470 cache_->WriteResource(id, kCacheBody)); |
| 470 if (!stream) { | 471 if (!stream) { |
| 471 LOG_F(LS_ERROR) << "Couldn't open body cache"; | 472 LOG_F(LS_ERROR) << "Couldn't open body cache"; |
| 472 return false; | 473 return false; |
| 473 } | 474 } |
| 474 lock.Commit(); | 475 lock.Commit(); |
| 475 | 476 |
| 476 // Let's secretly replace the response document with Folgers Crystals, | 477 // Let's secretly replace the response document with Folgers Crystals, |
| 477 // er, StreamTap, so that we can mirror the data to our cache. | 478 // er, StreamTap, so that we can mirror the data to our cache. |
| 478 StreamInterface* output = response().document.release(); | 479 StreamInterface* output = response().document.release(); |
| 479 if (!output) { | 480 if (!output) { |
| 480 output = new NullStream; | 481 output = new NullStream; |
| 481 } | 482 } |
| 482 StreamTap* tap = new StreamTap(output, stream.release()); | 483 StreamTap* tap = new StreamTap(output, stream.release()); |
| 483 response().document.reset(tap); | 484 response().document.reset(tap); |
| 484 return true; | 485 return true; |
| 485 } | 486 } |
| 486 | 487 |
| 487 HttpError HttpClient::WriteCacheHeaders(const std::string& id) { | 488 HttpError HttpClient::WriteCacheHeaders(const std::string& id) { |
| 488 scoped_ptr<StreamInterface> stream(cache_->WriteResource(id, kCacheHeader)); | 489 std::unique_ptr<StreamInterface> stream( |
| 490 cache_->WriteResource(id, kCacheHeader)); |
| 489 if (!stream) { | 491 if (!stream) { |
| 490 LOG_F(LS_ERROR) << "Couldn't open header cache"; | 492 LOG_F(LS_ERROR) << "Couldn't open header cache"; |
| 491 return HE_CACHE; | 493 return HE_CACHE; |
| 492 } | 494 } |
| 493 | 495 |
| 494 if (!HttpWriteCacheHeaders(&transaction_->response, stream.get(), NULL)) { | 496 if (!HttpWriteCacheHeaders(&transaction_->response, stream.get(), NULL)) { |
| 495 LOG_F(LS_ERROR) << "Couldn't write header cache"; | 497 LOG_F(LS_ERROR) << "Couldn't write header cache"; |
| 496 return HE_CACHE; | 498 return HE_CACHE; |
| 497 } | 499 } |
| 498 | 500 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 LOG_F(LS_WARNING) << "Cache failure, continuing with normal request"; | 558 LOG_F(LS_WARNING) << "Cache failure, continuing with normal request"; |
| 557 response().clear(false); | 559 response().clear(false); |
| 558 return false; | 560 return false; |
| 559 } | 561 } |
| 560 | 562 |
| 561 SignalHttpClientComplete(this, error); | 563 SignalHttpClientComplete(this, error); |
| 562 return true; | 564 return true; |
| 563 } | 565 } |
| 564 | 566 |
| 565 HttpError HttpClient::ReadCacheHeaders(const std::string& id, bool override) { | 567 HttpError HttpClient::ReadCacheHeaders(const std::string& id, bool override) { |
| 566 scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheHeader)); | 568 std::unique_ptr<StreamInterface> stream( |
| 569 cache_->ReadResource(id, kCacheHeader)); |
| 567 if (!stream) { | 570 if (!stream) { |
| 568 return HE_CACHE; | 571 return HE_CACHE; |
| 569 } | 572 } |
| 570 | 573 |
| 571 HttpData::HeaderCombine combine = | 574 HttpData::HeaderCombine combine = |
| 572 override ? HttpData::HC_REPLACE : HttpData::HC_AUTO; | 575 override ? HttpData::HC_REPLACE : HttpData::HC_AUTO; |
| 573 | 576 |
| 574 if (!HttpReadCacheHeaders(stream.get(), &transaction_->response, combine)) { | 577 if (!HttpReadCacheHeaders(stream.get(), &transaction_->response, combine)) { |
| 575 LOG_F(LS_ERROR) << "Error reading cache headers"; | 578 LOG_F(LS_ERROR) << "Error reading cache headers"; |
| 576 return HE_CACHE; | 579 return HE_CACHE; |
| 577 } | 580 } |
| 578 | 581 |
| 579 response().scode = HC_OK; | 582 response().scode = HC_OK; |
| 580 return HE_NONE; | 583 return HE_NONE; |
| 581 } | 584 } |
| 582 | 585 |
| 583 HttpError HttpClient::ReadCacheBody(const std::string& id) { | 586 HttpError HttpClient::ReadCacheBody(const std::string& id) { |
| 584 cache_state_ = CS_READING; | 587 cache_state_ = CS_READING; |
| 585 | 588 |
| 586 HttpError error = HE_NONE; | 589 HttpError error = HE_NONE; |
| 587 | 590 |
| 588 size_t data_size; | 591 size_t data_size; |
| 589 scoped_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheBody)); | 592 std::unique_ptr<StreamInterface> stream(cache_->ReadResource(id, kCacheBody)); |
| 590 if (!stream || !stream->GetAvailable(&data_size)) { | 593 if (!stream || !stream->GetAvailable(&data_size)) { |
| 591 LOG_F(LS_ERROR) << "Unavailable cache body"; | 594 LOG_F(LS_ERROR) << "Unavailable cache body"; |
| 592 error = HE_CACHE; | 595 error = HE_CACHE; |
| 593 } else { | 596 } else { |
| 594 error = OnHeaderAvailable(false, false, data_size); | 597 error = OnHeaderAvailable(false, false, data_size); |
| 595 } | 598 } |
| 596 | 599 |
| 597 if ((HE_NONE == error) | 600 if ((HE_NONE == error) |
| 598 && (HV_HEAD != request().verb) | 601 && (HV_HEAD != request().verb) |
| 599 && response().document) { | 602 && response().document) { |
| 600 // Allocate on heap to not explode the stack. | 603 // Allocate on heap to not explode the stack. |
| 601 const int array_size = 1024 * 64; | 604 const int array_size = 1024 * 64; |
| 602 scoped_ptr<char[]> buffer(new char[array_size]); | 605 std::unique_ptr<char[]> buffer(new char[array_size]); |
| 603 StreamResult result = Flow(stream.get(), buffer.get(), array_size, | 606 StreamResult result = Flow(stream.get(), buffer.get(), array_size, |
| 604 response().document.get()); | 607 response().document.get()); |
| 605 if (SR_SUCCESS != result) { | 608 if (SR_SUCCESS != result) { |
| 606 error = HE_STREAM; | 609 error = HE_STREAM; |
| 607 } | 610 } |
| 608 } | 611 } |
| 609 | 612 |
| 610 return error; | 613 return error; |
| 611 } | 614 } |
| 612 | 615 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 const std::string& agent, | 826 const std::string& agent, |
| 824 HttpTransaction* transaction) | 827 HttpTransaction* transaction) |
| 825 : ReuseSocketPool(factory ? factory : Thread::Current()->socketserver()), | 828 : ReuseSocketPool(factory ? factory : Thread::Current()->socketserver()), |
| 826 HttpClient(agent, NULL, transaction) { | 829 HttpClient(agent, NULL, transaction) { |
| 827 set_pool(this); | 830 set_pool(this); |
| 828 } | 831 } |
| 829 | 832 |
| 830 ////////////////////////////////////////////////////////////////////// | 833 ////////////////////////////////////////////////////////////////////// |
| 831 | 834 |
| 832 } // namespace rtc | 835 } // namespace rtc |
| OLD | NEW |