Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/www/URLConnection.java
41159 views
1
/*
2
* Copyright (c) 1995, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.net.www;
27
28
import java.io.IOException;
29
import java.net.URL;
30
import java.util.*;
31
32
/**
33
* A class to represent an active connection to an object
34
* represented by a URL.
35
* @author James Gosling
36
*/
37
38
public abstract class URLConnection extends java.net.URLConnection {
39
40
/** The URL that it is connected to */
41
42
private String contentType;
43
private int contentLength = -1;
44
45
protected MessageHeader properties;
46
47
/**
48
* Create a URLConnection object. These should not be created directly:
49
* instead they should be created by protocol handlers in response to
50
* URL.openConnection.
51
* @param u The URL that this connects to.
52
*/
53
public URLConnection (URL u) {
54
super(u);
55
properties = new MessageHeader();
56
}
57
58
/**
59
* Call this routine to get the property list for this object.
60
* Properties (like content-type) that have explicit getXX() methods
61
* associated with them should be accessed using those methods.
62
*/
63
public MessageHeader getProperties() {
64
return properties;
65
}
66
67
/** Call this routine to set the property list for this object. */
68
public void setProperties(MessageHeader properties) {
69
this.properties = properties;
70
}
71
72
public void setRequestProperty(String key, String value) {
73
if(connected)
74
throw new IllegalStateException("Already connected");
75
if (key == null)
76
throw new NullPointerException ("key cannot be null");
77
properties.set(key, value);
78
}
79
80
/**
81
* The following three methods addRequestProperty, getRequestProperty,
82
* and getRequestProperties were copied from the superclass implementation
83
* before it was changed by CR:6230836, to maintain backward compatibility.
84
*/
85
public void addRequestProperty(String key, String value) {
86
if (connected)
87
throw new IllegalStateException("Already connected");
88
if (key == null)
89
throw new NullPointerException ("key is null");
90
}
91
92
public String getRequestProperty(String key) {
93
if (connected)
94
throw new IllegalStateException("Already connected");
95
return null;
96
}
97
98
public Map<String,List<String>> getRequestProperties() {
99
if (connected)
100
throw new IllegalStateException("Already connected");
101
return Collections.emptyMap();
102
}
103
104
public String getHeaderField(String name) {
105
try {
106
getInputStream();
107
} catch (Exception e) {
108
return null;
109
}
110
return properties == null ? null : properties.findValue(name);
111
}
112
113
114
Map<String, List<String>> headerFields;
115
116
@Override
117
public Map<String, List<String>> getHeaderFields() {
118
if (headerFields == null) {
119
try {
120
getInputStream();
121
if (properties == null) {
122
headerFields = super.getHeaderFields();
123
} else {
124
headerFields = properties.getHeaders();
125
}
126
} catch (IOException e) {
127
return super.getHeaderFields();
128
}
129
}
130
return headerFields;
131
}
132
133
/**
134
* Return the key for the nth header field. Returns null if
135
* there are fewer than n fields. This can be used to iterate
136
* through all the headers in the message.
137
*/
138
public String getHeaderFieldKey(int n) {
139
try {
140
getInputStream();
141
} catch (Exception e) {
142
return null;
143
}
144
MessageHeader props = properties;
145
return props == null ? null : props.getKey(n);
146
}
147
148
/**
149
* Return the value for the nth header field. Returns null if
150
* there are fewer than n fields. This can be used in conjunction
151
* with getHeaderFieldKey to iterate through all the headers in the message.
152
*/
153
public String getHeaderField(int n) {
154
try {
155
getInputStream();
156
} catch (Exception e) {
157
return null;
158
}
159
MessageHeader props = properties;
160
return props == null ? null : props.getValue(n);
161
}
162
163
/**
164
* Call this routine to get the content-type associated with this
165
* object.
166
*/
167
public String getContentType() {
168
if (contentType == null)
169
contentType = getHeaderField("content-type");
170
if (contentType == null) {
171
String ct = null;
172
try {
173
ct = guessContentTypeFromStream(getInputStream());
174
} catch(java.io.IOException e) {
175
}
176
String ce = properties.findValue("content-encoding");
177
if (ct == null) {
178
ct = properties.findValue("content-type");
179
180
if (ct == null)
181
if (url.getFile().endsWith("/"))
182
ct = "text/html";
183
else
184
ct = guessContentTypeFromName(url.getFile());
185
}
186
187
/*
188
* If the Mime header had a Content-encoding field and its value
189
* was not one of the values that essentially indicate no
190
* encoding, we force the content type to be unknown. This will
191
* cause a save dialog to be presented to the user. It is not
192
* ideal but is better than what we were previously doing, namely
193
* bringing up an image tool for compressed tar files.
194
*/
195
196
if (ct == null || ce != null &&
197
!(ce.equalsIgnoreCase("7bit")
198
|| ce.equalsIgnoreCase("8bit")
199
|| ce.equalsIgnoreCase("binary")))
200
ct = "content/unknown";
201
setContentType(ct);
202
}
203
return contentType;
204
}
205
206
/**
207
* Set the content type of this URL to a specific value.
208
* @param type The content type to use. One of the
209
* content_* static variables in this
210
* class should be used.
211
* e.g. setType(URL.content_html);
212
*/
213
public void setContentType(String type) {
214
contentType = type;
215
properties.set("content-type", type);
216
}
217
218
/**
219
* Call this routine to get the content-length associated with this
220
* object.
221
*/
222
public int getContentLength() {
223
try {
224
getInputStream();
225
} catch (Exception e) {
226
return -1;
227
}
228
int l = contentLength;
229
if (l < 0) {
230
try {
231
l = Integer.parseInt(properties.findValue("content-length"));
232
setContentLength(l);
233
} catch(Exception e) {
234
}
235
}
236
return l;
237
}
238
239
/**
240
* Call this routine to set the content-length associated with this
241
* object.
242
*/
243
protected void setContentLength(int length) {
244
contentLength = length;
245
properties.set("content-length", String.valueOf(length));
246
}
247
248
/**
249
* Returns true if the data associated with this URL can be cached.
250
*/
251
public boolean canCache() {
252
return url.getFile().indexOf('?') < 0 /* && url.postData == null
253
REMIND */ ;
254
}
255
256
/**
257
* Call this to close the connection and flush any remaining data.
258
* Overriders must remember to call super.close()
259
*/
260
public void close() {
261
url = null;
262
}
263
264
private static HashMap<String,Void> proxiedHosts = new HashMap<>();
265
266
public static synchronized void setProxiedHost(String host) {
267
proxiedHosts.put(host.toLowerCase(), null);
268
}
269
270
public static synchronized boolean isProxiedHost(String host) {
271
return proxiedHosts.containsKey(host.toLowerCase());
272
}
273
}
274
275