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.ServerResponse; |
| 27 import org.adblockplus.libadblockplus.WebRequest; |
| 28 import org.adblockplus.libadblockplus.ServerResponse.NsStatus; |
| 29 |
| 30 import android.util.Log; |
| 31 |
| 32 import com.github.rjeschke.neetutils.Strings; |
| 33 import com.github.rjeschke.neetutils.collections.Tuple; |
| 34 |
| 35 public class AndroidWebRequest extends WebRequest |
| 36 { |
| 37 public final String TAG = Utils.getTag(WebRequest.class); |
| 38 |
| 39 @Override |
| 40 public ServerResponse httpGET(final String urlStr, final List<Tuple<String, St
ring>> headers) |
| 41 { |
| 42 try |
| 43 { |
| 44 final URL url = new URL(urlStr); |
| 45 Log.d(this.TAG, "Downloading from: " + url); |
| 46 |
| 47 final HttpURLConnection connection = (HttpURLConnection)url.openConnection
(); |
| 48 connection.setRequestMethod("GET"); |
| 49 connection.connect(); |
| 50 |
| 51 final ServerResponse response = new ServerResponse(); |
| 52 response.setResponseStatus(connection.getResponseCode()); |
| 53 |
| 54 if (response.getResponseStatus() == 200) |
| 55 { |
| 56 final InputStream in = connection.getInputStream(); |
| 57 |
| 58 final byte[] buffer = new byte[4096]; |
| 59 |
| 60 byte[] out = new byte[65536]; |
| 61 |
| 62 int pos = 0; |
| 63 for (;;) |
| 64 { |
| 65 final int read = in.read(buffer); |
| 66 if (read < 0) |
| 67 { |
| 68 break; |
| 69 } |
| 70 if (pos + read > out.length) |
| 71 { |
| 72 final byte[] old = out; |
| 73 out = new byte[out.length + 65536]; |
| 74 System.arraycopy(old, 0, out, 0, pos); |
| 75 } |
| 76 System.arraycopy(buffer, 0, out, pos, read); |
| 77 pos += read; |
| 78 } |
| 79 |
| 80 connection.disconnect(); |
| 81 |
| 82 response.setStatus(NsStatus.OK); |
| 83 response.setResponse(Strings.from(out, 0, pos)); |
| 84 } |
| 85 else |
| 86 { |
| 87 response.setStatus(NsStatus.ERROR_FAILURE); |
| 88 } |
| 89 return response; |
| 90 } |
| 91 catch (final Throwable t) |
| 92 { |
| 93 throw new AdblockPlusException("WebRequest failed", t); |
| 94 } |
| 95 } |
| 96 } |
OLD | NEW |