Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/URLConnection.java
38830 views
/*1* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.www;2627import java.net.URL;28import java.util.*;2930/**31* A class to represent an active connection to an object32* represented by a URL.33* @author James Gosling34*/3536abstract public class URLConnection extends java.net.URLConnection {3738/** The URL that it is connected to */3940private String contentType;41private int contentLength = -1;4243protected MessageHeader properties;4445/** Create a URLConnection object. These should not be created directly:46instead they should be created by protocol handers in response to47URL.openConnection.48@param u The URL that this connects to.49*/50public URLConnection (URL u) {51super(u);52properties = new MessageHeader();53}5455/** Call this routine to get the property list for this object.56* Properties (like content-type) that have explicit getXX() methods57* associated with them should be accessed using those methods. */58public MessageHeader getProperties() {59return properties;60}6162/** Call this routine to set the property list for this object. */63public void setProperties(MessageHeader properties) {64this.properties = properties;65}6667public void setRequestProperty(String key, String value) {68if(connected)69throw new IllegalAccessError("Already connected");70if (key == null)71throw new NullPointerException ("key cannot be null");72properties.set(key, value);73}7475/**76* The following three methods addRequestProperty, getRequestProperty,77* and getRequestProperties were copied from the superclass implementation78* before it was changed by CR:6230836, to maintain backward compatibility.79*/80public void addRequestProperty(String key, String value) {81if (connected)82throw new IllegalStateException("Already connected");83if (key == null)84throw new NullPointerException ("key is null");85}8687public String getRequestProperty(String key) {88if (connected)89throw new IllegalStateException("Already connected");90return null;91}9293public Map<String,List<String>> getRequestProperties() {94if (connected)95throw new IllegalStateException("Already connected");96return Collections.emptyMap();97}9899public String getHeaderField(String name) {100try {101getInputStream();102} catch (Exception e) {103return null;104}105return properties == null ? null : properties.findValue(name);106}107108/**109* Return the key for the nth header field. Returns null if110* there are fewer than n fields. This can be used to iterate111* through all the headers in the message.112*/113public String getHeaderFieldKey(int n) {114try {115getInputStream();116} catch (Exception e) {117return null;118}119MessageHeader props = properties;120return props == null ? null : props.getKey(n);121}122123/**124* Return the value for the nth header field. Returns null if125* there are fewer than n fields. This can be used in conjunction126* with getHeaderFieldKey to iterate through all the headers in the message.127*/128public String getHeaderField(int n) {129try {130getInputStream();131} catch (Exception e) {132return null;133}134MessageHeader props = properties;135return props == null ? null : props.getValue(n);136}137138/** Call this routine to get the content-type associated with this139* object.140*/141public String getContentType() {142if (contentType == null)143contentType = getHeaderField("content-type");144if (contentType == null) {145String ct = null;146try {147ct = guessContentTypeFromStream(getInputStream());148} catch(java.io.IOException e) {149}150String ce = properties.findValue("content-encoding");151if (ct == null) {152ct = properties.findValue("content-type");153154if (ct == null)155if (url.getFile().endsWith("/"))156ct = "text/html";157else158ct = guessContentTypeFromName(url.getFile());159}160161/*162* If the Mime header had a Content-encoding field and its value163* was not one of the values that essentially indicate no164* encoding, we force the content type to be unknown. This will165* cause a save dialog to be presented to the user. It is not166* ideal but is better than what we were previously doing, namely167* bringing up an image tool for compressed tar files.168*/169170if (ct == null || ce != null &&171!(ce.equalsIgnoreCase("7bit")172|| ce.equalsIgnoreCase("8bit")173|| ce.equalsIgnoreCase("binary")))174ct = "content/unknown";175setContentType(ct);176}177return contentType;178}179180/**181* Set the content type of this URL to a specific value.182* @param type The content type to use. One of the183* content_* static variables in this184* class should be used.185* eg. setType(URL.content_html);186*/187public void setContentType(String type) {188contentType = type;189properties.set("content-type", type);190}191192/** Call this routine to get the content-length associated with this193* object.194*/195public int getContentLength() {196try {197getInputStream();198} catch (Exception e) {199return -1;200}201int l = contentLength;202if (l < 0) {203try {204l = Integer.parseInt(properties.findValue("content-length"));205setContentLength(l);206} catch(Exception e) {207}208}209return l;210}211212/** Call this routine to set the content-length associated with this213* object.214*/215protected void setContentLength(int length) {216contentLength = length;217properties.set("content-length", String.valueOf(length));218}219220/**221* Returns true if the data associated with this URL can be cached.222*/223public boolean canCache() {224return url.getFile().indexOf('?') < 0 /* && url.postData == null225REMIND */ ;226}227228/**229* Call this to close the connection and flush any remaining data.230* Overriders must remember to call super.close()231*/232public void close() {233url = null;234}235236private static HashMap<String,Void> proxiedHosts = new HashMap<>();237238public synchronized static void setProxiedHost(String host) {239proxiedHosts.put(host.toLowerCase(), null);240}241242public synchronized static boolean isProxiedHost(String host) {243return proxiedHosts.containsKey(host.toLowerCase());244}245}246247248