mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anshuk <Anshuk.PalChaudh...@cognizant.com>
Subject Re: Using Proxy utiliy to send/receive http messages
Date Thu, 06 Dec 2007 08:06:40 GMT

Any kind of help or solution regarding this problem would be appreciated.

Thanks.

/
Anshuk


Anshuk wrote:
> 
> 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#a14188014
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Mime
View raw message