costin 01/02/06 23:01:31
Modified: src/share/org/apache/tomcat/core ContextManager.java
Request.java
src/share/org/apache/tomcat/modules/server
Ajp12Interceptor.java Ajp13Interceptor.java
Http10Interceptor.java JNIConnectionHandler.java
src/share/org/apache/tomcat/startup StopTomcat.java
src/share/org/apache/tomcat/util/http Parameters.java
Log:
Fixed a number of probles with reading the request body.
- moved the check for "available" in Request, and make sure all the
adapters are checking if more data is to be expected.
( it used to be in the facade, but this is not the only place where the
check must be made - it's much better to test it at the lower level )
- few changes in how the POST data is pushed in Parameters.
Note: reading the request body will be changed a bit to deal with the
encoding problems, right now the parameters have hardcoded charset
( that was the original code ), and needs to be fixed.
Revision Changes Path
1.167 +3 -0
jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManager.java
Index: ContextManager.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/ContextManager.java,v
retrieving revision 1.166
retrieving revision 1.167
diff -u -r1.166 -r1.167
--- ContextManager.java 2001/02/06 06:41:07 1.166
+++ ContextManager.java 2001/02/07 07:01:23 1.167
@@ -871,7 +871,10 @@
}
Request lr = new Request();
+ Response res = new Response();
lr.setContextManager( this );
+ lr.setResponse( res );
+ res.setRequest( lr );
lr.requestURI().setString( urlPath );
lr.queryString().setString(queryString );
1.89 +69 -27 jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java
Index: Request.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Request.java,v
retrieving revision 1.88
retrieving revision 1.89
diff -u -r1.88 -r1.89
--- Request.java 2001/02/06 06:29:19 1.88
+++ Request.java 2001/02/07 07:01:23 1.89
@@ -137,9 +137,12 @@
// Processed information ( redundant ! )
protected Parameters params=new Parameters();
// protected Hashtable parametersH = new Hashtable();
- protected boolean didReadFormData;
+ protected boolean didReadFormData=false;
protected int contentLength = -1;
+ // how much body we still have to read.
+ protected int available = -1;
+
protected String contentType = null;
protected String charEncoding = null;
protected MessageBytes serverNameMB=new MessageBytes();
@@ -327,12 +330,26 @@
* are available.
*/
public void handlePostParameters() {
- int needData=params.needContent();
+ if( didReadFormData )
+ return;
+ didReadFormData=true;
+
+ if( ! methodMB.equalsIgnoreCase("POST") )
+ return;
+ String contentType= getContentType();
+ if (contentType == null &&
+ contentType.startsWith("application/x-www-form-urlencoded")) {
+ return;
+ }
- if( needData > 0 ) {
+ int len=getContentLength();
+ int available=getAvailable();
+
+ // read only available ( someone else may have read the content )
+ if( available > 0 ) {
try {
- byte[] formData = new byte[needData];
- readBody( formData, needData );
+ byte[] formData = new byte[available];
+ readBody( formData, available );
params.processData( formData );
} catch(IOException ex ) {
// XXX should we throw exception or log ?
@@ -345,8 +362,6 @@
return params;
}
-
-
// -------------------- encoding/type --------------------
public String getCharacterEncoding() {
@@ -361,6 +376,7 @@
public void setContentLength( int len ) {
this.contentLength=len;
+ available=len;
}
public int getContentLength() {
@@ -368,6 +384,7 @@
MessageBytes clB=headers.getValue("content-length");
contentLength = (clB==null || clB.isNull() ) ? -1 : clB.getInt();
+ available=contentLength;
return contentLength;
}
@@ -690,38 +707,43 @@
return headers.names();
}
- // -------------------- Utils - facade for RequestUtil
+ // -------------------- Computed fields --------------------
+
- /** Read request data, filling a byte[]
+ // -------------------- For adapters --------------------
+ // This should move to an IntputBuffer - the reading of the
+ // request body is really bad in tomcat, it needs some work
+ // and optimizations.
+
+ // We need to make sure nobody reads more than is available
+ // That may happen if both POST and input stream are used
+ // ( illegal, but can happen - and then we're hunged )
+ // ( also, getParameter doesn't throw any exception - if the
+ // user reads the body and then calls getParameter() the best
+ // action is to get him the query params - which are available.
+
+
+ public void setAvailable( int len ) {
+ this.available=len;
+ }
+
+ /** How many bytes from the body are still available
*/
- public int readBody(byte body[], int len)
- throws IOException
- {
- int offset = 0;
+ public int getAvailable() {
- do {
- int inputLen = doRead(body, offset, len - offset);
- if (inputLen <= 0) {
- return offset;
- }
- offset += inputLen;
- } while ((len - offset) > 0);
- return len;
+ return available;
}
-
- // -------------------- Computed fields --------------------
-
- // -------------------- For adapters --------------------
-
/** Fill in the buffer. This method is probably easier to implement than
previous.
- This method should only be called from SerlvetInputStream implementations.
+ This method should only be called from SerlvetInputStream
+ implementations.
No need to implement it if your adapter implements ServletInputStream.
*/
// you need to override this method if you want non-empty InputStream
public int doRead( byte b[], int off, int len ) throws IOException {
+ // System.out.println( "doRead " );
return -1; // not implemented - implement getInputStream
}
@@ -743,7 +765,27 @@
// ??
//return ((int)b[0]) & 0x000000FF;
}
+
+ /** Read request data, filling a byte[]
+ */
+ public int readBody(byte body[], int len)
+ throws IOException
+ {
+ int offset = 0;
+ // System.out.println( "ReadBody ");
+ do {
+ int inputLen = doRead(body, offset, len - offset);
+ if (inputLen <= 0) {
+ return offset;
+ }
+ offset += inputLen;
+ } while ((len - offset) > 0);
+ return len;
+ }
+
+
+
// -------------------- debug --------------------
public String toString() {
1.12 +8 -1
jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp12Interceptor.java
Index: Ajp12Interceptor.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp12Interceptor.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Ajp12Interceptor.java 2000/12/30 08:26:45 1.11
+++ Ajp12Interceptor.java 2001/02/07 07:01:25 1.12
@@ -228,11 +228,18 @@
}
public int doRead() throws IOException {
+ if( available <= 0 )
+ return -1;
+ available--;
return ajp12.doRead();
}
public int doRead( byte b[], int off, int len ) throws IOException {
- return ajp12.doRead( b,off,len);
+ if( available <= 0 )
+ return -1;
+ int rd=ajp12.doRead( b,off,len);
+ available -= rd;
+ return rd;
}
public boolean isTomcatAuthentication() {
1.7 +11 -4
jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp13Interceptor.java
Index: Ajp13Interceptor.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp13Interceptor.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Ajp13Interceptor.java 2001/02/05 16:04:00 1.6
+++ Ajp13Interceptor.java 2001/02/07 07:01:25 1.7
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp13Interceptor.java,v
1.6 2001/02/05 16:04:00 hgomez Exp $
- * $Revision: 1.6 $
- * $Date: 2001/02/05 16:04:00 $
+ * $Header:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Ajp13Interceptor.java,v
1.7 2001/02/07 07:01:25 costin Exp $
+ * $Revision: 1.7 $
+ * $Date: 2001/02/07 07:01:25 $
*
* ====================================================================
*
@@ -215,12 +215,19 @@
public int doRead() throws IOException
{
+ if( available <= 0 )
+ return -1;
+ available--;
return ajp13.doRead();
}
public int doRead(byte[] b, int off, int len) throws IOException
{
- return ajp13.doRead( b,off, len );
+ if( available <= 0 )
+ return -1;
+ int rd=ajp13.doRead( b,off, len );
+ available -= rd;
+ return rd;
}
public void recycle()
1.13 +8 -1
jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Http10Interceptor.java
Index: Http10Interceptor.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/Http10Interceptor.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- Http10Interceptor.java 2001/01/23 05:08:39 1.12
+++ Http10Interceptor.java 2001/02/07 07:01:27 1.13
@@ -197,11 +197,18 @@
}
public int doRead() throws IOException {
+ if( available <= 0 )
+ return -1;
+ available--;
return http.doRead();
}
public int doRead(byte[] b, int off, int len) throws IOException {
- return http.doRead( b, off, len );
+ if( available <= 0 )
+ return 0;
+ int rd=http.doRead( b, off, len );
+ available -= rd;
+ return rd;
}
1.8 +7 -4
jakarta-tomcat/src/share/org/apache/tomcat/modules/server/JNIConnectionHandler.java
Index: JNIConnectionHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/JNIConnectionHandler.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- JNIConnectionHandler.java 2000/12/30 08:26:46 1.7
+++ JNIConnectionHandler.java 2001/02/07 07:01:27 1.8
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/JNIConnectionHandler.java,v
1.7 2000/12/30 08:26:46 costin Exp $
- * $Revision: 1.7 $
- * $Date: 2000/12/30 08:26:46 $
+ * $Header:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/modules/server/JNIConnectionHandler.java,v
1.8 2001/02/07 07:01:27 costin Exp $
+ * $Revision: 1.8 $
+ * $Date: 2001/02/07 07:01:27 $
*
* ====================================================================
*
@@ -274,6 +274,8 @@
}
public int doRead(byte b[], int off, int len) throws IOException {
+ if( available <= 0 )
+ return 0;
int rc = 0;
while(0 == rc) {
@@ -282,7 +284,8 @@
Thread.currentThread().yield();
}
}
- return rc;
+ available -= rc;
+ return rc;
}
protected void readNextRequest(long s, long l) throws IOException {
1.3 +9 -13
jakarta-tomcat/src/share/org/apache/tomcat/startup/StopTomcat.java
Index: StopTomcat.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/startup/StopTomcat.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- StopTomcat.java 2000/12/29 19:59:19 1.2
+++ StopTomcat.java 2001/02/07 07:01:29 1.3
@@ -76,7 +76,6 @@
private static StringManager sm =
StringManager.getManager("org.apache.tomcat.resources");
- String configFile;
String tomcatHome;
public StopTomcat()
@@ -85,16 +84,6 @@
// -------------------- Parameters --------------------
- public void setConfig( String s ) {
- configFile=s;
- }
-
- /** -f option
- */
- public void setF( String s ) {
- configFile=s;
- }
-
public void setH( String s ) {
tomcatHome=s;
System.getProperties().put("tomcat.home", s);
@@ -223,7 +212,10 @@
public boolean processArgs(String[] args) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
-
+
+ if (arg.equals("-?")) {
+ return false;
+ }
if (arg.equals("-h") || arg.equals("-home")) {
i++;
if (i < args.length)
@@ -238,7 +230,11 @@
public static void main(String args[] ) {
try {
StopTomcat tomcat=new StopTomcat();
- tomcat.processArgs( args );
+ if( ! tomcat.processArgs( args ) ) {
+ // XXX use sm, i18n
+ System.out.println("Usage: java org.apache.tomcat.startup.StopTomcat [
-home TOMCAT_HOME ] ");
+ return;
+ }
tomcat.execute();
} catch(Exception ex ) {
System.out.println(sm.getString("tomcat.fatal"));
1.8 +0 -33
jakarta-tomcat/src/share/org/apache/tomcat/util/http/Parameters.java
Index: Parameters.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/http/Parameters.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Parameters.java 2001/02/06 06:24:26 1.7
+++ Parameters.java 2001/02/07 07:01:30 1.8
@@ -75,7 +75,6 @@
// this class - we can switch to MultiMap
private Hashtable paramHashStringArray=new Hashtable();
private boolean didQueryParameters=false;
- private boolean didReadFormData=false;
private boolean didMerge=false;
MessageBytes queryMB;
@@ -112,7 +111,6 @@
super.recycle();
paramHashStringArray.clear();
didQueryParameters=false;
- didReadFormData=false;
currentChild=null;
didMerge=false;
}
@@ -249,7 +247,6 @@
// XXX ENCODING !!
public void processData(byte data[]) {
- didReadFormData = true;
// make sure the request line query is processed
handleQueryParameters();
@@ -269,36 +266,6 @@
}
}
- /**
- * Process the headers and return if body data is needed. This
- * class doesn't deal with reading.
- *
- * Future enahancements: reuse/access the read buffer. Chunks.
- * Additional encodings.
- */
- public int needContent() {
- if( didReadFormData )
- return 0;
-
- MessageBytes contentTypeMB=headers.getValue("content-type");
- if( contentTypeMB == null || contentTypeMB.isNull() )
- return 0;
-
- String contentType= contentTypeMB.toString();
-
- if (contentType != null &&
- contentType.startsWith("application/x-www-form-urlencoded")) {
-
- MessageBytes clB=headers.getValue("content-length");
- int contentLength = (clB==null || clB.isNull() ) ? -1 :
- clB.getInt();
- if( contentLength >0 ) return contentLength;
- }
- return 0;
-
- }
-
-
// --------------------
/** Combine 2 hashtables into a new one.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]