Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 315462

Summary: ContentExchange's setRequestContentSource does not work as expected
Product: [RT] Jetty Reporter: Sachin <snikumbh>
Component: clientAssignee: Jan Bartel <janb>
Status: CLOSED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: jetty-inbox, snikumbh
Version: unspecified   
Target Milestone: 7.0.2.RC0   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Sachin CLA 2010-06-02 15:06:53 EDT
Build Identifier: 

I have following code that creates an HTTP request:

HttpClient httpclient = new HttpClient();
httpclient.start();
ContentExchange exchange = new ContentExchange();                
exchange.setMethod("GET");
exchange.setURL("http://localhost:8080/MyServlet");
String trial = "Hello World";
ByteArrayInputStream byteStream = new ByteArrayInputStream(trial.getBytes());
exchange.setRequestContentSource( byteStream );                
byteStream.close();
httpclient.send(exchange);
exchange.waitForDone();

With this code, on the servlet side, I do not get anything in the request content.

Reproducible: Always

Steps to Reproduce:
1. Use the above client code
2. Use the following servlet code:

import javax.servlet.http.*;
import javax.servlet.ServletInputStream;
import java.io.*;

public class MyServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        
        try{                     
            byte[] input = new byte[req.getContentLength()];
            ServletInputStream sin = req.getInputStream();
            sin.readLine(input,0,input.length);
            sin.close();
            System.out.println("Request content : " + new String(input));
            resp.setContentLength(input.length);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.getOutputStream().write(input);
        } catch (IOException e) {
        }
    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);        
    }
}
Comment 1 Sachin CLA 2010-06-03 11:54:36 EDT
This is not a bug in Jetty. When the request content is set using an input stream, the content length does not get set (which seems obvious). I was not aware of this and the lack of content lenght on the servlet end was making me think that the Jetty client is not doing the right thing. I had to change the servlet code to take into account this and now everything works fine.