gcc/libjava/java/net/HttpURLConnection.java
Michael Koch 7393decb70 2002-10-08 Michael Koch <konqueror@gmx.de>
* java/net/HttpURLConnection.java
	(getPermission): New method.
	(getErrorStream): New stub method.
	(getHeaderFieldDate): New stub method.
	* java/net/Inet4Address.java:
	(isLinkLocalAddress): Typo fixed.
	* java/net/InetAddress.java:
	(readResolve): New stubbed method (for serialization).
	(isAnyLocalAddress): New stubbed method.
	(isLoopbackAddress): New stubbed method.
	(isLinkLocalAddress): New stubbed method.
	(isSiteLocalAddress): New stubbed method.
	(isMCGlobal): New stubbed method.
	(isMCNodeGlobal): New stubbed method.
	(isMCLinkLocal): New stubbed method.
	(isMCSiteLocal): New stubbed method.
	(isMCOrgLocal): New stubbed method.
	(getCanonicalHostName): New stubbed method.
	(getByAddress): Create instances of Inet4Address/Inet6Address,
	instead of InetAddress, documentation added.
	* java/net/MulticastSocket.java
	(getInterface): Removed FIXME.
	(getNetworkInterface): New method.
	(setNetworkInterface): New method.
	* java/net/NetworkInterface.java:
	(toString): Use property "line.separator" instead of "\n".
	* java/net/URLConnection.java
	(getContent): New stubbed method.
	* java/net/URLStreamHandler.java:
	(equals): New stubbed method.
	(hostsEqual): New stubbed method.
	(hashCode): New stubbed method.
	* java/net/natNetworkInterface.cc:
	(getRealNetworkInterfaces): Create Inet4Address object
	instead of InetAddress.

From-SVN: r58002
2002-10-10 05:19:22 +00:00

252 lines
7.1 KiB
Java

// HttpURLConnection.java - Subclass of communications links using
// Hypertext Transfer Protocol.
/* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
package java.net;
import java.io.*;
import java.security.Permission;
/**
* @author Warren Levy <warrenl@cygnus.com>
* @since 1.1
* @date March 29, 1999.
*/
/**
* Written using on-line Java Platform 1.2 API Specification, as well
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
* Status: Believed complete and correct.
*/
public abstract class HttpURLConnection extends URLConnection
{
/* HTTP Success Response Codes */
public static final int HTTP_OK = 200;
public static final int HTTP_CREATED = 201;
public static final int HTTP_ACCEPTED = 202;
public static final int HTTP_NOT_AUTHORITATIVE = 203;
public static final int HTTP_NO_CONTENT = 204;
public static final int HTTP_RESET = 205;
public static final int HTTP_PARTIAL = 206;
/* HTTP Redirection Response Codes */
public static final int HTTP_MULT_CHOICE = 300;
public static final int HTTP_MOVED_PERM = 301;
public static final int HTTP_MOVED_TEMP = 302;
public static final int HTTP_SEE_OTHER = 303;
public static final int HTTP_NOT_MODIFIED = 304;
public static final int HTTP_USE_PROXY = 305;
/* HTTP Client Error Response Codes */
public static final int HTTP_BAD_REQUEST = 400;
public static final int HTTP_UNAUTHORIZED = 401;
public static final int HTTP_PAYMENT_REQUIRED = 402;
public static final int HTTP_FORBIDDEN = 403;
public static final int HTTP_NOT_FOUND = 404;
public static final int HTTP_BAD_METHOD = 405;
public static final int HTTP_NOT_ACCEPTABLE = 406;
public static final int HTTP_PROXY_AUTH = 407;
public static final int HTTP_CLIENT_TIMEOUT = 408;
public static final int HTTP_CONFLICT = 409;
public static final int HTTP_GONE = 410;
public static final int HTTP_LENGTH_REQUIRED = 411;
public static final int HTTP_PRECON_FAILED = 412;
public static final int HTTP_ENTITY_TOO_LARGE = 413;
public static final int HTTP_REQ_TOO_LONG = 414;
public static final int HTTP_UNSUPPORTED_TYPE = 415;
/* HTTP Server Error Response Codes */
public static final int HTTP_SERVER_ERROR = 500;
public static final int HTTP_INTERNAL_ERROR = 500;
public static final int HTTP_NOT_IMPLEMENTED = 501;
public static final int HTTP_BAD_GATEWAY = 502;
public static final int HTTP_UNAVAILABLE = 503;
public static final int HTTP_GATEWAY_TIMEOUT = 504;
public static final int HTTP_VERSION = 505;
static boolean followRedirects = true;
protected String method = "GET";
protected int responseCode = -1;
protected String responseMessage;
protected boolean instanceFollowRedirects = followRedirects;
private boolean gotResponseVals = false;
protected HttpURLConnection(URL url)
{
super(url);
}
public abstract void disconnect();
public abstract boolean usingProxy();
/**
* Sets whether HTTP redirects (requests with response code 3xx) should be
* automatically followed by this class. True by default
*
* @exception SecurityException If a security manager exists and its
* checkSetFactory method doesn't allow the operation
*/
public static void setFollowRedirects(boolean set)
{
// Throw an exception if an extant security mgr precludes
// setting the factory.
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkSetFactory();
followRedirects = set;
}
public static boolean getFollowRedirects()
{
return followRedirects;
}
/**
* Returns the value of this HttpURLConnection's instanceFollowRedirects
* field
*/
public boolean getInstanceFollowRedirects ()
{
return instanceFollowRedirects;
}
/**
* Sets the value of this HttpURLConnection's instanceFollowRedirects field
*/
public void setInstanceFollowRedirects (boolean follow)
{
instanceFollowRedirects = follow;
}
/**
* Set the method for the URL request, one of:
* GET POST HEAD OPTIONS PUT DELETE TRACE are legal
*
* @exception ProtocolException If the method cannot be reset or if the
* requested method isn't valid for HTTP
*/
public void setRequestMethod(String method) throws ProtocolException
{
if (connected)
throw new ProtocolException("Already connected");
if (method.equals("GET") || method.equals("POST") ||
method.equals("HEAD") || method.equals("OPTIONS") ||
method.equals("PUT") || method.equals("DELETE") ||
method.equals("TRACE"))
this.method = method;
else
throw new ProtocolException("Invalid HTTP request method");
}
public String getRequestMethod()
{
return method;
}
/**
* Gets the status code from an HTTP response message
*
* @exception IOException If an error occurs
*/
public int getResponseCode() throws IOException
{
if (!gotResponseVals)
getResponseVals();
return responseCode;
}
/**
* Gets the HTTP response message, if any, returned along with the
* response code from a server
*
* @exception IOException If an error occurs
*/
public String getResponseMessage() throws IOException
{
if (!gotResponseVals)
getResponseVals();
return responseMessage;
}
private void getResponseVals() throws IOException
{
// getHeaderField() will connect for us, but do it here first in
// order to pick up IOExceptions.
if (!connected)
connect();
gotResponseVals = true;
// Response is the first header received from the connection.
String respField = getHeaderField(0);
if (respField == null || ! respField.startsWith("HTTP/"))
{
// Set to default values on failure.
responseCode = -1;
responseMessage = null;
return;
}
int firstSpc, nextSpc;
firstSpc = respField.indexOf(' ');
nextSpc = respField.indexOf(' ', firstSpc + 1);
responseMessage = respField.substring(nextSpc + 1);
String codeStr = respField.substring(firstSpc + 1, nextSpc);
try
{
responseCode = Integer.parseInt(codeStr);
}
catch (NumberFormatException e)
{
// Set to default values on failure.
responseCode = -1;
responseMessage = null;
}
}
/**
* Returns a permission object representing the permission necessary to make
* the connection represented by this object
*
* @exception IOException If an error occurs
*/
public Permission getPermission() throws IOException
{
return new SocketPermission (url.getHost (), "connect");
}
/**
* Returns the error stream if the connection failed but the server sent
* useful data nonetheless
*/
public InputStream getErrorStream ()
{
// FIXME: implement this
return null;
}
/**
* Returns the value of the named field parsed as date
*/
public long getHeaderFieldDate (String key, long value)
{
// FIXME: implement this correctly
// http://www.w3.org/Protocols/HTTP-NG/ng-notes.txt
return super.getHeaderFieldDate (key, value);
}
}