Skip to content

Commit bcf3e5c

Browse files
committed
FIX IndexOutOfBound in redirection / forwarding
When redirecting to the next step in the GitHub import wizard an IndexOutOfBound exception was thrown when using relative URL in gerrit.config for the wizard steps. Example: [github] wizardFlow = repositories-next.gh => pullrequests.html This was due to the inconsistence of requestURI vs. contextPath. Target URI needs always to be absolute and have contextPath as prefix of requestURI. Change-Id: Ib5ef32378f9b351bf568aaf615b67cf864904a32
1 parent f64fee9 commit bcf3e5c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,29 @@ private String trimFromChar(String string, char ch) {
121121

122122
private void redirectToNextStep(HttpServletRequest req,
123123
HttpServletResponse resp) throws IOException, ServletException {
124-
String sourcePath = req.getRequestURI();
125-
String sourcePage = sourcePath.substring(sourcePath.lastIndexOf('/') + 1);
124+
String sourceUri = req.getRequestURI();
125+
int pathPos = sourceUri.lastIndexOf('/') + 1;
126+
String sourcePage = sourceUri.substring(pathPos);
127+
String sourcePath = sourceUri.substring(0, pathPos);
126128
int queryStringStart = sourcePage.indexOf('?');
127129
if (queryStringStart > 0) {
128130
sourcePage = sourcePage.substring(0, queryStringStart);
129131
}
130132
NextPage nextPage = githubConfig.getNextPage(sourcePage);
131133
if (nextPage != null) {
132134
if (nextPage.redirect) {
133-
resp.sendRedirect(nextPage.uri);
135+
resp.sendRedirect(nextPageURL(sourcePath, nextPage));
134136
} else {
135137
RequestDispatcher requestDispatcher =
136-
req.getRequestDispatcher(nextPage.uri);
138+
req.getRequestDispatcher(nextPageURL(sourcePath, nextPage));
137139
req.setAttribute("destUrl", nextPage);
138140
requestDispatcher.forward(req, resp);
139141
}
140142
}
141143
}
142144

145+
private String nextPageURL(String sourcePath, NextPage nextPage) {
146+
return nextPage.uri.startsWith("/") ? nextPage.uri
147+
: sourcePath + nextPage.uri;
148+
}
143149
}

0 commit comments

Comments
 (0)