Commit 30e8a59c by Michael Koch Committed by Michael Koch

PR libgcj/14012, PR libgcj/14013, PR libgcj/15157, PR libgcj/15509

2005-01-11  Michael Koch  <konqueror@gmx.de>

	PR libgcj/14012, PR libgcj/14013, PR libgcj/15157, PR libgcj/15509
	* gnu/java/net/BASE64.java,
	gnu/java/net/EmptyX509TrustManager.java,
	gnu/java/net/LineInputStream.java,
	gnu/java/net/protocol/http/Authenticator.java,
	gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java,
	gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java,
	gnu/java/net/protocol/http/ChunkedInputStream.java,
	gnu/java/net/protocol/http/Cookie.java,
	gnu/java/net/protocol/http/CookieManager.java,
	gnu/java/net/protocol/http/Credentials.java,
	gnu/java/net/protocol/http/HTTPConnection.java,
	gnu/java/net/protocol/http/HTTPDateFormat.java,
	gnu/java/net/protocol/http/HTTPURLConnection.java,
	gnu/java/net/protocol/http/Headers.java,
	gnu/java/net/protocol/http/Request.java,
	gnu/java/net/protocol/http/RequestBodyWriter.java,
	gnu/java/net/protocol/http/Response.java,
	gnu/java/net/protocol/http/ResponseBodyReader.java,
	gnu/java/net/protocol/http/ResponseHeaderHandler.java,
	gnu/java/net/protocol/http/SimpleCookieManager.java,
	gnu/java/net/protocol/http/event/ConnectionEvent.java,
	gnu/java/net/protocol/http/event/ConnectionListener.java,
	gnu/java/net/protocol/http/event/RequestEvent.java,
	gnu/java/net/protocol/http/event/RequestListener.java: New files.
	* gnu/java/net/protocol/http/Connection.java: Removed.
	* gnu/java/net/protocol/http/Handler.java,
	javax/net/ssl/HttpsURLConnection.java: Updated.
	* Makefile.am: Added new files and remove old ones.
	* Makefile.in: Regenerated.

From-SVN: r93195
parent 7eb3b9ec
2005-01-11 Michael Koch <konqueror@gmx.de>
PR libgcj/14012, PR libgcj/14013, PR libgcj/15157, PR libgcj/15509
* gnu/java/net/BASE64.java,
gnu/java/net/EmptyX509TrustManager.java,
gnu/java/net/LineInputStream.java,
gnu/java/net/protocol/http/Authenticator.java,
gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java,
gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java,
gnu/java/net/protocol/http/ChunkedInputStream.java,
gnu/java/net/protocol/http/Cookie.java,
gnu/java/net/protocol/http/CookieManager.java,
gnu/java/net/protocol/http/Credentials.java,
gnu/java/net/protocol/http/HTTPConnection.java,
gnu/java/net/protocol/http/HTTPDateFormat.java,
gnu/java/net/protocol/http/HTTPURLConnection.java,
gnu/java/net/protocol/http/Headers.java,
gnu/java/net/protocol/http/Request.java,
gnu/java/net/protocol/http/RequestBodyWriter.java,
gnu/java/net/protocol/http/Response.java,
gnu/java/net/protocol/http/ResponseBodyReader.java,
gnu/java/net/protocol/http/ResponseHeaderHandler.java,
gnu/java/net/protocol/http/SimpleCookieManager.java,
gnu/java/net/protocol/http/event/ConnectionEvent.java,
gnu/java/net/protocol/http/event/ConnectionListener.java,
gnu/java/net/protocol/http/event/RequestEvent.java,
gnu/java/net/protocol/http/event/RequestListener.java: New files.
* gnu/java/net/protocol/http/Connection.java: Removed.
* gnu/java/net/protocol/http/Handler.java,
javax/net/ssl/HttpsURLConnection.java: Updated.
* Makefile.am: Added new files and remove old ones.
* Makefile.in: Regenerated.
2005-01-11 Tom Tromey <tromey@redhat.com>
PR libgcj/18840:
......
......@@ -2854,8 +2854,11 @@ gnu/java/locale/LocaleInformation_zh_HK.java \
gnu/java/locale/LocaleInformation_zh_SG.java \
gnu/java/locale/LocaleInformation_zh_TW.java \
gnu/java/math/MPN.java \
gnu/java/net/BASE64.java \
gnu/java/net/DefaultContentHandlerFactory.java \
gnu/java/net/EmptyX509TrustManager.java \
gnu/java/net/HeaderFieldHelper.java \
gnu/java/net/LineInputStream.java \
gnu/java/net/PlainDatagramSocketImpl.java \
gnu/java/net/PlainSocketImpl.java \
gnu/java/net/URLParseError.java \
......@@ -2866,8 +2869,28 @@ gnu/java/net/protocol/file/Connection.java \
gnu/java/net/protocol/file/Handler.java \
gnu/java/net/protocol/gcjlib/Connection.java \
gnu/java/net/protocol/gcjlib/Handler.java \
gnu/java/net/protocol/http/Connection.java \
gnu/java/net/protocol/http/Authenticator.java \
gnu/java/net/protocol/http/ByteArrayRequestBodyWriter.java \
gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java \
gnu/java/net/protocol/http/ChunkedInputStream.java \
gnu/java/net/protocol/http/Cookie.java \
gnu/java/net/protocol/http/CookieManager.java \
gnu/java/net/protocol/http/Credentials.java \
gnu/java/net/protocol/http/HTTPConnection.java \
gnu/java/net/protocol/http/HTTPDateFormat.java \
gnu/java/net/protocol/http/HTTPURLConnection.java \
gnu/java/net/protocol/http/Handler.java \
gnu/java/net/protocol/http/Headers.java \
gnu/java/net/protocol/http/Request.java \
gnu/java/net/protocol/http/RequestBodyWriter.java \
gnu/java/net/protocol/http/Response.java \
gnu/java/net/protocol/http/ResponseBodyReader.java \
gnu/java/net/protocol/http/ResponseHeaderHandler.java \
gnu/java/net/protocol/http/SimpleCookieManager.java \
gnu/java/net/protocol/http/event/ConnectionEvent.java \
gnu/java/net/protocol/http/event/ConnectionListener.java \
gnu/java/net/protocol/http/event/RequestEvent.java \
gnu/java/net/protocol/http/event/RequestListener.java \
gnu/java/net/protocol/jar/Connection.java \
gnu/java/net/protocol/jar/Handler.java \
gnu/java/nio/ChannelInputStream.java \
......
/* BASE.java --
Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net;
/**
* Encodes and decodes text according to the BASE64 encoding.
*
* @author Chris Burdess (dog@gnu.org)
*/
public final class BASE64
{
private static final byte[] src = {
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54,
0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x2b, 0x2f
};
private static final byte[] dst;
static
{
dst = new byte[0x100];
for (int i = 0x0; i < 0xff; i++)
{
dst[i] = -1;
}
for (int i = 0; i < src.length; i++)
{
dst[src[i]] = (byte) i;
}
}
private BASE64()
{
}
/**
* Encode the specified byte array using the BASE64 algorithm.
*
* @param bs the source byte array
*/
public static byte[] encode(byte[] bs)
{
int si = 0, ti = 0; // source/target array indices
byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array
for (; si < bs.length; si += 3)
{
int buflen = bs.length - si;
if (buflen == 1)
{
byte b = bs[si];
int i = 0;
bt[ti++] = src[b >>> 2 & 0x3f];
bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
}
else if (buflen == 2)
{
byte b1 = bs[si], b2 = bs[si + 1];
int i = 0;
bt[ti++] = src[b1 >>> 2 & 0x3f];
bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
}
else
{
byte b1 = bs[si], b2 = bs[si + 1], b3 = bs[si + 2];
bt[ti++] = src[b1 >>> 2 & 0x3f];
bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
bt[ti++] = src[b3 & 0x3f];
}
}
/*while (ti < bt.length)
{
bt[ti++] = 0x3d;
}*/
return bt;
}
/**
* Decode the specified byte array using the BASE64 algorithm.
*
* @param bs the source byte array
*/
public static byte[] decode(byte[] bs)
{
int srclen = bs.length;
while (srclen > 0 && bs[srclen - 1] == 0x3d)
{
srclen--; /* strip padding character */
}
byte[] buffer = new byte[srclen];
int buflen = 0;
int si = 0;
int len = srclen - si;
while (len > 0)
{
byte b0 = dst[bs[si++] & 0xff];
byte b2 = dst[bs[si++] & 0xff];
buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
if (len > 2)
{
b0 = b2;
b2 = dst[bs[si++] & 0xff];
buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
if (len > 3)
{
b0 = b2;
b2 = dst[bs[si++] & 0xff];
buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
}
}
len = srclen - si;
}
byte[] bt = new byte[buflen];
System.arraycopy(buffer, 0, bt, 0, buflen);
return bt;
}
public static void main(String[] args)
{
boolean decode = false;
for (int i = 0; i < args.length; i++)
{
if (args[i].equals("-d"))
{
decode = true;
}
else
{
try
{
byte[] in = args[i].getBytes("US-ASCII");
byte[] out = decode ? decode(in) : encode(in);
System.out.println(args[i] + " = " +
new String(out, "US-ASCII"));
}
catch (java.io.UnsupportedEncodingException e)
{
e.printStackTrace(System.err);
}
}
}
}
}
/* EmptyX509TrustManager.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* Empty implementation of an X509 trust manager.
* This implementation does not check any certificates in the chain.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class EmptyX509TrustManager
implements X509TrustManager
{
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException
{
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException
{
}
public X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[0];
}
}
/* LineInputStream.java --
Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
/**
* An input stream that can read lines of input.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class LineInputStream
extends FilterInputStream
{
/*
* Line buffer.
*/
private ByteArrayOutputStream buf;
/*
* Encoding to use when translating bytes to characters.
*/
private String encoding;
/*
* End-of-stream flag.
*/
private boolean eof;
/**
* Whether we can use block reads.
*/
private final boolean blockReads;
/**
* Constructor using the US-ASCII character encoding.
* @param in the underlying input stream
*/
public LineInputStream(InputStream in)
{
this(in, "US-ASCII");
}
/**
* Constructor.
* @param in the underlying input stream
* @param encoding the character encoding to use
*/
public LineInputStream(InputStream in, String encoding)
{
super(in);
buf = new ByteArrayOutputStream();
this.encoding = encoding;
eof = false;
blockReads = in.markSupported();
}
/**
* Read a line of input.
*/
public String readLine()
throws IOException
{
if (eof)
{
return null;
}
do
{
if (blockReads)
{
// Use mark and reset to read chunks of bytes
final int MIN_LENGTH = 1024;
int len, pos;
len = in.available();
len = (len < MIN_LENGTH) ? MIN_LENGTH : len;
byte[] b = new byte[len];
in.mark(len);
// Read into buffer b
len = in.read(b, 0, len);
// Handle EOF
if (len == -1)
{
eof = true;
if (buf.size() == 0)
{
return null;
}
else
{
// We don't care about resetting buf
return buf.toString(encoding);
}
}
// Get index of LF in b
pos = indexOf(b, len, (byte) 0x0a);
if (pos != -1)
{
// Write pos bytes to buf
buf.write(b, 0, pos);
// Reset stream, and read pos + 1 bytes
in.reset();
pos += 1;
while (pos > 0)
{
len = in.read(b, 0, pos);
pos = (len == -1) ? -1 : pos - len;
}
// Return line
String ret = buf.toString(encoding);
buf.reset();
return ret;
}
else
{
// Append everything to buf and fall through to re-read.
buf.write(b, 0, len);
}
}
else
{
// We must use character reads in order not to read too much
// from the underlying stream.
int c = in.read();
switch (c)
{
case -1:
eof = true;
if (buf.size() == 0)
{
return null;
}
// Fall through and return contents of buffer.
case 0x0a: // LF
String ret = buf.toString(encoding);
buf.reset();
return ret;
default:
buf.write(c);
}
}
}
while (true);
}
private int indexOf(byte[] b, int len, byte c)
{
for (int pos = 0; pos < len; pos++)
{
if (b[pos] == c)
{
return pos;
}
}
return -1;
}
}
/* Authenticator.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Callback interface for managing authentication.
* @see Request#setAuthenticator
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface Authenticator
{
/**
* Returns the credentials to supply for the given realm.
* @param realm the authentication realm
* @param attempt zero on first authentication attempt, increments on each
* unsuccessful attempt
*/
Credentials getCredentials(String realm, int attempt);
}
/* ByteArrayRequestBodyWriter.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* A simple request body writer using a byte array.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class ByteArrayRequestBodyWriter
implements RequestBodyWriter
{
/**
* The content.
*/
protected byte[] content;
/**
* The position within the content at which the next read will occur.
*/
protected int pos;
/**
* Constructs a new byte array request body writer with the specified
* content.
* @param content the content buffer
*/
public ByteArrayRequestBodyWriter(byte[] content)
{
this.content = content;
pos = 0;
}
/**
* Returns the total number of bytes that will be written in a single pass
* by this writer.
*/
public int getContentLength()
{
return content.length;
}
/**
* Initialises the writer.
* This will be called before each pass.
*/
public void reset()
{
pos = 0;
}
/**
* Writes body content to the supplied buffer.
* @param buffer the content buffer
* @return the number of bytes written
*/
public int write(byte[] buffer)
{
int len = content.length - pos;
len = (buffer.length < len) ? buffer.length : len;
if (len > -1)
{
System.arraycopy(content, pos, buffer, 0, len);
pos += len;
}
return len;
}
}
/* Authenticator.java --ByteArrayResponseBodyReader.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Simple response body reader that stores content in a byte array.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class ByteArrayResponseBodyReader
implements ResponseBodyReader
{
/**
* The content.
*/
protected byte[] content;
/**
* The position in the content at which the next write will occur.
*/
protected int pos;
/**
* The length of the buffer.
*/
protected int len;
/**
* Constructs a new byte array response body reader.
*/
public ByteArrayResponseBodyReader()
{
this(4096);
}
/**
* Constructs a new byte array response body reader with the specified
* initial buffer size.
* @param size the initial buffer size
*/
public ByteArrayResponseBodyReader(int size)
{
content = new byte[size];
pos = len = 0;
}
/**
* This reader accepts all responses.
*/
public boolean accept(Request request, Response response)
{
return true;
}
public void read(byte[] buffer, int offset, int length)
{
int l = length - offset;
if (pos + l > content.length)
{
byte[] tmp = new byte[content.length * 2];
System.arraycopy(content, 0, tmp, 0, pos);
content = tmp;
}
System.arraycopy(buffer, offset, content, pos, l);
pos += l;
len = pos;
}
public void close()
{
pos = 0;
}
/**
* Retrieves the content of this reader as a byte array.
* The size of the returned array is the number of bytes read.
*/
public byte[] toByteArray()
{
byte[] ret = new byte[len];
System.arraycopy(content, 0, ret, 0, len);
return ret;
}
}
/* ChunkedInputStream.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.ProtocolException;
/**
* Input stream wrapper for the "chunked" transfer-coding.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class ChunkedInputStream
extends FilterInputStream
{
private static final byte CR = 0x0d;
private static final byte LF = 0x0a;
int size;
int count;
boolean meta;
boolean eof;
Headers headers;
/**
* Constructor.
* @param in the response socket input stream
* @param headers the headers to receive additional header lines
*/
public ChunkedInputStream(InputStream in, Headers headers)
{
super(in);
this.headers = headers;
size = -1;
count = 0;
meta = true;
}
public int read()
throws IOException
{
byte[] buf = new byte[1];
int len = read(buf, 0, 1);
if (len == -1)
{
return -1;
}
int ret = (int) buf[0];
if (ret < 0)
{
ret += 0x100;
}
return ret;
}
public int read(byte[] buffer)
throws IOException
{
return read(buffer, 0, buffer.length);
}
public int read(byte[] buffer, int offset, int length)
throws IOException
{
if (eof)
{
return -1;
}
if (meta)
{
// Read chunk header
int c, last = 0;
boolean seenSemi = false;
StringBuffer buf = new StringBuffer();
do
{
c = in.read();
if (c == 0x3b) // ;
{
seenSemi = true;
}
else if (c == 0x0a && last == 0x0d) // CRLF
{
size = Integer.parseInt(buf.toString(), 16);
break;
}
else if (!seenSemi && c >= 0x30)
{
buf.append ((char) c);
}
last = c;
}
while(c != -1);
count = 0;
meta = false;
}
if (size == 0)
{
// Read trailer
headers.parse(in);
eof = true;
return -1;
}
else
{
int diff = length - offset;
int max = size - count;
max = (diff < max) ? diff : max;
int len = (max > 0) ? in.read(buffer, offset, max) : 0;
count += len;
if (count == size)
{
// Read CRLF
int c1 = in.read();
int c2 = in.read();
if (c1 == -1 && c2 == -1)
{
// EOF before CRLF: bad, but ignore
eof = true;
return -1;
}
if (c1 != 0x0d || c2 != 0x0a)
{
throw new ProtocolException("expecting CRLF: " + c1 + "," + c2);
}
meta = true;
}
return len;
}
}
}
/* Cookie.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import java.text.ParseException;
import java.util.Date;
/**
* An HTTP cookie, as specified in RFC 2109.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class Cookie
{
/**
* The name of the cookie.
*/
protected final String name;
/**
* The value of the cookie.
*/
protected final String value;
/**
* Optional documentation of the intended use of the cookie.
*/
protected final String comment;
/**
* The domain for which the cookie is valid.
*/
protected final String domain;
/**
* Optional subset of URL paths within the domain for which the cookie is
* valid.
*/
protected final String path;
/**
* Indicates that the user-agent should only use secure means to transmit
* this cookie to the server.
*/
protected final boolean secure;
/**
* The date at which this cookie expires.
*/
protected final Date expires;
public Cookie(String name, String value, String comment, String domain,
String path, boolean secure, Date expires)
{
this.name = name;
this.value = value;
this.comment = comment;
this.domain = domain;
this.path = path;
this.secure = secure;
this.expires = expires;
}
public String getName()
{
return name;
}
public String getValue()
{
return value;
}
public String getComment()
{
return comment;
}
public String getDomain()
{
return domain;
}
public String getPath()
{
return path;
}
public boolean isSecure()
{
return secure;
}
public Date getExpiryDate()
{
return expires;
}
public String toString()
{
return toString(true, true);
}
public String toString(boolean showPath, boolean showDomain)
{
StringBuffer buf = new StringBuffer();
buf.append(name);
buf.append('=');
buf.append(value);
if (showPath)
{
buf.append("; $Path=");
buf.append(path);
}
if (showDomain)
{
buf.append("; $Domain=");
buf.append(domain);
}
return buf.toString();
}
}
/* CookieManager.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Cookie manager interface.
* If an application wants to handle cookies, they should implement this
* interface and register the instance with each HTTPConnection they use.
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface CookieManager
{
/**
* Stores a cookie in the cookie manager.
* @param cookie the cookie to store
*/
void setCookie(Cookie cookie);
/**
* Retrieves the cookies matching the specified criteria.
* @param host the host name
* @param secure whether the connection is secure
* @param path the path to access
*/
Cookie[] getCookies(String host, boolean secure, String path);
}
/* Credentials.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Represents a username/password combination that can be used to
* authenticate to an HTTP server.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class Credentials
{
/**
* The username.
*/
private String username;
/**
* The password.
*/
private String password;
/**
* Constructor.
* @param username the username
* @param password the password
*/
public Credentials(String username, String password)
{
this.username = username;
this.password = password;
}
/**
* Returns the username.
*/
public String getUsername()
{
return username;
}
/**
* Returns the password.
*/
public String getPassword()
{
return password;
}
}
/* Handler.java -- HTTP protocol handler for java.net
Copyright (c) 1998, 1999, 2003 Free Software Foundation, Inc.
/* Handler.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
......@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
......@@ -35,6 +35,7 @@ this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import java.io.IOException;
......@@ -43,44 +44,30 @@ import java.net.URLConnection;
import java.net.URLStreamHandler;
/**
* This is the protocol handler for the HTTP protocol. It implements
* the abstract openConnection() method from URLStreamHandler by returning
* a new HttpURLConnection object (from this package). All other
* methods are inherited
* An HTTP URL stream handler.
*
* @author Aaron M. Renn (arenn@urbanophile.com)
* @author Warren Levy
* @author Anthony Green <green@redhat.com>
* @author Chris Burdess (dog@gnu.org)
*/
public class Handler extends URLStreamHandler
public class Handler
extends URLStreamHandler
{
/**
* A do nothing constructor
*/
public Handler()
{
}
/**
* This method returs a new HttpURLConnection for the specified URL
*
* @param url The URL to return a connection for
*
* @return The URLConnection
*
* @exception IOException If an error occurs
* Returns the default HTTP port (80).
*/
protected URLConnection openConnection (URL url) throws IOException
protected int getDefaultPort()
{
return new Connection (url);
return HTTPConnection.HTTP_PORT;
}
/**
* Returns the default port for a URL parsed by this handler.
* Returns an HTTPURLConnection for the given URL.
*/
protected int getDefaultPort()
public URLConnection openConnection(URL url)
throws IOException
{
return 80;
return new HTTPURLConnection(url);
}
} // class Handler
}
/* Headers.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import gnu.java.net.LineInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/**
* A collection of HTTP header names and associated values.
* Retrieval of values is case insensitive. An iteration over the keys
* returns the header names in the order they were received.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class Headers
implements Map
{
static final DateFormat dateFormat = new HTTPDateFormat();
static class Header
{
final String name;
Header(String name)
{
if (name == null || name.length() == 0)
{
throw new IllegalArgumentException(name);
}
this.name = name;
}
public int hashCode()
{
return name.toLowerCase().hashCode();
}
public boolean equals(Object other)
{
if (other instanceof Header)
{
return ((Header) other).name.equalsIgnoreCase(name);
}
return false;
}
public String toString()
{
return name;
}
}
static class HeaderEntry
implements Map.Entry
{
final Map.Entry entry;
HeaderEntry(Map.Entry entry)
{
this.entry = entry;
}
public Object getKey()
{
return ((Header) entry.getKey()).name;
}
public Object getValue()
{
return entry.getValue();
}
public Object setValue(Object value)
{
return entry.setValue(value);
}
public int hashCode()
{
return entry.hashCode();
}
public boolean equals(Object other)
{
return entry.equals(other);
}
public String toString()
{
return getKey().toString() + "=" + getValue();
}
}
private LinkedHashMap headers;
public Headers()
{
headers = new LinkedHashMap();
}
public int size()
{
return headers.size();
}
public boolean isEmpty()
{
return headers.isEmpty();
}
public boolean containsKey(Object key)
{
return headers.containsKey(new Header((String) key));
}
public boolean containsValue(Object value)
{
return headers.containsValue(value);
}
public Object get(Object key)
{
return headers.get(new Header((String) key));
}
/**
* Returns the value of the specified header as a string.
*/
public String getValue(String header)
{
return (String) headers.get(new Header(header));
}
/**
* Returns the value of the specified header as an integer,
* or -1 if the header is not present or not an integer.
*/
public int getIntValue(String header)
{
String val = getValue(header);
if (val == null)
{
return -1;
}
try
{
return Integer.parseInt(val);
}
catch (NumberFormatException e)
{
}
return -1;
}
/**
* Returns the value of the specified header as a date,
* or <code>null</code> if the header is not present or not a date.
*/
public Date getDateValue(String header)
{
String val = getValue(header);
if (val == null)
{
return null;
}
try
{
return dateFormat.parse(val);
}
catch (ParseException e)
{
return null;
}
}
public Object put(Object key, Object value)
{
return headers.put(new Header((String) key), value);
}
public Object remove(Object key)
{
return headers.remove(new Header((String) key));
}
public void putAll(Map t)
{
for (Iterator i = t.keySet().iterator(); i.hasNext(); )
{
String key = (String) i.next();
String value = (String) t.get(key);
headers.put(new Header(key), value);
}
}
public void clear()
{
headers.clear();
}
public Set keySet()
{
Set keys = headers.keySet();
Set ret = new LinkedHashSet();
for (Iterator i = keys.iterator(); i.hasNext(); )
{
ret.add(((Header) i.next()).name);
}
return ret;
}
public Collection values()
{
return headers.values();
}
public Set entrySet()
{
Set entries = headers.entrySet();
Set ret = new LinkedHashSet();
for (Iterator i = entries.iterator(); i.hasNext(); )
{
Map.Entry entry = (Map.Entry) i.next();
ret.add(new HeaderEntry(entry));
}
return ret;
}
public boolean equals(Object other)
{
return headers.equals(other);
}
public int hashCode()
{
return headers.hashCode();
}
/**
* Parse the specified input stream, adding headers to this collection.
*/
public void parse(InputStream in)
throws IOException
{
LineInputStream lin = (in instanceof LineInputStream) ?
(LineInputStream) in : new LineInputStream(in);
String name = null;
StringBuffer value = new StringBuffer();
while (true)
{
String line = lin.readLine();
if (line == null)
{
if (name != null)
{
addValue(name, value.toString());
}
break;
}
int len = line.length();
if (len < 2)
{
if (name != null)
{
addValue(name, value.toString());
}
break;
}
char c1 = line.charAt(0);
if (c1 == ' ' || c1 == '\t')
{
// Continuation
value.append(line.substring(0, len - 1));
}
else
{
if (name != null)
{
addValue(name, value.toString());
}
int di = line.indexOf(':');
name = line.substring(0, di);
value.setLength(0);
do
{
di++;
}
while (di < len && line.charAt(di) == ' ');
value.append(line.substring(di, len - 1));
}
}
}
private void addValue(String name, String value)
{
Header key = new Header(name);
String old = (String) headers.get(key);
if (old == null)
{
headers.put(key, value);
}
else
{
headers.put(key, old + ", " + value);
}
}
}
/* RequestBodyWriter.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Callback interface for writing request body content.
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface RequestBodyWriter
{
/**
* Returns the total number of bytes that will be written in a single pass
* by this writer.
*/
int getContentLength();
/**
* Initialises the writer.
* This will be called before each pass.
*/
void reset();
/**
* Writes body content to the supplied buffer.
* @param buffer the content buffer
* @return the number of bytes written
*/
int write(byte[] buffer);
}
/* Response.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import java.util.Date;
/**
* An HTTP response.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class Response
{
/**
* The HTTP major version of the server issuing the response.
*/
protected final int majorVersion;
/**
* The HTTP minor version of the server issuing the response.
*/
protected final int minorVersion;
/**
* The HTTP status code of the response.
*/
protected final int code;
/**
* The class of the response. This is the most significant digit of the
* status code.
* <dl>
* <dt><code>1xx</code></dt> <dd>Informational response</dd>
* <dt><code>2xx</code></dt> <dd>Success</dd>
* <dt><code>3xx</code></dt> <dd>Redirection</dd>
* <dt><code>4xx</code></dt> <dd>Client error</dd>
* <dt><code>5xx</code></dt> <dd>Server error</dd>
* </dl>
*/
protected final int codeClass;
/**
* Human-readable text of the response.
*/
protected final String message;
/**
* The response headers.
*/
protected final Headers headers;
/**
* Constructs a new response with the specified parameters.
*/
protected Response(int majorVersion, int minorVersion, int code,
int codeClass, String message,
Headers headers)
{
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.code = code;
this.codeClass = codeClass;
this.message = message;
this.headers = headers;
}
/**
* Returns the HTTP major version of the server issuing the response.
* @see #majorVersion
*/
public int getMajorVersion()
{
return majorVersion;
}
/**
* Returns the HTTP minor version of the server issuing the response.
* @see #minorVersion
*/
public int getMinorVersion()
{
return minorVersion;
}
/**
* Returns the HTTP status code of the response.
* @see #code
*/
public int getCode()
{
return code;
}
/**
* Returns the class of the response.
* @see #codeClass
*/
public int getCodeClass()
{
return codeClass;
}
/**
* Returns the human-readable text of the response.
* @see #message
*/
public String getMessage()
{
return message;
}
/**
* Returns the headers in the response.
*/
public Headers getHeaders()
{
return headers;
}
/**
* Returns the header value for the specified name.
* @param name the header name
*/
public String getHeader(String name)
{
return headers.getValue(name);
}
/**
* Returns the header value for the specified name as an integer.
* @param name the header name
*/
public int getIntHeader(String name)
{
return headers.getIntValue(name);
}
/**
* Returns the header value for the specified name as a date.
* @param name the header name
*/
public Date getDateHeader(String name)
{
return headers.getDateValue(name);
}
}
/* ResponseBodyReader.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Callback interface for receiving notification of response body content.
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface ResponseBodyReader
{
/**
* Indicate whether this reader is interested in the specified response.
* If it returns false, it will not receive body content notifications for
* that response.
*/
boolean accept(Request request, Response response);
/**
* Receive notification of body content.
* @param buffer the content buffer
* @param offset the offset within the buffer that content starts
* @param length the length of the content
*/
void read(byte[] buffer, int offset, int length);
/**
* Notifies the reader that the end of the content was reached.
*/
void close();
}
/* ResponseHeaderHandler.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
/**
* Callback interface for objects that wish to be notified of response
* header values.
* @see Request#setHeaderHandler(String)
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface ResponseHeaderHandler
{
/**
* Sets the value for the header associated with this handler.
*/
void setValue(String value);
}
/* CookieManager.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* A simple non-persistent cookie manager. This class can be extended to
* provide cookie persistence.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class SimpleCookieManager
implements CookieManager
{
/**
* The cookie cache.
* This is a dictionary mapping domains to maps of cookies by name.
*/
protected Map cookies;
/**
* Constructor.
*/
public SimpleCookieManager()
{
cookies = new HashMap();
}
public void setCookie(Cookie cookie)
{
String domain = cookie.getDomain();
Map map =(Map) cookies.get(domain);
if (map == null)
{
map = new HashMap();
cookies.put(domain, map);
}
String name = cookie.getName();
map.put(name, cookie); // will replace a cookie of the same name
}
public Cookie[] getCookies(String host, boolean secure, String path)
{
List matches = new ArrayList();
Date now = new Date();
if (Character.isLetter(host.charAt(0)))
{
int di = host.indexOf('.');
while (di != -1)
{
addCookies(matches, host, secure, path, now);
host = host.substring(di);
di = host.indexOf('.', 1);
}
}
addCookies(matches, host, secure, path, now);
Cookie[] ret = new Cookie[matches.size()];
matches.toArray(ret);
return ret;
}
private void addCookies(List matches, String domain, boolean secure,
String path, Date now)
{
Map map = (Map) cookies.get(domain);
if (map != null)
{
List expired = new ArrayList();
for (Iterator i = map.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = (Map.Entry) i.next();
Cookie cookie = (Cookie) entry.getValue();
Date expires = cookie.getExpiryDate();
if (expires != null && expires.before(now))
{
expired.add(entry.getKey());
continue;
}
if (secure && !cookie.isSecure())
{
continue;
}
if (path.startsWith(cookie.getPath()))
{
matches.add(cookie);
}
}
// Good housekeeping
for (Iterator i = expired.iterator(); i.hasNext(); )
{
map.remove(i.next());
}
}
}
}
/* ConnectionEvent.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http.event;
import java.util.EventObject;
/**
* A connection event.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class ConnectionEvent
extends EventObject
{
/**
* The connection closed event type.
*/
public static final int CONNECTION_CLOSED = 0;
/**
* The type of this event.
*/
protected int type;
/**
* Constructs a connection event with the specified source and type.
*/
public ConnectionEvent(Object source, int type)
{
super(source);
this.type = type;
}
/**
* Returns the type of this event.
* @see #type
*/
public int getType()
{
return type;
}
}
/* ConnectionListener.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http.event;
import java.util.EventListener;
/**
* A connection listener.
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface ConnectionListener
extends EventListener
{
/**
* Callback invoked when the associated connection is closed.
*/
void connectionClosed(ConnectionEvent event);
}
/* RequestEvent.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http.event;
import gnu.java.net.protocol.http.Request;
import java.util.EventObject;
/**
* A request event.
*
* @author Chris Burdess (dog@gnu.org)
*/
public class RequestEvent
extends EventObject
{
/**
* The request created event type.
*/
public static final int REQUEST_CREATED = 0;
/**
* The request sending event type.
*/
public static final int REQUEST_SENDING = 1;
/**
* The request sent event type.
*/
public static final int REQUEST_SENT = 2;
/**
* The type of this event.
*/
protected int type;
/**
* The request associated with this event.
*/
protected Request request;
/**
* Constructs a request event with the specified source, type, and request.
*/
public RequestEvent(Object source, int type, Request request)
{
super(source);
this.type = type;
this.request = request;
}
/**
* Returns the type of this event.
* @see #type
*/
public int getType()
{
return type;
}
/**
* Returns the request associated with this event.
*/
public Request getRequest()
{
return request;
}
}
/* RequestListener.java --
Copyright (C) 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.net.protocol.http.event;
import java.util.EventListener;
/**
* A request listener.
*
* @author Chris Burdess (dog@gnu.org)
*/
public interface RequestListener
extends EventListener
{
/**
* Callback invoked when a request is created from the associated
* connection.
*/
void requestCreated(RequestEvent event);
/**
* Callback invoked when the request has been initialised with all data
* and before sending this data to the server.
*/
void requestSending(RequestEvent event);
/**
* Callback invoked after all request data has been sent to the server.
*/
void requestSent(RequestEvent event);
}
......@@ -59,10 +59,18 @@ public abstract class HttpsURLConnection extends HttpURLConnection
// Fields.
// ------------------------------------------------------------------
/** The default verifier. */
/**
* The default verifier.
* This is lazily initialized as required.
* @see #getDefaultHostnameVerifier
*/
private static HostnameVerifier defaultVerifier;
/** The default factory. */
/**
* The default factory.
* This is lazily initialized as required.
* @see #getDefaultSSLSocketFactory
*/
private static SSLSocketFactory defaultFactory;
/**
......@@ -75,21 +83,6 @@ public abstract class HttpsURLConnection extends HttpURLConnection
*/
private SSLSocketFactory factory;
// Static initializer.
// ------------------------------------------------------------------
static {
defaultVerifier = new TrivialHostnameVerifier();
try
{
defaultFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
// Constructor.
// ------------------------------------------------------------------
......@@ -102,8 +95,6 @@ public abstract class HttpsURLConnection extends HttpURLConnection
protected HttpsURLConnection(URL url) throws IOException
{
super(url);
hostnameVerifier = defaultVerifier;
factory = defaultFactory;
}
// Class methods.
......@@ -112,11 +103,17 @@ public abstract class HttpsURLConnection extends HttpURLConnection
/**
* Returns the default hostname verifier used in all new
* connections.
* If the default verifier has not been set, a new default one will be
* provided by this method.
*
* @return The default hostname verifier.
*/
public static HostnameVerifier getDefaultHostnameVerifier()
public static synchronized HostnameVerifier getDefaultHostnameVerifier()
{
if (defaultVerifier == null)
{
defaultVerifier = new TrivialHostnameVerifier();
}
return defaultVerifier;
}
......@@ -137,17 +134,33 @@ public abstract class HttpsURLConnection extends HttpURLConnection
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(new SSLPermission("setHostnameVerifier"));
defaultVerifier = newDefault;
synchronized (HttpsURLConnection.class)
{
defaultVerifier = newDefault;
}
}
/**
* Returns the default SSL socket factory used in all new
* connections.
* If the default SSL socket factory has not been set, a new default one
* will be provided by this method.
*
* @return The default SSL socket factory.
*/
public static SSLSocketFactory getDefaultSSLSocketFactory()
public static synchronized SSLSocketFactory getDefaultSSLSocketFactory()
{
if (defaultFactory == null)
{
try
{
defaultFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
return defaultFactory;
}
......@@ -168,7 +181,10 @@ public abstract class HttpsURLConnection extends HttpURLConnection
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkSetFactory();
defaultFactory = newDefault;
synchronized (HttpsURLConnection.class)
{
defaultFactory = newDefault;
}
}
// Instance methods.
......@@ -181,6 +197,10 @@ public abstract class HttpsURLConnection extends HttpURLConnection
*/
public HostnameVerifier getHostnameVerifier()
{
if (hostnameVerifier == null)
{
hostnameVerifier = getDefaultHostnameVerifier();
}
return hostnameVerifier;
}
......@@ -205,6 +225,10 @@ public abstract class HttpsURLConnection extends HttpURLConnection
*/
public SSLSocketFactory getSSLSocketFactory()
{
if (factory == null)
{
factory = getDefaultSSLSocketFactory();
}
return factory;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment