Connection.java (gotHeaders): Removed.

2000-08-10  Bryce McKinlay  <bryce@albatross.co.nz>
	    John Stracke <francis@ecal.com>

	* gnu/gcj/protocol/http/Connection.java (gotHeaders): Removed.
	(connect): Don't falsely claim HTTP/1.1 compliance. Call
	getHttpHeaders().
	(disconnect): Don't unset connected flag.
	(getHeaderField (String)): Call connect() if not connected.
	(getHeaderField (int)): Ditto.
	(getHeaderFieldKey): Ditto.
	(getHttpHeaders): Don't call connect().
	* java/net/HttpURLConnection.java (instanceFollowRedirects,
	gotResponseVals): New fields.
	(getResponseCode): Call getResponseVals() conditionally.
	(getResponseMessage): Ditto.
	(getResponseVals): Call connect(). Don't throw
	FileNotFoundException.

From-SVN: r35603
This commit is contained in:
Bryce McKinlay 2000-08-10 10:10:25 +01:00
parent 9052d3bebc
commit b6ac804e3d
2 changed files with 58 additions and 52 deletions

View file

@ -1,6 +1,6 @@
// Connection.java - Implementation of HttpURLConnection for http protocol. // Connection.java - Implementation of HttpURLConnection for http protocol.
/* Copyright (C) 1999 Free Software Foundation /* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj. This file is part of libgcj.
@ -37,7 +37,6 @@ class Connection extends HttpURLConnection
private Hashtable requestProperties; private Hashtable requestProperties;
private Hashtable hdrHash = new Hashtable(); private Hashtable hdrHash = new Hashtable();
private Vector hdrVec = new Vector(); private Vector hdrVec = new Vector();
private boolean gotHeaders = false;
private BufferedInputStream bufferedIn; private BufferedInputStream bufferedIn;
public Connection(URL url) public Connection(URL url)
@ -94,14 +93,15 @@ class Connection extends HttpURLConnection
PrintWriter out = new PrintWriter(sock.getOutputStream()); PrintWriter out = new PrintWriter(sock.getOutputStream());
// Send request including any request properties that were set. // Send request including any request properties that were set.
out.print(getRequestMethod() + " " + url.getFile() + " HTTP/1.1\n"); out.print(getRequestMethod() + " " + url.getFile() + " HTTP/1.0\n");
out.print("Host: " + url.getHost() + ":" + port + "\n"); out.print("Host: " + url.getHost() + ":" + port + "\n");
Enumeration reqKeys = requestProperties.keys(); Enumeration reqKeys = requestProperties.keys();
Enumeration reqVals = requestProperties.elements(); Enumeration reqVals = requestProperties.elements();
while (reqKeys.hasMoreElements()) while (reqKeys.hasMoreElements())
out.print(reqKeys.nextElement() + ": " + reqVals.nextElement() + "\n"); out.print(reqKeys.nextElement() + ": " + reqVals.nextElement() + "\n");
out.print("\n"); out.print("\n");
out.flush(); out.flush();
getHttpHeaders();
connected = true; connected = true;
} }
@ -120,7 +120,6 @@ class Connection extends HttpURLConnection
} }
sock = null; sock = null;
} }
connected = false;
} }
// TODO: public boolean usingProxy() // TODO: public boolean usingProxy()
@ -135,10 +134,8 @@ class Connection extends HttpURLConnection
if (!connected) if (!connected)
connect(); connect();
if (! doInput) if (!doInput)
throw new ProtocolException("Can't open InputStream if doInput is false"); throw new ProtocolException("Can't open InputStream if doInput is false");
if (bufferedIn == null)
bufferedIn = new BufferedInputStream(sock.getInputStream());
return bufferedIn; return bufferedIn;
} }
@ -157,48 +154,52 @@ class Connection extends HttpURLConnection
// Override default method in URLConnection. // Override default method in URLConnection.
public String getHeaderField(String name) public String getHeaderField(String name)
{ {
try if (!connected)
{ try
getHttpHeaders(); {
} connect();
catch (IOException x) }
{ catch (IOException x)
return null; {
} return null;
}
return (String) hdrHash.get(name.toLowerCase()); return (String) hdrHash.get(name.toLowerCase());
} }
// Override default method in URLConnection. // Override default method in URLConnection.
public String getHeaderField(int n) public String getHeaderField(int n)
{ {
try if (!connected)
{ try
getHttpHeaders(); {
} connect();
catch (IOException x) }
{ catch (IOException x)
return null; {
} return null;
}
if (n < hdrVec.size()) if (n < hdrVec.size())
return getField((String) hdrVec.elementAt(n)); return getField((String) hdrVec.elementAt(n));
return null; return null;
} }
// Override default method in URLConnection. // Override default method in URLConnection.
public String getHeaderFieldKey(int n) public String getHeaderFieldKey(int n)
{ {
try if (!connected)
{ try
getHttpHeaders(); {
} connect();
catch (IOException x) }
{ catch (IOException x)
return null; {
} return null;
}
if (n < hdrVec.size()) if (n < hdrVec.size())
return getKey((String) hdrVec.elementAt(n)); return getKey((String) hdrVec.elementAt(n));
return null; return null;
} }
@ -226,20 +227,13 @@ class Connection extends HttpURLConnection
private void getHttpHeaders() throws IOException private void getHttpHeaders() throws IOException
{ {
if (gotHeaders)
return;
gotHeaders = true;
connect();
// Originally tried using a BufferedReader here to take advantage of // Originally tried using a BufferedReader here to take advantage of
// the readLine method and avoid the following, but the buffer read // the readLine method and avoid the following, but the buffer read
// past the end of the headers so the first part of the content was lost. // past the end of the headers so the first part of the content was lost.
// It is probably more robust than it needs to be, e.g. the byte[] // It is probably more robust than it needs to be, e.g. the byte[]
// is unlikely to overflow and a '\r' should always be followed by a '\n', // is unlikely to overflow and a '\r' should always be followed by a '\n',
// but it is better to be safe just in case. // but it is better to be safe just in case.
if (bufferedIn == null) bufferedIn = new BufferedInputStream(sock.getInputStream());
bufferedIn = new BufferedInputStream(sock.getInputStream());
int buflen = 100; int buflen = 100;
byte[] buf = new byte[buflen]; byte[] buf = new byte[buflen];
@ -247,6 +241,7 @@ class Connection extends HttpURLConnection
boolean gotnl = false; boolean gotnl = false;
byte[] ch = new byte[1]; byte[] ch = new byte[1];
ch[0] = (byte) '\n'; ch[0] = (byte) '\n';
while (true) while (true)
{ {
// Check for leftover byte from non-'\n' after a '\r'. // Check for leftover byte from non-'\n' after a '\r'.
@ -254,9 +249,12 @@ class Connection extends HttpURLConnection
line = line + '\r' + new String(ch, 0, 1); line = line + '\r' + new String(ch, 0, 1);
int i; int i;
// FIXME: This is rather inefficient.
for (i = 0; i < buflen; i++) for (i = 0; i < buflen; i++)
{ {
bufferedIn.read(buf, i, 1); buf[i] = (byte) bufferedIn.read();
if (buf[i] == -1)
throw new IOException("Malformed HTTP header");
if (buf[i] == '\r') if (buf[i] == '\r')
{ {
bufferedIn.read(ch, 0, 1); bufferedIn.read(ch, 0, 1);

View file

@ -1,7 +1,7 @@
// HttpURLConnection.java - Subclass of communications links using // HttpURLConnection.java - Subclass of communications links using
// Hypertext Transfer Protocol. // Hypertext Transfer Protocol.
/* Copyright (C) 1999 Free Software Foundation /* Copyright (C) 1999, 2000 Free Software Foundation
This file is part of libgcj. This file is part of libgcj.
@ -69,11 +69,14 @@ public abstract class HttpURLConnection extends URLConnection
public static final int HTTP_GATEWAY_TIMEOUT = 504; public static final int HTTP_GATEWAY_TIMEOUT = 504;
public static final int HTTP_VERSION = 505; public static final int HTTP_VERSION = 505;
static boolean followRedirects = true;
protected String method = "GET"; protected String method = "GET";
protected int responseCode = -1; protected int responseCode = -1;
protected String responseMessage; protected String responseMessage;
protected boolean instanceFollowRedirects = followRedirects;
static boolean followRedirects = true; private boolean gotResponseVals = false;
protected HttpURLConnection(URL url) protected HttpURLConnection(URL url)
{ {
@ -121,21 +124,30 @@ public abstract class HttpURLConnection extends URLConnection
public int getResponseCode() throws IOException public int getResponseCode() throws IOException
{ {
getResponseVals(); if (!gotResponseVals)
getResponseVals();
return responseCode; return responseCode;
} }
public String getResponseMessage() throws IOException public String getResponseMessage() throws IOException
{ {
getResponseVals(); if (!gotResponseVals)
getResponseVals();
return responseMessage; return responseMessage;
} }
private void getResponseVals() throws IOException 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. // Response is the first header received from the connection.
String respField = getHeaderField(0); String respField = getHeaderField(0);
if (! respField.startsWith("HTTP/"))
if (respField == null || ! respField.startsWith("HTTP/"))
{ {
// Set to default values on failure. // Set to default values on failure.
responseCode = -1; responseCode = -1;
@ -158,10 +170,6 @@ public abstract class HttpURLConnection extends URLConnection
responseCode = -1; responseCode = -1;
responseMessage = null; responseMessage = null;
} }
if (responseCode == HTTP_NOT_FOUND)
throw new FileNotFoundException(url.toString());
else if (responseCode >= 400)
throw new IOException(url.toString() + " " + respField);
} }
// TODO12: public Permission getPermission() throws IOException // TODO12: public Permission getPermission() throws IOException