gcc/libjava/java/net/HttpURLConnection.java

252 lines
7.1 KiB
Java
Raw Normal View History

1999-04-07 16:42:40 +02:00
// HttpURLConnection.java - Subclass of communications links using
// Hypertext Transfer Protocol.
/* Copyright (C) 1999, 2000 Free Software Foundation
1999-04-07 16:42:40 +02:00
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.*;
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
import java.security.Permission;
1999-04-07 16:42:40 +02:00
/**
* @author Warren Levy <warrenl@cygnus.com>
* @since 1.1
1999-04-07 16:42:40 +02:00
* @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;
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
public static final int HTTP_INTERNAL_ERROR = 500;
public static final int HTTP_NOT_IMPLEMENTED = 501;
1999-04-07 16:42:40 +02:00
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;
1999-04-07 16:42:40 +02:00
protected String method = "GET";
protected int responseCode = -1;
protected String responseMessage;
protected boolean instanceFollowRedirects = followRedirects;
1999-04-07 16:42:40 +02:00
private boolean gotResponseVals = false;
1999-04-07 16:42:40 +02:00
protected HttpURLConnection(URL url)
{
super(url);
}
public abstract void disconnect();
public abstract boolean usingProxy();
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
/**
* 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
*/
1999-04-07 16:42:40 +02:00
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;
}
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
/**
* 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
*/
1999-04-07 16:42:40 +02:00
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;
}
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
/**
* Gets the status code from an HTTP response message
*
* @exception IOException If an error occurs
*/
1999-04-07 16:42:40 +02:00
public int getResponseCode() throws IOException
{
if (!gotResponseVals)
getResponseVals();
1999-04-07 16:42:40 +02:00
return responseCode;
}
2002-09-25 Michael Koch <konqueror@gmx.de> * java/net/DatagramSocket.java (DatagramSocket): Exception documentation added. (bind): Exception documentation added, addded SecurityManager check, added SocketAddress type check. (getSoTimeout): Check impl. (receive): Fix SecurityManager check, check impl, documentation added. (send): Check channel mode, documentation added. (connect): New method. (disconnect): Implemented. (getLocalSocketAddress): New method. (getReceiveBufferSize): Check impl. (setReuseAddress): Check impl. (getReuseAddress): Check impl. (setBroadcast): Check impl. (getBroadcast): Check impl. (setTrafficClass): Check impl, Documentation cleared. (getTrafficClass): Check impl. (getSendBufferSize): Check impl. (setReceiveBufferSize): Check impl, documentation added. (setSendBufferSize): Documentation added. (setDatagramSocketImplFactory): New method. * java/net/HttpURLConnection.java (HTTP_INTERNAL_ERROR): The correct code is 500. (HTTP_NOT_IMPLEMENTED): Added new constant. (setFollowRedirects): Documentation added. (getInstanceFollowRedirects): New method. (setInstanceFollowRedirects): New method. (setRequestMethod): Documentation added. (getResponseCode): Documentation added. (getResponseMessage): Documentation added. * java/net/JarURLConnection.java (JarURLConnection): protected since JDK 1.4. (getJarEntry): java.io.IOException to IOException, documentation added. (getJarFile): Documentation added. * java/net/ServerSocket.java (ServerSocket): Private to public, exception added. (ServerSocket): java.io.IOException to IOException, documentation added. (bind): Check socket address type, documentation added. (bind): java.io.IOException to IOException, documentation added. (accept): Documentation added. (implAccept): Check ch is not non-blocking, documentation added. (setSoTimeout): Documentation fixed. (setReceiveBufferSize): Documentation added. * java/net/Socket.java (Socket): Documentation added. (bind): Documentation added. (connect): Check socket address type, documentation added. (getRemoteSocketAddress): New method. From-SVN: r57494
2002-09-25 11:05:53 +02:00
/**
* Gets the HTTP response message, if any, returned along with the
* response code from a server
*
* @exception IOException If an error occurs
*/
1999-04-07 16:42:40 +02:00
public String getResponseMessage() throws IOException
{
if (!gotResponseVals)
getResponseVals();
1999-04-07 16:42:40 +02:00
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;
1999-04-07 16:42:40 +02:00
// Response is the first header received from the connection.
String respField = getHeaderField(0);
if (respField == null || ! respField.startsWith("HTTP/"))
1999-04-07 16:42:40 +02:00
{
// 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;
}
1999-04-07 16:42:40 +02:00
/**
* 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);
}
1999-04-07 16:42:40 +02:00
}