You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/11/02 13:10:28 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth HttpAuthenticator.java
olegk 2003/11/02 04:10:28
Modified: httpclient/src/java/org/apache/commons/httpclient
Header.java HeaderGroup.java HttpMethodBase.java
httpclient/src/java/org/apache/commons/httpclient/auth
HttpAuthenticator.java
Log:
PR #24081 (Manually set 'Cookie' & 'Authorization' headers get discarded)
Fixes the problem HttpClient discarding all the 'Cookie' & 'Authorization' headers including those
manually set when populating request header collection with automatically generated headers.
Contributed by Oleg Kalnichevski
Reviewed by Roland Weber & Michael Becke
Revision Changes Path
1.13 +45 -4 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Header.java
Index: Header.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Header.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- Header.java 1 Nov 2003 21:17:25 -0000 1.12
+++ Header.java 2 Nov 2003 12:10:28 -0000 1.13
@@ -68,6 +68,7 @@
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
+ * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
* @version $Revision$ $Date$
*/
public class Header extends NameValuePair {
@@ -75,6 +76,11 @@
// ----------------------------------------------------------- Constructors
/**
+ * Autogenerated header flag.
+ */
+ private boolean isAutogenerated = false;
+
+ /**
* Default constructor.
*/
public Header() {
@@ -91,6 +97,19 @@
super(name, value);
}
+ /**
+ * Constructor with name and value
+ *
+ * @param name the header name
+ * @param value the header value
+ * @param isAutogenerated <tt>true</tt> if the header is autogenerated,
+ * <tt>false</tt> otherwise.
+ */
+ public Header(String name, String value, boolean isAutogenerated) {
+ super(name, value);
+ this.isAutogenerated = isAutogenerated;
+ }
+
// --------------------------------------------------------- Public Methods
/**
@@ -138,6 +157,28 @@
*/
public HeaderElement[] getElements() {
return HeaderElement.parseElements(getValue());
+ }
+
+ /**
+ * Returns the value of the auto-generated header flag.
+ *
+ * @return <tt>true</tt> if the header is autogenerated,
+ * <tt>false</tt> otherwise.
+ */
+ public boolean isAutogenerated()
+ {
+ return isAutogenerated;
+ }
+
+ /**
+ * Sets the values of the auto-generated header flag.
+ *
+ * @return b <tt>true</tt> if the header is autogenerated,
+ * <tt>false</tt> otherwise.
+ */
+ public void setAutogenerated(boolean b)
+ {
+ isAutogenerated = b;
}
}
1.5 +11 -3 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java
Index: HeaderGroup.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HeaderGroup.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- HeaderGroup.java 12 Aug 2003 02:55:22 -0000 1.4
+++ HeaderGroup.java 2 Nov 2003 12:10:28 -0000 1.5
@@ -250,4 +250,12 @@
return false;
}
+ /**
+ * Returns an iterator over this group of headers.
+ *
+ * @return iterator over this group of headers.
+ */
+ public Iterator getIterator() {
+ return this.headers.iterator();
+ }
}
1.187 +25 -11 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.186
retrieving revision 1.187
diff -u -r1.186 -r1.187
--- HttpMethodBase.java 22 Oct 2003 19:31:00 -0000 1.186
+++ HttpMethodBase.java 2 Nov 2003 12:10:28 -0000 1.187
@@ -68,6 +68,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
+import java.util.Iterator;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.HttpAuthenticator;
@@ -1153,22 +1154,21 @@
LOG.trace("enter HttpMethodBase.addCookieRequestHeader(HttpState, "
+ "HttpConnection)");
- // Clean up the cookie headers
- removeRequestHeader("cookie");
-
CookieSpec matcher = CookiePolicy.getCookieSpec(this.params.getCookiePolicy());
Cookie[] cookies = matcher.match(conn.getHost(), conn.getPort(),
getPath(), conn.isSecure(), state.getCookies());
if ((cookies != null) && (cookies.length > 0)) {
if (getParams().isParameterTrue(HttpMethodParams.SINGLE_COOKIE_HEADER)) {
// In strict mode put all cookies on the same header
- getRequestHeaderGroup().addHeader(
- matcher.formatCookieHeader(cookies));
+ Header header = matcher.formatCookieHeader(cookies);
+ header.setAutogenerated(true);
+ getRequestHeaderGroup().addHeader(header);
} else {
// In non-strict mode put each cookie on a separate header
for (int i = 0; i < cookies.length; i++) {
- getRequestHeaderGroup().addHeader(
- matcher.formatCookieHeader(cookies[i]));
+ Header header = matcher.formatCookieHeader(cookies[i]);
+ header.setAutogenerated(true);
+ getRequestHeaderGroup().addHeader(header);
}
}
}
@@ -2021,6 +2021,7 @@
throws IOException, HttpException {
LOG.trace("enter HttpMethodBase.writeRequestHeaders(HttpState,"
+ "HttpConnection)");
+ clearAutogeneratedHeaders();
addRequestHeaders(state, conn);
Header[] headers = getRequestHeaders();
@@ -2313,5 +2314,18 @@
this.responseHeaders = responseheaders;
this.responseBody = null;
this.responseStream = responseStream;
+ }
+
+ /**
+ * Removes all auto-generated headers
+ */
+ protected void clearAutogeneratedHeaders() {
+ Iterator items = getRequestHeaderGroup().getIterator();
+ while (items.hasNext()) {
+ Header header = (Header)items.next();
+ if (header.isAutogenerated()) {
+ items.remove();
+ }
+ }
}
}
1.13 +7 -5 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java
Index: HttpAuthenticator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/HttpAuthenticator.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- HttpAuthenticator.java 8 Aug 2003 07:58:33 -0000 1.12
+++ HttpAuthenticator.java 2 Nov 2003 12:10:28 -0000 1.13
@@ -208,7 +208,8 @@
String auth = BasicScheme.authenticate((UsernamePasswordCredentials) credentials);
if (auth != null) {
String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP;
- method.setRequestHeader(s, auth);
+ Header header = new Header(s, auth, true);
+ method.addRequestHeader(header);
return true;
} else {
return false;
@@ -330,7 +331,8 @@
String auth = authscheme.authenticate(credentials, method.getName(), method.getPath());
if (auth != null) {
String s = proxy ? PROXY_AUTH_RESP : WWW_AUTH_RESP;
- method.setRequestHeader(s, auth);
+ Header header = new Header(s, auth, true);
+ method.addRequestHeader(header);
return true;
} else {
return false;
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org