2727import android .net .ProxyInfo ;
2828import android .net .Uri ;
2929import android .os .Handler ;
30+ import android .os .HandlerThread ;
3031import android .os .IBinder ;
3132import android .os .RemoteException ;
3233import android .os .ServiceManager ;
3940import com .android .net .IProxyCallback ;
4041import com .android .net .IProxyPortListener ;
4142import com .android .net .IProxyService ;
42- import com .android .server .IoThread ;
4343
4444import libcore .io .Streams ;
4545
46+ import java .io .ByteArrayOutputStream ;
4647import java .io .IOException ;
4748import java .net .URL ;
4849import java .net .URLConnection ;
@@ -66,6 +67,7 @@ public class PacManager {
6667 private static final int DELAY_1 = 0 ;
6768 private static final int DELAY_4 = 3 ;
6869 private static final int DELAY_LONG = 4 ;
70+ private static final long MAX_PAC_SIZE = 20 * 1000 * 1000 ;
6971
7072 /** Keep these values up-to-date with ProxyService.java */
7173 public static final String KEY_PROXY = "keyProxy" ;
@@ -123,15 +125,21 @@ public void run() {
123125 }
124126 };
125127
128+ private final HandlerThread mNetThread = new HandlerThread ("android.pacmanager" ,
129+ android .os .Process .THREAD_PRIORITY_DEFAULT );
130+ private final Handler mNetThreadHandler ;
131+
126132 class PacRefreshIntentReceiver extends BroadcastReceiver {
127133 public void onReceive (Context context , Intent intent ) {
128- IoThread . getHandler () .post (mPacDownloader );
134+ mNetThreadHandler .post (mPacDownloader );
129135 }
130136 }
131137
132138 public PacManager (Context context , Handler handler , int proxyMessage ) {
133139 mContext = context ;
134140 mLastPort = -1 ;
141+ mNetThread .start ();
142+ mNetThreadHandler = new Handler (mNetThread .getLooper ());
135143
136144 mPacRefreshIntent = PendingIntent .getBroadcast (
137145 context , 0 , new Intent (ACTION_PAC_REFRESH ), 0 );
@@ -199,7 +207,25 @@ public synchronized boolean setCurrentProxyScriptUrl(ProxyInfo proxy) {
199207 private static String get (Uri pacUri ) throws IOException {
200208 URL url = new URL (pacUri .toString ());
201209 URLConnection urlConnection = url .openConnection (java .net .Proxy .NO_PROXY );
202- return new String (Streams .readFully (urlConnection .getInputStream ()));
210+ long contentLength = -1 ;
211+ try {
212+ contentLength = Long .parseLong (urlConnection .getHeaderField ("Content-Length" ));
213+ } catch (NumberFormatException e ) {
214+ // Ignore
215+ }
216+ if (contentLength > MAX_PAC_SIZE ) {
217+ throw new IOException ("PAC too big: " + contentLength + " bytes" );
218+ }
219+ ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
220+ byte [] buffer = new byte [1024 ];
221+ int count ;
222+ while ((count = urlConnection .getInputStream ().read (buffer )) != -1 ) {
223+ bytes .write (buffer , 0 , count );
224+ if (bytes .size () > MAX_PAC_SIZE ) {
225+ throw new IOException ("PAC too big" );
226+ }
227+ }
228+ return bytes .toString ();
203229 }
204230
205231 private int getNextDelay (int currentDelay ) {
@@ -297,7 +323,7 @@ public void onServiceConnected(ComponentName component, IBinder binder) {
297323 } catch (RemoteException e ) {
298324 Log .e (TAG , "Unable to reach ProxyService - PAC will not be started" , e );
299325 }
300- IoThread . getHandler () .post (mPacDownloader );
326+ mNetThreadHandler .post (mPacDownloader );
301327 }
302328 }
303329 }
0 commit comments