mitre / HTTP-Proxy-Servlet

Smiley's HTTP Proxy implemented as a Java servlet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can I change the inputstream when proxy a Post request?

yuanlin-work opened this issue · comments

in the code i see
eProxyRequest.setEntity(
new InputStreamEntity(servletRequest.getInputStream(), getContentLength(servletRequest)));

so, Can I get getInputStream and parse it to a json object, and then put a new inputStream (who maked by the json object) back?

I don't think so. That said, assuming your code occurs before this servlet (e.g. a ServletFilter), then perhaps you can create a new ServletRequest based on the original, then pass this to the proxy servlet.

very glad to receive your reply!
the fact is I want to change it in particular cases, so I do it like this:

if(necessary)
	{
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(servletRequest.getInputStream()));
		String line;
		StringBuilder stringBuilder = new StringBuilder();
		while ((line = bufferedReader.readLine())!=null){
			stringBuilder.append(line);
		}
		JSONObject jsonObject = JSONObject.parseObject(stringBuilder.toString());
		/*do what you want*/
		jsonObject.put("newKey","new Value");
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(jsonObject.toJSONString().getBytes());
		/*don't forget to recount the length by .available()*/
		eProxyRequest.setEntity(
				new InputStreamEntity(byteArrayInputStream, byteArrayInputStream.available()));
	}else{
		...
	}

it worked when I post a Ajax request with content-type= 'Application/json'
Thank you !

As you say,ServletFilter can help,but I haven to deal the request headers to make a new request,witch HTTP-Proxy-Servlet have done all for me,so I just need to make the HTTP-Proxy-Servlet more stronger. so we can create a filter
interface to let the users to implement it,almost like this:

public interface RequestBodyEnhanceFilter {
    public boolean care(HttpServletRequest request);
    public InputStream getExchangeInput(HttpServletRequest request);
}

and the ProxyServlet.newProxyRequestWithEntity:

if(requestBodyEnhanceFilter.care(servletRequest)){
            InputStream exchangeInput = requestBodyEnhanceFilter.getExchangeInput(servletRequest);
            eProxyRequest.setEntity(new InputStreamEntity(exchangeInput,exchangeInput.available()));
}else{
	...
}

thand you