BinCatRequest.java示例代码片段:
package com.anbai.sec.server.servlet;import org.javaweb.utils.StringUtils;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.net.Socket;import java.net.URLDecoder;import java.security.Principal;import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.logging.Logger;/*** BinCat 请求解析实现对象,解析Http请求协议和参数*/public class BinCatRequest implements HttpServletRequest {// 客户端Socket连接对象private final Socket clientSocket;// Socket输入流对象private final InputStream socketInputStream;// Http请求头对象private Map<String, String> headerMap;// Http请求参数对象private Map<String, String[]> parameterMap;// Http请求attribute对象private final Map<String, Object> attributeMap = new ConcurrentHashMap<String, Object>();// Http请求Cookie对象private Cookie[] cookie;// Http请求Cookie对象private final Map<String, String> cookieMap = new ConcurrentHashMap<String, String>();// Http请求Session对象private final Map<String, BinCatSession> sessionMap = new ConcurrentHashMap<String, BinCatSession>();// Http请求方法类型private String requestMethod;// Http请求URLprivate String requestURL;// Http请求QueryStringprivate String queryString;// Http请求协议版本信息private String httpVersion;// 是否已经解析过Http请求参数,防止多次解析请求参数private volatile boolean parsedParameter = false;// Http请求内容长度private int contentLength;// Http请求内容类型private String contentType;// 存储Session的ID名称private static final String SESSION_ID_NAME = "JSESSIONID";// Http请求主机名private String host;// Http请求主机端口private int port;private static final Logger LOG = Logger.getLogger("info");public BinCatRequest(Socket clientSocket) throws IOException {this.clientSocket = clientSocket;this.socketInputStream = clientSocket.getInputStream();// 解析Http协议parse();}/*** 解析Http请求协议,不解析Body部分** @throws IOException*/private void parse() throws IOException {// 此处省略Http请求协议解析、参数解析等内容...}/*** 解析Http请求参数** @throws IOException Http协议解析异常*/private synchronized void parseParameter() {// 此处省略Http请求协议解析、参数解析等内容...}// 此处省略HttpServletRequest接口中的大部分方法,仅保留几个示例方法...public String getHeader(String name) {return this.headerMap.get(name);}public ServletInputStream getInputStream() throws IOException {return new ServletInputStream() {@Overridepublic int read() throws IOException {return socketInputStream.read();}};}public String getParameter(String name) {if (!parsedParameter) {this.parseParameter();}if (parameterMap.containsKey(name)) {return this.parameterMap.get(name)[0];}return null;}public String getRemoteAddr() {return clientSocket.getInetAddress().getHostAddress();}public void setAttribute(String name, Object o) {attributeMap.put(name, o);}}
