| 75 | import org.thymeleaf.context.Context; |
| 76 | |
| 77 | public class HttpExchange { |
| 78 | protected static final int CLOSE_FINISH = 0; // 正常结束HttpExchange,不关闭连接 |
| 79 | protected static final int CLOSE_ON_FLUSH = 1; // 结束HttpExchange,发送完时关闭连接 |
| 80 | protected static final int CLOSE_FORCE = 2; // 结束HttpExchange,不等发送完强制关闭连接 |
| 81 | protected static final int CLOSE_TIMEOUT = 3; // 同上,只是因idle超时而关闭 |
| 82 | protected static final int CLOSE_PASSIVE = 4; // 同上,只是因远程主动关闭而关闭 |
| 83 | |
| 84 | protected static final @NotNull Pattern rangePattern = Pattern.compile("[ =\\-/]"); |
| 85 | protected static final OpenOption[] readOnlyOpenOptions = new OpenOption[]{StandardOpenOption.READ}; |
| 86 | protected static final @NotNull VarHandle detachedHandle; |
| 87 | protected static final HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false); |
| 88 | protected static final HttpHeadersFactory headersFactory = DefaultHttpHeadersFactory.headersFactory().withValidation(false); |
| 89 | protected static final HttpHeadersFactory trailersFactory = DefaultHttpHeadersFactory.trailersFactory().withValidation(false); |
| 90 | |
| 91 | static { |
| 92 | try { |
| 93 | detachedHandle = MethodHandles.lookup().findVarHandle(HttpExchange.class, "detached", int.class); |
| 94 | } catch (ReflectiveOperationException e) { |
| 95 | throw new ExceptionInInitializerError(e); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | protected final @NotNull HttpServer server; // 所属的HttpServer对象,每个对象管理监听端口的所有连接 |
| 100 | protected final @NotNull ChannelHandlerContext context; // netty的连接上下文,每个连接可能会依次绑定到多个HttpExchange对象 |
| 101 | protected @Nullable HttpRequest request; // 收到完整HTTP header部分会赋值 |
| 102 | protected @Nullable HttpHandler handler; // 收到完整HTTP header部分会查找对应handler并赋值 |
| 103 | protected @NotNull ByteBuf content = Unpooled.EMPTY_BUFFER; // 当前收集的HTTP body部分, 只用于非流模式 |
| 104 | protected @Nullable Object userState; |
| 105 | protected @Nullable ArrayList<Object> resHeaders; // key,value,key,value,... |
| 106 | protected @Nullable List<Cookie> cookies; |
| 107 | protected @Nullable HttpSession.CookieSession cookieSession; |
| 108 | protected @Nullable String path; |
| 109 | protected volatile @SuppressWarnings("unused") int detached; // 0:not detached; 1:detached; 2:detached and closed |
| 110 | protected boolean willCloseConnection; // true表示close时会关闭连接 |
| 111 | protected boolean inStreamMode; // 是否在流/WebSocket模式过程中 |
| 112 | protected boolean isWebSocketTextContent; |
| 113 | |
| 114 | public HttpExchange(@NotNull HttpServer server, @NotNull ChannelHandlerContext context) { |
| 115 | this.server = server; |
| 116 | this.context = context; |
| 117 | } |
| 118 | |
| 119 | public HttpServer getHttpServer() { |
| 120 | return server; |
| 121 | } |
| 122 | |
| 123 | public @Nullable Object getUserState() { |
| 124 | return userState; |
| 125 | } |
| 126 | |
| 127 | public void setUserState(@Nullable Object userState) { |
| 128 | this.userState = userState; |
| 129 | } |
| 130 | |
| 131 | public @Nullable HttpSession.CookieSession getCookieSession() { |
| 132 | return cookieSession; |
| 133 | } |
| 134 | |