Bug 47410

Summary: HttpServletRequest.getInputStream is null with Content-Type: application/x-www-form-urlencoded
Product: Tomcat 6 Reporter: neng1998
Component: Servlet & JSP APIAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED INVALID    
Severity: critical Keywords: APIBug
Priority: P2    
Version: unspecified   
Target Milestone: default   
Hardware: All   
OS: All   

Description neng1998 2009-06-23 06:28:08 UTC
When client send xml message with Content-Type: application/x-www-form-urlencoded. if you do "request.getParameter("message")" first. then subsequent call request.getInportStream()return an empty inputStream (not null, just not data).

Note: 
1. If client set Content-Type: text/xml in request HTTP header, no problem.
2. If you do not do "request.getParameter("message")"; request.getInportStream() works. (can read the body data).
3. I understand the Content Type and client should not set "x-www-form-urlencoded" if they just send XML message in HTTP body. 

But, I still think it is bug.  request.getInportStream() should be able to read
the data in HTTP body regardless what Content Type is. 

Ed Xu


===============================================
POST /ecpr/a2a/MessageDispatcher HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: ecprsit.wellsfargo.com:9001
Content-Length: 738
Expect: 100-continue
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
<KODL009>
  <Header version="1.0">
    <RequestorId>seth.a.miller</RequestorId>
    <AuthorizationId>seth.a.miller</AuthorizationId>
    <ApplicationCode>5C</ApplicationCode>
    <ChannelCode>INET</ChannelCode>
    <DeviceId>DTC1D0090B7B0BD</DeviceId>
    <AccountingUnit>74501</AccountingUnit>
    <CorrelationId>bbf84e4a-e5ff-4fbc-be94-846b538a3dde</CorrelationId>
    <CreationTimestamp>633810128982475233</CreationTimestamp>
    <MessageType>RQST</MessageType>
    <Timeout>30</Timeout>
  </Header>
  <Body>
    <IndividualSearch>
      <IndividualFirstName>w</IndividualFirstName>
      <IndividualLastName>w</IndividualLastName>
    </IndividualSearch>
  </Body>

=====================================
public void doPost(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException {	
		String message = request.getParameter("message"); // Cause input stream empty
	if (message == null) {
			// In case the client posted the data raw...
		int BUFSIZE = 4096;
		InputStream in = null;
		ByteArrayOutputStream byteStream = null;
		try {
			in = request.getInputStream();
			byteStream = new ByteArrayOutputStream(BUFSIZE);
			byte[] buf = new byte[BUFSIZE];
			int len;
			long READ_TIME_LIMIT = 2000;		
			while ((len = in.read(buf, 0, BUFSIZE)) != -1) {
                         ..................
            // Problem:
            // InputStream has nothing althoung the HTTP request contains body)
              
            }
      }
Comment 1 Mark Thomas 2009-06-23 06:45:15 UTC
As per section SRV.3.1.1 of the Servlet spec.