]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/GetUrlSizeHelper.kt
6c3b3f8771c066d6142c76b21e3b0cb191ed14b1
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / GetUrlSizeHelper.kt
1 /*
2  * Copyright 2020-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers
21
22 import android.content.Context
23 import android.webkit.CookieManager
24
25 import com.stoutner.privacybrowser.R
26
27 import java.lang.Exception
28 import java.net.HttpURLConnection
29 import java.net.URL
30 import java.text.NumberFormat
31
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)
36
37         // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch exceptions.
38         try {
39             // Instantiate the proxy helper.
40             val proxyHelper = ProxyHelper()
41
42             // Get the current proxy.
43             val proxy = proxyHelper.getCurrentProxy(context)
44
45             // Open a connection to the URL.  No data is actually sent at this point.
46             val httpUrlConnection = url.openConnection(proxy) as HttpURLConnection
47
48             // Add the user agent to the header property.
49             httpUrlConnection.setRequestProperty("User-Agent", userAgent)
50
51             // Add the cookies if they are enabled.
52             if (cookiesEnabled) {
53                 // Get the cookies for the current domain.
54                 val cookiesString = CookieManager.getInstance().getCookie(url.toString())
55
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)
60                 }
61             }
62
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.
64             try {
65                 // Get the status code.  This initiates a network connection.
66                 val responseCode = httpUrlConnection.responseCode
67
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")
75
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()
80
81                         // Format the file size.
82                         formattedFileSize = NumberFormat.getInstance().format(fileSize) + " " + context.getString(R.string.bytes)
83                     }
84                 }
85             } finally {
86                 // Disconnect the HTTP URL connection.
87                 httpUrlConnection.disconnect()
88             }
89         } catch (exception: Exception) {
90             // Set the formatted file size to indicate a bad URL.
91             formattedFileSize = context.getString(R.string.invalid_url)
92         }
93
94         // Return the formatted file size.
95         return formattedFileSize
96     }
97 }