1. A simple example of writing
PackageForWord;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.annotation.WebFilter;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.apache.commons.io.IOUtils;Importorg.apache.commons.lang.StringUtils; @WebFilter ("/*") Public classProxyfilterImplementsFilter { PublicProxyfilter () {} Public voiddestroy () {} Public voidDoFilter (ServletRequest request1, Servletresponse response1, Filterchain chain1)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) request1; HttpServletResponse Response=(HttpServletResponse) response1; Request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); String BasePath= Request.getscheme () + "://" + "127.0.0.1:9080"; String Locationurl= BasePath +Request.getrequesturi (); String QueryValue=request.getquerystring (); if(Stringutils.isnotblank (QueryValue)) {Locationurl+= "?" +QueryValue; } ioutils.write (Httputils.sendget (Locationurl, request), Response.getoutputstream ()); } Public voidInit (Filterconfig fconfig)throwsservletexception {} @SuppressWarnings ("Unused") PrivateString Solveurl (httpservletrequest request) {returnRequest.getrequesturi (). ReplaceAll (Request.getcontextpath (), ""); } Public Static voidMain (string[] args) {}}
Ideas are as follows:
1. First of all, so the request must go through the proxy server, there is a proxy server according to the corresponding rules (custom), select the appropriate target server for forwarding,
2. The implementation of the forwarding, we can create their own httpurlconnection connect to the target server to obtain data, can also use the mature httpclient to request data
3. Respond back to the data.
The difficulty: the place of misery is mainly in the parsing of the HTTP protocol, where I use httpclient to block these underlying implementations, but we ourselves should know how to parse the HTTP request
Examples of parsing requests:
Public classParseutils { Public Static byte[] ReadLine (InputStream in)throwsIOException {byteappender Appender=NewByteappender (); int_data =-1; while(true) { intdata =In.read (); if(data = =-1) Break; if(_data = = ' \ r ')) { if(data = = ' \ n ') Break; Else{appender.append (byte) _data); Appender.append ((byte) data); } } if(Data! = ' \ r ')) {appender.append (byte) data); } _data=data; } returnAppender.getdatas (); }}
//Parse the message passed over
Private voidPARSE_ (Socket socket)throwsIOException {InputStream in=Socket.getinputstream (); byte[] Firstline =Parseutils.readline (in); if(Firstline.length = = 0) { Throw NewHttpException ("Heart-beaten Test from client Side"); } MessageHeader=NewString (Firstline); Parseheaders (in); Bodyinput=In ; InetAddress Address=socket.getinetaddress (); Setclienthost (Address.gethostaddress ()); }
Java implementation reverse Proxy server