1 /*
2  * Copyright (c) 1996, 2017, 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 /*
27  * NOTE: This class lives in the package sun.net.www.protocol.https.
28  * There is a copy in com.sun.net.ssl.internal.www.protocol.https for JSSE
29  * 1.0.2 compatibility. It is 100% identical except the package and extends
30  * lines. Any changes should be made to be class in sun.net.* and then copied
31  * to com.sun.net.*.
32  */
33 
34 // For both copies of the file, uncomment one line and comment the other
35 // package sun.net.www.protocol.https;
36 package com.sun.net.ssl.internal.www.protocol.https;
37 
38 import java.net.URL;
39 import java.net.Proxy;
40 import java.net.ProtocolException;
41 import java.net.MalformedURLException;
42 import java.io.*;
43 import java.net.Authenticator;
44 import javax.net.ssl.*;
45 import java.security.Permission;
46 import java.util.Map;
47 import java.util.List;
48 import sun.net.www.http.HttpClient;
49 
50 /**
51  * A class to represent an HTTP connection to a remote object.
52  *
53  * Ideally, this class should subclass and inherit the http handler
54  * implementation, but it can't do so because that class have the
55  * wrong Java Type.  Thus it uses the delegate (aka, the
56  * Adapter/Wrapper design pattern) to reuse code from the http
57  * handler.
58  *
59  * Since it would use a delegate to access
60  * sun.net.www.protocol.http.HttpURLConnection functionalities, it
61  * needs to implement all public methods in it's super class and all
62  * the way to Object.
63  *
64  */
65 
66 // For both copies of the file, uncomment one line and comment the other
67 // public class HttpsURLConnectionImpl
68 //      extends javax.net.ssl.HttpsURLConnection {
69 @Deprecated(since="9")
70 @SuppressWarnings("deprecation") // HttpsURLConnection is deprecated
71 public class HttpsURLConnectionOldImpl
72         extends com.sun.net.ssl.HttpsURLConnection {
73 
74     private DelegateHttpsURLConnection delegate;
75 
76 // For both copies of the file, uncomment one line and comment the other
77 //    HttpsURLConnectionImpl(URL u, Handler handler) throws IOException {
HttpsURLConnectionOldImpl(URL u, Handler handler)78     HttpsURLConnectionOldImpl(URL u, Handler handler) throws IOException {
79         this(u, null, handler);
80     }
81 
checkURL(URL u)82     static URL checkURL(URL u) throws IOException {
83         if (u != null) {
84             if (u.toExternalForm().indexOf('\n') > -1) {
85                 throw new MalformedURLException("Illegal character in URL");
86             }
87         }
88         return u;
89     }
90 // For both copies of the file, uncomment one line and comment the other
91 //    HttpsURLConnectionImpl(URL u, Handler handler) throws IOException {
HttpsURLConnectionOldImpl(URL u, Proxy p, Handler handler)92     HttpsURLConnectionOldImpl(URL u, Proxy p, Handler handler) throws IOException {
93         super(checkURL(u));
94         delegate = new DelegateHttpsURLConnection(url, p, handler, this);
95     }
96 
97     /**
98      * Create a new HttpClient object, bypassing the cache of
99      * HTTP client objects/connections.
100      *
101      * @param url       the URL being accessed
102      */
setNewClient(URL url)103     protected void setNewClient(URL url) throws IOException {
104         delegate.setNewClient(url, false);
105     }
106 
107     /**
108      * Obtain a HttpClient object. Use the cached copy if specified.
109      *
110      * @param url       the URL being accessed
111      * @param useCache  whether the cached connection should be used
112      *                  if present
113      */
setNewClient(URL url, boolean useCache)114     protected void setNewClient(URL url, boolean useCache)
115             throws IOException {
116         delegate.setNewClient(url, useCache);
117     }
118 
119     /**
120      * Create a new HttpClient object, set up so that it uses
121      * per-instance proxying to the given HTTP proxy.  This
122      * bypasses the cache of HTTP client objects/connections.
123      *
124      * @param url       the URL being accessed
125      * @param proxyHost the proxy host to use
126      * @param proxyPort the proxy port to use
127      */
setProxiedClient(URL url, String proxyHost, int proxyPort)128     protected void setProxiedClient(URL url, String proxyHost, int proxyPort)
129             throws IOException {
130         delegate.setProxiedClient(url, proxyHost, proxyPort);
131     }
132 
133     /**
134      * Obtain a HttpClient object, set up so that it uses per-instance
135      * proxying to the given HTTP proxy. Use the cached copy of HTTP
136      * client objects/connections if specified.
137      *
138      * @param url       the URL being accessed
139      * @param proxyHost the proxy host to use
140      * @param proxyPort the proxy port to use
141      * @param useCache  whether the cached connection should be used
142      *                  if present
143      */
setProxiedClient(URL url, String proxyHost, int proxyPort, boolean useCache)144     protected void setProxiedClient(URL url, String proxyHost, int proxyPort,
145             boolean useCache) throws IOException {
146         delegate.setProxiedClient(url, proxyHost, proxyPort, useCache);
147     }
148 
149     /**
150      * Implements the HTTP protocol handler's "connect" method,
151      * establishing an SSL connection to the server as necessary.
152      */
connect()153     public void connect() throws IOException {
154         delegate.connect();
155     }
156 
157     /**
158      * Used by subclass to access "connected" variable.  Since we are
159      * delegating the actual implementation to "delegate", we need to
160      * delegate the access of "connected" as well.
161      */
isConnected()162     protected boolean isConnected() {
163         return delegate.isConnected();
164     }
165 
166     /**
167      * Used by subclass to access "connected" variable.  Since we are
168      * delegating the actual implementation to "delegate", we need to
169      * delegate the access of "connected" as well.
170      */
setConnected(boolean conn)171     protected void setConnected(boolean conn) {
172         delegate.setConnected(conn);
173     }
174 
175     /**
176      * Returns the cipher suite in use on this connection.
177      */
getCipherSuite()178     public String getCipherSuite() {
179         return delegate.getCipherSuite();
180     }
181 
182     /**
183      * Returns the certificate chain the client sent to the
184      * server, or null if the client did not authenticate.
185      */
186     public java.security.cert.Certificate []
getLocalCertificates()187         getLocalCertificates() {
188         return delegate.getLocalCertificates();
189     }
190 
191     /**
192      * Returns the server's certificate chain, or throws
193      * SSLPeerUnverified Exception if
194      * the server did not authenticate.
195      */
196     public java.security.cert.Certificate []
getServerCertificates()197         getServerCertificates() throws SSLPeerUnverifiedException {
198         return delegate.getServerCertificates();
199     }
200 
201     /*
202      * Allowable input/output sequences:
203      * [interpreted as POST/PUT]
204      * - get output, [write output,] get input, [read input]
205      * - get output, [write output]
206      * [interpreted as GET]
207      * - get input, [read input]
208      * Disallowed:
209      * - get input, [read input,] get output, [write output]
210      */
211 
getOutputStream()212     public synchronized OutputStream getOutputStream() throws IOException {
213         return delegate.getOutputStream();
214     }
215 
getInputStream()216     public synchronized InputStream getInputStream() throws IOException {
217         return delegate.getInputStream();
218     }
219 
getErrorStream()220     public InputStream getErrorStream() {
221         return delegate.getErrorStream();
222     }
223 
224     /**
225      * Disconnect from the server.
226      */
disconnect()227     public void disconnect() {
228         delegate.disconnect();
229     }
230 
usingProxy()231     public boolean usingProxy() {
232         return delegate.usingProxy();
233     }
234 
235     /**
236      * Returns an unmodifiable Map of the header fields.
237      * The Map keys are Strings that represent the
238      * response-header field names. Each Map value is an
239      * unmodifiable List of Strings that represents
240      * the corresponding field values.
241      *
242      * @return a Map of header fields
243      * @since 1.4
244      */
getHeaderFields()245     public Map<String,List<String>> getHeaderFields() {
246         return delegate.getHeaderFields();
247     }
248 
249     /**
250      * Gets a header field by name. Returns null if not known.
251      * @param name the name of the header field
252      */
getHeaderField(String name)253     public String getHeaderField(String name) {
254         return delegate.getHeaderField(name);
255     }
256 
257     /**
258      * Gets a header field by index. Returns null if not known.
259      * @param n the index of the header field
260      */
getHeaderField(int n)261     public String getHeaderField(int n) {
262         return delegate.getHeaderField(n);
263     }
264 
265     /**
266      * Gets a header field by index. Returns null if not known.
267      * @param n the index of the header field
268      */
getHeaderFieldKey(int n)269     public String getHeaderFieldKey(int n) {
270         return delegate.getHeaderFieldKey(n);
271     }
272 
273     /**
274      * Sets request property. If a property with the key already
275      * exists, overwrite its value with the new value.
276      * @param value the value to be set
277      */
setRequestProperty(String key, String value)278     public void setRequestProperty(String key, String value) {
279         delegate.setRequestProperty(key, value);
280     }
281 
282     /**
283      * Adds a general request property specified by a
284      * key-value pair.  This method will not overwrite
285      * existing values associated with the same key.
286      *
287      * @param   key     the keyword by which the request is known
288      *                  (e.g., "<code>accept</code>").
289      * @param   value  the value associated with it.
290      * @see #getRequestProperties(java.lang.String)
291      * @since 1.4
292      */
addRequestProperty(String key, String value)293     public void addRequestProperty(String key, String value) {
294         delegate.addRequestProperty(key, value);
295     }
296 
297     /**
298      * Overwrite super class method
299      */
getResponseCode()300     public int getResponseCode() throws IOException {
301         return delegate.getResponseCode();
302     }
303 
getRequestProperty(String key)304     public String getRequestProperty(String key) {
305         return delegate.getRequestProperty(key);
306     }
307 
308     /**
309      * Returns an unmodifiable Map of general request
310      * properties for this connection. The Map keys
311      * are Strings that represent the request-header
312      * field names. Each Map value is a unmodifiable List
313      * of Strings that represents the corresponding
314      * field values.
315      *
316      * @return  a Map of the general request properties for this connection.
317      * @throws IllegalStateException if already connected
318      * @since 1.4
319      */
getRequestProperties()320     public Map<String,List<String>> getRequestProperties() {
321         return delegate.getRequestProperties();
322     }
323 
324     /*
325      * We support JDK 1.2.x so we can't count on these from JDK 1.3.
326      * We override and supply our own version.
327      */
setInstanceFollowRedirects(boolean shouldFollow)328     public void setInstanceFollowRedirects(boolean shouldFollow) {
329         delegate.setInstanceFollowRedirects(shouldFollow);
330     }
331 
getInstanceFollowRedirects()332     public boolean getInstanceFollowRedirects() {
333         return delegate.getInstanceFollowRedirects();
334     }
335 
setRequestMethod(String method)336     public void setRequestMethod(String method) throws ProtocolException {
337         delegate.setRequestMethod(method);
338     }
339 
getRequestMethod()340     public String getRequestMethod() {
341         return delegate.getRequestMethod();
342     }
343 
getResponseMessage()344     public String getResponseMessage() throws IOException {
345         return delegate.getResponseMessage();
346     }
347 
getHeaderFieldDate(String name, long Default)348     public long getHeaderFieldDate(String name, long Default) {
349         return delegate.getHeaderFieldDate(name, Default);
350     }
351 
getPermission()352     public Permission getPermission() throws IOException {
353         return delegate.getPermission();
354     }
355 
getURL()356     public URL getURL() {
357         return delegate.getURL();
358     }
359 
getContentLength()360     public int getContentLength() {
361         return delegate.getContentLength();
362     }
363 
getContentLengthLong()364     public long getContentLengthLong() {
365         return delegate.getContentLengthLong();
366     }
367 
getContentType()368     public String getContentType() {
369         return delegate.getContentType();
370     }
371 
getContentEncoding()372     public String getContentEncoding() {
373         return delegate.getContentEncoding();
374     }
375 
getExpiration()376     public long getExpiration() {
377         return delegate.getExpiration();
378     }
379 
getDate()380     public long getDate() {
381         return delegate.getDate();
382     }
383 
getLastModified()384     public long getLastModified() {
385         return delegate.getLastModified();
386     }
387 
getHeaderFieldInt(String name, int Default)388     public int getHeaderFieldInt(String name, int Default) {
389         return delegate.getHeaderFieldInt(name, Default);
390     }
391 
getHeaderFieldLong(String name, long Default)392     public long getHeaderFieldLong(String name, long Default) {
393         return delegate.getHeaderFieldLong(name, Default);
394     }
395 
getContent()396     public Object getContent() throws IOException {
397         return delegate.getContent();
398     }
399 
400     @SuppressWarnings("rawtypes")
getContent(Class[] classes)401     public Object getContent(Class[] classes) throws IOException {
402         return delegate.getContent(classes);
403     }
404 
toString()405     public String toString() {
406         return delegate.toString();
407     }
408 
setDoInput(boolean doinput)409     public void setDoInput(boolean doinput) {
410         delegate.setDoInput(doinput);
411     }
412 
getDoInput()413     public boolean getDoInput() {
414         return delegate.getDoInput();
415     }
416 
setDoOutput(boolean dooutput)417     public void setDoOutput(boolean dooutput) {
418         delegate.setDoOutput(dooutput);
419     }
420 
getDoOutput()421     public boolean getDoOutput() {
422         return delegate.getDoOutput();
423     }
424 
setAllowUserInteraction(boolean allowuserinteraction)425     public void setAllowUserInteraction(boolean allowuserinteraction) {
426         delegate.setAllowUserInteraction(allowuserinteraction);
427     }
428 
getAllowUserInteraction()429     public boolean getAllowUserInteraction() {
430         return delegate.getAllowUserInteraction();
431     }
432 
setUseCaches(boolean usecaches)433     public void setUseCaches(boolean usecaches) {
434         delegate.setUseCaches(usecaches);
435     }
436 
getUseCaches()437     public boolean getUseCaches() {
438         return delegate.getUseCaches();
439     }
440 
setIfModifiedSince(long ifmodifiedsince)441     public void setIfModifiedSince(long ifmodifiedsince) {
442         delegate.setIfModifiedSince(ifmodifiedsince);
443     }
444 
getIfModifiedSince()445     public long getIfModifiedSince() {
446         return delegate.getIfModifiedSince();
447     }
448 
getDefaultUseCaches()449     public boolean getDefaultUseCaches() {
450         return delegate.getDefaultUseCaches();
451     }
452 
setDefaultUseCaches(boolean defaultusecaches)453     public void setDefaultUseCaches(boolean defaultusecaches) {
454         delegate.setDefaultUseCaches(defaultusecaches);
455     }
456 
457     /*
458      * finalize (dispose) the delegated object.  Otherwise
459      * sun.net.www.protocol.http.HttpURLConnection's finalize()
460      * would have to be made public.
461      */
finalize()462     protected void finalize() throws Throwable {
463         delegate.dispose();
464     }
465 
equals(Object obj)466     public boolean equals(Object obj) {
467         return delegate.equals(obj);
468     }
469 
hashCode()470     public int hashCode() {
471         return delegate.hashCode();
472     }
473 
setConnectTimeout(int timeout)474     public void setConnectTimeout(int timeout) {
475         delegate.setConnectTimeout(timeout);
476     }
477 
getConnectTimeout()478     public int getConnectTimeout() {
479         return delegate.getConnectTimeout();
480     }
481 
setReadTimeout(int timeout)482     public void setReadTimeout(int timeout) {
483         delegate.setReadTimeout(timeout);
484     }
485 
getReadTimeout()486     public int getReadTimeout() {
487         return delegate.getReadTimeout();
488     }
489 
setFixedLengthStreamingMode(int contentLength)490     public void setFixedLengthStreamingMode (int contentLength) {
491         delegate.setFixedLengthStreamingMode(contentLength);
492     }
493 
setFixedLengthStreamingMode(long contentLength)494     public void setFixedLengthStreamingMode(long contentLength) {
495         delegate.setFixedLengthStreamingMode(contentLength);
496     }
497 
setChunkedStreamingMode(int chunklen)498     public void setChunkedStreamingMode (int chunklen) {
499         delegate.setChunkedStreamingMode(chunklen);
500     }
501 
502     @Override
setAuthenticator(Authenticator auth)503     public void setAuthenticator(Authenticator auth) {
504         delegate.setAuthenticator(auth);
505     }
506 }
507