Author: remy.maucherat(a)jboss.com
Date: 2013-12-05 10:24:11 -0500 (Thu, 05 Dec 2013)
New Revision: 2320
Modified:
branches/7.4.x/src/main/java/org/apache/catalina/authenticator/FormAuthenticator.java
Log:
BZ 1030050: Sync some changes that offer more flexibility (in addition to using
createInputStream which allows using a reader for the restored request).
Modified:
branches/7.4.x/src/main/java/org/apache/catalina/authenticator/FormAuthenticator.java
===================================================================
---
branches/7.4.x/src/main/java/org/apache/catalina/authenticator/FormAuthenticator.java 2013-12-03
09:17:47 UTC (rev 2319)
+++
branches/7.4.x/src/main/java/org/apache/catalina/authenticator/FormAuthenticator.java 2013-12-05
15:24:11 UTC (rev 2320)
@@ -300,6 +300,7 @@
// the landing page
String uri = request.getContextPath() + landingPage;
SavedRequest saved = new SavedRequest();
+ saved.setMethod("GET");
saved.setRequestURI(uri);
saved.setDecodedRequestURI(uri);
request.getSessionInternal(true).setNote(
@@ -330,6 +331,7 @@
// the landing page
String uri = request.getContextPath() + landingPage;
SavedRequest saved = new SavedRequest();
+ saved.setMethod("GET");
saved.setRequestURI(uri);
saved.setDecodedRequestURI(uri);
session.setNote(Constants.FORM_REQUEST_NOTE, saved);
@@ -366,6 +368,10 @@
request.changeSessionId(session.getId());
}
}
+ // Always use GET for the login page, regardless of the method used
+ String oldMethod = request.getMethod();
+ request.getCoyoteRequest().method().setString("GET");
+
RequestDispatcher disp =
context.getServletContext().getRequestDispatcher(config.getLoginPage());
try {
@@ -376,6 +382,9 @@
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
msg);
+ } finally {
+ // Restore original method so that it is written into access log
+ request.getCoyoteRequest().method().setString(oldMethod);
}
}
@@ -459,6 +468,16 @@
if (saved == null)
return (false);
+ // Swallow any request body since we will be replacing it
+ // Need to do this before headers are restored as AJP connector uses
+ // content length header to determine how much data needs to be read for
+ // request body
+ byte[] buffer = new byte[4096];
+ InputStream is = request.createInputStream();
+ while (is.read(buffer) >= 0) {
+ // Ignore request body
+ }
+
// Modify our current request to reflect the original one
request.clearCookies();
Iterator cookies = saved.getCookies();
@@ -466,10 +485,11 @@
request.addCookie((Cookie) cookies.next());
}
+ String method = saved.getMethod();
MimeHeaders rmh = request.getCoyoteRequest().getMimeHeaders();
rmh.recycle();
- boolean cachable = "GET".equalsIgnoreCase(saved.getMethod()) ||
- "HEAD".equalsIgnoreCase(saved.getMethod());
+ boolean cachable = "GET".equalsIgnoreCase(method) ||
+ "HEAD".equalsIgnoreCase(method);
Iterator names = saved.getHeaderNames();
while (names.hasNext()) {
String name = (String) names.next();
@@ -495,45 +515,36 @@
request.getCoyoteRequest().getParameters().setQueryStringEncoding(request.getConnector().getURIEncoding());
- // Swallow any request body since we will be replacing it
- byte[] buffer = new byte[4096];
- InputStream is = request.getInputStream();
- while (is.read(buffer) >= 0) {
- // Ignore request body
- }
+ ByteChunk body = saved.getBody();
- if ("POST".equalsIgnoreCase(saved.getMethod())) {
- ByteChunk body = saved.getBody();
-
- if (body != null && body.getLength() > 0) {
- request.clearParameters();
- request.getCoyoteRequest().action
- (ActionCode.ACTION_REQ_SET_BODY_REPLAY, body);
-
- // Set content type
- MessageBytes contentType = MessageBytes.newInstance();
-
- //If no content type specified, use default for POST
- String savedContentType = saved.getContentType();
- if (savedContentType == null) {
- savedContentType = "application/x-www-form-urlencoded";
- }
+ if (body != null) {
+ request.clearParameters();
+ request.getCoyoteRequest().action
+ (ActionCode.ACTION_REQ_SET_BODY_REPLAY, body);
- contentType.setString(savedContentType);
- request.getCoyoteRequest().setContentType(contentType);
+ // Set content type
+ MessageBytes contentType = MessageBytes.newInstance();
- } else {
- // Restore the parameters.
- Iterator params = saved.getParameterNames();
- while (params.hasNext()) {
- String name = (String) params.next();
- request.addParameter(name,
- saved.getParameterValues(name));
- }
-
+ //If no content type specified, use default for POST
+ String savedContentType = saved.getContentType();
+ if (savedContentType == null &&
"POST".equalsIgnoreCase(method)) {
+ savedContentType = "application/x-www-form-urlencoded";
}
+
+ contentType.setString(savedContentType);
+ request.getCoyoteRequest().setContentType(contentType);
+
+ } else {
+ // Restore the parameters.
+ Iterator params = saved.getParameterNames();
+ while (params.hasNext()) {
+ String name = (String) params.next();
+ request.addParameter(name,
+ saved.getParameterValues(name));
+ }
+
}
- request.getCoyoteRequest().method().setString(saved.getMethod());
+ request.getCoyoteRequest().method().setString(method);
request.getCoyoteRequest().queryString().setString
(saved.getQueryString());
@@ -577,30 +588,29 @@
saved.addLocale(locale);
}
- if ("POST".equalsIgnoreCase(request.getMethod())) {
- // May need to acknowledge a 100-continue expectation
- request.getResponse().sendAcknowledgement();
- ByteChunk body = new ByteChunk();
- body.setLimit(request.getConnector().getMaxSavePostSize());
+ // May need to acknowledge a 100-continue expectation
+ request.getResponse().sendAcknowledgement();
+ ByteChunk body = new ByteChunk();
+ body.setLimit(request.getConnector().getMaxSavePostSize());
- byte[] buffer = new byte[4096];
- int bytesRead;
- InputStream is = request.getInputStream();
-
- while ( (bytesRead = is.read(buffer) ) >= 0) {
- body.append(buffer, 0, bytesRead);
- }
+ byte[] buffer = new byte[4096];
+ int bytesRead;
+ InputStream is = request.getInputStream();
+
+ while ( (bytesRead = is.read(buffer) ) >= 0) {
+ body.append(buffer, 0, bytesRead);
+ }
+ if (body.getLength() > 0) {
saved.setContentType(request.getContentType());
saved.setBody(body);
-
- if (body.getLength() == 0) {
- // It means that parameters have already been parsed.
- Enumeration e = request.getParameterNames();
- for ( ; e.hasMoreElements() ;) {
- String name = (String) e.nextElement();
- String[] val = request.getParameterValues(name);
- saved.addParameter(name, val);
- }
+ }
+ if (body.getLength() == 0) {
+ // It means that parameters have already been parsed.
+ Enumeration e = request.getParameterNames();
+ for ( ; e.hasMoreElements() ;) {
+ String name = (String) e.nextElement();
+ String[] val = request.getParameterValues(name);
+ saved.addParameter(name, val);
}
}