Use mina to parse http and mina to parse http
In the process of using mina, we usually Customize various packets to use our own services. Today, we will bring you a decoding process similar to the http protocol.
Mina has its own decoding class for parsing http packets. You can use maven to configure the content to obtain the source code:
<dependency> <groupId>org.apache.mina</groupId> <artifactId>mina-http</artifactId> <version>3.0.0-M2</version></dependency>
Or download the mina source code package and view the org. apache. mina. http. HttpServerDecoder class. The following is a self-written parsing method:
Package com. server; import java. lang. reflect. method; import java. nio. charset. charset; import org. apache. commons. lang3.StringUtils; import org. apache. log4j. logger; import org. apache. mina. core. buffer. ioBuffer; import org. apache. mina. core. session. ioSession; import org. apache. mina. filter. codec. cumulativeProtocolDecoder; import org. apache. mina. filter. codec. protocolDecoderOutput; import org. apache. mina. filter. cod Ec. textline. lineDelimiter;/*** decodes the HTTP protocol class * @ author Herman. xiong * @ date 09:36:59 * @ version V3.0 * @ since Tomcat6.0, Jdk1.6 * @ copyright Copyright (c) 2015 */public class HttpServerDecoder extends CumulativeProtocolDecoder {private static final Logger log = Logger. getLogger (HttpServerDecoder. class); private LineDelimiter codecLineDelimiter = null; private Charset charset = null; private sta Tic final String MessageLength = "MessageLength"; public HttpServerDecoder (Charset charset, LineDelimiter codecLineDelimiter) {this. charset = charset; this. codecLineDelimiter = codecLineDelimiter;} public Charset getCharset () {return charset;} public void setCharset (Charset charset) {this. charset = charset;} public LineDelimiter getCodecLineDelimiter () {return codecLineDelimiter;} public void setCodecLi NeDelimiter (LineDelimiter codecLineDelimiter) {this. codecLineDelimiter = codecLineDelimiter;} public static void main (String [] args) {IoBuffer buf = IoBuffer. allocate (100 ). setAutoExpand (true); Charset charset = Charset. forName ("UTF-8"); CharsetEncoder ce = charset. newEncoder (); HttpServerDecoder socket = new HttpServerDecoder (Charset. forName ("UTF-8"), LineDelimiter. CRLF); try {System. out. println ("test data, test data ". GetBytes (charset ). length); buf. putString ("MessageMethod: UserAction", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("MessageType: GET", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("Content-Type: text/html; charset = iso-8859-1", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("Connection: keep-alive", ce); buf. putString (LineDelimiter. CRLF. getV Alue (), ce); buf. putString ("Keep-Alive: 200", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("CompressType: jzip", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("Params: id = 1 & uid = 2 & name = 3", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("accept-ranges: bytes", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("DateTime: DateTi Me ", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. putString ("MessageLength: 27", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); // bodybuf. putString ("test data, test data", ce); buf. putString (LineDelimiter. CRLF. getValue (), ce); buf. flip (); System. out. println ("output content:" + buf. getString (charset. newDecoder (); buf. flip (); socket. doDecode (null, buf, null);} catch (Exception e) {e. printStackTrace () ;}}/*** data Package decoding */protected boolean doDecode (IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {Map <String, Object> map = (Map <String, Object>) session. getAttribute ("pocket"); IoBuffer buf = (IoBuffer) session. getAttribute ("pocket_state"); if (null = buf) buf = IoBuffer. allocate (100 ). setAutoExpand (true ). setAutoShrink (true); if (null = map) map = new HashMap <String, Object> (); Integer len = 0; wh Ile (in. hasRemaining () {len = 0; byte B = in. get (); switch (B) {case '\ R': buf. put (B); break; case '\ N': buf. put (B); buf. flip (); String msg = buf. getString (charset. newDecoder (); String [] arr = msg. split (":"); if (StringUtils. isEmpty (map. get ("MessageLength") {if (2 = arr. length & StringUtils. isNotEmpty (arr [0]) & StringUtils. isNotEmpty (arr [1]) {map. put (arr [0], arr [1]);} else {log. error ("in the received packet Illegal content exists! ");} If (Constant. HUPU_ZERO.equals (map. get ("MessageLength") {out. write (map); if (in. position () = in. limit () {session. setAttribute ("pocket_state", null); session. setAttribute ("pocket", null); return true;} map = new HashMap <String, Object> () ;}} else {map. put (StringUtils. isEmpty (map. get ("MessageContent "))? Msg: map. get ("MessageContent") + msg); len = (null = map. get ("MessageContent "))? StringUtils. EMPTY: String. valueOf (map. get ("MessageContent "))). getBytes (charset ). length; if (len = Integer. parseInt (map. get ("MessageContent") + 2) {out. write (map); if (in. position () = in. limit () {session. setAttribute ("pocket_state", null); session. setAttribute ("pocket", null); return true ;}map = new HashMap <String, Object> () ;} buf. clear (); break; default: buf. put (B) ;}} if (StringUtils. isEmpty (map. get ("MessageLength") | len <Integer. parseInt (map. get ("MessageLength") {session. setAttribute ("pocket", map); session. setAttribute ("pocket_state", buf) ;}return false ;}}
Welcome to my blog! If you have any questions, please join the QQ group: 454796847 to learn together!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.