3
3
import org .schabi .newpipe .extractor .NewPipe ;
4
4
import org .schabi .newpipe .extractor .exceptions .ReCaptchaException ;
5
5
import org .schabi .newpipe .extractor .localization .Localization ;
6
+ import org .schabi .newpipe .extractor .utils .Utils ;
6
7
7
8
import javax .annotation .Nonnull ;
8
9
import javax .annotation .Nullable ;
@@ -25,10 +26,10 @@ public abstract class Downloader {
25
26
* localization. It should only be used when the resource that will be fetched won't be affected
26
27
* by the localization.
27
28
*
28
- * @param url the URL that is pointing to the wanted resource
29
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
29
30
* @return the result of the GET request
30
31
*/
31
- public Response get (final String url ) throws IOException , ReCaptchaException {
32
+ public final Response get (final String url ) throws IOException , ReCaptchaException {
32
33
return get (url , null , NewPipe .getPreferredLocalization ());
33
34
}
34
35
@@ -37,24 +38,24 @@ public Response get(final String url) throws IOException, ReCaptchaException {
37
38
* <br>
38
39
* It will set the {@code Accept-Language} header to the language of the localization parameter.
39
40
*
40
- * @param url the URL that is pointing to the wanted resource
41
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
41
42
* @param localization the source of the value of the {@code Accept-Language} header
42
43
* @return the result of the GET request
43
44
*/
44
- public Response get (final String url , final Localization localization )
45
+ public final Response get (final String url , final Localization localization )
45
46
throws IOException , ReCaptchaException {
46
47
return get (url , null , localization );
47
48
}
48
49
49
50
/**
50
51
* Do a GET request with the specified headers.
51
52
*
52
- * @param url the URL that is pointing to the wanted resource
53
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
53
54
* @param headers a list of headers that will be used in the request.
54
55
* Any default headers <b>should</b> be overridden by these.
55
56
* @return the result of the GET request
56
57
*/
57
- public Response get (final String url , @ Nullable final Map <String , List <String >> headers )
58
+ public final Response get (final String url , @ Nullable final Map <String , List <String >> headers )
58
59
throws IOException , ReCaptchaException {
59
60
return get (url , headers , NewPipe .getPreferredLocalization ());
60
61
}
@@ -64,44 +65,42 @@ public Response get(final String url, @Nullable final Map<String, List<String>>
64
65
* <br>
65
66
* It will set the {@code Accept-Language} header to the language of the localization parameter.
66
67
*
67
- * @param url the URL that is pointing to the wanted resource
68
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
68
69
* @param headers a list of headers that will be used in the request.
69
70
* Any default headers <b>should</b> be overridden by these.
70
71
* @param localization the source of the value of the {@code Accept-Language} header
71
72
* @return the result of the GET request
72
73
*/
73
- public Response get (final String url ,
74
+ public final Response get (final String url ,
74
75
@ Nullable final Map <String , List <String >> headers ,
75
76
final Localization localization )
76
77
throws IOException , ReCaptchaException {
77
- return execute (Request .newBuilder ()
78
+ return executeIfHttps (Request .newBuilder ()
78
79
.get (url )
79
- .headers (headers )
80
- .localization (localization )
81
80
.build ());
82
81
}
83
82
84
83
/**
85
84
* Do a HEAD request.
86
85
*
87
- * @param url the URL that is pointing to the wanted resource
86
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
88
87
* @return the result of the HEAD request
89
88
*/
90
- public Response head (final String url ) throws IOException , ReCaptchaException {
89
+ public final Response head (final String url ) throws IOException , ReCaptchaException {
91
90
return head (url , null );
92
91
}
93
92
94
93
/**
95
94
* Do a HEAD request with the specified headers.
96
95
*
97
- * @param url the URL that is pointing to the wanted resource
96
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
98
97
* @param headers a list of headers that will be used in the request.
99
98
* Any default headers <b>should</b> be overridden by these.
100
99
* @return the result of the HEAD request
101
100
*/
102
- public Response head (final String url , @ Nullable final Map <String , List <String >> headers )
101
+ public final Response head (final String url , @ Nullable final Map <String , List <String >> headers )
103
102
throws IOException , ReCaptchaException {
104
- return execute (Request .newBuilder ()
103
+ return executeIfHttps (Request .newBuilder ()
105
104
.head (url )
106
105
.headers (headers )
107
106
.build ());
@@ -110,13 +109,13 @@ public Response head(final String url, @Nullable final Map<String, List<String>>
110
109
/**
111
110
* Do a POST request with the specified headers, sending the data array.
112
111
*
113
- * @param url the URL that is pointing to the wanted resource
112
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
114
113
* @param headers a list of headers that will be used in the request.
115
114
* Any default headers <b>should</b> be overridden by these.
116
115
* @param dataToSend byte array that will be sent when doing the request.
117
116
* @return the result of the POST request
118
117
*/
119
- public Response post (final String url ,
118
+ public final Response post (final String url ,
120
119
@ Nullable final Map <String , List <String >> headers ,
121
120
@ Nullable final byte [] dataToSend )
122
121
throws IOException , ReCaptchaException {
@@ -128,19 +127,19 @@ public Response post(final String url,
128
127
* <br>
129
128
* It will set the {@code Accept-Language} header to the language of the localization parameter.
130
129
*
131
- * @param url the URL that is pointing to the wanted resource
130
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
132
131
* @param headers a list of headers that will be used in the request.
133
132
* Any default headers <b>should</b> be overridden by these.
134
133
* @param dataToSend byte array that will be sent when doing the request.
135
134
* @param localization the source of the value of the {@code Accept-Language} header
136
135
* @return the result of the POST request
137
136
*/
138
- public Response post (final String url ,
137
+ public final Response post (final String url ,
139
138
@ Nullable final Map <String , List <String >> headers ,
140
139
@ Nullable final byte [] dataToSend ,
141
140
final Localization localization )
142
141
throws IOException , ReCaptchaException {
143
- return execute (Request .newBuilder ()
142
+ return executeIfHttps (Request .newBuilder ()
144
143
.post (url , dataToSend )
145
144
.headers (headers )
146
145
.localization (localization )
@@ -151,7 +150,7 @@ public Response post(final String url,
151
150
* Convenient method to send a POST request using the specified value of the
152
151
* {@code Content-Type} header with a given {@link Localization}.
153
152
*
154
- * @param url the URL that is pointing to the wanted resource
153
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
155
154
* @param headers a list of headers that will be used in the request.
156
155
* Any default headers <b>should</b> be overridden by these.
157
156
* @param dataToSend byte array that will be sent when doing the request.
@@ -161,7 +160,7 @@ public Response post(final String url,
161
160
* @return the result of the POST request
162
161
* @see #post(String, Map, byte[], Localization)
163
162
*/
164
- public Response postWithContentType (final String url ,
163
+ public final Response postWithContentType (final String url ,
165
164
@ Nullable final Map <String , List <String >> headers ,
166
165
@ Nullable final byte [] dataToSend ,
167
166
final Localization localization ,
@@ -179,7 +178,7 @@ public Response postWithContentType(final String url,
179
178
* Convenient method to send a POST request using the specified value of the
180
179
* {@code Content-Type} header.
181
180
*
182
- * @param url the URL that is pointing to the wanted resource
181
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
183
182
* @param headers a list of headers that will be used in the request.
184
183
* Any default headers <b>should</b> be overridden by these.
185
184
* @param dataToSend byte array that will be sent when doing the request.
@@ -188,7 +187,7 @@ public Response postWithContentType(final String url,
188
187
* @return the result of the POST request
189
188
* @see #post(String, Map, byte[], Localization)
190
189
*/
191
- public Response postWithContentType (final String url ,
190
+ public final Response postWithContentType (final String url ,
192
191
@ Nullable final Map <String , List <String >> headers ,
193
192
@ Nullable final byte [] dataToSend ,
194
193
final String contentType )
@@ -201,15 +200,15 @@ public Response postWithContentType(final String url,
201
200
* Convenient method to send a POST request the JSON mime type as the value of the
202
201
* {@code Content-Type} header with a given {@link Localization}.
203
202
*
204
- * @param url the URL that is pointing to the wanted resource
203
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
205
204
* @param headers a list of headers that will be used in the request.
206
205
* Any default headers <b>should</b> be overridden by these.
207
206
* @param dataToSend byte array that will be sent when doing the request.
208
207
* @param localization the source of the value of the {@code Accept-Language} header
209
208
* @return the result of the POST request
210
209
* @see #post(String, Map, byte[], Localization)
211
210
*/
212
- public Response postWithContentTypeJson (final String url ,
211
+ public final Response postWithContentTypeJson (final String url ,
213
212
@ Nullable final Map <String , List <String >> headers ,
214
213
@ Nullable final byte [] dataToSend ,
215
214
final Localization localization )
@@ -221,26 +220,39 @@ public Response postWithContentTypeJson(final String url,
221
220
* Convenient method to send a POST request the JSON mime type as the value of the
222
221
* {@code Content-Type} header.
223
222
*
224
- * @param url the URL that is pointing to the wanted resource
223
+ * @param url the URL that is pointing to the wanted resource (must start with HTTPS)
225
224
* @param headers a list of headers that will be used in the request.
226
225
* Any default headers <b>should</b> be overridden by these.
227
226
* @param dataToSend byte array that will be sent when doing the request.
228
227
* @return the result of the POST request
229
228
* @see #post(String, Map, byte[], Localization)
230
229
*/
231
- public Response postWithContentTypeJson (final String url ,
230
+ public final Response postWithContentTypeJson (final String url ,
232
231
@ Nullable final Map <String , List <String >> headers ,
233
232
@ Nullable final byte [] dataToSend )
234
233
throws IOException , ReCaptchaException {
235
234
return postWithContentTypeJson (url , headers , dataToSend ,
236
235
NewPipe .getPreferredLocalization ());
237
236
}
238
237
238
+ public final Response executeIfHttps (final @ Nonnull Request request )
239
+ throws IOException , ReCaptchaException {
240
+
241
+ if (!request .url ().equals (Utils .replaceHttpWithHttps (request .url ()))) {
242
+ throw new IOException (
243
+ "All queries must be made using HTTPS. Extractors must guarantee "
244
+ + "that HTTPS links are provided."
245
+ );
246
+ } else {
247
+ return execute (request );
248
+ }
249
+ }
250
+
239
251
/**
240
252
* Do a request using the specified {@link Request} object.
241
253
*
242
254
* @return the result of the request
243
255
*/
244
- public abstract Response execute (@ Nonnull Request request )
256
+ protected abstract Response execute (@ Nonnull Request request )
245
257
throws IOException , ReCaptchaException ;
246
258
}
0 commit comments