Updater avoids download in case of an error HTTP response.

(a) LibcurlHttpFetcher avoids download if the HTTP reponse indicates an
error; corresponding change to unit test code and test HTTP server.  (b)
Added a method for returning the total bytes downloaded to HttpFetcher
and all subclasses, needed for unit testing.  (c) Generalized check for
successful HTTP response code in LibcurlHttpFetcher.

BUG=chromium-os:9648
TEST=unit tests

Change-Id: I46d72fbde0ecfb53823b0705ce17f9547515ee61
Reviewed-on: https://gerrit.chromium.org/gerrit/11773
Tested-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Andrew de los Reyes <adlr@chromium.org>
Commit-Ready: Gilad Arnold <garnold@chromium.org>
diff --git a/libcurl_http_fetcher.cc b/libcurl_http_fetcher.cc
index 7f5f859..499087f 100644
--- a/libcurl_http_fetcher.cc
+++ b/libcurl_http_fetcher.cc
@@ -246,8 +246,7 @@
       return;
     }
 
-    if (!sent_byte_ &&
-        (http_response_code_ < 200 || http_response_code_ >= 300)) {
+    if (!sent_byte_ && ! IsHttpResponseSuccess()) {
       // The transfer completed w/ error and we didn't get any bytes.
       // If we have another proxy to try, try that.
 
@@ -282,8 +281,7 @@
     } else {
       if (delegate_) {
         // success is when http_response_code is 2xx
-        bool success = (http_response_code_ >= 200) &&
-            (http_response_code_ < 300);
+        bool success = IsHttpResponseSuccess();
         delegate_->TransferComplete(this, success);
       }
     }
@@ -294,10 +292,18 @@
 }
 
 size_t LibcurlHttpFetcher::LibcurlWrite(void *ptr, size_t size, size_t nmemb) {
-  if (size == 0)
-    return 0;
-  sent_byte_ = true;
+  // Update HTTP response first.
   GetHttpResponseCode();
+  const size_t payload_size = size * nmemb;
+
+  // Do nothing if no payload or HTTP response is an error.
+  if (payload_size == 0 || ! IsHttpResponseSuccess()) {
+    LOG(INFO) << "HTTP response unsuccessful (" << http_response_code_
+              << ") or no payload (" << payload_size << "), nothing to do";
+    return 0;
+  }
+
+  sent_byte_ = true;
   {
     double transfer_size_double;
     CHECK_EQ(curl_easy_getinfo(curl_handle_,
@@ -308,12 +314,12 @@
       transfer_size_ = resume_offset_ + new_transfer_size;
     }
   }
-  bytes_downloaded_ += size * nmemb;
+  bytes_downloaded_ += payload_size;
   in_write_callback_ = true;
   if (delegate_)
-    delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), size * nmemb);
+    delegate_->ReceivedBytes(this, reinterpret_cast<char*>(ptr), payload_size);
   in_write_callback_ = false;
-  return size * nmemb;
+  return payload_size;
 }
 
 void LibcurlHttpFetcher::Pause() {