git_http_controller.rb 2.2 KB
Newer Older
Jacob Vosmaer's avatar
Jacob Vosmaer committed
1
class Projects::GitHttpController < Projects::GitHttpClientController
2
  include WorkhorseRequest
3

4 5 6 7
  before_action :access_check

  rescue_from Gitlab::GitAccess::UnauthorizedError, with: :render_403
  rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404
8
  rescue_from Gitlab::GitAccess::ProjectCreationError, with: :render_422
9

10 11 12
  # GET /foo/bar.git/info/refs?service=git-upload-pack (git pull)
  # GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
  def info_refs
13
    log_user_activity if upload_pack?
14

15
    render_ok
Jacob Vosmaer's avatar
Jacob Vosmaer committed
16
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
17

18 19
  # POST /foo/bar.git/git-upload-pack (git pull)
  def git_upload_pack
20
    render_ok
21 22 23 24
  end

  # POST /foo/bar.git/git-receive-pack" (git push)
  def git_receive_pack
25
    render_ok
Jacob Vosmaer's avatar
Jacob Vosmaer committed
26 27 28 29
  end

  private

Jacob Vosmaer's avatar
Jacob Vosmaer committed
30 31
  def download_request?
    upload_pack?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
32 33 34
  end

  def upload_pack?
35
    git_command == 'git-upload-pack'
Jacob Vosmaer's avatar
Jacob Vosmaer committed
36 37
  end

38
  def git_command
Jacob Vosmaer's avatar
Jacob Vosmaer committed
39
    if action_name == 'info_refs'
Jacob Vosmaer's avatar
Jacob Vosmaer committed
40
      params[:service]
Jacob Vosmaer's avatar
Jacob Vosmaer committed
41
    else
42
      action_name.dasherize
Jacob Vosmaer's avatar
Jacob Vosmaer committed
43 44
    end
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
45

Jacob Vosmaer's avatar
Jacob Vosmaer committed
46
  def render_ok
47
    set_workhorse_internal_api_content_type
48
    render json: Gitlab::Workhorse.git_http_ok(repository, wiki?, user, action_name)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
49
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
50

51 52
  def render_403(exception)
    render plain: exception.message, status: :forbidden
53 54
  end

55 56
  def render_404(exception)
    render plain: exception.message, status: :not_found
Jacob Vosmaer's avatar
Jacob Vosmaer committed
57
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
58

59 60 61 62
  def render_422(exception)
    render plain: exception.message, status: :unprocessable_entity
  end

63
  def access
64 65 66 67
    @access ||= access_klass.new(access_actor, project,
      'http', authentication_abilities: authentication_abilities,
              namespace_path: params[:namespace_id], project_path: project_path,
              redirected_path: redirected_path)
68 69
  end

70 71 72
  def access_actor
    return user if user
    return :ci if ci?
73 74
  end

75 76 77
  def access_check
    # Use the magic string '_any' to indicate we do not know what the
    # changes are. This is also what gitlab-shell does.
78
    access.check(git_command, '_any')
79
    @project ||= access.project
Jacob Vosmaer's avatar
Jacob Vosmaer committed
80
  end
81 82 83 84

  def access_klass
    @access_klass ||= wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess
  end
85

86 87
  def project_path
    @project_path ||= params[:project_id].sub(/\.git$/, '')
88 89
  end

90 91 92
  def log_user_activity
    Users::ActivityService.new(user, 'pull').execute
  end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
93
end