dion 02/02/17 04:04:29 Modified: httpclient/src/java/org/apache/commons/httpclient HttpMethodBase.java Log: Patches provided by Sean C Sullivan, fixed if statement formatting Revision Changes Path 1.25 +122 -88 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java Index: HttpMethodBase.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- HttpMethodBase.java 15 Jan 2002 20:53:47 -0000 1.24 +++ HttpMethodBase.java 17 Feb 2002 12:04:29 -0000 1.25 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.24 2002/01/15 20:53:47 rwaldhoff Exp $ - * $Revision: 1.24 $ - * $Date: 2002/01/15 20:53:47 $ + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v 1.25 2002/02/17 12:04:29 dion Exp $ + * $Revision: 1.25 $ + * $Date: 2002/02/17 12:04:29 $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -109,7 +109,9 @@ * * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a> * @author Rodney Waldhoff - * @version $Revision: 1.24 $ $Date: 2002/01/15 20:53:47 $ + * @author Sean C. Sullivan + * @author <a href="mailto:[EMAIL PROTECTED]">dIon Gillard</a> + * @version $Revision: 1.25 $ $Date: 2002/02/17 12:04:29 $ */ public abstract class HttpMethodBase implements HttpMethod { @@ -191,7 +193,7 @@ // to the first, each separated by a comma." // - HTTP/1.0 (4.3) Header header = (Header)(requestHeaders.get(headerName.toLowerCase())); - if(null == header) { + if (null == header) { header = new Header(headerName, headerValue); } else { header.setValue( (null == header.getValue() ? "" : header.getValue()) + @@ -214,7 +216,7 @@ // to the first, each separated by a comma." // - HTTP/1.0 (4.3) Header orig = (Header)(requestHeaders.get(header.getName().toLowerCase())); - if(null == orig) { + if (null == orig) { orig = header; } else { orig.setValue( (null == orig.getValue() ? "" : orig.getValue()) + @@ -278,15 +280,15 @@ StringBuffer buf = new StringBuffer(); boolean needAmp = false; for(int i=0;i<params.length;i++) { - if(needAmp) { + if (needAmp) { buf.append("&"); } else { needAmp = true; } - if(null != params[i].getName()) { + if (null != params[i].getName()) { buf.append(URIUtil.encode(params[i].getName())); } - if(null != params[i].getValue()) { + if (null != params[i].getValue()) { buf.append("="); buf.append(URIUtil.encode(params[i].getValue())); } @@ -390,8 +392,8 @@ /** * Execute this method. * - * @param state {@link HttpState} information to associate with this request - * @param connection the {@link HttpConnection} to write to/read from + * @param state {@link HttpState} information to associate with this request. Must be non-null. + * @param connection the {@link HttpConnection} to write to/read from. Must be non-null. * * @throws IOException if an I/O error occurs * @throws HttpException if an protocol exception occurs @@ -399,13 +401,23 @@ * @return the integer status code if one was obtained, or <tt>-1</tt> */ public int execute(HttpState state, HttpConnection connection) throws HttpException, IOException { - log.debug("HttpMethodBase.execute(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.execute(HttpState,HttpConnection)"); + } + + if (null == state) { + throw new NullPointerException("HttpState parameter"); + } + + if (null == connection) { + throw new NullPointerException("HttpConnection parameter"); + } - if(hasBeenUsed()) { + if (hasBeenUsed()) { throw new HttpException("Already used, but not recycled."); } - if(!validate()) { + if (!validate()) { throw new HttpException("Not valid"); } @@ -415,10 +427,14 @@ for(;;) { visited.add(connection.getHost() + ":" + connection.getPort() + "|" + HttpMethodBase.generateRequestLine(connection, getName(),getPath(),getQueryString(),(http11 ? "HTTP/1.1" : "HTTP/1.0"))); - log.debug("HttpMethodBase.execute(): looping."); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.execute(): looping."); + } - if(!connection.isOpen()) { - log.debug("HttpMethodBase.execute(): opening connection."); + if (!connection.isOpen()) { + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.execute(): opening connection."); + } connection.open(); } @@ -429,8 +445,8 @@ readResponse(state,connection); - if(HttpStatus.SC_CONTINUE == statusCode) { - if(!bodySent) { + if (HttpStatus.SC_CONTINUE == statusCode) { + if (!bodySent) { bodySent = writeRequestBody(state,connection); readResponse(state,connection); } else { @@ -439,23 +455,27 @@ } } - if(!http11) { - log.debug("HttpMethodBase.execute(): closing connection since we're using HTTP/1.0"); + if (!http11) { + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.execute(): closing connection since we're using HTTP/1.0"); + } connection.close(); } else { Header connectionHeader = getResponseHeader("connection"); - if(null != connectionHeader && "close".equalsIgnoreCase(connectionHeader.getValue())) { - log.debug("HttpMethodBase.execute(): closing connection since \"Connection: close\" header found."); + if (null != connectionHeader && "close".equalsIgnoreCase(connectionHeader.getValue())) { + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.execute(): closing connection since \"Connection: close\" header found."); + } connection.close(); } } - if(HttpStatus.SC_UNAUTHORIZED == statusCode) { + if (HttpStatus.SC_UNAUTHORIZED == statusCode) { Header wwwauth = getResponseHeader("WWW-Authenticate"); - if(null != wwwauth) { + if (null != wwwauth) { String pathAndCreds = getPath() + ":" + wwwauth.getValue(); - if(realms.contains(pathAndCreds)) { - if(log.isInfoEnabled()) { + if (realms.contains(pathAndCreds)) { + if (log.isInfoEnabled()) { log.info("Already tried to authenticate to \"" + wwwauth.getValue() + "\" but still receiving " + HttpStatus.SC_UNAUTHORIZED + "."); } break; @@ -469,25 +489,25 @@ } catch(HttpException e) { // ignored } - if(!authenticated) { + if (!authenticated) { // won't be able to authenticate to this challenge // without additional information - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("HttpMethodBase.execute(): Server demands authentication credentials, but none are available, so aborting."); } break; } else { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("HttpMethodBase.execute(): Server demanded authentication credentials, will try again."); } // let's try it again, using the credentials continue; } } - } else if(HttpStatus.SC_MOVED_TEMPORARILY == statusCode || + } else if (HttpStatus.SC_MOVED_TEMPORARILY == statusCode || HttpStatus.SC_MOVED_PERMANENTLY == statusCode || HttpStatus.SC_TEMPORARY_REDIRECT == statusCode) { - if(getFollowRedirects()) { + if (getFollowRedirects()) { // // Note that we cannot current support // redirects that change the HttpConnection @@ -500,16 +520,16 @@ // agent to resubmit if desired. // Header location = getResponseHeader("location"); - if(location != null) { + if (location != null) { URL url = null; try { - if(location.getValue().startsWith("/")) { - if(log.isDebugEnabled()) { + if (location.getValue().startsWith("/")) { + if (log.isDebugEnabled()) { log.debug("Following relative Location header \"" + location + "\"."); } String protocol = connection.isSecure() ? "https" : "http"; int port = connection.getPort(); - if(-1 == port) { + if (-1 == port) { port = connection.isSecure() ? 443 : 80; } url = new URL(protocol,connection.getHost(),port,location.getValue()); @@ -520,48 +540,48 @@ log.error("Exception while parsing location header \"" + location + "\"",e); throw new HttpException(e.toString()); } - if("http".equalsIgnoreCase(url.getProtocol())) { - if(connection.isSecure()) { + if ("http".equalsIgnoreCase(url.getProtocol())) { + if (connection.isSecure()) { log.info("Server is attempting to redirect an HTTPS request to an HTTP one."); throw new HttpException("Server is attempting to redirect an HTTPS request to an HTTP one."); } - } else if("https".equalsIgnoreCase(url.getProtocol())) { - if(!connection.isSecure()) { + } else if ("https".equalsIgnoreCase(url.getProtocol())) { + if (!connection.isSecure()) { log.info("Server is attempting to convert an HTTP request to an HTTP one, which is currently not supported. Returning " + statusCode + "."); break; } } - if(!connection.getHost().equalsIgnoreCase(url.getHost())) { + if (!connection.getHost().equalsIgnoreCase(url.getHost())) { log.info("Server is attempting to redirect a different host, which is currently not supported. Returning " + statusCode + "."); break; } - if(url.getPort() == -1) { - if(connection.isSecure()) { - if(connection.getPort() != 443) { + if (url.getPort() == -1) { + if (connection.isSecure()) { + if (connection.getPort() != 443) { log.info("Server is attempting to redirect a different port, which is currently not supported. Returning " + statusCode + "."); break; } } else { - if(connection.getPort() != 80) { + if (connection.getPort() != 80) { log.info("Server is attempting to redirect a different port, which is currently not supported. Returning " + statusCode + "."); break; } } - } else if(connection.getPort() != url.getPort()) { + } else if (connection.getPort() != url.getPort()) { log.info("Server is attempting to redirect a different port, which is currently not supported. Returning " + statusCode + "."); break; } String absolutePath = url.getPath(); - if(null == absolutePath) { + if (null == absolutePath) { absolutePath = "/"; } String qs = url.getQuery(); // if we haven't already, let's try it again with the new path - if(visited.contains(connection.getHost() + ":" + connection.getPort() + "|" + HttpMethodBase.generateRequestLine(connection, getName(),absolutePath,qs,(http11 ? "HTTP/1.1" : "HTTP/1.0")))) { + if (visited.contains(connection.getHost() + ":" + connection.getPort() + "|" + HttpMethodBase.generateRequestLine(connection, getName(),absolutePath,qs,(http11 ? "HTTP/1.1" : "HTTP/1.0")))) { throw new HttpException("Redirect going into a loop, visited \"" + absolutePath + "\" already."); } else { - if(log.isDebugEnabled()) { + if (log.isDebugEnabled()) { log.debug("Changing path from \"" + getPath() + "\" to \"" + absolutePath + "\" in response to " + statusCode + " response."); log.debug("Changing query string from \"" + getQueryString() + "\" to \"" + qs + "\" in response to " + statusCode + " response."); } @@ -571,7 +591,7 @@ } } else { // got a redirect response, but no location header - if(log.isInfoEnabled()) { + if (log.isInfoEnabled()) { log.info("HttpMethodBase.execute(): Received " + statusCode + " response, but no \"Location\" header. Returning " + statusCode + "."); } break; @@ -621,7 +641,9 @@ * @param conn the {@link HttpConnection} to write the request to */ protected void writeRequest(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.writeRequest(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.writeRequest(HttpState,HttpConnection)"); + } writeRequestLine(state,conn); writeRequestHeaders(state,conn); conn.writeLine(); // close head @@ -641,7 +663,9 @@ * @param conn the {@link HttpConnection} to write to */ protected void writeRequestLine(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.writeRequestLine(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.writeRequestLine(HttpState,HttpConnection)"); + } String requestLine = HttpMethodBase.generateRequestLine(conn, getName(),getPath(),getQueryString(),(http11 ? "HTTP/1.1" : "HTTP/1.0")); conn.print(requestLine); } @@ -663,7 +687,9 @@ * @param conn the {@link HttpConnection} to write to */ protected void writeRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.writeRequestHeaders(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.writeRequestHeaders(HttpState,HttpConnection)"); + } addRequestHeaders(state,conn); Iterator it = requestHeaders.values().iterator(); while(it.hasNext()) { @@ -730,7 +756,7 @@ protected void addCookieRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException { if (!requestHeaders.containsKey("cookie")) { Header cookieHeader = Cookie.createCookieHeader(conn.getHost(), conn.getPort(), getPath(), conn.isSecure(), new Date(), state.getCookies()); - if(null != cookieHeader) { + if (null != cookieHeader) { setRequestHeader(cookieHeader); } } @@ -743,9 +769,9 @@ */ protected void addAuthorizationRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException { // add authorization header, if needed - if(!requestHeaders.containsKey("authorization")) { + if (!requestHeaders.containsKey("authorization")) { Header wwwAuthenticateHeader = (Header)(responseHeaders.get("www-authenticate")); - if(null != wwwAuthenticateHeader) { + if (null != wwwAuthenticateHeader) { try { Authenticator.authenticate(this,state); } catch(HttpException e) { @@ -764,10 +790,10 @@ protected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException { // add content length or chunking int len = getRequestContentLength(); - if(!requestHeaders.containsKey("content-length")) { - if(-1 < len) { + if (!requestHeaders.containsKey("content-length")) { + if (-1 < len) { setRequestHeader("Content-Length",String.valueOf(len)); - } else if(http11 && (len < 0)) { + } else if (http11 && (len < 0)) { setRequestHeader("Transfer-Encoding","chunked"); } } @@ -848,7 +874,9 @@ * @param conn the {@link HttpConnection} to read the response from */ protected void readResponse(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.readResponse(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.readResponse(HttpState,HttpConnection)"); + } readStatusLine(state,conn); processStatusLine(state,conn); readResponseHeaders(state,conn); @@ -872,7 +900,9 @@ * @param conn the {@link HttpConnection} to read the response from */ protected void readStatusLine(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.readStatusLine(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.readStatusLine(HttpState,HttpConnection)"); + } statusCode = -1; statusText = null; @@ -881,11 +911,11 @@ while(statusLine != null && !statusLine.startsWith("HTTP/")) { statusLine = conn.readLine(); } - if(statusLine == null) { + if (statusLine == null) { throw new HttpException("Error in parsing the status line from the response: unable to find line starting with \"HTTP/\""); } - if((!statusLine.startsWith("HTTP/1.1") && + if ((!statusLine.startsWith("HTTP/1.1") && !statusLine.startsWith("HTTP/1.0"))) { throw new HttpException("Unrecognized server protocol :" + statusLine); } @@ -893,12 +923,12 @@ http11 = statusLine.startsWith("HTTP/1.1"); int at = statusLine.indexOf(" "); - if(at < 0) { + if (at < 0) { throw new HttpException("Unable to parse the status line: " + statusLine); } int to = statusLine.indexOf(" ", at + 1); - if(to < 0) { + if (to < 0) { to = statusLine.length(); } @@ -909,7 +939,7 @@ } try { - if(to < statusLine.length()) { + if (to < statusLine.length()) { statusText = statusLine.substring(to + 1); } } catch (StringIndexOutOfBoundsException e) { @@ -955,12 +985,14 @@ // to the first, each separated by a comma." // - HTTP/1.0 (4.3) - log.debug("HttpMethodBase.readResponseHeaders(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.readResponseHeaders(HttpState,HttpConnection)"); + } responseHeaders.clear(); for(;;) { String line = conn.readLine(); - if((line == null) || (line.length() < 1)) { + if ((line == null) || (line.length() < 1)) { break; } @@ -973,11 +1005,11 @@ String match = name.toLowerCase(); String value = line.substring(colon + 1).trim(); Header header = (Header)(responseHeaders.get(match)); - if(null == header) { + if (null == header) { header = new Header(name, value); } else { String oldvalue = header.getValue(); - if(null != oldvalue) { + if (null != oldvalue) { header = new Header(name,oldvalue + ", " + value); } else { header = new Header(name,value); @@ -1013,11 +1045,11 @@ // add cookies, if any // should we set cookies? Header setCookieHeader = getResponseHeader("set-cookie2"); - if(null == setCookieHeader) { //ignore old-style if new is supported + if (null == setCookieHeader) { //ignore old-style if new is supported setCookieHeader = getResponseHeader("set-cookie"); } - if(setCookieHeader != null) { + if (setCookieHeader != null) { try { Cookie[] cookies = Cookie.parse(conn.getHost(), conn.getPort(), getPath(), conn.isSecure(), setCookieHeader); state.addCookies(cookies); @@ -1046,7 +1078,9 @@ * @param conn the {@link HttpConnection} to read the response from */ protected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException { - log.debug("HttpMethodBase.readResponseBody(HttpState,HttpConnection)"); + if (log.isDebugEnabled()) { + log.debug("HttpMethodBase.readResponseBody(HttpState,HttpConnection)"); + } responseBody = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); int expectedLength = 0; @@ -1054,14 +1088,14 @@ { Header lengthHeader = getResponseHeader("Content-Length"); Header transferEncodingHeader = getResponseHeader("Transfer-Encoding"); - if(null != lengthHeader) { + if (null != lengthHeader) { try { expectedLength = Integer.parseInt(lengthHeader.getValue()); } catch(NumberFormatException e) { // ignored } - } else if(null != transferEncodingHeader) { - if("chunked".equalsIgnoreCase(transferEncodingHeader.getValue())) { + } else if (null != transferEncodingHeader) { + if ("chunked".equalsIgnoreCase(transferEncodingHeader.getValue())) { expectedLength = -1; } } @@ -1071,21 +1105,21 @@ int nb = 0; while(expectedLength == -1 || foundLength < expectedLength) { nb = is.read(buffer); - if(nb == -1) { + if (nb == -1) { break; } - if(out == null) { + if (out == null) { throw new IOException("Unable to buffer data"); } - if(wireLog.isInfoEnabled()) { + if (wireLog.isInfoEnabled()) { wireLog.info("<< \"" + new String(buffer,0,nb) + "\""); } out.write(buffer, 0, nb); foundLength += nb; - if(expectedLength > -1) { - if(foundLength == expectedLength) { + if (expectedLength > -1) { + if (foundLength == expectedLength) { break; - } else if(foundLength > expectedLength) { + } else if (foundLength > expectedLength) { log.warn("HttpMethodBase.readResponseBody(): expected length (" + expectedLength + ") exceeded. Found " + foundLength + " bytes."); break; } @@ -1155,7 +1189,7 @@ * used by not recycled. */ protected void checkNotUsed() { - if(used) { + if (used) { throw new IllegalStateException("Already used."); } } @@ -1165,7 +1199,7 @@ * not used since last recycle. */ protected void checkUsed() { - if(!used) { + if (!used) { throw new IllegalStateException("Not Used."); } } @@ -1179,17 +1213,17 @@ protected static String generateRequestLine(HttpConnection connection, String name, String reqPath, String qString, String protocol) { StringBuffer buf = new StringBuffer(); buf.append(null == reqPath ? "/" : URIUtil.encode(reqPath,URIUtil.pathSafe())); - if(null != qString) { - if(qString.indexOf("?") < 0) { + if (null != qString) { + if (qString.indexOf("?") < 0) { buf.append("?"); } buf.append(qString); } - if(!connection.isProxied()) { + if (!connection.isProxied()) { return (name + " " + buf.toString() + " " + protocol + "\r\n"); } else { - if(connection.isSecure()) { + if (connection.isSecure()) { return (name + " https://" + connection.getHost() +
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>