You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2005/04/01 19:54:18 UTC
svn commit: r159713 - in jakarta/httpclient/trunk/http-common/src:
java/org/apache/http/ java/org/apache/http/io/ java/org/apache/http/util/
test/org/apache/http/io/ test/org/apache/http/util/
Author: olegk
Date: Fri Apr 1 09:54:16 2005
New Revision: 159713
URL: http://svn.apache.org/viewcvs?view=rev&rev=159713
Log:
SVN copied from Jakarta Commons HttpClient
Added:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java
- copied, changed from r159689, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedInputStream.java
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedOutputStream.java
- copied, changed from r159689, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java
- copied, changed from r159705, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/EncodingUtil.java
- copied, changed from r159667, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ExceptionUtil.java
- copied, changed from r159667, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java
- copied, changed from r159698, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpParser.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java
- copied, changed from r159704, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestStreams.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java (with props)
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java (with props)
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java
- copied, changed from r159704, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpParser.java
Modified:
jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpException.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java
jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestLangUtils.java
Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpException.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpException.java?view=diff&r1=159712&r2=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpException.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/HttpException.java Fri Apr 1 09:54:16 2005
@@ -29,6 +29,8 @@
package org.apache.http;
+import org.apache.http.util.ExceptionUtil;
+
/**
* Signals that an HTTP exception has occurred.
*
@@ -64,7 +66,8 @@
* @since 3.0
*/
public HttpException(final String message, final Throwable cause) {
- super(message, cause);
+ super(message);
+ ExceptionUtil.initCause(this, cause);
}
}
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java (from r159689, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedInputStream.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedInputStream.java&r1=159689&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedInputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedInputStream.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/ChunkedInputStream.java,v 1.24 2004/10/10 15:18:55 olegk Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -27,17 +27,20 @@
*
*/
-package org.apache.commons.httpclient;
+package org.apache.http.io;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import org.apache.commons.httpclient.util.EncodingUtil;
-import org.apache.commons.httpclient.util.ExceptionUtil;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.util.EncodingUtil;
+import org.apache.http.util.ExceptionUtil;
+import org.apache.http.util.HeadersParser;
+import org.apache.http.util.HttpLineParser;
/**
* <p>Transparently coalesces chunks of a HTTP stream that uses
@@ -55,14 +58,15 @@
* @author Eric Johnson
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
* @author Michael Becke
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0
*
*/
public class ChunkedInputStream extends InputStream {
- /** The inputstream that we're wrapping */
- private InputStream in;
+
+ /** The data receiver that we're wrapping */
+ private HttpDataReceiver in;
/** The chunk size */
private int chunkSize;
@@ -78,47 +82,22 @@
/** True if this stream is closed */
private boolean closed = false;
+
+ private Header[] footers = new Header[] {};
- /** The method that this stream came from */
- private HttpMethod method = null;
-
- /** Log object for this class. */
- private static final Log LOG = LogFactory.getLog(ChunkedInputStream.class);
-
- /**
- * ChunkedInputStream constructor that associates the chunked input stream with a
- * {@link HttpMethod HTTP method}. Usually it should be the same {@link HttpMethod
- * HTTP method} the chunked input stream originates from. If chunked input stream
- * contains any footers (trailing headers), they will be added to the associated
- * {@link HttpMethod HTTP method}.
- *
- * @param in the raw input stream
- * @param method the HTTP method to associate this input stream with. Can be <tt>null</tt>.
- *
- * @throws IOException If an IO error occurs
- */
- public ChunkedInputStream(
- final InputStream in, final HttpMethod method) throws IOException {
-
+ public ChunkedInputStream(final HttpDataReceiver in) throws IOException {
+ super();
if (in == null) {
throw new IllegalArgumentException("InputStream parameter may not be null");
}
this.in = in;
- this.method = method;
this.pos = 0;
}
- /**
- * ChunkedInputStream constructor
- *
- * @param in the raw input stream
- *
- * @throws IOException If an IO error occurs
- */
- public ChunkedInputStream(final InputStream in) throws IOException {
- this(in, null);
+ public ChunkedInputStream(final InputStream instream) throws IOException {
+ this(new InputStreamHttpDataReceiver(instream));
}
-
+
/**
* <p> Returns all the data in a chunked stream in coalesced form. A chunk
* is followed by a CRLF. The method returns -1 as soon as a chunksize of 0
@@ -239,7 +218,7 @@
*
* @throws IOException when the chunk size could not be parsed
*/
- private static int getChunkSizeFromInputStream(final InputStream in)
+ private static int getChunkSizeFromInputStream(final HttpDataReceiver in)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -312,24 +291,13 @@
* @throws IOException If an IO problem occurs
*/
private void parseTrailerHeaders() throws IOException {
- Header[] footers = null;
try {
- String charset = "US-ASCII";
- if (this.method != null) {
- charset = this.method.getParams().getHttpElementCharset();
- }
- footers = HttpParser.parseHeaders(in, charset);
- } catch(HttpException e) {
- LOG.error("Error parsing trailer headers", e);
+ this.footers = HeadersParser.processHeaders(in);
+ } catch (HttpException e) {
IOException ioe = new IOException(e.getMessage());
ExceptionUtil.initCause(ioe, e);
throw ioe;
}
- if (this.method != null) {
- for (int i = 0; i < footers.length; i++) {
- this.method.addResponseFooter(footers[i]);
- }
- }
}
/**
@@ -351,6 +319,10 @@
}
}
+ public Header[] getFooters() {
+ return this.footers;
+ }
+
/**
* Exhaust an input stream, reading until EOF has been encountered.
*
@@ -362,11 +334,51 @@
* @param inStream The {@link InputStream} to exhaust.
* @throws IOException If an IO problem occurs
*/
- static void exhaustInputStream(InputStream inStream) throws IOException {
+ static void exhaustInputStream(final InputStream inStream) throws IOException {
// read and discard the remainder of the message
byte buffer[] = new byte[1024];
while (inStream.read(buffer) >= 0) {
;
+ }
+ }
+
+ static class InputStreamHttpDataReceiver implements HttpDataReceiver {
+
+ private final InputStream instream;
+
+ private String charset = "US-ASCII";
+
+ public InputStreamHttpDataReceiver(final InputStream instream) {
+ super();
+ if (instream == null) {
+ throw new IllegalArgumentException("Input stream may not be null");
+ }
+ this.instream = instream;
+ }
+
+ public boolean isDataAvailable(int timeout) throws IOException {
+ return this.instream.available() > 0;
+ }
+
+ public int read() throws IOException {
+ return this.instream.read();
+ }
+
+ public int read(final byte[] b, int off, int len) throws IOException {
+ return this.instream.read(b, off, len);
+ }
+
+ public int read(final byte[] b) throws IOException {
+ return this.instream.read(b);
+ }
+
+ public String readLine() throws IOException {
+ return HttpLineParser.readLine(this.instream, this.charset);
+ }
+
+ public void reset(final HttpParams params) {
+ HttpProtocolParams protocolParams = new HttpProtocolParams(params);
+ this.charset = protocolParams.getHttpElementCharset();
}
}
}
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedOutputStream.java (from r159689, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedOutputStream.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java&r1=159689&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedOutputStream.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ChunkedOutputStream.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v 1.16 2004/05/13 04:03:25 mbecke Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -27,12 +27,12 @@
*
*/
-package org.apache.commons.httpclient;
+package org.apache.http.io;
import java.io.IOException;
import java.io.OutputStream;
-import org.apache.commons.httpclient.util.EncodingUtil;
+import org.apache.http.util.EncodingUtil;
/**
* Implements HTTP chunking support. Writes are buffered to an internal buffer (2048 default size).
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java (from r159705, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java&r1=159705&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/io/ContentLengthInputStream.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v 1.12 2004/10/04 22:05:44 olegk Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -27,7 +27,7 @@
*
*/
-package org.apache.commons.httpclient;
+package org.apache.http.io;
import java.io.IOException;
import java.io.InputStream;
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/EncodingUtil.java (from r159667, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/EncodingUtil.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java&r1=159667&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/EncodingUtil.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/EncodingUtil.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/EncodingUtil.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/EncodingUtil.java,v 1.8 2004/05/13 04:01:22 mbecke Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -26,110 +26,22 @@
* <http://www.apache.org/>.
*
*/
-package org.apache.commons.httpclient.util;
+package org.apache.http.util;
import java.io.UnsupportedEncodingException;
-import org.apache.commons.codec.net.URLCodec;
-import org.apache.commons.httpclient.HttpClientError;
-import org.apache.commons.httpclient.NameValuePair;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
/**
* The home for utility methods that handle various encoding tasks.
*
* @author Michael Becke
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*
* @since 2.0 final
*/
public class EncodingUtil {
- /** Default content encoding chatset */
- private static final String DEFAULT_CHARSET = "ISO-8859-1";
-
- /** Log object for this class. */
- private static final Log LOG = LogFactory.getLog(EncodingUtil.class);
-
- /**
- * Form-urlencoding routine.
- *
- * The default encoding for all forms is `application/x-www-form-urlencoded'.
- * A form data set is represented in this media type as follows:
- *
- * The form field names and values are escaped: space characters are replaced
- * by `+', and then reserved characters are escaped as per [URL]; that is,
- * non-alphanumeric characters are replaced by `%HH', a percent sign and two
- * hexadecimal digits representing the ASCII code of the character. Line breaks,
- * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'.
- *
- * <p>
- * if the given charset is not supported, ISO-8859-1 is used instead.
- * </p>
- *
- * @param pairs the values to be encoded
- * @param charset the character set of pairs to be encoded
- *
- * @return the urlencoded pairs
- *
- * @since 2.0 final
- */
- public static String formUrlEncode(NameValuePair[] pairs, String charset) {
- try {
- return doFormUrlEncode(pairs, charset);
- } catch (UnsupportedEncodingException e) {
- LOG.error("Encoding not supported: " + charset);
- try {
- return doFormUrlEncode(pairs, DEFAULT_CHARSET);
- } catch (UnsupportedEncodingException fatal) {
- // Should never happen. ISO-8859-1 must be supported on all JVMs
- throw new HttpClientError("Encoding not supported: " +
- DEFAULT_CHARSET);
- }
- }
- }
-
- /**
- * Form-urlencoding routine.
- *
- * The default encoding for all forms is `application/x-www-form-urlencoded'.
- * A form data set is represented in this media type as follows:
- *
- * The form field names and values are escaped: space characters are replaced
- * by `+', and then reserved characters are escaped as per [URL]; that is,
- * non-alphanumeric characters are replaced by `%HH', a percent sign and two
- * hexadecimal digits representing the ASCII code of the character. Line breaks,
- * as in multi-line text field values, are represented as CR LF pairs, i.e. `%0D%0A'.
- *
- * @param pairs the values to be encoded
- * @param charset the character set of pairs to be encoded
- *
- * @return the urlencoded pairs
- * @throws UnsupportedEncodingException if charset is not supported
- *
- * @since 2.0 final
- */
- private static String doFormUrlEncode(NameValuePair[] pairs, String charset)
- throws UnsupportedEncodingException
- {
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < pairs.length; i++) {
- URLCodec codec = new URLCodec();
- NameValuePair pair = pairs[i];
- if (pair.getName() != null) {
- if (i > 0) {
- buf.append("&");
- }
- buf.append(codec.encode(pair.getName(), charset));
- buf.append("=");
- if (pair.getValue() != null) {
- buf.append(codec.encode(pair.getValue(), charset));
- }
- }
- }
- return buf.toString();
- }
+ /** ASCII chatset */
+ private static final String ASCII_CHARSET = "US-ASCII";
/**
* Converts the byte array of HTTP content characters to a string. If
@@ -162,10 +74,6 @@
try {
return new String(data, offset, length, charset);
} catch (UnsupportedEncodingException e) {
-
- if (LOG.isWarnEnabled()) {
- LOG.warn("Unsupported encoding: " + charset + ". System encoding used");
- }
return new String(data, offset, length);
}
}
@@ -182,7 +90,10 @@
*
* @since 3.0
*/
- public static String getString(final byte[] data, String charset) {
+ public static String getString(final byte[] data, final String charset) {
+ if (data == null) {
+ throw new IllegalArgumentException("Parameter may not be null");
+ }
return getString(data, 0, data.length, charset);
}
@@ -196,7 +107,7 @@
*
* @since 3.0
*/
- public static byte[] getBytes(final String data, String charset) {
+ public static byte[] getBytes(final String data, final String charset) {
if (data == null) {
throw new IllegalArgumentException("data may not be null");
@@ -209,11 +120,6 @@
try {
return data.getBytes(charset);
} catch (UnsupportedEncodingException e) {
-
- if (LOG.isWarnEnabled()) {
- LOG.warn("Unsupported encoding: " + charset + ". System encoding used.");
- }
-
return data.getBytes();
}
}
@@ -233,9 +139,9 @@
}
try {
- return data.getBytes("US-ASCII");
+ return data.getBytes(ASCII_CHARSET);
} catch (UnsupportedEncodingException e) {
- throw new HttpClientError("HttpClient requires ASCII support");
+ throw new AsciiNotSupportedError("HttpClient requires ASCII support");
}
}
@@ -258,9 +164,9 @@
}
try {
- return new String(data, offset, length, "US-ASCII");
+ return new String(data, offset, length, ASCII_CHARSET);
} catch (UnsupportedEncodingException e) {
- throw new HttpClientError("HttpClient requires ASCII support");
+ throw new AsciiNotSupportedError("HttpClient requires ASCII support");
}
}
@@ -282,6 +188,15 @@
* This class should not be instantiated.
*/
private EncodingUtil() {
+ super();
}
+ static class AsciiNotSupportedError extends Error {
+
+ public AsciiNotSupportedError(final String message) {
+ super(message);
+ }
+
+ }
+
}
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ExceptionUtil.java (from r159667, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ExceptionUtil.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java&r1=159667&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ExceptionUtil.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/ExceptionUtil.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java,v 1.5 2004/10/19 18:09:46 olegk Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -26,33 +26,23 @@
* <http://www.apache.org/>.
*
*/
-package org.apache.commons.httpclient.util;
+package org.apache.http.util;
-import java.io.InterruptedIOException;
import java.lang.reflect.Method;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
/**
* The home for utility methods that handle various exception-related tasks.
*
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
* @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
*
* @since 3.0
*/
public class ExceptionUtil {
- /** Log object for this class. */
- private static final Log LOG = LogFactory.getLog(ExceptionUtil.class);
-
/** A reference to Throwable's initCause method, or null if it's not there in this JVM */
static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
- /** A reference to SocketTimeoutExceptionClass class, or null if it's not there in this JVM */
- static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
-
/**
* Returns a <code>Method<code> allowing access to
* {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
@@ -71,20 +61,6 @@
}
}
- /**
- * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
- * does not exist.
- *
- * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
- */
- static private Class SocketTimeoutExceptionClass() {
- try {
- return Class.forName("java.net.SocketTimeoutException");
- } catch (ClassNotFoundException e) {
- return null;
- }
- }
-
/**
* If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
*
@@ -96,26 +72,9 @@
try {
INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
} catch (Exception e) {
- LOG.warn("Exception invoking Throwable.initCause", e);
+ // Well, with no logging, the only option is to munch the exception
}
}
}
- /**
- * If SocketTimeoutExceptionClass is defined, returns <tt>true</tt> only if the
- * exception is an instance of SocketTimeoutExceptionClass. If
- * SocketTimeoutExceptionClass is undefined, always returns <tt>true</tt>.
- *
- * @param e an instance of InterruptedIOException class.
- *
- * @return <tt>true</tt> if the exception signals socket timeout, <tt>false</tt>
- * otherwise.
- */
- public static boolean isSocketTimeoutException(final InterruptedIOException e) {
- if (SOCKET_TIMEOUT_CLASS != null) {
- return SOCKET_TIMEOUT_CLASS.isInstance(e);
- } else {
- return true;
- }
- }
}
Copied: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java (from r159698, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpParser.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpParser.java&r1=159698&p2=jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/HttpParser.java (original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/util/HttpLineParser.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpParser.java,v 1.13 2005/01/11 13:57:06 oglueck Exp $
+ * $HeadURL$
* $Revision$
* $Date$
*
@@ -27,35 +27,27 @@
*
*/
-package org.apache.commons.httpclient;
+package org.apache.http.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
-import java.util.ArrayList;
-
-import org.apache.commons.httpclient.util.EncodingUtil;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
/**
* A utility class for parsing http header values according to
* RFC-2616 Section 4 and 19.3.
*
* @author Michael Becke
- * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
- *
- * @since 2.0beta1
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
*/
-public class HttpParser {
+public class HttpLineParser {
- /** Log object for this class. */
- private static final Log LOG = LogFactory.getLog(HttpParser.class);
-
/**
* Constructor for HttpParser.
*/
- private HttpParser() { }
+ private HttpLineParser() {
+ super();
+ }
/**
* Return byte array from an (unchunked) input stream.
@@ -69,9 +61,8 @@
* @throws IOException if an I/O problem occurs
* @return a byte array from the stream
*/
- public static byte[] readRawLine(InputStream inputStream) throws IOException {
- LOG.trace("enter HttpParser.readRawLine()");
-
+ public static byte[] readRawLine(final InputStream inputStream)
+ throws IOException {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int ch;
while ((ch = inputStream.read()) >= 0) {
@@ -100,8 +91,8 @@
*
* @since 3.0
*/
- public static String readLine(InputStream inputStream, String charset) throws IOException {
- LOG.trace("enter HttpParser.readLine(InputStream, String)");
+ public static String readLine(final InputStream inputStream, final String charset)
+ throws IOException {
byte[] rawdata = readRawLine(inputStream);
if (rawdata == null) {
return null;
@@ -136,89 +127,8 @@
* @deprecated use #readLine(InputStream, String)
*/
- public static String readLine(InputStream inputStream) throws IOException {
- LOG.trace("enter HttpParser.readLine(InputStream)");
+ public static String readLine(final InputStream inputStream) throws IOException {
return readLine(inputStream, "US-ASCII");
}
- /**
- * Parses headers from the given stream. Headers with the same name are not
- * combined.
- *
- * @param is the stream to read headers from
- * @param charset the charset to use for reading the data
- *
- * @return an array of headers in the order in which they were parsed
- *
- * @throws IOException if an IO error occurs while reading from the stream
- * @throws HttpException if there is an error parsing a header value
- *
- * @since 3.0
- */
- public static Header[] parseHeaders(InputStream is, String charset) throws IOException, HttpException {
- LOG.trace("enter HeaderParser.parseHeaders(InputStream, String)");
-
- ArrayList headers = new ArrayList();
- String name = null;
- StringBuffer value = null;
- for (; ;) {
- String line = HttpParser.readLine(is, charset);
- if ((line == null) || (line.length() < 1)) {
- break;
- }
-
- // Parse the header name and value
- // Check for folded headers first
- // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
- // discussion on folded headers
- if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) {
- // we have continuation folded header
- // so append value
- if (value != null) {
- value.append(' ');
- value.append(line.trim());
- }
- } else {
- // make sure we save the previous name,value pair if present
- if (name != null) {
- headers.add(new Header(name, value.toString()));
- }
-
- // Otherwise we should have normal HTTP header line
- // Parse the header name and value
- int colon = line.indexOf(":");
- if (colon < 0) {
- throw new ProtocolException("Unable to parse header: " + line);
- }
- name = line.substring(0, colon).trim();
- value = new StringBuffer(line.substring(colon + 1).trim());
- }
-
- }
-
- // make sure we save the last name,value pair if present
- if (name != null) {
- headers.add(new Header(name, value.toString()));
- }
-
- return (Header[]) headers.toArray(new Header[headers.size()]);
- }
-
- /**
- * Parses headers from the given stream. Headers with the same name are not
- * combined.
- *
- * @param is the stream to read headers from
- *
- * @return an array of headers in the order in which they were parsed
- *
- * @throws IOException if an IO error occurs while reading from the stream
- * @throws HttpException if there is an error parsing a header value
- *
- * @deprecated use #parseHeaders(InputStream, String)
- */
- public static Header[] parseHeaders(InputStream is) throws IOException, HttpException {
- LOG.trace("enter HeaderParser.parseHeaders(InputStream, String)");
- return parseHeaders(is, "US-ASCII");
- }
}
Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java?view=diff&r1=159712&r2=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestAllIO.java Fri Apr 1 09:54:16 2005
@@ -40,6 +40,7 @@
TestSuite suite = new TestSuite();
suite.addTest(TestHttpDataInputStream.suite());
suite.addTest(TestHttpDataOutputStream.suite());
+ suite.addTest(TestChunkCoding.suite());
return suite;
}
Copied: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java (from r159704, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestStreams.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestStreams.java&r1=159704&p2=jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestStreams.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/io/TestChunkCoding.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v 1.19 2004/10/31 14:04:13 olegk Exp $
+ * $HeadURL$
* $Revision$
* $Date$
* ====================================================================
@@ -24,11 +24,9 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
-package org.apache.commons.httpclient;
+package org.apache.http.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -36,30 +34,41 @@
import java.io.InputStream;
import java.io.OutputStream;
+import org.apache.http.Header;
+import org.apache.http.util.EncodingUtil;
+
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.util.EncodingUtil;
-
-
-public class TestStreams extends TestCase {
+public class TestChunkCoding extends TestCase {
private static final String CONTENT_CHARSET = "ISO-8859-1";
- public TestStreams(String testName) {
+ public TestChunkCoding(String testName) {
super(testName);
}
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestChunkCoding.class);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestChunkCoding.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
public void testChunkedInputStream() throws IOException {
String correctInput = "10;key=\"value\r\nnewline\"\r\n1234567890123456\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
String correctResult = "123456789012345612345";
- HttpMethod method = new FakeHttpMethod();
//Test for when buffer is larger than chunk size
- InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
- EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
+ ChunkedInputStream in = new ChunkedInputStream(
+ new ByteArrayInputStream(
+ EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)));
byte[] buffer = new byte[300];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
@@ -68,36 +77,41 @@
}
String result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
assertEquals(result, correctResult);
- Header footer = method.getResponseFooter("footer1");
- assertEquals(footer.getValue(), "abcde");
- footer = method.getResponseFooter("footer2");
- assertEquals(footer.getValue(), "fghij");
-
- method = new FakeHttpMethod();
-
+
+ Header[] footers = in.getFooters();
+ assertNotNull(footers);
+ assertEquals(2, footers.length);
+ assertEquals("Footer1", footers[0].getName());
+ assertEquals("abcde", footers[0].getValue());
+ assertEquals("Footer2", footers[1].getName());
+ assertEquals("fghij", footers[1].getValue());
+
//Test for when buffer is smaller than chunk size.
- in = new ChunkedInputStream(new ByteArrayInputStream(
- EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)), method);
+ in = new ChunkedInputStream(
+ new ByteArrayInputStream(
+ EncodingUtil.getBytes(correctInput, CONTENT_CHARSET)));
buffer = new byte[7];
out = new ByteArrayOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result = EncodingUtil.getString(out.toByteArray(), CONTENT_CHARSET);
- assertEquals(result, correctResult);
- footer = method.getResponseFooter("footer1");
- assertEquals(footer.getValue(), "abcde");
- footer = method.getResponseFooter("footer2");
- assertEquals(footer.getValue(), "fghij");
+ footers = in.getFooters();
+ assertNotNull(footers);
+ assertEquals(2, footers.length);
+ assertEquals("Footer1", footers[0].getName());
+ assertEquals("abcde", footers[0].getValue());
+ assertEquals("Footer2", footers[1].getName());
+ assertEquals("fghij", footers[1].getValue());
}
public void testCorruptChunkedInputStream1() throws IOException {
//missing \r\n at the end of the first chunk
String corrupInput = "10;key=\"value\"\r\n123456789012345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\nFooter2: fghij\r\n";
- HttpMethod method = new FakeHttpMethod();
- InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
- EncodingUtil.getBytes(corrupInput, CONTENT_CHARSET)), method);
+ InputStream in = new ChunkedInputStream(
+ new ByteArrayInputStream(
+ EncodingUtil.getBytes(corrupInput, CONTENT_CHARSET)));
byte[] buffer = new byte[300];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
@@ -113,10 +127,9 @@
public void testEmptyChunkedInputStream() throws IOException {
String input = "0\r\n";
- HttpMethod method = new FakeHttpMethod();
-
- InputStream in = new ChunkedInputStream(new ByteArrayInputStream(
- EncodingUtil.getBytes(input, CONTENT_CHARSET)), method);
+ InputStream in = new ChunkedInputStream(
+ new ByteArrayInputStream(
+ EncodingUtil.getBytes(input, CONTENT_CHARSET)));
byte[] buffer = new byte[300];
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
@@ -162,7 +175,9 @@
out.write(EncodingUtil.getBytes(input, CONTENT_CHARSET));
out.close();
buffer.close();
- InputStream in = new ChunkedInputStream(new ByteArrayInputStream(buffer.toByteArray()), new GetMethod());
+ InputStream in = new ChunkedInputStream(
+ new ByteArrayInputStream(
+ buffer.toByteArray()));
byte[] d = new byte[10];
ByteArrayOutputStream result = new ByteArrayOutputStream();
@@ -258,16 +273,5 @@
assertEquals('\n', rawdata[10]);
}
- // ------------------------------------------------------- TestCase Methods
-
- public static Test suite() {
- return new TestSuite(TestStreams.class);
- }
-
- // ------------------------------------------------------------------- Main
- public static void main(String args[]) {
- String[] testCaseName = { TestStreams.class.getName() };
- junit.textui.TestRunner.main(testCaseName);
- }
}
Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java?view=diff&r1=159712&r2=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestAllUtil.java Fri Apr 1 09:54:16 2005
@@ -39,6 +39,9 @@
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestLangUtils.suite());
+ suite.addTest(TestExceptionUtils.suite());
+ suite.addTest(TestEncodingUtils.suite());
+ suite.addTest(TestHttpLineParser.suite());
suite.addTest(TestParameterParser.suite());
suite.addTest(TestHeadersParser.suite());
return suite;
Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java?view=auto&rev=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java Fri Apr 1 09:54:16 2005
@@ -0,0 +1,167 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link TestEncodingUtils}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestEncodingUtils extends TestCase {
+
+ public TestEncodingUtils(String testName) {
+ super(testName);
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestEncodingUtils.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestEncodingUtils.class);
+ }
+
+ private static String constructString(int [] unicodeChars) {
+ StringBuffer buffer = new StringBuffer();
+ if (unicodeChars != null) {
+ for (int i = 0; i < unicodeChars.length; i++) {
+ buffer.append((char)unicodeChars[i]);
+ }
+ }
+ return buffer.toString();
+ }
+
+ static final int SWISS_GERMAN_HELLO [] = {
+ 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
+ };
+
+ public void testBytesToString() throws Exception {
+ String s = constructString(SWISS_GERMAN_HELLO);
+ byte[] utf = s.getBytes("UTF-8");
+ byte[] latin1 = s.getBytes("ISO-8859-1");
+ byte[] def = s.getBytes();
+
+ String s1 = EncodingUtil.getString(utf, "UTF-8");
+ String s2 = EncodingUtil.getString(latin1, "ISO-8859-1");
+ String s3 = EncodingUtil.getString(def, "THIS JUST DOES NOT SEEM RIGHT");
+
+ assertEquals(s, s1);
+ assertEquals(s, s2);
+ assertEquals(s, s3);
+
+ try {
+ EncodingUtil.getString(null, "UTF-8");
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ EncodingUtil.getString(new byte[] {}, null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ EncodingUtil.getString(new byte[] {}, "");
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testStringToBytesToString() throws Exception {
+ String s = constructString(SWISS_GERMAN_HELLO);
+ byte[] utf = s.getBytes("UTF-8");
+ byte[] latin1 = s.getBytes("ISO-8859-1");
+ byte[] def = s.getBytes();
+
+ byte[] data1 = EncodingUtil.getBytes(s, "UTF-8");
+ byte[] data2 = EncodingUtil.getBytes(s, "ISO-8859-1");
+ byte[] data3 = EncodingUtil.getBytes(s, "THIS JUST DOES NOT SEEM RIGHT");
+
+ assertNotNull(data1);
+ assertEquals(utf.length, data1.length);
+ for (int i = 0; i < utf.length; i++) {
+ assertEquals(utf[i], data1[i]);
+ }
+ assertNotNull(data2);
+ assertEquals(latin1.length, data2.length);
+ for (int i = 0; i < latin1.length; i++) {
+ assertEquals(latin1[i], data2[i]);
+ }
+ assertNotNull(data3);
+ assertEquals(def.length, data3.length);
+ for (int i = 0; i < def.length; i++) {
+ assertEquals(def[i], data3[i]);
+ }
+
+ try {
+ EncodingUtil.getBytes(null, "UTF-8");
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ EncodingUtil.getBytes("what not", null);
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ EncodingUtil.getBytes("what not", "");
+ fail("IllegalArgumentException should have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testAsciiBytesToString() throws Exception {
+ String s = "ascii only, I mean it!";
+ assertEquals(s, EncodingUtil.getAsciiString(s.getBytes("US-ASCII")));
+ }
+
+ public void testAsciiStringToBytes() throws Exception {
+ String s = "ascii only, I mean it!";
+ byte[] ascii = s.getBytes("US-ASCII");
+ byte[] data = EncodingUtil.getAsciiBytes(s);
+
+ assertNotNull(data);
+ assertEquals(ascii.length, data.length);
+ for (int i = 0; i < ascii.length; i++) {
+ assertEquals(ascii[i], data[i]);
+ }
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestEncodingUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java?view=auto&rev=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java (added)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java Fri Apr 1 09:54:16 2005
@@ -0,0 +1,64 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 1999-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link TestExceptionUtils}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestExceptionUtils extends TestCase {
+
+ public TestExceptionUtils(String testName) {
+ super(testName);
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestExceptionUtils.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestExceptionUtils.class);
+ }
+
+ public void testExceptionChaining() throws Exception {
+ Exception ex1 = new Exception();
+ Exception ex2 = new Exception();
+ ExceptionUtil.initCause(ex1, ex2);
+ assertNotNull(ex1.getCause());
+ assertTrue(ex1.getCause() == ex2);
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestExceptionUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java (from r159704, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpParser.java)
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java?view=diff&rev=159713&p1=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpParser.java&r1=159704&p2=jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java&r2=159713
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestHttpParser.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestHttpLineParser.java Fri Apr 1 09:54:16 2005
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpParser.java,v 1.3 2004/02/22 18:08:49 olegk Exp $
+ * $HeadURL$
* $Revision$
* $Date$
* ====================================================================
@@ -24,11 +24,9 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- * [Additional notices, if required by prior licensing conditions]
- *
*/
-package org.apache.commons.httpclient;
+package org.apache.http.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
@@ -41,39 +39,70 @@
* @author Oleg Kalnichevski
* @version $Id$
*/
-public class TestHttpParser extends TestCase {
+public class TestHttpLineParser extends TestCase {
- private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
+ private static final String ASCII = "US-ASCII";
+ private static final String UTF8 = "UTF-8";
// ------------------------------------------------------------ Constructor
- public TestHttpParser(String testName) {
+ public TestHttpLineParser(String testName) {
super(testName);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
- String[] testCaseName = { TestHeader.class.getName() };
+ String[] testCaseName = { TestHttpLineParser.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
- return new TestSuite(TestHttpParser.class);
+ return new TestSuite(TestHttpLineParser.class);
}
public void testReadHttpLine() throws Exception {
InputStream instream = new ByteArrayInputStream(
- "\r\r\nstuff\r\n".getBytes(HTTP_ELEMENT_CHARSET));
- assertEquals("\r", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
- assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
- assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
+ "\r\r\nstuff\r\n".getBytes(ASCII));
+ assertEquals("\r", HttpLineParser.readLine(instream, ASCII));
+ assertEquals("stuff", HttpLineParser.readLine(instream, ASCII));
+ assertEquals(null, HttpLineParser.readLine(instream, ASCII));
+
+ instream = new ByteArrayInputStream(
+ "\n\r\nstuff\r\n".getBytes(ASCII));
+ assertEquals("", HttpLineParser.readLine(instream, ASCII));
+ assertEquals("", HttpLineParser.readLine(instream, ASCII));
+ assertEquals("stuff", HttpLineParser.readLine(instream, ASCII));
+ assertEquals(null, HttpLineParser.readLine(instream, ASCII));
+ }
+
+ private static String constructString(int [] unicodeChars) {
+ StringBuffer buffer = new StringBuffer();
+ if (unicodeChars != null) {
+ for (int i = 0; i < unicodeChars.length; i++) {
+ buffer.append((char)unicodeChars[i]);
+ }
+ }
+ return buffer.toString();
+ }
+
+ static final int SWISS_GERMAN_HELLO [] = {
+ 0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
+ };
+
+ public void testReadMultibyeHttpLine() throws Exception {
+ String s = constructString(SWISS_GERMAN_HELLO);
+ InputStream instream = new ByteArrayInputStream(
+ ("\r\r\n" + s + "\r\n").getBytes(UTF8));
+ assertEquals("\r", HttpLineParser.readLine(instream, UTF8));
+ assertEquals(s, HttpLineParser.readLine(instream, UTF8));
+ assertEquals(null, HttpLineParser.readLine(instream, UTF8));
instream = new ByteArrayInputStream(
- "\n\r\nstuff\r\n".getBytes("US-ASCII"));
- assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
- assertEquals("", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
- assertEquals("stuff", HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
- assertEquals(null, HttpParser.readLine(instream, HTTP_ELEMENT_CHARSET));
+ ("\n\r\n" + s + "\r\n").getBytes(UTF8));
+ assertEquals("", HttpLineParser.readLine(instream, UTF8));
+ assertEquals("", HttpLineParser.readLine(instream, UTF8));
+ assertEquals(s, HttpLineParser.readLine(instream, UTF8));
+ assertEquals(null, HttpLineParser.readLine(instream, UTF8));
}
}
Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestLangUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestLangUtils.java?view=diff&r1=159712&r2=159713
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestLangUtils.java (original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/util/TestLangUtils.java Fri Apr 1 09:54:16 2005
@@ -66,6 +66,16 @@
assertTrue(h1 == h2);
}
+ public void testBooleanHash() {
+ int h1 = LangUtils.hashCode(LangUtils.HASH_SEED, true);
+ int h2 = LangUtils.hashCode(LangUtils.HASH_SEED, false);
+ int h3 = LangUtils.hashCode(LangUtils.HASH_SEED, true);
+ int h4 = LangUtils.hashCode(LangUtils.HASH_SEED, false);
+ assertTrue(h1 != h2);
+ assertTrue(h1 == h3);
+ assertTrue(h2 == h4);
+ }
+
public void testBasicEquality() {
assertTrue(LangUtils.equals(null, null));
assertFalse(LangUtils.equals(null, "abc"));