Hello;
I am trying to implement a web proxy which can reformat web pages using
HTTPComponents project.
I have modified ElementalHTTPServer (packaged as example) in a way that when
a request comes, Server requests the real
page from underlying web server and responses this to client.
However, in ElementalHTTPServer HttpRequestHandler handle method, whenever I
try to provide the response according to
comming page, the content was always smaller than accual one.
Some important parts of my code is as follows;
HttpResponse responseRedirect=redirect("localhost", target);
response.setStatusCode(HttpStatus.SC_OK);
EntityTemplate body = new EntityTemplate(new RedirectContentProducer(
responseRedirect.getEntity()));
body.setContentType("text/html; charset=UTF-8");
response.setEntity(body);
static class RedirectContentProducer implements ContentProducer{
private HttpEntity entity;
public RedirectContentProducer(HttpEntity entity) {
this.entity=entity;
}
public void writeTo(final OutputStream outstream) throws IOException
{
System.out.print("CONTENT LENGTH: "+entity.getContentLength()+"
||");
InputStream in=entity.getContent();
byte[] buffer = new byte[1024];
int totalread=0;
int readCount = 0;
while ( (readCount = in.read(buffer)) != -1) {
outstream.write(buffer, 0, readCount);
totalread+=readCount;
}
System.out.println("CONTENT WRITE "+totalread);
outstream.flush();
}
}
And I get response
Listening on port 8090
Incoming connection from /127.0.0.1
New connection thread
localhost:8090
TARGET=/tomcat-docs/jndi-datasource-examples-howto.html
Request URI: /tomcat-docs/jndi-datasource-examples-howto.html
<< Response: HTTP/1.1 200 OK
Connection kept alive...
CONTENT LENGTH: 51619 ||CONTENT WRITE 7984
Incoming connection from /127.0.0.1
New connection thread
localhost:8090
TARGET=/tomcat-docs/images/tomcat.gif
Request URI: /tomcat-docs/images/tomcat.gif
Incoming connection from /127.0.0.1
<< Response: HTTP/1.1 200 OK
Connection kept alive...
CONTENT LENGTH: 1934 ||CONTENT WRITE 1934
New connection thread
localhost:8090
TARGET=/tomcat-docs/images/asf-logo.gif
Incoming connection from /127.0.0.1
New connection thread
localhost:8090
TARGET=/tomcat-docs/images/printer.gif
Request URI: /tomcat-docs/images/asf-logo.gif
Request URI: /tomcat-docs/images/printer.gif
<< Response: HTTP/1.1 200 OK
Connection kept alive...
CONTENT LENGTH: 7279 ||CONTENT WRITE 7279
<< Response: HTTP/1.1 200 OK
Connection kept alive...
CONTENT LENGTH: 438 ||CONTENT WRITE 438
The situation is the same if I use
HttpResponse responseRedirect=redirect("localhost", target);
BasicHttpEntity body= new BasicHttpEntity();
body.setContent(responseRedirect.getEntity().getContent());
body.setContentType("text/html; charset=UTF-8");
response.setEntity(body);
As you can see, although accual the content length is 51519, I can only have
7984 bytes and send it back to client.
Most possibly, I made a mistake somewhere, but I could not find it out.
Any helps will welcome,
Thanks in advance.
Ozer Metin