Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 5647 invoked from network); 4 Sep 2002 17:22:54 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 4 Sep 2002 17:22:54 -0000 Received: (qmail 24104 invoked by uid 97); 4 Sep 2002 17:23:26 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 24058 invoked by uid 97); 4 Sep 2002 17:23:25 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 24041 invoked by uid 98); 4 Sep 2002 17:23:25 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Message-ID: <3549C09B853DD5119B540002A52CDD3404B009CA@zcard0ka.ca.nortel.com> From: "Jeff Dever" To: Jakarta Commons Developers List Subject: RE: [httpclient][patch] StatusLine encapsulation Date: Wed, 4 Sep 2002 13:22:42 -0400 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C25437.AD0C85EE" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ------_=_NextPart_001_01C25437.AD0C85EE Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Ah! That explains why it didn't highlight correctly in gvim ... -----Original Message----- From: gunnar.ahlberg@ibs.se [mailto:gunnar.ahlberg@ibs.se] Sent: Wednesday, September 04, 2002 12:14 PM To: Jakarta Commons Developers List Subject: Re: [httpclient][patch] StatusLine encapsulation Found a minor type, @deprecated, not depricated ... 2 occurances..=20 =20 @@ -446,9 +444,10 @@ * Provide access to the status code. *=20 * @return the status code associated with the latest response. + * @depricated use getStatusLine().getStatusCode() */ public int getStatusCode() { - return statusCode; + return statusLine.getStatusCode(); } Other than that, I'm full on!=20 /G=20 Jeff Dever=20 Sent by: jsdever=20 2002-09-04 02:46=20 Please respond to "Jakarta Commons Developers List"=20 =20 To: Jakarta Commons Developers List =20 cc: =20 Subject: [httpclient][patch] StatusLine encapsulation This is my response for the bug: http://issues.apache.org/bugzilla/show_bug.cgi?id=3D12245 I added a new StatusLine class and added it as an instance variable to HttpMethodBase. Removed individual statusCode and statusText members. Removed the parsing of the status line into the StatusLine constructor. HttpMethodBase.readStatusLine() still checks for the http version and sets the (dreaded) isHttp11 boolean. There was an interface change to the HttpMethod interface to getStatusLine(). I like this solution as it get some of the code out of HttpMethodBase and recognizes the uniqueness of the Status-Line. What do ya'll think? PS: This is based on a suggestion by Vincent Massol. ? StatusLine.java ? statusline.diff Index: HttpMethod.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/h= ttpc lient/HttpMethod.java,v retrieving revision 1.16 diff -u -u -r1.16 HttpMethod.java --- HttpMethod.java 3 Sep 2002 01:36:26 -0000 1.16 +++ HttpMethod.java 4 Sep 2002 00:35:33 -0000 @@ -302,4 +302,10 @@ * Use this method internally to add footers. */ public void addResponseFooter(Header footer); + + /**=20 + * Return the Status-Line from the response. + * @since 2.0 + */ + public String getStatusLine(); } Index: HttpMethodBase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/h= ttpc lient/HttpMethodBase.java,v retrieving revision 1.55 diff -u -u -r1.55 HttpMethodBase.java --- HttpMethodBase.java 3 Sep 2002 11:19:35 -0000 1.55 +++ HttpMethodBase.java 4 Sep 2002 00:35:39 -0000 @@ -201,8 +201,8 @@ /** My query string, if any. */ private String queryString =3D null; - /** My response status text, if any. */ - private String statusText =3D null; + /** The Status-Line from the response. */ + private StatusLine statusLine =3D null; /** The response body, assuming it has not be intercepted by a sub-class. */ private InputStream responseStream =3D null; @@ -231,8 +231,6 @@ */ private int maxRetries =3D 3; - /** My response status code, if any. */ - private int statusCode =3D -1; //~ Constructors =B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7= =B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7= =B7=B7=B7=B7=B7=B7=B7=B7=B7=B7=B7 @@ -446,9 +444,10 @@ * Provide access to the status code. *=20 * @return the status code associated with the latest response. + * @depricated use getStatusLine().getStatusCode() */ public int getStatusCode() { - return statusCode; + return statusLine.getStatusCode(); } /** @@ -585,9 +584,10 @@ *=20 * @return the status text (or "reason phrase") associated with the latest * response. + * @depricated use getStatusLine.getReasonPhrase() */ public String getStatusText() { - return statusText; + return statusLine.getReasonPhrase(); } /** @@ -659,7 +659,7 @@ private boolean shouldCloseConnection() { if (!http11) {=20 if (getName().equals(ConnectMethod.NAME) &&=20 - (statusCode =3D=3D HttpStatus.SC_OK)) { + (statusLine.getStatusCode() =3D=3D = HttpStatus.SC_OK)) { log.debug("Will leave connection open for tunneling"); return false; } else { @@ -741,6 +741,7 @@ //if SC_CONTINUE write the request body writeRemainingRequestBody(state, conn); + int statusCode =3D statusLine.getStatusCode(); switch (statusCode) { case HttpStatus.SC_UNAUTHORIZED: case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED: @@ -886,8 +887,7 @@ queryString =3D null; requestHeaders.clear(); responseHeaders.clear(); - statusCode =3D -1; - statusText =3D null; + statusLine =3D null; used =3D false; http11 =3D true; bodySent =3D false; @@ -1425,7 +1425,8 @@ * @param conn the {@link HttpConnection} to read the response from * @return InputStream to read the response body from */ - private InputStream _readResponseBody(HttpState state, = HttpConnection conn) throws IOException { + private InputStream _readResponseBody(HttpState state, = HttpConnection conn)=20 + throws IOException { log.trace("enter HttpMethodBase.readResponseBody(HttpState, HttpConnection)"); responseBody =3D null; // is this desired? @@ -1447,7 +1448,8 @@ if ("chunked".equalsIgnoreCase(transferEncodingHeader.getValue())) { result =3D new ChunkedInputStream(is, this); } - } else if(canResponseHaveBody(statusCode) && !getName().equals(ConnectMethod.NAME)){ + } else if(canResponseHaveBody(statusLine.getStatusCode())=20 + && !getName().equals(ConnectMethod.NAME)){ result =3D is; } if (result =3D=3D null) return null; @@ -1558,63 +1560,40 @@ * @throws HttpRecoverableException when the status line is null = and the * request should be retried *=20 - * @see #readResponse - * @see #processStatusLine + * @see StatusLine */ protected void readStatusLine(HttpState state, HttpConnection conn) throws IOException, HttpRecoverableException, HttpException { log.trace( "enter HttpMethodBase.readStatusLine(HttpState, HttpConnection)"); - statusCode =3D -1; - statusText =3D null; - - String statusLine =3D conn.readLine(); - - while ((statusLine !=3D null) && = !statusLine.startsWith("HTTP/")) { - statusLine =3D conn.readLine(); + //read out the HTTP status string + String statusString =3D conn.readLine(); + while ((statusString !=3D null) && = !statusString.startsWith("HTTP/")) { + statusString =3D conn.readLine(); } - if (statusLine =3D=3D null) { - // A null statusLine means the connection was lost before = we got a + if (statusString =3D=3D null) { + // A null statusString means the connection was lost = before we got a // response. Try again. throw new HttpRecoverableException("Error in parsing the = status " + " line from the response: unable to find line = starting with" + " \"HTTP/\""); } - if ((!statusLine.startsWith("HTTP/1.1") - && !statusLine.startsWith("HTTP/1.0"))) { - throw new HttpException("Unrecognized server protocol :" - + statusLine); - } - - http11 =3D statusLine.startsWith("HTTP/1.1"); - - int at =3D statusLine.indexOf(" "); - if (at < 0) { - throw new HttpException("Unable to parse the status line: = " - + statusLine); - } - - int to =3D statusLine.indexOf(" ", at + 1); - if (to < 0) { - to =3D statusLine.length(); - } + //create the status line from the status string + statusLine =3D new StatusLine(statusString); - try { - statusCode =3D Integer.parseInt(statusLine.substring(at + = 1, to)); - } catch (NumberFormatException e) { - throw new HttpException( - "Unable to parse status code from status line: " + statusLine); + //check for a valid HTTP-Version + String httpVersion =3D statusLine.getHttpVersion(); + if (httpVersion.equals("HTTP/1.0")){ + http11 =3D false; + } else if (httpVersion.equals("HTTP/1.1")){ + http11 =3D true;=20 + } else { + throw new HttpException("Unrecognized server protocol: '" + + httpVersion + "'"); } - try { - if (to < statusLine.length()) { - statusText =3D statusLine.substring(to + 1); - } - } catch (StringIndexOutOfBoundsException e) { - throw new HttpException("Status text not specified: " + statusLine); - } } // ------------------------------------------------------ Protected Methods @@ -1892,6 +1871,7 @@ log.trace("enter HttpMethodBase.processAuthenticationResponse(" + "HttpState, HttpConnection)"); + int statusCode =3D statusLine.getStatusCode(); // handle authentication required Header wwwauth =3D null; Set realmsUsed =3D null; @@ -2046,7 +2026,7 @@ throws HttpException, IOException { log.trace("enter writeRemainingRequestBody(HttpState, HttpConnection)"); - if (HttpStatus.SC_CONTINUE =3D=3D statusCode) { + if (HttpStatus.SC_CONTINUE =3D=3D statusLine.getStatusCode()) = { if (!bodySent) { bodySent =3D writeRequestBody(state, connection); } else { Index: util/HttpURLConnection.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/h= ttpc lient/util/HttpURLConnection.java,v retrieving revision 1.4 diff -u -u -r1.4 HttpURLConnection.java --- util/HttpURLConnection.java 2 Sep 2002 15:37:29 = -0000 1.4 +++ util/HttpURLConnection.java 4 Sep 2002 00:35:41 = -0000 @@ -63,7 +63,6 @@ package org.apache.commons.httpclient.util; import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.Header; import org.apache.commons.logging.LogFactory; @@ -277,13 +276,7 @@ // return the status line. Hence the special case below ... =20 if (thePosition =3D=3D 0) { - if (((HttpMethodBase) this.method).isHttp11()) { - return "HTTP/1.1 " + this.method.getStatusCode()=20 - + " " + this.method.getStatusText(); - } else { - return "HTTP/1.0 " + this.method.getStatusCode()=20 - + " " + this.method.getStatusText(); - } + return this.method.getStatusLine(); } // Note: I hope the header fields are kept in the correct order = when /* * $Header: $ * $Revision: $ * $Date: $ * = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software = itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "HttpClient", and "Apache = Software * Foundation" must not be used to endorse or promote products = derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,=20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * * 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 org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; /** * Represents a Status-Line as returned from a HTTP server. * * RFC2616 states * the following regarding the Status-Line: *
* 6.1 Status-Line
*
*  The first line of a Response message is the Status-Line, consisting
*  of the protocol version followed by a numeric status code and its
*  associated textual phrase, with each element separated by SP
*  characters. No CR or LF is allowed except in the final CRLF =
sequence.
*
*      Status-Line =3D HTTP-Version SP Status-Code SP Reason-Phrase =
CRLF
* 
*

* This class is immutable and is inherently thread safe. * * @author Jeff Dever * @version $Id: $ * @since 2.0 */ public class StatusLine { // ----------------------------------------------------- Instance Variables /** The Status-Line. */ private final String statusLine; /** The HTTP-Version. */ private final String httpVersion; /** The Status-Code. */ private final int statusCode; /** The Reason-Phrase. */ private final String reasonPhrase; // ----------------------------------------------------------- Constructors /** * Default constructor. * * @param statusLine the status line returned from the HTTP server * @throws HttpException if the status line is invalid */ public StatusLine(String statusLine)=20 throws HttpException{ super(); //save the original status line this.statusLine =3D statusLine; //handle the HTTP-Version int at =3D statusLine.indexOf(" "); if (at <=3D 0) { throw new HttpException("Unable to parse the status line: '" + statusLine + "'"); } this.httpVersion =3D (statusLine.substring(0, = at)).toUpperCase(); //handle the Status-Code int to =3D statusLine.indexOf(" ", at + 1); if (to < 0) { to =3D statusLine.length(); } try { this.statusCode =3D = Integer.parseInt(statusLine.substring(at+1, to)); } catch (NumberFormatException e) { throw new HttpException( "Unable to parse status code from status line: '"=20 + statusLine + "'"); } //handle the Reason-Phrase try { if (to < statusLine.length()) { this.reasonPhrase =3D statusLine.substring(to + 1); } else { this.reasonPhrase =3D ""; } } catch (StringIndexOutOfBoundsException e) { throw new HttpException("Status text not specified: '"=20 + statusLine + "'");=20 } } // --------------------------------------------------------- Public Methods /** * @return the Status-Code */ public final int getStatusCode() { return statusCode; } /** * @return the HTTP-Version */ public final String getHttpVersion() { return httpVersion; } /** * @return the Reason-Phrase */ public final String getReasonPhrase() { return reasonPhrase; } public final String toString() { return statusLine; } } -- To unsubscribe, e-mail: For additional commands, e-mail: =20 ------_=_NextPart_001_01C25437.AD0C85EE--