Skip to content

Instantly share code, notes, and snippets.

@dongchao-cn
Created July 23, 2014 13:36
Show Gist options
  • Save dongchao-cn/44fdde929451198dcf3c to your computer and use it in GitHub Desktop.
Save dongchao-cn/44fdde929451198dcf3c to your computer and use it in GitHub Desktop.
Add `Package-Name` and `Package-Sig` in HTTP Header for Android(KitKat - 4.4)
diff --git a/frameworks/webview/chromium/java/com/android/webview/chromium/WebViewChromium.java b/frameworks/webview/chromium/java/com/android/webview/chromium/WebViewChromium.java
index efeabf9..064ebb0 100644
--- a/frameworks/webview/chromium/java/com/android/webview/chromium/WebViewChromium.java
+++ b/frameworks/webview/chromium/java/com/android/webview/chromium/WebViewChromium.java
@@ -16,6 +16,9 @@
package com.android.webview.chromium;
+import android.app.ActivityThread;
+import android.content.pm.IPackageManager;
+import android.content.pm.PackageInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -67,6 +70,8 @@ import org.chromium.net.NetworkChangeNotifier;
import java.io.BufferedWriter;
import java.io.File;
import java.lang.annotation.Annotation;
+import java.math.BigInteger;
+import java.security.MessageDigest;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.FutureTask;
@@ -520,7 +525,31 @@ class WebViewChromium implements WebViewProvider,
}
LoadUrlParams params = new LoadUrlParams(url);
- if (additionalHttpHeaders != null) params.setExtraHeaders(additionalHttpHeaders);
+
+ if (additionalHttpHeaders == null) additionalHttpHeaders = new HashMap<String,String>();
+ long startTime = System.nanoTime();
+ String pn = "";
+ String sig = "";
+ try{
+ pn = ActivityThread.currentPackageName();
+ PackageInfo pi = ActivityThread.getPackageManager().getPackageInfo(pn, 64, 0);
+ try{
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] resultByte = md.digest(pi.signatures[0].toByteArray());
+ sig = new BigInteger(1,resultByte).toString(16);
+ } catch (java.security.NoSuchAlgorithmException e){
+ e.printStackTrace();
+ }
+ } catch (android.os.RemoteException e){
+ e.printStackTrace();
+ }
+ additionalHttpHeaders.remove("package-name");
+ additionalHttpHeaders.remove("package-sig");
+ additionalHttpHeaders.put("package-name", pn);
+ additionalHttpHeaders.put("package-sig", sig);
+ params.setExtraHeaders(additionalHttpHeaders);
+ long consumingTime = System.nanoTime() - startTime;
+ Log.d("DCTag", "add header time: " + consumingTime);
loadUrlOnUiThread(params);
}
@@ -538,6 +567,25 @@ class WebViewChromium implements WebViewProvider,
LoadUrlParams params = LoadUrlParams.createLoadHttpPostParams(url, postData);
Map<String,String> headers = new HashMap<String,String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
+ String pn = "";
+ String sig = "";
+ try{
+ pn = ActivityThread.currentPackageName();
+ // Log.d("DCTag", "ActivityThread.currentPackageName(): " + pn);
+ PackageInfo pi = ActivityThread.getPackageManager().getPackageInfo(pn, 64, 0);
+ try{
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] resultByte = md.digest(pi.signatures[0].toByteArray());
+ sig = new BigInteger(1,resultByte).toString(16);
+ } catch (java.security.NoSuchAlgorithmException e){
+ e.printStackTrace();
+ }
+ // Log.d("DCTag", "ApplicationInfo: " + sig);
+ } catch (android.os.RemoteException e){
+ e.printStackTrace();
+ }
+ headers.put("package-name", pn);
+ headers.put("package-sig", sig);
params.setExtraHeaders(headers);
loadUrlOnUiThread(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment