读取请求体只能使用字节流,使用字符流读不到 @param in @throws RequestParseException
(InputStream in)
| 76 | * @throws RequestParseException |
| 77 | */ |
| 78 | public Request(InputStream in) throws RequestParseException, RequestInvalidException { |
| 79 | this.attributes = new HashMap<>(); |
| 80 | log.info("开始读取Request"); |
| 81 | BufferedInputStream bin = new BufferedInputStream(in); |
| 82 | byte[] buf = null; |
| 83 | try { |
| 84 | buf = new byte[bin.available()]; |
| 85 | int len = bin.read(buf); |
| 86 | if (len <= 0) { |
| 87 | throw new RequestInvalidException(); |
| 88 | } |
| 89 | } catch (IOException e) { |
| 90 | e.printStackTrace(); |
| 91 | } |
| 92 | |
| 93 | String[] lines = null; |
| 94 | try { |
| 95 | //支持中文,对中文进行URL解码 |
| 96 | lines = URLDecoder.decode(new String(buf, CharsetProperties.UTF_8_CHARSET), CharsetProperties.UTF_8).split(CharConstant.CRLF); |
| 97 | } catch (UnsupportedEncodingException e) { |
| 98 | e.printStackTrace(); |
| 99 | } |
| 100 | log.info("Request读取完毕"); |
| 101 | log.info("{}", Arrays.toString(lines)); |
| 102 | try { |
| 103 | parseHeaders(lines);//解析header |
| 104 | if (headers.containsKey("Content-Length") && !headers.get("Content-Length").get(0).equals("0")) { |
| 105 | parseBody(lines[lines.length - 1]);//解析请求体(post传参) |
| 106 | } |
| 107 | } catch (Throwable e) { |
| 108 | e.printStackTrace(); |
| 109 | throw new RequestParseException(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public void setAttribute(String key, Object value) { |
| 114 | attributes.put(key, value); |
nothing calls this directly
no test coverage detected