阅读本源码,请先掌握Http协议本身
1 Http解析器
1 package waf.net.http.httpparser;
2
3
4 /**
5 *
6 * @author waf.wang
7 *
8 */
9 public class HttpParser
10 {
11 public static HttpResponse parse(byte[] headAndBody)
12 {
13 String endLine="\r\n";
14
15
16 HttpHeader header=HeaderParse.parse(headAndBody);
17 HttpBody body=BodyParser.parse(header, headAndBody);
18
19 HttpResponse resp=new HttpResponse();
20 resp.setBody(body);
21 resp.setHeader(header);
22 resp.setContent(headAndBody);
23
24 return resp;
25
26 }
27
28 private static String filterCharset(String charset)
29 {
30 String ret="";
31
32 if(charset.toLowerCase().equalsIgnoreCase("utf-8"))
33 {
34 ret="utf-8";
35 }
36 else if(charset.toLowerCase().indexOf("gb")>=0)
37 {
38 ret="gbk";
39 }
40 else
41 {
42 ret="utf-8";
43 }
44
45 return ret;
46 }
47
48 }
2 Http协议体解析器
1 package waf.net.http.httpparser;
2
3 import waf.convert.Conv;
4 import waf.lang.StringUtil;
5 import waf.util.zip.GZip;
6
7 /**
8 *
9 * @author waf.wang
10 *
11 */
12 public class BodyParser
13 {
14
15 public static HttpBody parse(HttpHeader header,byte[] headAndBody)
16 {
17 String bodyHex=StringUtil.subStringToEnd(Conv.bytes2Hex(headAndBody),Conv.bytes2Hex("\r\n\r\n".getBytes()));
18 byte[] bodyBytes=Conv.hex2Bytes(bodyHex);
19 String bodyText="";
20
21
22 String charset="";
23 if(header.getContentTypeCharset().equalsIgnoreCase("utf-8"))
24 {
25 charset="utf-8";
26 }
27 else if(header.getContentTypeCharset().indexOf("GB")>=0)
28 {
29 charset="gbk";
30 }
31 else
32 {
33 charset="utf-8";
34 }
35 if(header.isGzip())
36 {
37 bodyText=GZip.uncompress(bodyBytes, charset);
38 }
39 else
40 {
41 bodyText=Conv.bytes2String(bodyBytes, charset);
42 }
43
44 HttpBody body=new HttpBody();
45 body.setText(bodyText);
46 body.setBytes(Conv.str2bytes(bodyText, charset));
47
48 body.setCharset(header.getContentTypeCharset());
49
50 return body;
51 }
52 }
3 Http协议头解析器
1 package waf.net.http.httpparser;
2
3 import waf.convert.Conv;
4 import waf.lang.StringUtil;
5
6 /**
7 *
8 * @author waf.wang
9 *
10 */
11
12 public class HeaderParse
13 {
14 public static HttpHeader parse(byte[] bytes)
15 {
16 String resp=Conv.bytes2String(bytes, "utf-8");
17 String headerText=StringUtil.subStringFromBegin(resp,Constants.endLine+Constants.endLine)+Constants.endLine;
18
19 String contentEncoding=StringUtil.subString(headerText,"Content-Encoding"+Constants.afterKey, Constants.endLine);
20 String contentType=StringUtil.subString(headerText,"Content-Type"+Constants.afterKey, Constants.endLine);
21 String contentTypeCharset=StringUtil.subString(headerText,"charset=", Constants.endLine);
22 String sessionId=StringUtil.subString(headerText,"sessionId"+Constants.afterKey, Constants.endLine);
23 String setCookie=StringUtil.subString(headerText,"Set-Cookie"+Constants.afterKey, Constants.endLine);
24
25 int m=0;
26
27 HttpHeader header=new HttpHeader();
28 header.setContentEncoding(contentEncoding);
29 header.setContentType(contentType);
30 header.setContentTypeCharset(contentTypeCharset);
31 header.setSessionId(sessionId);
32 header.setSetCookie(setCookie);
33 header.setText(headerText);
34
35 return header;
36 }
37
38 }
4 Http协议体对象
1 package waf.net.http.httpparser;
2
3 /**
4 *
5 * @author waf.wang
6 *
7 */
8
9 public class HttpBody
10 {
11 private String charset="";
12
13 private byte[] bytes=null;
14
15 private String text=null;
16
17 public byte[] getBytes()
18 {
19 return bytes;
20 }
21
22 public void setBytes(byte[] bytes)
23 {
24 this.bytes = bytes;
25 }
26
27 public String getText()
28 {
29 return text;
30 }
31
32 public void setText(String text)
33 {
34 this.text = text;
35 }
36
37 public String getCharset()
38 {
39 return charset;
40 }
41
42 public void setCharset(String charset)
43 {
44 this.charset = charset;
45 }
46
47
48 }
5 Http响应对象
1 package waf.net.http.httpparser;
2
3 /**
4 *
5 * @author waf.wang
6 *
7 */
8
9 public class HttpResponse
10 {
11 private byte[] content=null;
12
13
14 HttpHeader header=new HttpHeader();
15 HttpBody body=new HttpBody();
16 public byte[] getContent()
17 {
18 return content;
19 }
20 public void setContent(byte[] content)
21 {
22 this.content = content;
23 }
24 public HttpHeader getHeader()
25 {
26 return header;
27 }
28 public void setHeader(HttpHeader header)
29 {
30 this.header = header;
31 }
32 public HttpBody getBody()
33 {
34 return body;
35 }
36 public void setBody(HttpBody body)
37 {
38 this.body = body;
39 }
40
41
42
43
44 }