From 6b1f54569c72bcb962d7803028e7354b17de8886 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Thu, 10 Jan 2013 11:12:52 -0800 Subject: Only add one User-Agent header. Also include more details when reporting HTTP error codes. Bug: 7966393 Change-Id: I251b1ec7c827693817391b6e9fb8b0cab995395e --- src/com/android/providers/downloads/DownloadThread.java | 13 +++++++++---- .../android/providers/downloads/StopRequestException.java | 13 +++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index dc2ef571..c77224a7 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -306,7 +306,8 @@ public class DownloadThread extends Thread { case HTTP_PRECON_FAILED: // TODO: probably means our etag precondition was // changed; flush and retry again - StopRequestException.throwUnhandledHttpError(responseCode); + StopRequestException.throwUnhandledHttpError( + responseCode, conn.getResponseMessage()); case HTTP_UNAVAILABLE: parseRetryAfterHeaders(state, conn); @@ -320,7 +321,8 @@ public class DownloadThread extends Thread { throw new StopRequestException(STATUS_WAITING_TO_RETRY, "Internal error"); default: - StopRequestException.throwUnhandledHttpError(responseCode); + StopRequestException.throwUnhandledHttpError( + responseCode, conn.getResponseMessage()); } } catch (IOException e) { // Trouble with low-level sockets @@ -791,12 +793,15 @@ public class DownloadThread extends Thread { * Add custom headers for this download to the HTTP request. */ private void addRequestHeaders(State state, HttpURLConnection conn) { - conn.addRequestProperty("User-Agent", userAgent()); - for (Pair header : mInfo.getHeaders()) { conn.addRequestProperty(header.first, header.second); } + // Only splice in user agent when not already defined + if (conn.getRequestProperty("User-Agent") == null) { + conn.addRequestProperty("User-Agent", userAgent()); + } + if (state.mContinuingDownload) { if (state.mHeaderETag != null) { conn.addRequestProperty("If-Match", state.mHeaderETag); diff --git a/src/com/android/providers/downloads/StopRequestException.java b/src/com/android/providers/downloads/StopRequestException.java index 6df61dce..a2b642d8 100644 --- a/src/com/android/providers/downloads/StopRequestException.java +++ b/src/com/android/providers/downloads/StopRequestException.java @@ -47,14 +47,15 @@ class StopRequestException extends Exception { return mFinalStatus; } - public static StopRequestException throwUnhandledHttpError(int responseCode) + public static StopRequestException throwUnhandledHttpError(int code, String message) throws StopRequestException { - if (responseCode >= 400 && responseCode < 600) { - throw new StopRequestException(responseCode, "Unhandled HTTP response"); - } else if (responseCode >= 300 && responseCode < 400) { - throw new StopRequestException(STATUS_UNHANDLED_REDIRECT, "Unhandled HTTP response"); + final String error = "Unhandled HTTP response: " + code + " " + message; + if (code >= 400 && code < 600) { + throw new StopRequestException(code, error); + } else if (code >= 300 && code < 400) { + throw new StopRequestException(STATUS_UNHANDLED_REDIRECT, error); } else { - throw new StopRequestException(STATUS_UNHANDLED_HTTP_CODE, "Unhandled HTTP response"); + throw new StopRequestException(STATUS_UNHANDLED_HTTP_CODE, error); } } } -- cgit v1.2.3