2 * Copyright © 2017-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.backgroundtasks;
22 import android.annotation.SuppressLint;
23 import android.content.ContentResolver;
24 import android.database.Cursor;
25 import android.graphics.Typeface;
26 import android.net.Uri;
27 import android.os.Build;
28 import android.text.SpannableStringBuilder;
29 import android.text.Spanned;
30 import android.text.style.StyleSpan;
31 import android.webkit.CookieManager;
33 import com.stoutner.privacybrowser.viewmodels.WebViewSource;
35 import java.io.BufferedInputStream;
36 import java.io.BufferedReader;
37 import java.io.ByteArrayOutputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.net.HttpURLConnection;
42 import java.net.Proxy;
44 import java.security.SecureRandom;
45 import java.security.cert.X509Certificate;
47 import javax.net.ssl.HostnameVerifier;
48 import javax.net.ssl.HttpsURLConnection;
49 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.SSLSocketFactory;
51 import javax.net.ssl.TrustManager;
52 import javax.net.ssl.X509TrustManager;
54 public class GetSourceBackgroundTask {
55 public SpannableStringBuilder[] acquire(String urlString, String userAgent, String localeString, Proxy proxy, ContentResolver contentResolver, WebViewSource webViewSource, boolean ignoreSslErrors) {
56 // Initialize the spannable string builders.
57 SpannableStringBuilder requestHeadersBuilder = new SpannableStringBuilder();
58 SpannableStringBuilder responseMessageBuilder = new SpannableStringBuilder();
59 SpannableStringBuilder responseHeadersBuilder = new SpannableStringBuilder();
60 SpannableStringBuilder responseBodyBuilder = new SpannableStringBuilder();
62 if (urlString.startsWith("content://")) { // This is a content URL.
63 // Attempt to read the content data. Return an error if this fails.
65 // Get a URI for the content URL.
66 Uri contentUri = Uri.parse(urlString);
68 // Get a cursor with metadata about the content URL.
69 Cursor contentCursor = contentResolver.query(contentUri, null, null, null, null);
71 // Move the content cursor to the first row.
72 contentCursor.moveToFirst();
74 for (int i = 0; i < contentCursor.getColumnCount(); i++) {
75 // Add a new line if this is not the first entry.
77 responseHeadersBuilder.append(System.getProperty("line.separator"));
80 // Add each header to the string builder.
81 responseHeadersBuilder.append(contentCursor.getColumnName(i), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
82 responseHeadersBuilder.append(": ");
83 responseHeadersBuilder.append(contentCursor.getString(i));
86 // Close the content cursor.
87 contentCursor.close();
89 // Create a buffered string reader for the content data.
90 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(contentResolver.openInputStream(contentUri)));
92 // Get the data from the buffered reader one line at a time.
93 for (String contentLineString; ((contentLineString = bufferedReader.readLine()) != null);) {
94 // Add the line to the response body builder.
95 responseBodyBuilder.append(contentLineString);
98 responseBodyBuilder.append("\n");
100 } catch (Exception exception) {
101 // Return the error message.
102 webViewSource.returnError(exception.toString());
104 } else { // This is not a content URL.
105 // Because everything relating to requesting data from a webserver can throw errors, the entire section must catch `IOExceptions`.
107 // Get the current URL from the main activity.
108 URL url = new URL(urlString);
110 // Open a connection to the URL. No data is actually sent at this point.
111 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
113 // Set the `Host` header property.
114 httpUrlConnection.setRequestProperty("Host", url.getHost());
116 // Add the `Host` header to the string builder and format the text.
117 requestHeadersBuilder.append("Host", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
118 requestHeadersBuilder.append(": ");
119 requestHeadersBuilder.append(url.getHost());
122 // Set the `Connection` header property.
123 httpUrlConnection.setRequestProperty("Connection", "keep-alive");
125 // Add the `Connection` header to the string builder and format the text.
126 requestHeadersBuilder.append(System.getProperty("line.separator"));
127 requestHeadersBuilder.append("Connection", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
128 requestHeadersBuilder.append(": keep-alive");
131 // Set the `Upgrade-Insecure-Requests` header property.
132 httpUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
134 // Add the `Upgrade-Insecure-Requests` header to the string builder and format the text.
135 requestHeadersBuilder.append(System.getProperty("line.separator"));
136 requestHeadersBuilder.append("Upgrade-Insecure-Requests", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
137 requestHeadersBuilder.append(": 1");
140 // Set the `User-Agent` header property.
141 httpUrlConnection.setRequestProperty("User-Agent", userAgent);
143 // Add the `User-Agent` header to the string builder and format the text.
144 requestHeadersBuilder.append(System.getProperty("line.separator"));
145 requestHeadersBuilder.append("User-Agent", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
146 requestHeadersBuilder.append(": ");
147 requestHeadersBuilder.append(userAgent);
150 // Set the `x-requested-with` header property.
151 httpUrlConnection.setRequestProperty("x-requested-with", "");
153 // Add the `x-requested-with` header to the string builder and format the text.
154 requestHeadersBuilder.append(System.getProperty("line.separator"));
155 requestHeadersBuilder.append("x-requested-with", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
156 requestHeadersBuilder.append(": ");
159 // Set the `Sec-Fetch-Site` header property.
160 httpUrlConnection.setRequestProperty("Sec-Fetch-Site", "none");
162 // Add the `Sec-Fetch-Site` header to the string builder and format the text.
163 requestHeadersBuilder.append(System.getProperty("line.separator"));
164 requestHeadersBuilder.append("Sec-Fetch-Site", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
165 requestHeadersBuilder.append(": none");
168 // Set the `Sec-Fetch-Mode` header property.
169 httpUrlConnection.setRequestProperty("Sec-Fetch-Mode", "navigate");
171 // Add the `Sec-Fetch-Mode` header to the string builder and format the text.
172 requestHeadersBuilder.append(System.getProperty("line.separator"));
173 requestHeadersBuilder.append("Sec-Fetch-Mode", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
174 requestHeadersBuilder.append(": navigate");
177 // Set the `Sec-Fetch-User` header property.
178 httpUrlConnection.setRequestProperty("Sec-Fetch-User", "?1");
180 // Add the `Sec-Fetch-User` header to the string builder and format the text.
181 requestHeadersBuilder.append(System.getProperty("line.separator"));
182 requestHeadersBuilder.append("Sec-Fetch-User", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
183 requestHeadersBuilder.append(": ?1");
186 // Set the `Accept` header property.
187 httpUrlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
189 // Add the `Accept` header to the string builder and format the text.
190 requestHeadersBuilder.append(System.getProperty("line.separator"));
191 requestHeadersBuilder.append("Accept", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
192 requestHeadersBuilder.append(": ");
193 requestHeadersBuilder.append("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
196 // Set the `Accept-Language` header property.
197 httpUrlConnection.setRequestProperty("Accept-Language", localeString);
199 // Add the `Accept-Language` header to the string builder and format the text.
200 requestHeadersBuilder.append(System.getProperty("line.separator"));
201 requestHeadersBuilder.append("Accept-Language", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
202 requestHeadersBuilder.append(": ");
203 requestHeadersBuilder.append(localeString);
206 // Get the cookies for the current domain.
207 String cookiesString = CookieManager.getInstance().getCookie(url.toString());
209 // Only process the cookies if they are not null.
210 if (cookiesString != null) {
211 // Add the cookies to the header property.
212 httpUrlConnection.setRequestProperty("Cookie", cookiesString);
214 // Add the cookie header to the string builder and format the text.
215 requestHeadersBuilder.append(System.getProperty("line.separator"));
216 requestHeadersBuilder.append("Cookie", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
217 requestHeadersBuilder.append(": ");
218 requestHeadersBuilder.append(cookiesString);
222 // `HttpUrlConnection` sets `Accept-Encoding` to be `gzip` by default. If the property is manually set, than `HttpUrlConnection` does not process the decoding.
223 // Add the `Accept-Encoding` header to the string builder and format the text.
224 requestHeadersBuilder.append(System.getProperty("line.separator"));
225 requestHeadersBuilder.append("Accept-Encoding", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
226 requestHeadersBuilder.append(": gzip");
228 // Ignore SSL errors if requested.
229 if (ignoreSslErrors){
230 // Create a new host name verifier.
231 HostnameVerifier hostnameVerifier = (hostname, sslSession) -> {
232 // Allow all host names.
236 // Create a new trust manager.
237 TrustManager[] trustManager = new TrustManager[] {
238 new X509TrustManager() {
239 @SuppressLint("TrustAllX509TrustManager")
241 public void checkClientTrusted(X509Certificate[] chain, String authType) {
242 // Do nothing, which trusts all client certificates.
245 @SuppressLint("TrustAllX509TrustManager")
247 public void checkServerTrusted(X509Certificate[] chain, String authType) {
248 // Do nothing, which trusts all server certificates.
252 public X509Certificate[] getAcceptedIssuers() {
258 // Get an SSL context. `TLS` provides a base instance available from API 1. <https://developer.android.com/reference/javax/net/ssl/SSLContext>
259 SSLContext sslContext = SSLContext.getInstance("TLS");
261 // Initialize the SSL context with the blank trust manager.
262 sslContext.init(null, trustManager, new SecureRandom());
264 // Get the SSL socket factory with the blank trust manager.
265 SSLSocketFactory socketFactory = sslContext.getSocketFactory();
267 // Set the HTTPS URL Connection to use the blank host name verifier.
268 ((HttpsURLConnection) httpUrlConnection).setHostnameVerifier(hostnameVerifier);
270 // Set the HTTPS URL connection to use the socket factory with the blank trust manager.
271 ((HttpsURLConnection) httpUrlConnection).setSSLSocketFactory(socketFactory);
274 // 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.
276 // Get the response code, which causes the connection to the server to be made.
277 int responseCode = httpUrlConnection.getResponseCode();
279 // Populate the response message string builder.
280 responseMessageBuilder.append(String.valueOf(responseCode), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
281 responseMessageBuilder.append(": ");
282 responseMessageBuilder.append(httpUrlConnection.getResponseMessage());
284 // Initialize the iteration variable.
287 // Iterate through the received header fields.
288 while (httpUrlConnection.getHeaderField(i) != null) {
289 // Add a new line if there is already information in the string builder.
291 responseHeadersBuilder.append(System.getProperty("line.separator"));
294 // Add the header to the string builder and format the text.
295 responseHeadersBuilder.append(httpUrlConnection.getHeaderFieldKey(i), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
296 responseHeadersBuilder.append(": ");
297 responseHeadersBuilder.append(httpUrlConnection.getHeaderField(i));
299 // Increment the iteration variable.
303 // Instantiate an input stream for the response body.
304 InputStream inputStream;
306 // Get the correct input stream based on the response code.
307 if (responseCode == 404) { // Get the error stream.
308 inputStream = new BufferedInputStream(httpUrlConnection.getErrorStream());
309 } else { // Get the response body stream.
310 inputStream = new BufferedInputStream(httpUrlConnection.getInputStream());
313 // Initialize the byte array output stream and the conversion buffer byte array.
314 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
315 byte[] conversionBufferByteArray = new byte[1024];
317 // Define the buffer length variable.
321 // Attempt to read data from the input stream and store it in the conversion buffer byte array. Also store the amount of data read in the buffer length variable.
322 while ((bufferLength = inputStream.read(conversionBufferByteArray)) > 0) { // Proceed while the amount of data stored in the buffer is > 0.
323 // Write the contents of the conversion buffer to the byte array output stream.
324 byteArrayOutputStream.write(conversionBufferByteArray, 0, bufferLength);
326 } catch (IOException exception) {
327 // Return the error message.
328 webViewSource.returnError(exception.toString());
331 // Close the input stream.
334 // Populate the response body string with the contents of the byte array output stream.
335 responseBodyBuilder.append(byteArrayOutputStream.toString());
337 // Disconnect HTTP URL connection.
338 httpUrlConnection.disconnect();
340 } catch (Exception exception) {
341 // Return the error message.
342 webViewSource.returnError(exception.toString());
346 // Return the spannable string builders.
347 return new SpannableStringBuilder[] {requestHeadersBuilder, responseMessageBuilder, responseHeadersBuilder, responseBodyBuilder};