Skip to content

Commit fdbe642

Browse files
authored
refactor!: Remove StaticFileHandler.isStaticResourceRequest (#12263)
This simplifies static file handling so the same logic is not duplicate in two places. It also makes it possible to always query the dev server for files instead of knowing upfront what the dev server can serve. The method is removed instead of retained as there are two ways it could have been used: 1. Called to check if a given request is a static resource request 2. Overridden in a custom StaticFileHandler To keep the method we would need to do the following for the cases: 1. Make isStaticResourceRequest return serveStaticResource() != null 2. Make serveStaticResource() call isStaticResourceRequest() as the first thing We cannot do both so it is safer to remove the method to force code to be updated
1 parent 2e9c915 commit fdbe642

10 files changed

Lines changed: 141 additions & 133 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/StaticFileHandler.java‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@
3232
*/
3333
public interface StaticFileHandler extends Serializable {
3434

35-
/**
36-
* Checks if a static resource can be found for the requested path.
37-
*
38-
* @param request
39-
* the request to check
40-
* @return <code>true</code> if a static resource exists and can be sent as
41-
* a response to this request, <code>false</code> otherwise
42-
*/
43-
boolean isStaticResourceRequest(HttpServletRequest request);
44-
4535
/**
4636
* Serves a static resource for the requested path if a resource can be
4737
* found.

‎flow-server/src/main/java/com/vaadin/flow/server/StaticFileServer.java‎

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -101,51 +101,6 @@ public StaticFileServer(VaadinService vaadinService) {
101101
.getDevModeHandler(vaadinService).orElse(null);
102102
}
103103

104-
@Override
105-
public boolean isStaticResourceRequest(HttpServletRequest request) {
106-
URL resource;
107-
108-
String requestFilename = getRequestFilename(request);
109-
if (requestFilename.endsWith("/")) {
110-
// Directories are not static resources although
111-
// servletContext.getResource will return a URL for them, at
112-
// least with Jetty
113-
return false;
114-
}
115-
116-
if (devModeHandler != null
117-
&& devModeHandler.isDevModeRequest(request)) {
118-
return true;
119-
}
120-
121-
if (APP_THEME_PATTERN.matcher(requestFilename).find()
122-
|| requestFilename.startsWith("/" + VAADIN_STATIC_FILES_PATH)
123-
|| requestFilename.startsWith("/" + VAADIN_BUILD_FILES_PATH)) {
124-
// The path is reserved for internal resources only
125-
// We rather serve 404 than let it fall through
126-
return true;
127-
}
128-
129-
if (manifestPaths.contains(requestFilename)) {
130-
// The path is on the webpack manifest list, so it is a static
131-
// resource as well.
132-
return true;
133-
}
134-
resource = getStaticResource(requestFilename);
135-
136-
if (resource != null && resourceIsDirectory(resource)) {
137-
return false;
138-
}
139-
140-
if (resource == null && shouldFixIncorrectWebjarPaths()
141-
&& isIncorrectWebjarPath(requestFilename)) {
142-
// Flow issue #4601
143-
return true;
144-
}
145-
146-
return resource != null;
147-
}
148-
149104
private boolean resourceIsDirectory(URL resource) {
150105
if (resource.getPath().endsWith("/")) {
151106
return true;
@@ -277,18 +232,25 @@ public boolean serveStaticResource(HttpServletRequest request,
277232
HttpServletResponse response) throws IOException {
278233

279234
String filenameWithPath = getRequestFilename(request);
235+
if (filenameWithPath.endsWith("/")) {
236+
// Directories are not static resources although
237+
// servletContext.getResource will return a URL for them, at
238+
// least with Jetty
239+
return false;
240+
}
241+
280242
if (HandlerHelper.isPathUnsafe(filenameWithPath)) {
281243
getLogger().info(HandlerHelper.UNSAFE_PATH_ERROR_MESSAGE_PATTERN,
282244
filenameWithPath);
283-
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
245+
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
284246
return true;
285247
}
286248

287249
if (devModeHandler != null
288-
&& devModeHandler.isDevModeRequest(request)) {
289-
if (devModeHandler.serveDevModeRequest(request, response)) {
290-
return true;
291-
}
250+
&& devModeHandler.serveDevModeRequest(request, response)) {
251+
// We don't know what the dev server can serve, but if it served
252+
// something we are happy.
253+
return true;
292254
}
293255

294256
URL resourceUrl = null;
@@ -317,8 +279,14 @@ && isIncorrectWebjarPath(filenameWithPath)) {
317279

318280
if (resourceUrl == null) {
319281
// Not found in webcontent or in META-INF/resources in some JAR
320-
response.sendError(HttpServletResponse.SC_NOT_FOUND);
321-
return true;
282+
return false;
283+
}
284+
285+
if (resourceIsDirectory(resourceUrl)) {
286+
// Directories are not static resources although
287+
// servletContext.getResource will return a URL for them, at
288+
// least with Jetty
289+
return false;
322290
}
323291

324292
// There is a resource!

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinServlet.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ protected void service(HttpServletRequest request,
328328
protected boolean serveStaticOrWebJarRequest(HttpServletRequest request,
329329
HttpServletResponse response) throws IOException {
330330

331-
if (staticFileHandler.isStaticResourceRequest(request)) {
332-
staticFileHandler.serveStaticResource(request, response);
331+
if (staticFileHandler.serveStaticResource(request, response)) {
333332
return true;
334333
}
335334

0 commit comments

Comments
 (0)