src/java.base/share/classes/sun/net/NetworkClient.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 52499 768b1c612100
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents: 29986
diff changeset
     2
 * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5162
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This is the base class for network clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author      Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class NetworkClient {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    43
    /* Default value of read timeout, if not specified (infinity) */
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    44
    public static final int DEFAULT_READ_TIMEOUT = -1;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    45
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    46
    /* Default value of connect timeout, if not specified (infinity) */
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    47
    public static final int DEFAULT_CONNECT_TIMEOUT = -1;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected Proxy     proxy = Proxy.NO_PROXY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** Socket for communicating with server. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected Socket    serverSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Stream for printing to the server. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public PrintStream  serverOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Buffered stream for reading replies from server. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public InputStream  serverInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected static int defaultSoTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected static int defaultConnectTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    62
    protected int readTimeout = DEFAULT_READ_TIMEOUT;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    63
    protected int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /* Name of encoding to use for output */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected static String encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        final int vals[] = {0, 0};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        final String encs[] = { null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
    72
                new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    73
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        vals[0] = Integer.getInteger("sun.net.client.defaultReadTimeout", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        vals[1] = Integer.getInteger("sun.net.client.defaultConnectTimeout", 0).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        encs[0] = System.getProperty("file.encoding", "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        });
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    80
        if (vals[0] != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            defaultSoTimeout = vals[0];
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    82
        }
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    83
        if (vals[1] != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            defaultConnectTimeout = vals[1];
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    85
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        encoding = encs[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (!isASCIISuperset (encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                encoding = "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            encoding = "ISO8859_1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Test the named character encoding to verify that it converts ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * characters correctly. We have to use an ASCII based encoding, or else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * the NetworkClients will not work correctly in EBCDIC based systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * However, we cannot just use ASCII or ISO8859_1 universally, because in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Asian locales, non-ASCII characters may be embedded in otherwise
52499
768b1c612100 8213490: Networking area typos and inconsistencies cleanup
prappo
parents: 47216
diff changeset
   104
     * ASCII based protocols (e.g. HTTP). The specifications (RFC2616, 2398)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * are a little ambiguous in this matter. For instance, RFC2398 [part 2.1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * says that the HTTP request URI should be escaped using a defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * mechanism, but there is no way to specify in the escaped string what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * the original character set is. It is not correct to assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * UTF-8 is always used (as in URLs in HTML 4.0).  For this reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * until the specifications are updated to deal with this issue more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * comprehensively, and more importantly, HTTP servers are known to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * support these mechanisms, we will maintain the current behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * where it is possible to send non-ASCII characters in their original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * unescaped form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static boolean isASCIISuperset (String encoding) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String chkS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        "abcdefghijklmnopqrstuvwxyz-_.!~*'();/?:@&=+$,";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // Expected byte sequence for string above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        byte[] chkB = { 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                115,116,117,118,119,120,121,122,45,95,46,33,126,42,39,40,41,59,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                47,63,58,64,38,61,43,36,44};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        byte[] b = chkS.getBytes (encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return Arrays.equals (b, chkB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /** Open a connection to the server. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void openServer(String server, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (serverSocket != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        serverSocket = doConnect (server, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            serverOutput = new PrintStream(new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                        serverSocket.getOutputStream()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                        true, encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (UnsupportedEncodingException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 7668
diff changeset
   142
            throw new InternalError(encoding +"encoding not found", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        serverInput = new BufferedInputStream(serverSocket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Return a socket connected to the server, with any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * appropriate options pre-established
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected Socket doConnect (String server, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    throws IOException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (proxy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (proxy.type() == Proxy.Type.SOCKS) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   156
                s = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   157
                    new PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   158
                        public Socket run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                       return new Socket(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                   }});
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   161
            } else if (proxy.type() == Proxy.Type.DIRECT) {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   162
                s = createSocket();
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   163
            } else {
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   164
                // Still connecting through a proxy
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   165
                // server & port will be the proxy address and port
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                s = new Socket(Proxy.NO_PROXY);
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   167
            }
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents: 29986
diff changeset
   168
        } else {
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   169
            s = createSocket();
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents: 29986
diff changeset
   170
        }
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents: 29986
diff changeset
   171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // Instance specific timeouts do have priority, that means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // connectTimeout & readTimeout (-1 means not set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Then global default timeouts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Then no timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (connectTimeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            s.connect(new InetSocketAddress(server, port), connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (defaultConnectTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                s.connect(new InetSocketAddress(server, port), defaultConnectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                s.connect(new InetSocketAddress(server, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            s.setSoTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        else if (defaultSoTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            s.setSoTimeout(defaultSoTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   193
    /**
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   194
     * The following method, createSocket, is provided to allow the
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   195
     * https client to override it so that it may use its socket factory
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   196
     * to create the socket.
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   197
     */
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   198
    protected Socket createSocket() throws IOException {
39128
e991a1c8b41b 8144008: Setting NO_PROXY on HTTP URL connections does not stop proxying
vtewari
parents: 29986
diff changeset
   199
        return new java.net.Socket(Proxy.NO_PROXY);  // direct connection
5162
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   200
    }
0dbedf4fdb8c 6614957: HttpsURLConnection not using the set SSLSocketFactory for creating all its Sockets
chegar
parents: 715
diff changeset
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected InetAddress getLocalAddress() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (serverSocket == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new IOException("not connected");
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   205
        return  AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   206
                        new PrivilegedAction<>() {
18212
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   207
                            public InetAddress run() {
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   208
                                return serverSocket.getLocalAddress();
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   209
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   210
                            }
22f8c33b0690 8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents: 14342
diff changeset
   211
                        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /** Close an open connection to the server. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void closeServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (! serverIsOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        serverSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        serverInput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        serverOutput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /** Return server connection status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public boolean serverIsOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return serverSocket != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /** Create connection with host <i>host</i> on port <i>port</i> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public NetworkClient(String host, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        openServer(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public NetworkClient() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return connectTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   245
    /**
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   246
     * Sets the read timeout.
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   247
     *
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   248
     * Note: Public URLConnection (and protocol specific implementations)
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18212
diff changeset
   249
     * protect against negative timeout values being set. This implementation,
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   250
     * and protocol specific implementations, use -1 to represent the default
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   251
     * read timeout.
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   252
     *
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   253
     * This method may be invoked with the default timeout value when the
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   254
     * protocol handler is trying to reset the timeout after doing a
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   255
     * potentially blocking internal operation, e.g. cleaning up unread
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   256
     * response data, buffering error stream response data, etc
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   257
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public void setReadTimeout(int timeout) {
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   259
        if (timeout == DEFAULT_READ_TIMEOUT)
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   260
            timeout = defaultSoTimeout;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   261
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (serverSocket != null && timeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                serverSocket.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // We tried...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}