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请求URL
private String requestURL;
// Http请求QueryString
private 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() {
@Override
public 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);
}
}