Skip to content

Commit bde9fff

Browse files
authored
fix: Use encoded path for dev server (#12225)
Allows project folders with spaces to work Fixes #12221
1 parent 8a1cad7 commit bde9fff

4 files changed

Lines changed: 107 additions & 3 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/internal/UrlUtil.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public static boolean isExternal(String url) {
6363
public static String encodeURI(String path) {
6464
try {
6565
return URLEncoder.encode(path, StandardCharsets.UTF_8.name())
66-
.replace("+", "%20").replace("%2F", "/");
66+
.replace("+", "%20").replace("%2F", "/")
67+
.replace("%40", "@");
6768
} catch (UnsupportedEncodingException e) {
6869
// Runtime exception as this doesn't really happen
6970
throw new RuntimeException("Encoding the URI failed", e); // NOSONAR

‎flow-server/src/test/java/com/vaadin/flow/internal/UrlUtilTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
public class UrlUtilTest {
2222

23-
private String shouldNotBeEscaped = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/_-.*";
23+
private String shouldNotBeEscaped = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/_-.*@";
2424

2525
@Test
2626
public void isExternal_URLStartsWithTwoSlashes_returnsTrue() {

‎vaadin-dev-server/src/main/java/com/vaadin/base/devserver/AbstractDevServerRunner.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.vaadin.flow.internal.BrowserLiveReload;
4343
import com.vaadin.flow.internal.BrowserLiveReloadAccessor;
4444
import com.vaadin.flow.internal.DevModeHandler;
45+
import com.vaadin.flow.internal.UrlUtil;
4546
import com.vaadin.flow.server.Constants;
4647
import com.vaadin.flow.server.ExecutionFailedException;
4748
import com.vaadin.flow.server.HandlerHelper;
@@ -685,7 +686,7 @@ public boolean serveDevModeRequest(HttpServletRequest request,
685686
requestFilename = "/VAADIN/static" + requestFilename;
686687
}
687688

688-
String devServerRequestPath = requestFilename;
689+
String devServerRequestPath = UrlUtil.encodeURI(requestFilename);
689690
if (request.getQueryString() != null) {
690691
devServerRequestPath += "?" + request.getQueryString();
691692
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.vaadin.base.devserver;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.HttpURLConnection;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.concurrent.CompletableFuture;
10+
import java.util.concurrent.atomic.AtomicReference;
11+
import java.util.regex.Pattern;
12+
13+
import javax.servlet.ServletOutputStream;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
17+
import com.vaadin.base.devserver.startup.AbstractDevModeTest;
18+
import com.vaadin.flow.internal.DevModeHandler;
19+
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
import org.mockito.Mockito;
23+
24+
public class AbstractDevServerRunnerTest extends AbstractDevModeTest {
25+
26+
private class DummyRunner extends AbstractDevServerRunner {
27+
28+
protected DummyRunner() {
29+
super(lookup, 0, npmFolder,
30+
CompletableFuture.completedFuture(null));
31+
}
32+
33+
@Override
34+
protected File getServerBinary() {
35+
return new File("dummy.bin");
36+
}
37+
38+
@Override
39+
protected File getServerConfig() {
40+
return new File("dummy.config");
41+
}
42+
43+
@Override
44+
protected String getServerName() {
45+
return "Dummy server";
46+
}
47+
48+
@Override
49+
protected List<String> getServerStartupCommand(String nodeExec) {
50+
List<String> commands = new ArrayList<>();
51+
commands.add("echo");
52+
return commands;
53+
}
54+
55+
@Override
56+
protected Pattern getServerSuccessPattern() {
57+
return Pattern.compile("Dummy success");
58+
}
59+
60+
@Override
61+
protected Pattern getServerFailurePattern() {
62+
return Pattern.compile("Dummy fail");
63+
}
64+
65+
@Override
66+
protected boolean checkConnection() {
67+
return true;
68+
}
69+
70+
@Override
71+
public HttpURLConnection prepareConnection(String path, String method)
72+
throws IOException {
73+
return Mockito.mock(HttpURLConnection.class);
74+
}
75+
76+
}
77+
78+
@Test
79+
public void shouldPassEncodedUrlToDevServer() throws Exception {
80+
handler = new DummyRunner();
81+
DevModeHandler devServer = Mockito.spy(handler);
82+
83+
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
84+
Mockito.when(response.getOutputStream())
85+
.thenReturn(Mockito.mock(ServletOutputStream.class));
86+
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
87+
Mockito.when(request.getRequestURI()).thenReturn("/foo%20bar");
88+
Mockito.when(request.getPathInfo()).thenReturn("foo bar");
89+
Mockito.when(request.getHeaderNames())
90+
.thenReturn(Collections.emptyEnumeration());
91+
92+
AtomicReference<String> requestedPath = new AtomicReference<>();
93+
Mockito.when(devServer.prepareConnection(Mockito.any(), Mockito.any()))
94+
.then(invocation -> {
95+
requestedPath.set((String) invocation.getArguments()[0]);
96+
return Mockito.mock(HttpURLConnection.class);
97+
});
98+
devServer.serveDevModeRequest(request, response);
99+
Assert.assertEquals("foo%20bar", requestedPath.get());
100+
101+
}
102+
}

0 commit comments

Comments
 (0)