Friday, 15 March 2013

apache - How can I forward HttpResponse to an open client socket? (Java Program acts as a proxy) -



apache - How can I forward HttpResponse to an open client socket? (Java Program acts as a proxy) -

i using httpclient lib apache. managed httpresponse sending request server. trying send response got clientsocket output stream.

so want send whatever received server open client connection. since using httpclient response in form of httpresponse object. tried following:

private void forwardrequest(string header, string url){ seek { closeablehttpclient httpclient = httpclients.createdefault(); httpget httpget = new httpget(url); closeablehttpresponse response; //adding request headers httpget string lines[] = header.split("\\n"); (string str : lines) { string parts[] = str.split(":", 2); httpget.addheader(parts[0], parts[1]); } httpresponse respone; response = httpclient.execute(httpget); //it works till here can read response , print out html page //but after don't know how send client outputstream bos = clientsocket.getoutputstream(); printwriter pw = new printwriter(bos); bufferedreader rd = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); while ((line = rd.readline()) != null) { pw.println(line); //bos.write(line.getbytes()); //this doesn't work } response.close(); }

also clientsocket global variable associcated serversocket like:

clientsocket = serversocket.accept();

i don't expect total solution. point me in right direction.. ton!

edit:

i tried next based on ejp suggested.. it's still not working. wondering if correctly implemented?

int portnumber = 8012; // port on programme listens serversocket serversocket = new serversocket(portnumber); //the socket @ programme listens socket clientsocket = serversocket.accept(); //clientsocket of programme socket toserver = new socket("localhost", 8089); //proxy server programme connects printwriter out = new printwriter(toserver.getoutputstream(), true); printwriter outclient = new printwriter(clientsocket.getoutputstream(), true); bufferedreader in = new bufferedreader( new inputstreamreader(clientsocket.getinputstream())); bufferedreader inserver = new bufferedreader( new inputstreamreader(toserver.getinputstream())); ) { string inputline; while ((inputline = in.readline()) != null) { out.println(inputline); //writing proxy server outclient.println(inserver.readline()); //writing original request sender system.out.println(inputline); }

the client made http request, expecting http response. if global clientsocket raw tcp socket , not httpclient, need add together http response protocol header yourself.

you have content server, you'll want first homecoming http response 200 ok, empty line carriage homecoming + linefeed (cr+lf), content-length: , document. if proxying text documents, convert string here, otherwise, pass mime type, charset, , entity through raw bytes web server responded, way can proxy document, including images or binary files.

it this:

http/1.1 200 ok content-type: text/html content-length: length <html> ... </html>

to pass http headers through server:

httpentity entity = response.getentity(); // technically should check http response rather assume 200 int statuscode = httpresp.getstatusline().getstatuscode(); if(statuscode != 200) ... // non 200 responses ? clientsocket.write("http/1.1 200 ok\r\n"); header[] responseheaders = response.getallheaders(); for(header header : responseheaders) { clientsocket.write(header.tostring() + "\r\n"); } clientsocket.write("\r\n"); // empty line required // utilize bufferedinputstream deal in bytes bufferedinputstream input = new bufferedinputstream(entity.getcontent()); byte[] buf = new byte[8192]; int bytesread; while ((bytesread = input.read(buf, 8192)) > 0) { clientsocket.write(buf, bytesread); }

i "something this", don't take literal, uncertainty compiles. don't have dev station in front end of me, general idea.

note: since using apache client lib, should able utilize specific http client instead of writing raw protocol. abstract http protocol away somewhat. i'll update reply later if nobody else provides improve one.

java apache sockets proxy httpresponse

No comments:

Post a Comment