Skip to content

Commit 5dfe7d7

Browse files
committed
Fix #26805: don't restart when requesting CSS or JS
1 parent 0f51dec commit 5dfe7d7

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/java/eu/openanalytics/shinyproxy/AppRequestInfo.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static AppRequestInfo fromRequestOrException(HttpServletRequest request)
5353
if (result == null) {
5454
throw new BadRequestException("Error parsing URL.");
5555
}
56-
// do not user request.getParam() hear as it will inspect the request body and proxying the request will fail
56+
// do not use request.getParam() here as it will inspect the request body and proxying the request will fail
5757
List<String> param = ServletUriComponentsBuilder.fromRequest(request).build().getQueryParams().get(PROXY_HINT_PARAM);
5858
if (param != null && param.size() > 0) {
5959
result = new AppRequestInfo(result.appName, result.appInstance, result.subPath, param.get(0));
@@ -132,4 +132,13 @@ public String getSubPath() {
132132
public String getProxyIdHint() {
133133
return proxyIdHint;
134134
}
135+
136+
public Boolean isCssOrJsResource() {
137+
if (subPath != null) {
138+
String lowerSubPath = subPath.toLowerCase();
139+
return lowerSubPath.endsWith(".js") || lowerSubPath.endsWith(".css");
140+
}
141+
return false;
142+
}
143+
135144
}

src/main/java/eu/openanalytics/shinyproxy/ShinyProxyTestStrategy.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public boolean testProxy(Proxy proxy) {
6868
// proxy got stopped while loading -> no need to try to connect it since the container will already be deleted
6969
return true;
7070
}
71-
System.out.println(proxy.getStatus());
7271
URL testURL = new URL(targetURI.toString());
7372
HttpURLConnection connection = ((HttpURLConnection) testURL.openConnection());
7473
connection.setConnectTimeout(timeoutMs);

src/main/java/eu/openanalytics/shinyproxy/controllers/AppController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import eu.openanalytics.shinyproxy.runtimevalues.AppInstanceKey;
3333
import eu.openanalytics.shinyproxy.runtimevalues.PublicPathKey;
3434
import eu.openanalytics.shinyproxy.runtimevalues.WebSocketReconnectionModeKey;
35+
import io.undertow.server.HttpServerExchange;
36+
import io.undertow.servlet.handlers.ServletRequestContext;
3537
import org.springframework.stereotype.Controller;
3638
import org.springframework.ui.ModelMap;
3739
import org.springframework.web.bind.annotation.RequestMapping;
@@ -40,6 +42,8 @@
4042
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
4143

4244
import javax.inject.Inject;
45+
import javax.servlet.AsyncContext;
46+
import javax.servlet.ServletResponse;
4347
import javax.servlet.http.HttpServletRequest;
4448
import javax.servlet.http.HttpServletResponse;
4549
import java.io.IOException;
@@ -114,6 +118,21 @@ public void appDirect(HttpServletRequest request, HttpServletResponse response)
114118
response.setStatus(410);
115119
response.getWriter().write("{\"status\":\"error\", \"message\":\"app_stopped_or_non_existent\"}");
116120
return;
121+
} else if (proxy == null && (appRequestInfo.isCssOrJsResource() || request.getHeader("Upgrade") != null)) {
122+
// No proxy found and the request is for a CSS or JS file or a websocket connection.
123+
// We do not start a new proxy instance, because this is most probably a background request by an old, stopped app
124+
// We reply to the request after 10 seconds. This way the app should detect that it has been stopped before the resource is loaded.
125+
// (simply returning an error code could trigger the error handling logic of the app)
126+
response.setStatus(200);
127+
final AsyncContext acontext = request.startAsync();
128+
acontext.start(() -> {
129+
try {
130+
Thread.sleep(10000);
131+
} catch (InterruptedException ignored) {
132+
}
133+
acontext.complete();
134+
});
135+
return;
117136
} else if (proxy != null && appRequestInfo.getProxyIdHint() != null && !proxy.getId().equals(appRequestInfo.getProxyIdHint())) {
118137
// the proxy was restarted by the client before the client was aware that the app was stopped externally
119138
// therefore the proxy_id_hints are different.

0 commit comments

Comments
 (0)