Skip to content

Commit

Permalink
raw: Refactor RawServlet:getBranch and :getPath parameters
Browse files Browse the repository at this point in the history
Refactor the `getBranch` and `getPath` methods to take a String as
second parameter, which is the already sanitised path info. Don't get
the path info from a passed in request anymore.

The methods are only ever called from within `processRequest`, which
already does some checks on the path info, like removing a leading
slash character. So no need to do that every time again the methods
and passing a request for that.
  • Loading branch information
flaix committed Nov 9, 2020
1 parent 2394397 commit 26db318
Show file tree
Hide file tree
Showing 3 changed files with 727 additions and 59 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/gitblit/servlet/PagesServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ public PagesServlet(
}

@Override
protected String getBranch(String repository, HttpServletRequest request) {
String getBranch(String repository, String pathInfo)
{
return "gh-pages";
}

@Override
protected String getPath(String repository, String branch, HttpServletRequest request) {
String pi = request.getPathInfo().substring(1);
String getPath(String repository, String branch, String pi)
{
if (pi.equals(repository)) {
return "";
}
Expand Down
50 changes: 38 additions & 12 deletions src/main/java/com/gitblit/servlet/RawServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,23 @@ public static String asLink(String baseURL, String repository, String branch, St
return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath));
}

protected String getBranch(String repository, HttpServletRequest request) {
String pi = request.getPathInfo();
if (pi == null || pi.isEmpty() || pi.equals("/")) return "";
String branch = pi.substring(pi.indexOf(repository) + repository.length() + 1);

/**
* Find and return the name of a branch from a given repository in a HTTP request path info.
* The branch name returned is transformed to the form in the repository, i.e. a transformation
* of the forward slash character in the URL is reversed.
*
* @param repository
* Path of repository, no leading slash, no trailing slash
* @param pathInfo
* The sanitised path info from a HTTP request, i.e. without the leading slash.
*
* @return The name of the branch from the path info, unescaped.
*/
String getBranch(String repository, String pathInfo)
{
if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) return "";
String branch = pathInfo.substring(pathInfo.indexOf(repository) + repository.length() + 1);
int fs = branch.indexOf('/');
if (fs > -1) {
branch = branch.substring(0, fs);
Expand All @@ -134,15 +147,28 @@ protected String getBranch(String repository, HttpServletRequest request) {
return branch.replace('!', '/').replace(c, '/');
}

protected String getPath(String repository, String branch, HttpServletRequest request) {
/**
* Find and return the path from a given repository and given branch in a HTTP request path info.
* The path string returned is transformed to the form in the repository, i.e. a transformation
* of the forward slash character in the URL is reversed.
*
* @param repository
* Path of repository, no leading slash, no trailing slash
* @param branch
* Branch name from the repository, i.e. with forward slash character, no leading slash, no trailing slash.
* @param pathInfo
* The sanitised path info from a HTTP request, i.e. without the leading slash.
*
* @return The file/folder path part from the path info, in unescaped form.
*/
String getPath(String repository, String branch, String pathInfo)
{
if (pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/")) return "";
String base = repository + "/" + branch;
String pi = request.getPathInfo();
if (pi == null || pi.isEmpty() || pi.equals("/")) return "";
pi = pi.substring(1);
if (pi.equals(base)) {
if (pathInfo.equals(base)) {
return "";
}
String path = pi.substring(pi.indexOf(base) + base.length() + 1);
String path = pathInfo.substring(pathInfo.indexOf(base) + base.length() + 1);
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
Expand Down Expand Up @@ -200,7 +226,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
}

// identify the branch
String branch = getBranch(repository, request);
String branch = getBranch(repository, path);
if (StringUtils.isEmpty(branch)) {
branch = r.getBranch();
if (branch == null) {
Expand All @@ -219,7 +245,7 @@ private void processRequest(HttpServletRequest request, HttpServletResponse resp
}

// identify the requested path
String requestedPath = getPath(repository, branch, request);
String requestedPath = getPath(repository, branch, path);

// identify the commit
RevCommit commit = JGitUtils.getCommit(r, branch);
Expand Down
Loading

0 comments on commit 26db318

Please sign in to comment.