X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FGetUrlSizeHelper.kt;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FGetUrlSizeHelper.kt;h=6c3b3f8771c066d6142c76b21e3b0cb191ed14b1;hb=c2b5bdf009503f6761dc830fb65502ad2910c284;hp=0000000000000000000000000000000000000000;hpb=58dbf864ccd5dd341517e1ff0adaf681476d4a4f;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/GetUrlSizeHelper.kt b/app/src/main/java/com/stoutner/privacybrowser/helpers/GetUrlSizeHelper.kt new file mode 100644 index 00000000..6c3b3f87 --- /dev/null +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/GetUrlSizeHelper.kt @@ -0,0 +1,97 @@ +/* + * Copyright 2020-2022 Soren Stoutner . + * + * This file is part of Privacy Browser Android . + * + * Privacy Browser Android is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Privacy Browser Android is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser Android. If not, see . + */ + +package com.stoutner.privacybrowser.helpers + +import android.content.Context +import android.webkit.CookieManager + +import com.stoutner.privacybrowser.R + +import java.lang.Exception +import java.net.HttpURLConnection +import java.net.URL +import java.text.NumberFormat + +object GetUrlSizeHelper { + fun getUrl(context: Context, url: URL, userAgent: String, cookiesEnabled: Boolean): String { + // Initialize the formatted file size string. + var formattedFileSize = context.getString(R.string.unknown_size) + + // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions. + try { + // Instantiate the proxy helper. + val proxyHelper = ProxyHelper() + + // Get the current proxy. + val proxy = proxyHelper.getCurrentProxy(context) + + // Open a connection to the URL. No data is actually sent at this point. + val httpUrlConnection = url.openConnection(proxy) as HttpURLConnection + + // Add the user agent to the header property. + httpUrlConnection.setRequestProperty("User-Agent", userAgent) + + // Add the cookies if they are enabled. + if (cookiesEnabled) { + // Get the cookies for the current domain. + val cookiesString = CookieManager.getInstance().getCookie(url.toString()) + + // Only add the cookies if they are not null. + if (cookiesString != null) { + // Add the cookies to the header property. + httpUrlConnection.setRequestProperty("Cookie", cookiesString) + } + } + + // The actual network request is in a `try` bracket so that `disconnect()` is run in the `finally` section even if an error is encountered in the main block. + try { + // Get the status code. This initiates a network connection. + val responseCode = httpUrlConnection.responseCode + + // Check the response code. + if (responseCode >= 400) { // The response code is an error message. + // Set the formatted file size to indicate a bad URL. + formattedFileSize = context.getString(R.string.invalid_url) + } else { // The response code is not an error message. + // Get the content length header. + val contentLengthString = httpUrlConnection.getHeaderField("Content-Length") + + // Only process the content length string if it isn't null. + if (contentLengthString != null) { + // Convert the content length string to a long. + val fileSize = contentLengthString.toLong() + + // Format the file size. + formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes) + } + } + } finally { + // Disconnect the HTTP URL connection. + httpUrlConnection.disconnect() + } + } catch (exception: Exception) { + // Set the formatted file size to indicate a bad URL. + formattedFileSize = context.getString(R.string.invalid_url) + } + + // Return the formatted file size. + return formattedFileSize + } +}