Browse Source

Merge branch 'fix-raw-slash-branch' into master

tags/v1.9.2
Florian Zschocke 3 years ago
parent
commit
6f15e41eb5

+ 2
- 1
releases.moxie View File

html: ~ html: ~
text: ~ text: ~
security: ~ security: ~
fixes: ~
fixes:
- Fix raw links broken for branches with a forward slash in the name (issue-1290, issue-1234, issue-813)
changes: ~ changes: ~
additions: ~ additions: ~
dependencyChanges: ~ dependencyChanges: ~

+ 4
- 3
src/main/java/com/gitblit/servlet/PagesServlet.java View File

} }


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


@Override @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)) { if (pi.equals(repository)) {
return ""; return "";
} }

+ 76
- 16
src/main/java/com/gitblit/servlet/RawServlet.java View File

@Singleton @Singleton
public class RawServlet extends HttpServlet { public class RawServlet extends HttpServlet {


// Forward slash character
static final char FSC = '!';

private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;


private transient Logger logger = LoggerFactory.getLogger(RawServlet.class); private transient Logger logger = LoggerFactory.getLogger(RawServlet.class);
if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') { if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
baseURL = baseURL.substring(0, baseURL.length() - 1); baseURL = baseURL.substring(0, baseURL.length() - 1);
} }
if (repository.length() > 0 && repository.charAt(repository.length() - 1) == '/') {
repository = repository.substring(0, repository.length() - 1);
}
if (repository.length() > 0 && repository.charAt(0) == '/') {
repository = repository.substring(1);
}


char fsc = '!';
char c = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
if (c != '/') {
fsc = c;
char fsc = GitblitContext.getManager(IRuntimeManager.class).getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
if (fsc == '/') {
fsc = FSC;
} }
if (branch != null) { if (branch != null) {
branch = Repository.shortenRefName(branch).replace('/', fsc); branch = Repository.shortenRefName(branch).replace('/', fsc);
} }


if (path != null && path.length() > 0 && path.charAt(0) == '/') {
path = path.substring(1);
}
String encodedPath = path == null ? "" : path.replace('/', fsc); String encodedPath = path == null ? "" : path.replace('/', fsc);
return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath)); return baseURL + Constants.RAW_PATH + repository + "/" + (branch == null ? "" : (branch + "/" + encodedPath));
} }


protected String getBranch(String repository, HttpServletRequest request) {
String pi = request.getPathInfo();
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('/'); int fs = branch.indexOf('/');
if (fs > -1) { if (fs > -1) {
branch = branch.substring(0, fs); branch = branch.substring(0, fs);
return branch.replace('!', '/').replace(c, '/'); return branch.replace('!', '/').replace(c, '/');
} }


protected String getPath(String repository, String branch, HttpServletRequest request) {
String base = repository + "/" + branch;
String pi = request.getPathInfo().substring(1);
if (pi.equals(base)) {
/**
* 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 "";

// Make the branch look like in the URL, or else it won't match later in the `indexOf`.
char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
char fsc = (c == '/') ? FSC : c;
String base = repository + "/" + Repository.shortenRefName(branch).replace('/', fsc);

// 'repository/' or 'repository/branch' or 'repository/branch/'
if (pathInfo.equals(base)) {
return ""; return "";
} }
String path = pi.substring(pi.indexOf(base) + base.length() + 1);
// I have no idea why 'indexOf(base)' is used, which assumes something could come before 'base' in
// the pathInfo string. But since it is here, we handle it until we completly refactor the paths used
// in Gitblit to something sensible.
// 'leadin/repository/'
// 'leadin/repository/branch'
int pathStart = pathInfo.indexOf(base) + base.length();
// 'leadin/repository/branch/'
if (pathStart < pathInfo.length() && pathInfo.charAt(pathStart) == '/') pathStart++;
if (pathInfo.length() == pathStart) return "";
// 'leadin/repository/branch/path'
String path = pathInfo.substring(pathStart);

path = path.replace('!', '/').replace(c, '/');

// 'repository/branch/path/'
// 'leadin/repository/branch/path/'
if (path.endsWith("/")) { if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1); path = path.substring(0, path.length() - 1);
} }
char c = runtimeManager.getSettings().getChar(Keys.web.forwardSlashCharacter, '/');
return path.replace('!', '/').replace(c, '/');
return path;
} }


protected boolean renderIndex() { protected boolean renderIndex() {
} }


// identify the branch // identify the branch
String branch = getBranch(repository, request);
String branch = getBranch(repository, path);
if (StringUtils.isEmpty(branch)) { if (StringUtils.isEmpty(branch)) {
branch = r.getBranch(); branch = r.getBranch();
if (branch == null) { if (branch == null) {
} }


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


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

+ 1426
- 0
src/test/java/com/gitblit/servlet/RawServletTest.java
File diff suppressed because it is too large
View File


+ 12
- 0
src/test/java/com/gitblit/tests/mock/MockGitblitContext.java View File

package com.gitblit.tests.mock;

import com.gitblit.manager.IManager;
import com.gitblit.servlet.GitblitContext;

public class MockGitblitContext extends GitblitContext
{
public <X extends IManager> void addManager(X x)
{
startManager(x);
}
}

Loading…
Cancel
Save