AU: Manual proxy support

Utilize the ChromeProxyResolver to resolve proxies in our network
requests. This means the following changes:

- HttpFetcher classes take a ProxyResolver* in their ctor. Also, a few
  useful functions in HttpFetcher to allow subclasses to iterate
  through the proxies.

- LibcurlHttpFetcher support for using the ProxyResolver. It will
  attempt to use each proxy in the order specified. If any data comes
  in from any proxy, it won't continue down the list and will continue
  to use that proxy for its lifetime.

- UpdateAttempter can choose, for a given update session, whether or
  not to use the ChromeProxyResolver or DirectProxyResolver. For now,
  the logic is: for automatic checks, 80% of the time use
  ChromeProxyResolver, 20% DirectProxyResolver. For manual checks, the
  first 19 manual checks in a row use Chrome, then once it uses
  Direct, then starts over again. The idea is that the updater doesn't
  necessarily trust Chrome, so some requests should skip it. If a
  manual check is performed, the user likely wants her proxy settings
  honored, so use them, but don't allow frequent manual checks to
  starve out usage of the DirectProxyResolver.

- Updates to tests

BUG=3167
TEST=unittests, tested on device

Review URL: http://codereview.chromium.org/5205002

Change-Id: Iee0f589e5b28d4b804afe1f5b6729ba066d48d62
diff --git a/libcurl_http_fetcher.cc b/libcurl_http_fetcher.cc
index 460b980..c6293b1 100644
--- a/libcurl_http_fetcher.cc
+++ b/libcurl_http_fetcher.cc
@@ -9,6 +9,7 @@
 
 #include <base/logging.h>
 
+#include "update_engine/chrome_proxy_resolver.h"
 #include "update_engine/dbus_interface.h"
 #include "update_engine/flimflam_proxy.h"
 #include "update_engine/utils.h"
@@ -59,6 +60,25 @@
   curl_handle_ = curl_easy_init();
   CHECK(curl_handle_);
 
+  CHECK(HasProxy());
+  LOG(INFO) << "Using proxy: " << GetCurrentProxy();
+  if (GetCurrentProxy() == kNoProxy) {
+    CHECK_EQ(curl_easy_setopt(curl_handle_,
+                              CURLOPT_PROXY,
+                              ""), CURLE_OK);
+  } else {
+    CHECK_EQ(curl_easy_setopt(curl_handle_,
+                              CURLOPT_PROXY,
+                              GetCurrentProxy().c_str()), CURLE_OK);
+    // Curl seems to require us to set the protocol
+    curl_proxytype type;
+    if (ChromeProxyResolver::GetProxyType(GetCurrentProxy(), &type)) {
+      CHECK_EQ(curl_easy_setopt(curl_handle_,
+                                CURLOPT_PROXYTYPE,
+                                type), CURLE_OK);
+    }
+  }
+
   if (post_data_set_) {
     CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POST, 1), CURLE_OK);
     CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_POSTFIELDS,
@@ -133,6 +153,7 @@
   resume_offset_ = 0;
   retry_count_ = 0;
   http_response_code_ = 0;
+  ResolveProxiesForUrl(url);
   ResumeTransfer(url);
   CurlPerformOnce();
 }
@@ -178,6 +199,24 @@
     // we're done!
     CleanUp();
 
+    if (!sent_byte_ &&
+        (http_response_code_ < 200 || http_response_code_ >= 300)) {
+      // The transfer completed w/ error and we didn't get any bytes.
+      // If we have another proxy to try, try that.
+
+      PopProxy();  // Delete the proxy we just gave up on.
+
+      if (HasProxy()) {
+        // We have another proxy. Retry immediately.
+        g_idle_add(&LibcurlHttpFetcher::StaticRetryTimeoutCallback, this);
+      } else {
+        // Out of proxies. Give up.
+        if (delegate_)
+          delegate_->TransferComplete(this, false);  // success
+      }
+      return;
+    }
+
     if ((transfer_size_ >= 0) && (bytes_downloaded_ < transfer_size_)) {
       // Need to restart transfer
       retry_count_++;
@@ -208,6 +247,9 @@
 }
 
 size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
+  if (size == 0)
+    return 0;
+  sent_byte_ = true;
   GetHttpResponseCode();
   {
     double transfer_size_double;