OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.android; |
| 19 |
| 20 import java.io.InputStream; |
| 21 import java.net.HttpURLConnection; |
| 22 import java.net.URL; |
| 23 import java.util.List; |
| 24 |
| 25 import org.adblockplus.libadblockplus.AdblockPlusException; |
| 26 import org.adblockplus.libadblockplus.HeaderEntry; |
| 27 import org.adblockplus.libadblockplus.ServerResponse; |
| 28 import org.adblockplus.libadblockplus.WebRequest; |
| 29 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
| 30 |
| 31 import android.util.Log; |
| 32 |
| 33 public class AndroidWebRequest extends WebRequest |
| 34 { |
| 35 public final String TAG = Utils.getTag(WebRequest.class); |
| 36 |
| 37 @Override |
| 38 public ServerResponse httpGET(final String urlStr, final List<HeaderEntry> hea
ders) |
| 39 { |
| 40 try |
| 41 { |
| 42 final URL url = new URL(urlStr); |
| 43 Log.d(this.TAG, "Downloading from: " + url); |
| 44 |
| 45 final HttpURLConnection connection = (HttpURLConnection) url.openConnectio
n(); |
| 46 connection.setRequestMethod("GET"); |
| 47 connection.connect(); |
| 48 |
| 49 final ServerResponse response = new ServerResponse(); |
| 50 response.setResponseStatus(connection.getResponseCode()); |
| 51 |
| 52 if (response.getResponseStatus() == 200) |
| 53 { |
| 54 final InputStream in = connection.getInputStream(); |
| 55 |
| 56 final byte[] buffer = new byte[4096]; |
| 57 |
| 58 byte[] out = new byte[65536]; |
| 59 |
| 60 int pos = 0; |
| 61 for (;;) |
| 62 { |
| 63 final int read = in.read(buffer); |
| 64 if (read < 0) |
| 65 { |
| 66 break; |
| 67 } |
| 68 if (pos + read > out.length) |
| 69 { |
| 70 final byte[] old = out; |
| 71 out = new byte[out.length + 65536]; |
| 72 System.arraycopy(old, 0, out, 0, pos); |
| 73 } |
| 74 System.arraycopy(buffer, 0, out, pos, read); |
| 75 pos += read; |
| 76 } |
| 77 |
| 78 connection.disconnect(); |
| 79 |
| 80 response.setStatus(NsStatus.OK); |
| 81 response.setResponse(new String(out, 0, pos, "utf-8")); |
| 82 } |
| 83 else |
| 84 { |
| 85 response.setStatus(NsStatus.ERROR_FAILURE); |
| 86 } |
| 87 return response; |
| 88 } |
| 89 catch (final Throwable t) |
| 90 { |
| 91 throw new AdblockPlusException("WebRequest failed", t); |
| 92 } |
| 93 } |
| 94 } |
OLD | NEW |