Index: java/org/apache/commons/httpclient/HttpConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v retrieving revision 1.89 diff -u -r1.89 HttpConnection.java --- java/org/apache/commons/httpclient/HttpConnection.java 18 Apr 2004 23:51:34 -0000 1.89 +++ java/org/apache/commons/httpclient/HttpConnection.java 9 May 2004 21:51:39 -0000 @@ -1035,6 +1035,8 @@ /** * Attempts to shutdown the {@link Socket}'s output, via Socket.shutdownOutput() * when running on JVM 1.3 or higher. + * + * @deprecated unused */ public void shutdownOutput() { LOG.trace("enter HttpConnection.shutdownOutput()"); Index: java/org/apache/commons/httpclient/HttpMethod.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v retrieving revision 1.36 diff -u -r1.36 HttpMethod.java --- java/org/apache/commons/httpclient/HttpMethod.java 18 Apr 2004 23:51:35 -0000 1.36 +++ java/org/apache/commons/httpclient/HttpMethod.java 9 May 2004 21:51:41 -0000 @@ -33,7 +33,7 @@ import java.io.InputStream; import org.apache.commons.httpclient.auth.AuthState; -import org.apache.commons.httpclient.params.*; +import org.apache.commons.httpclient.params.HttpMethodParams; /** *

@@ -433,6 +433,13 @@ */ int execute(HttpState state, HttpConnection connection) throws HttpException, IOException; + + /** + * Aborts the execution of the HTTP method. + * + * @see #execute(HttpState, HttpConnection) + */ + void abort(); /** * Recycles the HTTP method so that it can be used again. Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.205 diff -u -r1.205 HttpMethodBase.java --- java/org/apache/commons/httpclient/HttpMethodBase.java 8 May 2004 10:12:07 -0000 1.205 +++ java/org/apache/commons/httpclient/HttpMethodBase.java 9 May 2004 21:51:47 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.205 2004/05/08 10:12:07 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.205 2004/05/08 10:12:07 olegk Exp $ * $Revision: 1.205 $ * $Date: 2004/05/08 10:12:07 $ * @@ -168,6 +168,9 @@ /** HTTP protocol version used for execution of this method. */ private HttpVersion effectiveVersion = null; + /** Whether the execution of this method has been aborted */ + private boolean aborted = false; + // ----------------------------------------------------------- Constructors /** @@ -917,10 +920,9 @@ if (conn == null) { throw new IllegalArgumentException("HttpConnection parameter may not be null"); } - // TODO: do we care -// if (hasBeenUsed()) { -// throw new IllegalStateException("Already used, but not recycled."); -// } + if (this.aborted) { + throw new IllegalStateException("Method has been aborted"); + } if (!validate()) { throw new ProtocolException("HttpMethodBase object not valid"); } @@ -974,6 +976,21 @@ } /** + * Aborts the execution of this method. + */ + + public void abort() { + if (this.aborted) { + return; + } + this.aborted = true; + HttpConnection conn = this.responseConnection; + if (conn != null) { + conn.close(); + } + } + + /** * Returns true if the HTTP method has been already {@link #execute executed}, * but not {@link #recycle recycled}. * @@ -1004,6 +1021,8 @@ getResponseHeaderGroup().clear(); getResponseTrailerHeaderGroup().clear(); statusLine = null; + effectiveVersion = null; + aborted = false; used = false; params = new HttpMethodParams(); responseBody = null; @@ -2288,4 +2307,16 @@ public AuthState getProxyAuthState() { return this.proxyAuthState; } + + /** + * Tests whether the execution of this method has been aborted + * + * @return true if the execution of this method has been aborted, + * false otherwise + * + * @since 3.0 + */ + public boolean isAborted() { + return this.aborted; + } } Index: test/org/apache/commons/httpclient/TestMethodAbort.java =================================================================== RCS file: test/org/apache/commons/httpclient/TestMethodAbort.java diff -N test/org/apache/commons/httpclient/TestMethodAbort.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/TestMethodAbort.java 9 May 2004 21:51:48 -0000 @@ -0,0 +1,147 @@ +/* + * $Header$ + * $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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.server.HttpRequestHandler; +import org.apache.commons.httpclient.server.ResponseWriter; +import org.apache.commons.httpclient.server.SimpleHttpServerConnection; +import org.apache.commons.httpclient.server.SimpleRequest; + +/** + * Tests ability to abort method execution. + * + * @author Oleg Kalnichevski + * + * @version $Revision$ + */ +public class TestMethodAbort extends HttpClientTestBase { + + // ------------------------------------------------------------ Constructor + public TestMethodAbort(String testName) { + super(testName); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestMethodAbort.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestMethodAbort.class); + } + + private class ProduceGarbageHandler implements HttpRequestHandler { + + public ProduceGarbageHandler() { + super(); + } + + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { + + final String garbage = "garbage!\r\n"; + final long count = 1000000000; + + String protocol = request.getRequestLine().getProtocol(); + ResponseWriter out = conn.getWriter(); + out.println(protocol + " 200 OK"); + out.println("Content-Type: text/plain"); + out.println("Content-Length: " + count * garbage.length()) ; + out.println("Connection: close"); + out.println(); + for (int i = 0; i < count; i++) { + out.print(garbage); + } + return true; + } + } + + public void testAbortMethod() throws IOException { + this.server.setRequestHandler(new ProduceGarbageHandler()); + final GetMethod httpget = new GetMethod("/test/"); + + Thread thread = new Thread(new Runnable() { + public void run() { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + } + httpget.abort(); + } + + }); + thread.setDaemon(true); + thread.start(); + + try { + this.client.executeMethod(httpget); + BufferedReader in = new BufferedReader(new InputStreamReader( + httpget.getResponseBodyAsStream())); + String line = null; + while ((line = in.readLine()) != null) { + } + fail("IOException must have been thrown"); + } catch (IOException e) { + // expected + } finally { + httpget.releaseConnection(); + } + assertTrue(httpget.isAborted()); + } + + public void testAbortedMethodExecute() throws IOException { + final GetMethod httpget = new GetMethod("/test/"); + + try { + httpget.abort(); + try { + this.client.executeMethod(httpget); + fail("IllegalStateException must have been thrown"); + } catch (IllegalStateException e) { + } + } finally { + httpget.releaseConnection(); + } + assertTrue(httpget.isAborted()); + } +} Index: test/org/apache/commons/httpclient/TestNoHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.37 diff -u -r1.37 TestNoHost.java --- test/org/apache/commons/httpclient/TestNoHost.java 9 May 2004 12:16:12 -0000 1.37 +++ test/org/apache/commons/httpclient/TestNoHost.java 9 May 2004 21:51:49 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.37 2004/05/09 12:16:12 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v 1.37 2004/05/09 12:16:12 olegk Exp $ * $Revision: 1.37 $ * $Date: 2004/05/09 12:16:12 $ * ==================================================================== @@ -89,6 +89,7 @@ suite.addTest(TestBadContentLength.suite()); suite.addTest(TestEquals.suite()); suite.addTestSuite(TestIdleConnectionTimeout.class); + suite.addTest(TestMethodAbort.suite()); return suite; } Index: test/org/apache/commons/httpclient/server/SimpleHttpServer.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v retrieving revision 1.5 diff -u -r1.5 SimpleHttpServer.java --- test/org/apache/commons/httpclient/server/SimpleHttpServer.java 27 Feb 2004 19:01:34 -0000 1.5 +++ test/org/apache/commons/httpclient/server/SimpleHttpServer.java 9 May 2004 21:51:49 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v 1.5 2004/02/27 19:01:34 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v 1.5 2004/02/27 19:01:34 olegk Exp $ * $Revision: 1.5 $ * $Date: 2004/02/27 19:01:34 $ * @@ -35,7 +35,6 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; -import java.net.SocketException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -277,20 +276,15 @@ t.setDaemon(true); t.start(); } catch (IOException e) { - LOG.error("SimpleHttpServer error", e); - throw new RuntimeException(e.getMessage()); + LOG.error("I/O error: " + e.getMessage()); } Thread.sleep(100); } } catch (InterruptedException accept) { - } catch (SocketException e) { + } catch (IOException e) { if (!stopped) { - LOG.error("SimpleHttpServer error", e); - throw new RuntimeException(e.getMessage()); + LOG.error("I/O error: " + e.getMessage()); } - } catch (IOException e) { - LOG.error("SimpleHttpServer error", e); - throw new RuntimeException(e.getMessage()); } finally { destroy(); } Index: test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v retrieving revision 1.7 diff -u -r1.7 SimpleHttpServerConnection.java --- test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java 27 Feb 2004 19:01:34 -0000 1.7 +++ test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java 9 May 2004 21:51:50 -0000 @@ -1,5 +1,5 @@ /* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v 1.7 2004/02/27 19:01:34 olegk Exp $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v 1.7 2004/02/27 19:01:34 olegk Exp $ * $Revision: 1.7 $ * $Date: 2004/02/27 19:01:34 $ * @@ -36,7 +36,6 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.Socket; -import java.net.SocketException; import org.apache.commons.httpclient.HttpParser; import org.apache.commons.httpclient.HttpStatus; @@ -92,10 +91,8 @@ ++this.requestNo; readRequest(); } while (keepAlive); - } catch (SocketException ignore) { } catch (IOException e) { - LOG.error("ServerConnection read error", e); - throw new RuntimeException(e.getMessage()); + LOG.error("I/O error: " + e.getMessage()); } finally { destroy(); }