mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anshuk <Anshuk.PalChaudh...@cognizant.com>
Subject Using Proxy utiliy to send/receive http messages
Date Wed, 28 Nov 2007 06:52:05 GMT

Hi,

I was trying to make a small utility like a proxy server which would be used
to get hold of the httprequest message sent by the http client, decode the
request, and then pass the same httprequest to the actual server. In the
same context, I was trying to use the example provide by mina for httpserver
and proxy, reusing the codes of HttpRequestDecoder, HttpRequestMessage,
HttpResponseEncoder,
HttpResponseMessage,ClientToProxyIoHandler,ServerToProxyIoHandler and
AbstractProxyIoHandler.

Now the proxy gets started at a particular port, say 1234 and also binds to
the actual destination server, the code for which is:

-----------------------------------------------------------------------------------------------------------------------------------------

	 IoAcceptor acceptor = new SocketAcceptor();
	 int DEFAULT_PORT = 1234;
	 int port = DEFAULT_PORT;
	 int svrport = 8080;
	 String svrhost = "localhost";
	 IoAcceptor acceptor = new SocketAcceptor();
 	 SocketAcceptorConfig cfg = new SocketAcceptorConfig();
	 cfg.setReuseAddress(true);
	 cfg.getFilterChain().addLast("protocolFilter", new ProtocolCodecFilter(new             
                                                                         
         HttpServerProtocolCodecFactory()));
         IoConnector connector = new SocketConnector();
        ((IoConnectorConfig)
connector.getDefaultConfig()).setConnectTimeout(30);
        ClientToProxyIoHandler handler = new ClientToProxyIoHandler(
                new ServerToProxyIoHandler(), connector, new
InetSocketAddress(
                		svrhost, svrport));
        acceptor.bind(new InetSocketAddress(port), handler, cfg);
-----------------------------------------------------------------------------------------------------------------------------------------

The issue that I am facing is when the client send the httprequest to the
proxy, the proxy decodes the message and then the messagereceived function
of the ClientToProxyIoHandler is fired, the code for which is:

-----------------------------------------------------------------------------------------------------------------------------------------
CharsetEncoder encoder = Charset.defaultCharset().newEncoder();
HttpRequestMessage requestMessage = (HttpRequestMessage) message;
ByteBuffer buf = ByteBuffer.allocate(64);
buf.setAutoExpand(true);
        buf.put(CRLF);
        for (Iterator it =
requestMessage.getHeaders().entrySet().iterator(); it
        .hasNext();) 
        {
        		Entry entry = (Entry) it.next();
        		buf.putString((String)entry.getKey(),encoder);
        		buf.putString(": ", encoder);
        		//buf.putObject(entry.getValue());
        		String[] entryValueArry = (String[]) entry.getValue();
        		for(int i = 0; i < entryValueArry.length;i++)
        		{
        			buf.putString(entryValueArry[i],encoder);
        		}
        		buf.put(CRLF);
    
        }
        buf.put(CRLF);
buf.putString(requestMessage.getRequestBody(),encoder);
buf.flip();
ByteBuffer wb = ByteBuffer.allocate(buf.remaining());
buf.mark();
wb.put(buf);
wb.flip();
((IoSession) session.getAttachment()).write(wb);
buf.reset();
SessionLog.info(session,
buf.getString(Charset.defaultCharset().newDecoder()));
-----------------------------------------------------------------------------------------------------------------------------------------

After the buffer is written into the session, it is getting stuck somehow,
and the message is not being sent to the actual server,I am not really sure,
where it is getting stuck and why.

Can anyone let me know what can be the problem here, please let if anybody
requires the complete source code so that I can send it across.
        
Thanks.

Regards,
Anshuk
-- 
View this message in context: http://www.nabble.com/Using-Proxy-utiliy-to-send-receive-http-messages-tf4886685s16868.html#a13986908
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Mime
View raw message