mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 15:05:50 +01:00
Merge branch 'master' into pr-support-textmsg-in-email
This commit is contained in:
@@ -25,11 +25,9 @@ class ScalatraBootstrap extends LifeCycle with SystemSettingsService {
|
||||
context.getFilterRegistration("gitAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/git/*")
|
||||
context.addFilter("apiAuthenticationFilter", new ApiAuthenticationFilter)
|
||||
context.getFilterRegistration("apiAuthenticationFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/api/v3/*")
|
||||
context.addFilter("ghCompatRepositoryAccessFilter", new GHCompatRepositoryAccessFilter)
|
||||
context.getFilterRegistration("ghCompatRepositoryAccessFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
|
||||
|
||||
// Register controllers
|
||||
context.mount(new AnonymousAccessController, "/*")
|
||||
context.mount(new PreProcessController, "/*")
|
||||
|
||||
context.addFilter("pluginControllerFilter", new PluginControllerFilter)
|
||||
context.getFilterRegistration("pluginControllerFilter").addMappingForUrlPatterns(EnumSet.allOf(classOf[DispatcherType]), true, "/*")
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package gitbucket.core.controller
|
||||
|
||||
class AnonymousAccessController extends AnonymousAccessControllerBase
|
||||
|
||||
trait AnonymousAccessControllerBase extends ControllerBase {
|
||||
get(!context.settings.allowAnonymousAccess, context.loginAccount.isEmpty) {
|
||||
if(!context.currentPath.startsWith("/assets") && !context.currentPath.startsWith("/signin") &&
|
||||
!context.currentPath.startsWith("/register")) {
|
||||
Unauthorized()
|
||||
} else {
|
||||
pass()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package gitbucket.core.controller
|
||||
|
||||
import org.scalatra.MovedPermanently
|
||||
|
||||
class PreProcessController extends PreProcessControllerBase
|
||||
|
||||
trait PreProcessControllerBase extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Provides GitHub compatible URLs for Git client.
|
||||
*
|
||||
* <ul>
|
||||
* <li>git clone http://localhost:8080/owner/repo</li>
|
||||
* <li>git clone http://localhost:8080/owner/repo.git</li>
|
||||
* </ul>
|
||||
*
|
||||
* @see https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols
|
||||
*/
|
||||
get("/*/*/info/refs") {
|
||||
val query = Option(request.getQueryString).map("?" + _).getOrElse("")
|
||||
halt(MovedPermanently(baseUrl + "/git" + request.getRequestURI + query))
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter requests from anonymous users.
|
||||
*
|
||||
* If anonymous access is allowed, pass all requests.
|
||||
* But if it's not allowed, demands authentication except some paths.
|
||||
*/
|
||||
get(!context.settings.allowAnonymousAccess, context.loginAccount.isEmpty) {
|
||||
if(!context.currentPath.startsWith("/assets") && !context.currentPath.startsWith("/signin") &&
|
||||
!context.currentPath.startsWith("/register")) {
|
||||
Unauthorized()
|
||||
} else {
|
||||
pass()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package gitbucket.core.servlet
|
||||
|
||||
import javax.servlet._
|
||||
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
|
||||
|
||||
import gitbucket.core.service.SystemSettingsService
|
||||
|
||||
/**
|
||||
* A controller to provide GitHub compatible URL for Git clients.
|
||||
*/
|
||||
class GHCompatRepositoryAccessFilter extends Filter with SystemSettingsService {
|
||||
|
||||
/**
|
||||
* Pattern of GitHub compatible repository URL.
|
||||
* <code>/:user/:repo.git/</code>
|
||||
*/
|
||||
private val githubRepositoryPattern = """^/[^/]+/[^/]+\.git/.*""".r
|
||||
|
||||
override def init(filterConfig: FilterConfig) = {}
|
||||
|
||||
override def doFilter(req: ServletRequest, res: ServletResponse, chain: FilterChain) = {
|
||||
implicit val request = req.asInstanceOf[HttpServletRequest]
|
||||
val agent = request.getHeader("USER-AGENT")
|
||||
val response = res.asInstanceOf[HttpServletResponse]
|
||||
val requestPath = request.getRequestURI.substring(request.getContextPath.length)
|
||||
val queryString = if (request.getQueryString != null) "?" + request.getQueryString else ""
|
||||
|
||||
requestPath match {
|
||||
case githubRepositoryPattern() if agent != null && agent.toLowerCase.indexOf("git") >= 0 =>
|
||||
response.sendRedirect(baseUrl + "/git" + requestPath + queryString)
|
||||
case _ =>
|
||||
chain.doFilter(req, res)
|
||||
}
|
||||
}
|
||||
|
||||
override def destroy() = {}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user