From: Soren Stoutner Date: Wed, 2 Nov 2016 07:30:21 +0000 (-0700) Subject: More intelligent parsing of the Content-Disposition header to prevent crashing while... X-Git-Tag: v1.12~9 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=6df5f6e84ab873b5628ba3769e62dd2c4f49ea52;p=PrivacyBrowserAndroid.git More intelligent parsing of the Content-Disposition header to prevent crashing while downloading some files. --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/DownloadFile.java b/app/src/main/java/com/stoutner/privacybrowser/DownloadFile.java index 1a3b76da..8e04cf64 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/DownloadFile.java +++ b/app/src/main/java/com/stoutner/privacybrowser/DownloadFile.java @@ -50,9 +50,15 @@ public class DownloadFile extends AppCompatDialogFragment { Bundle argumentsBundle = new Bundle(); String fileNameString; - if (!contentDisposition.isEmpty()) { // Extract `fileNameString` from `contentDisposition` using the substring beginning after `filename="` and ending with the next `"`. + + // Parse `filename` from `contentDisposition`. + if (contentDisposition.contains("filename=\"")) { // The file name is contained in a string surrounded by `""`. fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=\"") + 10, contentDisposition.indexOf("\"", contentDisposition.indexOf("filename=\"") + 10)); - } else { // `contentDisposition` is empty, so use the last path segment of the URL as the file name. + } else if (contentDisposition.contains("filename=") && ((contentDisposition.indexOf(";", contentDisposition.indexOf("filename=") + 9)) > 0 )) { // The file name is contained in a string beginning with `filename=` and ending with `;`. + fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9, contentDisposition.indexOf(";", contentDisposition.indexOf("filename=") + 9)); + } else if (contentDisposition.contains("filename=")) { // The file name is contained in a string beginning with `filename=` and proceeding to the end of `contentDisposition`. + fileNameString = contentDisposition.substring(contentDisposition.indexOf("filename=") + 9, contentDisposition.length()); + } else { // `contentDisposition` does not contain the filename, so use the last path segment of the URL. Uri downloadUri = Uri.parse(urlString); fileNameString = downloadUri.getLastPathSegment(); }