remm 2004/07/15 07:36:46
Modified: catalina/src/share/org/apache/catalina/authenticator
DigestAuthenticator.java BasicAuthenticator.java
Log:
- Optimize BASIC authentication. It's not really critical, but it is used relatively
often and was inefficient.
Revision Changes Path
1.10 +2 -2
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/authenticator/DigestAuthenticator.java
Index: DigestAuthenticator.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/authenticator/DigestAuthenticator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- DigestAuthenticator.java 7 Jul 2004 16:39:46 -0000 1.9
+++ DigestAuthenticator.java 15 Jul 2004 14:36:46 -0000 1.10
@@ -212,7 +212,7 @@
*/
// Validate any credentials already included with this request
- String authorization = request.getAuthorization();
+ String authorization = request.getHeader("authorization");
if (authorization != null) {
principal = findPrincipal(request, authorization, context.getRealm());
if (principal != null) {
1.10 +75 -68
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/authenticator/BasicAuthenticator.java
Index: BasicAuthenticator.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/authenticator/BasicAuthenticator.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- BasicAuthenticator.java 7 Jul 2004 16:39:46 -0000 1.9
+++ BasicAuthenticator.java 15 Jul 2004 14:36:46 -0000 1.10
@@ -29,6 +29,9 @@
import org.apache.catalina.util.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.buf.CharChunk;
+import org.apache.tomcat.util.buf.MessageBytes;
@@ -47,7 +50,30 @@
- // ----------------------------------------------------- Instance Variables
+ /**
+ * Authenticate bytes.
+ */
+ public static final byte[] AUTHENTICATE_BYTES = {
+ (byte) 'W',
+ (byte) 'W',
+ (byte) 'W',
+ (byte) '-',
+ (byte) 'A',
+ (byte) 'u',
+ (byte) 't',
+ (byte) 'h',
+ (byte) 'e',
+ (byte) 'n',
+ (byte) 't',
+ (byte) 'i',
+ (byte) 'c',
+ (byte) 'a',
+ (byte) 't',
+ (byte) 'e'
+ };
+
+
+ // ----------------------------------------------------- Instance Variables
/**
@@ -119,9 +145,39 @@
}
// Validate any credentials already included with this request
- String authorization = request.getAuthorization();
- String username = parseUsername(authorization);
- String password = parsePassword(authorization);
+ String username = null;
+ String password = null;
+
+ MessageBytes authorization =
+ request.getCoyoteRequest().getMimeHeaders()
+ .getValue("authorization");
+
+ if (authorization != null) {
+ authorization.toBytes();
+ ByteChunk authorizationBC = authorization.getByteChunk();
+ if (authorizationBC.startsWithIgnoreCase("basic ", 0)) {
+ authorizationBC.setOffset(authorizationBC.getOffset() + 6);
+ // FIXME: Add trimming
+ // authorizationBC.trim();
+
+ CharChunk authorizationCC = authorization.getCharChunk();
+ Base64.decode(authorizationBC, authorizationCC);
+
+ // Get username and password
+ int colon = authorizationCC.indexOf(':');
+ if (colon < 0) {
+ username = authorizationCC.toString();
+ } else {
+ char[] buf = authorizationCC.getBuffer();
+ username = new String(buf, 0, colon);
+ password = new String(buf, colon + 1,
+ buf.length - colon - 1);
+ }
+
+ authorizationBC.setOffset(authorizationBC.getOffset() - 6);
+ }
+ }
+
principal = context.getRealm().authenticate(username, password);
if (principal != null) {
register(request, response, principal, Constants.BASIC_METHOD,
@@ -130,74 +186,25 @@
}
// Send an "unauthorized" response and an appropriate challenge
- String realmName = config.getRealmName();
- if (realmName == null)
- realmName = request.getServerName() + ":" + request.getServerPort();
- response.setHeader("WWW-Authenticate",
- "Basic realm=\"" + realmName + "\"");
+ MessageBytes authenticate =
+ response.getCoyoteResponse().getMimeHeaders()
+ .addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length);
+ CharChunk authenticateCC = authenticate.getCharChunk();
+ authenticateCC.append("Basic realm=\"");
+ if (config.getRealmName() == null) {
+ authenticateCC.append(request.getServerName());
+ authenticateCC.append(':');
+ authenticateCC.append(Integer.toString(request.getServerPort()));
+ } else {
+ authenticateCC.append(config.getRealmName());
+ }
+ authenticateCC.append('\"');
+ authenticate.toChars();
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//response.flushBuffer();
return (false);
}
-
-
- // ------------------------------------------------------ Protected Methods
-
-
- /**
- * Parse the username from the specified authorization credentials.
- * If none can be found, return <code>null</code>.
- *
- * @param authorization Authorization credentials from this request
- */
- protected String parseUsername(String authorization) {
-
- if (authorization == null)
- return (null);
- if (!authorization.toLowerCase().startsWith("basic "))
- return (null);
- authorization = authorization.substring(6).trim();
-
- // Decode and parse the authorization credentials
- String unencoded =
- new String(Base64.decode(authorization.getBytes()));
- int colon = unencoded.indexOf(':');
- if (colon < 0)
- return (null);
- String username = unencoded.substring(0, colon);
- // String password = unencoded.substring(colon + 1).trim();
- return (username);
-
- }
-
-
- /**
- * Parse the password from the specified authorization credentials.
- * If none can be found, return <code>null</code>.
- *
- * @param authorization Authorization credentials from this request
- */
- protected String parsePassword(String authorization) {
-
- if (authorization == null)
- return (null);
- if (!authorization.startsWith("Basic "))
- return (null);
- authorization = authorization.substring(6).trim();
-
- // Decode and parse the authorization credentials
- String unencoded =
- new String(Base64.decode(authorization.getBytes()));
- int colon = unencoded.indexOf(':');
- if (colon < 0)
- return (null);
- // String username = unencoded.substring(0, colon).trim();
- String password = unencoded.substring(colon + 1);
- return (password);
-
- }
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]