Esempio n. 1
0
def git_export(request, project, subproject, path):
    """Git HTTP server view.

    Wrapper around git-http-backend to provide Git repositories export over
    HTTP. Performs permission checks and hands over execution to the wrapper.
    """
    # Probably browser access
    if path == '':
        return redirect('subproject',
                        project=project,
                        subproject=subproject,
                        permanent=False)

    # HTTP authentication
    auth = request.META.get('HTTP_AUTHORIZATION', b'')

    if auth and not authenticate(request, auth):
        return response_authenticate()

    # Permissions
    try:
        obj = get_subproject(request, project, subproject)
    except Http404:
        if not request.user.is_authenticated():
            return response_authenticate()
        raise
    if not can_access_vcs(request.user, obj.project):
        raise PermissionDenied('No VCS permissions')

    return run_git_http(request, obj, path)
Esempio n. 2
0
def git_export(request, project, subproject, path):
    """Git HTTP server view.

    Wrapper around git-http-backend to provide Git repositories export over
    HTTP. Performs permission checks and hands over execution to the wrapper.
    """
    # Probably browser access
    if path == '':
        return redirect(
            'subproject',
            project=project,
            subproject=subproject,
            permanent=False
        )

    # HTTP authentication
    auth = request.META.get('HTTP_AUTHORIZATION', b'')

    if auth and not authenticate(request, auth):
        return response_authenticate()

    # Permissions
    obj = get_subproject(request, project, subproject)
    if not can_access_vcs(request.user, obj.project):
        raise PermissionDenied('No VCS permissions')

    return run_git_http(request, obj, path)
Esempio n. 3
0
def git_export(request, project, subproject, path):
    """
    Wrapper around git-http-backend to provide Git
    repositories export over HTTP.
    """
    # Probably browser access
    if path == '':
        return redirect('subproject',
                        project=project,
                        subproject=subproject,
                        permanent=False)

    # HTTP authentication
    auth = request.META.get('HTTP_AUTHORIZATION', b'')

    if auth and not authenticate(request, auth):
        return response_authenticate()

    # Permissions
    try:
        obj = get_subproject(request, project, subproject)
        if not can_access_vcs(request.user, obj.project):
            raise PermissionDenied('No VCS permissions')
    except PermissionDenied:
        if not request.user.is_authenticated:
            return response_authenticate()
        raise

    # Find Git HTTP backend
    git_http_backend = find_git_http_backend()
    if git_http_backend is None:
        return HttpResponseServerError('git-http-backend not found')

    # Invoke Git HTTP backend
    process = subprocess.Popen(
        [git_http_backend],
        env={
            'REQUEST_METHOD': request.method,
            'PATH_TRANSLATED': os.path.join(obj.get_path(), path),
            'GIT_HTTP_EXPORT_ALL': '1',
            'CONTENT_TYPE': request.META.get('CONTENT_TYPE', ''),
            'QUERY_STRING': request.META.get('QUERY_STRING', ''),
            'HTTP_CONTENT_ENCODING': request.META.get('HTTP_CONTENT_ENCODING',
                                                      ''),
        },
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    output, output_err = process.communicate(request.body)
    retcode = process.poll()

    # Log error
    if output_err:
        obj.log_error('git: {0}'.format(force_text(output_err)))

    # Handle failure
    if retcode:
        return HttpResponseServerError(output_err)

    headers, content = output.split(b'\r\n\r\n', 1)
    message = message_from_string(headers.decode('utf-8'))

    # Handle status in response
    if 'status' in message:
        return HttpResponse(status=int(message['status'].split()[0]))

    # Send content
    response = HttpResponse(content_type=message['content-type'])
    response.write(content)
    return response