|
4 | 4 | import org.schabi.newpipe.extractor.downloader.Request; |
5 | 5 | import org.schabi.newpipe.extractor.downloader.Response; |
6 | 6 | import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; |
7 | | -import org.schabi.newpipe.extractor.localization.Localization; |
8 | 7 |
|
9 | | -import javax.annotation.Nonnull; |
10 | | -import javax.annotation.Nullable; |
11 | | -import javax.net.ssl.HttpsURLConnection; |
12 | 8 | import java.io.IOException; |
13 | | -import java.io.InputStream; |
14 | | -import java.io.InputStreamReader; |
15 | | -import java.io.OutputStream; |
16 | | -import java.net.URL; |
17 | | -import java.net.URLConnection; |
18 | 9 | import java.util.List; |
19 | 10 | import java.util.Map; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import javax.annotation.Nonnull; |
| 14 | +import javax.annotation.Nullable; |
20 | 15 |
|
21 | | -public class DownloaderTestImpl extends Downloader { |
| 16 | +import okhttp3.OkHttpClient; |
| 17 | +import okhttp3.RequestBody; |
| 18 | +import okhttp3.ResponseBody; |
22 | 19 |
|
23 | | - private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0"; |
24 | | - private static final String DEFAULT_HTTP_ACCEPT_LANGUAGE = "en"; |
| 20 | +public final class DownloaderTestImpl extends Downloader { |
| 21 | + private static final String USER_AGENT |
| 22 | + = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0"; |
| 23 | + private static DownloaderTestImpl instance; |
| 24 | + private OkHttpClient client; |
25 | 25 |
|
26 | | - private static DownloaderTestImpl instance = null; |
| 26 | + private DownloaderTestImpl(final OkHttpClient.Builder builder) { |
| 27 | + this.client = builder.readTimeout(30, TimeUnit.SECONDS).build(); |
| 28 | + } |
27 | 29 |
|
28 | | - private DownloaderTestImpl() { |
| 30 | + /** |
| 31 | + * It's recommended to call exactly once in the entire lifetime of the application. |
| 32 | + * |
| 33 | + * @param builder if null, default builder will be used |
| 34 | + * @return a new instance of {@link DownloaderTestImpl} |
| 35 | + */ |
| 36 | + public static DownloaderTestImpl init(@Nullable final OkHttpClient.Builder builder) { |
| 37 | + instance = new DownloaderTestImpl( |
| 38 | + builder != null ? builder : new OkHttpClient.Builder()); |
| 39 | + return instance; |
29 | 40 | } |
30 | 41 |
|
31 | 42 | public static DownloaderTestImpl getInstance() { |
32 | 43 | if (instance == null) { |
33 | | - synchronized (DownloaderTestImpl.class) { |
34 | | - if (instance == null) { |
35 | | - instance = new DownloaderTestImpl(); |
36 | | - } |
37 | | - } |
| 44 | + init(null); |
38 | 45 | } |
39 | 46 | return instance; |
40 | 47 | } |
41 | 48 |
|
42 | | - private void setDefaultHeaders(URLConnection connection) { |
43 | | - connection.setRequestProperty("User-Agent", USER_AGENT); |
44 | | - connection.setRequestProperty("Accept-Language", DEFAULT_HTTP_ACCEPT_LANGUAGE); |
45 | | - } |
46 | | - |
47 | 49 | @Override |
48 | | - public Response execute(@Nonnull Request request) throws IOException, ReCaptchaException { |
| 50 | + public Response execute(@Nonnull final Request request) |
| 51 | + throws IOException, ReCaptchaException { |
49 | 52 | final String httpMethod = request.httpMethod(); |
50 | 53 | final String url = request.url(); |
51 | 54 | final Map<String, List<String>> headers = request.headers(); |
52 | | - @Nullable final byte[] dataToSend = request.dataToSend(); |
53 | | - @Nullable final Localization localization = request.localization(); |
54 | | - |
55 | | - final HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); |
| 55 | + final byte[] dataToSend = request.dataToSend(); |
56 | 56 |
|
57 | | - connection.setConnectTimeout(30 * 1000); // 30s |
58 | | - connection.setReadTimeout(30 * 1000); // 30s |
59 | | - connection.setRequestMethod(httpMethod); |
| 57 | + RequestBody requestBody = null; |
| 58 | + if (dataToSend != null) { |
| 59 | + requestBody = RequestBody.create(null, dataToSend); |
| 60 | + } |
60 | 61 |
|
61 | | - setDefaultHeaders(connection); |
| 62 | + final okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder() |
| 63 | + .method(httpMethod, requestBody).url(url) |
| 64 | + .addHeader("User-Agent", USER_AGENT); |
62 | 65 |
|
63 | 66 | for (Map.Entry<String, List<String>> pair : headers.entrySet()) { |
64 | 67 | final String headerName = pair.getKey(); |
65 | 68 | final List<String> headerValueList = pair.getValue(); |
66 | 69 |
|
67 | 70 | if (headerValueList.size() > 1) { |
68 | | - connection.setRequestProperty(headerName, null); |
| 71 | + requestBuilder.removeHeader(headerName); |
69 | 72 | for (String headerValue : headerValueList) { |
70 | | - connection.addRequestProperty(headerName, headerValue); |
| 73 | + requestBuilder.addHeader(headerName, headerValue); |
71 | 74 | } |
72 | 75 | } else if (headerValueList.size() == 1) { |
73 | | - connection.setRequestProperty(headerName, headerValueList.get(0)); |
| 76 | + requestBuilder.header(headerName, headerValueList.get(0)); |
74 | 77 | } |
75 | | - } |
76 | 78 |
|
77 | | - @Nullable OutputStream outputStream = null; |
78 | | - @Nullable InputStreamReader input = null; |
79 | | - try { |
80 | | - if (dataToSend != null && dataToSend.length > 0) { |
81 | | - connection.setDoOutput(true); |
82 | | - connection.setRequestProperty("Content-Length", dataToSend.length + ""); |
83 | | - outputStream = connection.getOutputStream(); |
84 | | - outputStream.write(dataToSend); |
85 | | - } |
| 79 | + } |
86 | 80 |
|
87 | | - final InputStream inputStream = connection.getInputStream(); |
88 | | - final StringBuilder response = new StringBuilder(); |
| 81 | + final okhttp3.Response response = client.newCall(requestBuilder.build()).execute(); |
89 | 82 |
|
90 | | - // Not passing any charset for decoding here... something to keep in mind. |
91 | | - input = new InputStreamReader(inputStream); |
| 83 | + if (response.code() == 429) { |
| 84 | + response.close(); |
92 | 85 |
|
93 | | - int readCount; |
94 | | - char[] buffer = new char[32 * 1024]; |
95 | | - while ((readCount = input.read(buffer)) != -1) { |
96 | | - response.append(buffer, 0, readCount); |
97 | | - } |
| 86 | + throw new ReCaptchaException("reCaptcha Challenge requested", url); |
| 87 | + } |
98 | 88 |
|
99 | | - final int responseCode = connection.getResponseCode(); |
100 | | - final String responseMessage = connection.getResponseMessage(); |
101 | | - final Map<String, List<String>> responseHeaders = connection.getHeaderFields(); |
102 | | - final String latestUrl = connection.getURL().toString(); |
103 | | - |
104 | | - return new Response(responseCode, responseMessage, responseHeaders, response.toString(), latestUrl); |
105 | | - } catch (Exception e) { |
106 | | - final int responseCode = connection.getResponseCode(); |
107 | | - |
108 | | - /* |
109 | | - * HTTP 429 == Too Many Request |
110 | | - * Receive from Youtube.com = ReCaptcha challenge request |
111 | | - * See : https://github.com/rg3/youtube-dl/issues/5138 |
112 | | - */ |
113 | | - if (responseCode == 429) { |
114 | | - throw new ReCaptchaException("reCaptcha Challenge requested", url); |
115 | | - } else if (responseCode != -1) { |
116 | | - final String latestUrl = connection.getURL().toString(); |
117 | | - return new Response(responseCode, connection.getResponseMessage(), connection.getHeaderFields(), null, latestUrl); |
118 | | - } |
| 89 | + final ResponseBody body = response.body(); |
| 90 | + String responseBodyToReturn = null; |
119 | 91 |
|
120 | | - throw new IOException("Error occurred while fetching the content", e); |
121 | | - } finally { |
122 | | - if (outputStream != null) outputStream.close(); |
123 | | - if (input != null) input.close(); |
| 92 | + if (body != null) { |
| 93 | + responseBodyToReturn = body.string(); |
124 | 94 | } |
| 95 | + |
| 96 | + final String latestUrl = response.request().url().toString(); |
| 97 | + return new Response(response.code(), response.message(), response.headers().toMultimap(), |
| 98 | + responseBodyToReturn, latestUrl); |
125 | 99 | } |
126 | 100 | } |
0 commit comments