Skip to content

Commit 7980984

Browse files
committed
Web Context: Initialize HTTP method and requestPath at context creation due they both are required for route matching
1 parent 920b57d commit 7980984

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ public class JettyContext implements DefaultContext {
9292
private HashMap<String, String> responseCookies;
9393
private boolean responseStarted;
9494
private Boolean resetHeadersOnError;
95-
private String method;
96-
private String pathString;
95+
private final String method;
96+
private final String requestPath;
9797

9898
public JettyContext(Request request, Router router, int bufferSize, long maxRequestSize) {
9999
this.request = request;
100100
this.response = request.getResponse();
101101
this.router = router;
102102
this.bufferSize = bufferSize;
103103
this.maxRequestSize = maxRequestSize;
104+
this.method = request.getMethod().toUpperCase();
105+
this.requestPath = request.getRequestURI();
104106
}
105107

106108
@Nonnull @Override public Map<String, Object> getAttributes() {
@@ -139,9 +141,6 @@ public JettyContext(Request request, Router router, int bufferSize, long maxRequ
139141
}
140142

141143
@Nonnull @Override public String getMethod() {
142-
if (method == null) {
143-
method = request.getMethod().toUpperCase();
144-
}
145144
return method;
146145
}
147146

@@ -159,10 +158,7 @@ public JettyContext(Request request, Router router, int bufferSize, long maxRequ
159158
}
160159

161160
@Nonnull @Override public String getRequestPath() {
162-
if (pathString == null) {
163-
pathString = request.getRequestURI();
164-
}
165-
return pathString;
161+
return requestPath;
166162
}
167163

168164
@Nonnull @Override public Map<String, String> pathMap() {

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class NettyContext implements DefaultContext, ChannelFutureListener {
111111
private Route route;
112112
ChannelHandlerContext ctx;
113113
private HttpRequest req;
114-
private String path;
114+
private final String path;
115115
private HttpResponseStatus status = HttpResponseStatus.OK;
116116
private boolean responseStarted;
117117
private QueryString query;
@@ -128,7 +128,7 @@ public class NettyContext implements DefaultContext, ChannelFutureListener {
128128
private Map<String, String> responseCookies;
129129
private Boolean resetHeadersOnError;
130130
NettyWebSocket webSocket;
131-
private String method;
131+
private final String method;
132132

133133
public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, String path,
134134
int bufferSize) {
@@ -137,6 +137,7 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
137137
this.req = req;
138138
this.router = router;
139139
this.bufferSize = bufferSize;
140+
this.method = req.method().name().toUpperCase();
140141
}
141142

142143
@Nonnull @Override public Router getRouter() {
@@ -153,9 +154,6 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
153154
}
154155

155156
@Nonnull @Override public String getMethod() {
156-
if (method == null) {
157-
method = req.method().name().toUpperCase();
158-
}
159157
return method;
160158
}
161159

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ public class UtowContext implements DefaultContext, IoCallback {
8383
private HashMap<String, String> responseCookies;
8484
private long responseLength = -1;
8585
private Boolean resetHeadersOnError;
86-
private String method;
87-
private String requestPath;
86+
private final String method;
87+
private final String requestPath;
8888

8989
public UtowContext(HttpServerExchange exchange, Router router) {
9090
this.exchange = exchange;
9191
this.router = router;
92+
this.method = exchange.getRequestMethod().toString().toUpperCase();
93+
this.requestPath = exchange.getRequestPath();
9294
}
9395

9496
@Nonnull @Override public Router getRouter() {
@@ -123,9 +125,6 @@ public UtowContext(HttpServerExchange exchange, Router router) {
123125
}
124126

125127
@Nonnull @Override public String getMethod() {
126-
if (method == null) {
127-
method = exchange.getRequestMethod().toString().toUpperCase();
128-
}
129128
return method;
130129
}
131130

@@ -143,9 +142,6 @@ public UtowContext(HttpServerExchange exchange, Router router) {
143142
}
144143

145144
@Nonnull @Override public String getRequestPath() {
146-
if (requestPath == null) {
147-
requestPath = exchange.getRequestPath();
148-
}
149145
return requestPath;
150146
}
151147

0 commit comments

Comments
 (0)