You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitblitReceivePackFactory.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.git;
  17. import javax.servlet.http.HttpServletRequest;
  18. import org.eclipse.jgit.lib.PersonIdent;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.transport.ReceivePack;
  21. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  22. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  23. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import com.gitblit.GitBlit;
  27. import com.gitblit.models.RepositoryModel;
  28. import com.gitblit.models.UserModel;
  29. import com.gitblit.utils.HttpUtils;
  30. import com.gitblit.utils.StringUtils;
  31. /**
  32. * The receive pack factory creates the receive pack which processes pushes.
  33. *
  34. * @author James Moger
  35. *
  36. * @param <X> the connection type
  37. */
  38. public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> {
  39. protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class);
  40. @Override
  41. public ReceivePack create(X req, Repository db)
  42. throws ServiceNotEnabledException, ServiceNotAuthorizedException {
  43. UserModel user = UserModel.ANONYMOUS;
  44. String repositoryName = "";
  45. String origin = "";
  46. String gitblitUrl = "";
  47. String repositoryUrl = "";
  48. int timeout = 0;
  49. if (req instanceof HttpServletRequest) {
  50. // http/https request may or may not be authenticated
  51. HttpServletRequest request = (HttpServletRequest) req;
  52. repositoryName = request.getAttribute("gitblitRepositoryName").toString();
  53. origin = request.getRemoteHost();
  54. gitblitUrl = HttpUtils.getGitblitURL(request);
  55. repositoryUrl = request.getRequestURI();
  56. // determine pushing user
  57. String username = request.getRemoteUser();
  58. if (!StringUtils.isEmpty(username)) {
  59. UserModel u = GitBlit.self().getUserModel(username);
  60. if (u != null) {
  61. user = u;
  62. }
  63. }
  64. } else if (req instanceof GitDaemonClient) {
  65. // git daemon request is always anonymous
  66. GitDaemonClient client = (GitDaemonClient) req;
  67. repositoryName = client.getRepositoryName();
  68. origin = client.getRemoteAddress().getHostAddress();
  69. // set timeout from Git daemon
  70. timeout = client.getDaemon().getTimeout();
  71. }
  72. // TODO make this a setting
  73. boolean allowAnonymousPushes = true;
  74. if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
  75. // prohibit anonymous pushes
  76. throw new ServiceNotEnabledException();
  77. }
  78. final RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName);
  79. final GitblitReceivePack rp = new GitblitReceivePack(db, repository, user);
  80. rp.setGitblitUrl(gitblitUrl);
  81. rp.setRepositoryUrl(repositoryUrl);
  82. rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
  83. rp.setTimeout(timeout);
  84. return rp;
  85. }
  86. }