Add the Sec-Fetch headers to View Source. https://redmine.stoutner.com/issues/503
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / asynctasks / GetSource.java
index 680fb7f88c2492b235e899fd2cd7e80bac1dead4..30f9aee9e2f4521388c0fd5eb66fe9bacca58df9 100644 (file)
@@ -108,7 +108,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
             // Open a connection to the URL.  No data is actually sent at this point.
             HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
 
-            // Instantiate the variables necessary to build the request headers.
+            // Define the variables necessary to build the request headers.
             requestHeadersBuilder = new SpannableStringBuilder();
             int oldRequestHeadersBuilderLength;
             int newRequestHeadersBuilderLength;
@@ -124,7 +124,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("Host");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  ");
             requestHeadersBuilder.append(url.getHost());
@@ -141,11 +141,27 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("Connection");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  keep-alive");
 
 
+            // Set the `Upgrade-Insecure-Requests` header property.
+            httpUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
+
+            // Add the `Upgrade-Insecure-Requests` header to the string builder and format the text.
+            requestHeadersBuilder.append(System.getProperty("line.separator"));
+            if (Build.VERSION.SDK_INT >= 21) {  // Newer versions of Android are so smart.
+                requestHeadersBuilder.append("Upgrade-Insecure-Requests", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            } else {  // Older versions not so much.
+                oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.append("Upgrade-Insecure_Requests");
+                newRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            }
+            requestHeadersBuilder.append(":  1");
+
+
             // Set the `User-Agent` header property.
             httpUrlConnection.setRequestProperty("User-Agent", userAgent);
 
@@ -157,42 +173,74 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("User-Agent");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  ");
             requestHeadersBuilder.append(userAgent);
 
 
-            // Set the `Upgrade-Insecure-Requests` header property.
-            httpUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
+            // Set the `x-requested-with` header property.
+            httpUrlConnection.setRequestProperty("x-requested-with", "");
 
-            // Add the `Upgrade-Insecure-Requests` header to the string builder and format the text.
+            // Add the `x-requested-with` header to the string builder and format the text.
             requestHeadersBuilder.append(System.getProperty("line.separator"));
             if (Build.VERSION.SDK_INT >= 21) {  // Newer versions of Android are so smart.
-                requestHeadersBuilder.append("Upgrade-Insecure-Requests", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.append("x-requested-with", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             } else {  // Older versions not so much.
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.append("Upgrade-Insecure_Requests");
+                requestHeadersBuilder.append("x-requested-with");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
-            requestHeadersBuilder.append(":  1");
+            requestHeadersBuilder.append(":  ");
 
 
-            // Set the `x-requested-with` header property.
-            httpUrlConnection.setRequestProperty("x-requested-with", "");
+            // Set the `Sec-Fetch-Site` header property.
+            httpUrlConnection.setRequestProperty("Sec-Fetch-Site", "none");
 
-            // Add the `x-requested-with` header to the string builder and format the text.
+            // Add the `Sec-Fetch-Site` header to the string builder and format the text.
             requestHeadersBuilder.append(System.getProperty("line.separator"));
             if (Build.VERSION.SDK_INT >= 21) {  // Newer versions of Android are so smart.
-                requestHeadersBuilder.append("x-requested-with", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.append("Sec-Fetch-Site", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             } else {  // Older versions not so much.
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.append("x-requested-with");
+                requestHeadersBuilder.append("Sec-Fetch-Site");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
-            requestHeadersBuilder.append(":  ");
+            requestHeadersBuilder.append(":  none");
+
+
+            // Set the `Sec-Fetch-Mode` header property.
+            httpUrlConnection.setRequestProperty("Sec-Fetch-Mode", "navigate");
+
+            // Add the `Sec-Fetch-Mode` header to the string builder and format the text.
+            requestHeadersBuilder.append(System.getProperty("line.separator"));
+            if (Build.VERSION.SDK_INT >= 21) {  // Newer versions of Android are so smart.
+                requestHeadersBuilder.append("Sec-Fetch-Mode", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            } else {  // Older versions not so much.
+                oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.append("Sec-Fetch-Mode");
+                newRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            }
+            requestHeadersBuilder.append(":  navigate");
+
+
+            // Set the `Sec-Fetch-User` header property.
+            httpUrlConnection.setRequestProperty("Sec-Fetch-User", "?1");
+
+            // Add the `Sec-Fetch-User` header to the string builder and format the text.
+            requestHeadersBuilder.append(System.getProperty("line.separator"));
+            if (Build.VERSION.SDK_INT >= 21) {  // Newer versions of Android are so smart.
+                requestHeadersBuilder.append("Sec-Fetch-User", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            } else {  // Older versions not so much.
+                oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.append("Sec-Fetch-User");
+                newRequestHeadersBuilderLength = requestHeadersBuilder.length();
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            }
+            requestHeadersBuilder.append(":  ?1");
 
 
             // Get a handle for the shared preferences.
@@ -211,14 +259,14 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                     oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                     requestHeadersBuilder.append("dnt");
                     newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                    requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                    requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                 }
                 requestHeadersBuilder.append(":  1");
             }
 
 
             // Set the `Accept` header property.
-            httpUrlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
+            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");
 
             // Add the `Accept` header to the string builder and format the text.
             requestHeadersBuilder.append(System.getProperty("line.separator"));
@@ -228,10 +276,10 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("Accept");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  ");
-            requestHeadersBuilder.append("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
+            requestHeadersBuilder.append("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
 
 
             // Instantiate a locale string.
@@ -255,17 +303,37 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                         localesStringBuilder.append(",");
                     }
 
-                    // Get the indicated locale from the list.
-                    localesStringBuilder.append(localeList.get(i));
+                    // Get the locale from the list.
+                    Locale locale = localeList.get(i);
+
+                    // Add the locale to the string.  `locale` by default displays as `en_US`, but WebView uses the `en-US` format.
+                    localesStringBuilder.append(locale.getLanguage());
+                    localesStringBuilder.append("-");
+                    localesStringBuilder.append(locale.getCountry());
 
-                    // If not the first locale, append `;q=0.i`, which drops by .1 for each removal from the main locale.
+                    // If not the first locale, append `;q=0.x`, which drops by .1 for each removal from the main locale until q=0.1.
                     if (q < 10) {
                         localesStringBuilder.append(";q=0.");
                         localesStringBuilder.append(q);
                     }
 
-                    // Decrement `q`.
-                    q--;
+                    // Decrement `q` if it is greater than 1.
+                    if (q > 1) {
+                        q--;
+                    }
+
+                    // Add a second entry for the language only portion of the locale.
+                    localesStringBuilder.append(",");
+                    localesStringBuilder.append(locale.getLanguage());
+
+                    // Append `1;q=0.x`, which drops by .1 for each removal form the main locale until q=0.1.
+                    localesStringBuilder.append(";q=0.");
+                    localesStringBuilder.append(q);
+
+                    // Decrement `q` if it is greater than 1.
+                    if (q > 1) {
+                        q--;
+                    }
                 }
 
                 // Store the populated string builder in the locale string.
@@ -286,7 +354,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("Accept-Language");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  ");
             requestHeadersBuilder.append(localeString);
@@ -308,7 +376,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                     oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                     requestHeadersBuilder.append("Cookie");
                     newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                    requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                    requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                 }
                 requestHeadersBuilder.append(":  ");
                 requestHeadersBuilder.append(cookiesString);
@@ -324,7 +392,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                 oldRequestHeadersBuilderLength = requestHeadersBuilder.length();
                 requestHeadersBuilder.append("Accept-Encoding");
                 newRequestHeadersBuilderLength = requestHeadersBuilder.length();
-                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength + 1, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                requestHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldRequestHeadersBuilderLength, newRequestHeadersBuilderLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             requestHeadersBuilder.append(":  gzip");
 
@@ -366,7 +434,7 @@ public class GetSource extends AsyncTask<String, Void, SpannableStringBuilder[]>
                         int oldLength = responseHeadersBuilder.length();
                         responseHeadersBuilder.append(httpUrlConnection.getHeaderFieldKey(i));
                         int newLength = responseHeadersBuilder.length();
-                        responseHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldLength + 1, newLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+                        responseHeadersBuilder.setSpan(new StyleSpan(Typeface.BOLD), oldLength, newLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                     }
                     responseHeadersBuilder.append(":  ");
                     responseHeadersBuilder.append(httpUrlConnection.getHeaderField(i));