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.

83 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. import subprocess, os, io
  3. from odoo import http
  4. from odoo.tools import config
  5. import werkzeug
  6. from ..http_chunked_fix import http_input_stream
  7. class Main(http.Controller):
  8. @http.route(
  9. [
  10. '/git/<repo>',
  11. '/git/<repo>/<path:path>',
  12. ],
  13. auth='public',
  14. csrf=False
  15. )
  16. def git(self, request, repo, **kw):
  17. auth = request.httprequest.authorization
  18. if auth:
  19. request.session.authenticate(request.session.db, auth.username, auth.password)
  20. if not request.env.uid or request.env.user.login == 'public':
  21. return werkzeug.Response(
  22. headers=[('WWW-Authenticate', 'Basic')],
  23. status=401
  24. )
  25. try:
  26. repository = request.env['galicea_git.repository'].search(
  27. [('system_name', '=', repo)]
  28. )
  29. except AccessError:
  30. return werkzeug.Response(
  31. status=403
  32. )
  33. if not repository.exists():
  34. return werkzeug.Response(
  35. status=404
  36. )
  37. http_environment = request.httprequest.environ
  38. git_env = {
  39. 'REQUEST_METHOD': http_environment['REQUEST_METHOD'],
  40. 'QUERY_STRING': http_environment['QUERY_STRING'],
  41. 'CONTENT_TYPE': request.httprequest.headers.get('Content-Type'),
  42. 'REMOTE_ADDR': http_environment['REMOTE_ADDR'],
  43. 'GIT_PROJECT_ROOT': os.path.join(config['data_dir'], 'git'),
  44. 'GIT_HTTP_EXPORT_ALL': '1',
  45. 'PATH_INFO': http_environment['PATH_INFO'][4:],
  46. 'REMOTE_USER': request.env.user.login
  47. }
  48. command_env = os.environ.copy()
  49. for var in git_env:
  50. command_env[var] = git_env[var]
  51. git = subprocess.Popen(
  52. ['/usr/lib/git-core/git-http-backend'],
  53. stdin=subprocess.PIPE,
  54. stdout=subprocess.PIPE,
  55. stderr=subprocess.PIPE,
  56. env=command_env,
  57. shell=True
  58. )
  59. stdout, stderr = git.communicate(http_input_stream(request).read())
  60. headers_str, body = stdout.split("\r\n\r\n", 2)
  61. http_status_code = 200
  62. headers = []
  63. for header in headers_str.split("\r\n"):
  64. name, value = header.split(': ', 2)
  65. if name == 'Status':
  66. http_code = int(value.split(' ')[0])
  67. else:
  68. headers.append((name, value))
  69. return werkzeug.Response(
  70. body,
  71. status = http_status_code,
  72. headers = headers
  73. )