2 * Copyright 2020-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser Android is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.helpers
22 import android.content.Context
23 import android.webkit.CookieManager
25 import com.stoutner.privacybrowser.R
27 import java.lang.Exception
28 import java.net.HttpURLConnection
30 import java.text.NumberFormat
32 object GetUrlSizeHelper {
33 fun getUrl(context: Context, url: URL, userAgent: String, cookiesEnabled: Boolean): String {
34 // Initialize the formatted file size string.
35 var formattedFileSize = context.getString(R.string.unknown_size)
37 // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions.
39 // Instantiate the proxy helper.
40 val proxyHelper = ProxyHelper()
42 // Get the current proxy.
43 val proxy = proxyHelper.getCurrentProxy(context)
45 // Open a connection to the URL. No data is actually sent at this point.
46 val httpUrlConnection = url.openConnection(proxy) as HttpURLConnection
48 // Add the user agent to the header property.
49 httpUrlConnection.setRequestProperty("User-Agent", userAgent)
51 // Add the cookies if they are enabled.
53 // Get the cookies for the current domain.
54 val cookiesString = CookieManager.getInstance().getCookie(url.toString())
56 // Only add the cookies if they are not null.
57 if (cookiesString != null) {
58 // Add the cookies to the header property.
59 httpUrlConnection.setRequestProperty("Cookie", cookiesString)
63 // 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.
65 // Get the status code. This initiates a network connection.
66 val responseCode = httpUrlConnection.responseCode
68 // Check the response code.
69 if (responseCode >= 400) { // The response code is an error message.
70 // Set the formatted file size to indicate a bad URL.
71 formattedFileSize = context.getString(R.string.invalid_url)
72 } else { // The response code is not an error message.
73 // Get the content length header.
74 val contentLengthString = httpUrlConnection.getHeaderField("Content-Length")
76 // Only process the content length string if it isn't null.
77 if (contentLengthString != null) {
78 // Convert the content length string to a long.
79 val fileSize = contentLengthString.toLong()
81 // Format the file size.
82 formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes)
86 // Disconnect the HTTP URL connection.
87 httpUrlConnection.disconnect()
89 } catch (exception: Exception) {
90 // Set the formatted file size to indicate a bad URL.
91 formattedFileSize = context.getString(R.string.invalid_url)
94 // Return the formatted file size.
95 return formattedFileSize