BinCatRequest.java示例代码片段:

    1. package com.anbai.sec.server.servlet;
    2. import org.javaweb.utils.StringUtils;
    3. import javax.servlet.*;
    4. import javax.servlet.http.*;
    5. import java.io.*;
    6. import java.net.Socket;
    7. import java.net.URLDecoder;
    8. import java.security.Principal;
    9. import java.util.*;
    10. import java.util.concurrent.ConcurrentHashMap;
    11. import java.util.logging.Logger;
    12. /**
    13. * BinCat 请求解析实现对象,解析Http请求协议和参数
    14. */
    15. public class BinCatRequest implements HttpServletRequest {
    16. // 客户端Socket连接对象
    17. private final Socket clientSocket;
    18. // Socket输入流对象
    19. private final InputStream socketInputStream;
    20. // Http请求头对象
    21. private Map<String, String> headerMap;
    22. // Http请求参数对象
    23. private Map<String, String[]> parameterMap;
    24. // Http请求attribute对象
    25. private final Map<String, Object> attributeMap = new ConcurrentHashMap<String, Object>();
    26. // Http请求Cookie对象
    27. private Cookie[] cookie;
    28. // Http请求Cookie对象
    29. private final Map<String, String> cookieMap = new ConcurrentHashMap<String, String>();
    30. // Http请求Session对象
    31. private final Map<String, BinCatSession> sessionMap = new ConcurrentHashMap<String, BinCatSession>();
    32. // Http请求方法类型
    33. private String requestMethod;
    34. // Http请求URL
    35. private String requestURL;
    36. // Http请求QueryString
    37. private String queryString;
    38. // Http请求协议版本信息
    39. private String httpVersion;
    40. // 是否已经解析过Http请求参数,防止多次解析请求参数
    41. private volatile boolean parsedParameter = false;
    42. // Http请求内容长度
    43. private int contentLength;
    44. // Http请求内容类型
    45. private String contentType;
    46. // 存储Session的ID名称
    47. private static final String SESSION_ID_NAME = "JSESSIONID";
    48. // Http请求主机名
    49. private String host;
    50. // Http请求主机端口
    51. private int port;
    52. private static final Logger LOG = Logger.getLogger("info");
    53. public BinCatRequest(Socket clientSocket) throws IOException {
    54. this.clientSocket = clientSocket;
    55. this.socketInputStream = clientSocket.getInputStream();
    56. // 解析Http协议
    57. parse();
    58. }
    59. /**
    60. * 解析Http请求协议,不解析Body部分
    61. *
    62. * @throws IOException
    63. */
    64. private void parse() throws IOException {
    65. // 此处省略Http请求协议解析、参数解析等内容...
    66. }
    67. /**
    68. * 解析Http请求参数
    69. *
    70. * @throws IOException Http协议解析异常
    71. */
    72. private synchronized void parseParameter() {
    73. // 此处省略Http请求协议解析、参数解析等内容...
    74. }
    75. // 此处省略HttpServletRequest接口中的大部分方法,仅保留几个示例方法...
    76. public String getHeader(String name) {
    77. return this.headerMap.get(name);
    78. }
    79. public ServletInputStream getInputStream() throws IOException {
    80. return new ServletInputStream() {
    81. @Override
    82. public int read() throws IOException {
    83. return socketInputStream.read();
    84. }
    85. };
    86. }
    87. public String getParameter(String name) {
    88. if (!parsedParameter) {
    89. this.parseParameter();
    90. }
    91. if (parameterMap.containsKey(name)) {
    92. return this.parameterMap.get(name)[0];
    93. }
    94. return null;
    95. }
    96. public String getRemoteAddr() {
    97. return clientSocket.getInetAddress().getHostAddress();
    98. }
    99. public void setAttribute(String name, Object o) {
    100. attributeMap.put(name, o);
    101. }
    102. }