web.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # web.py -- WSGI smart-http server
  2. # Copryight (C) 2010 Google, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """HTTP server for dulwich that implements the git smart HTTP protocol."""
  19. from cStringIO import StringIO
  20. import cgi
  21. import os
  22. import re
  23. import time
  24. from dulwich.objects import (
  25. Tag,
  26. )
  27. from dulwich.repo import (
  28. Repo,
  29. )
  30. from dulwich.server import (
  31. GitBackend,
  32. ReceivePackHandler,
  33. UploadPackHandler,
  34. )
  35. HTTP_OK = '200 OK'
  36. HTTP_NOT_FOUND = '404 Not Found'
  37. HTTP_FORBIDDEN = '403 Forbidden'
  38. def date_time_string(self, timestamp=None):
  39. # Based on BaseHTTPServer.py in python2.5
  40. weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  41. months = [None,
  42. 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  43. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  44. if timestamp is None:
  45. timestamp = time.time()
  46. year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
  47. return '%s, %02d %3s %4d %02d:%02d:%02d GMD' % (
  48. weekdays[wd], day, months[month], year, hh, mm, ss)
  49. def send_file(req, f, content_type):
  50. """Send a file-like object to the request output.
  51. :param req: The HTTPGitRequest object to send output to.
  52. :param f: An open file-like object to send; will be closed.
  53. :param content_type: The MIME type for the file.
  54. :yield: The contents of the file.
  55. """
  56. if f is None:
  57. yield req.not_found('File not found')
  58. return
  59. try:
  60. try:
  61. req.respond(HTTP_OK, content_type)
  62. while True:
  63. data = f.read(10240)
  64. if not data:
  65. break
  66. yield data
  67. except IOError:
  68. yield req.not_found('Error reading file')
  69. finally:
  70. f.close()
  71. def get_text_file(req, backend, mat):
  72. req.nocache()
  73. return send_file(req, backend.repo.get_named_file(mat.group()),
  74. 'text/plain')
  75. def get_loose_object(req, backend, mat):
  76. sha = mat.group(1) + mat.group(2)
  77. object_store = backend.object_store
  78. if not object_store.contains_loose(sha):
  79. yield req.not_found('Object not found')
  80. return
  81. try:
  82. data = object_store[sha].as_legacy_object()
  83. except IOError:
  84. yield req.not_found('Error reading object')
  85. req.cache_forever()
  86. req.respond(HTTP_OK, 'application/x-git-loose-object')
  87. yield data
  88. def get_pack_file(req, backend, mat):
  89. req.cache_forever()
  90. return send_file(req, backend.repo.get_named_file(mat.group()),
  91. 'application/x-git-packed-objects', False)
  92. def get_idx_file(req, backend, mat):
  93. req.cache_forever()
  94. return send_file(req, backend.repo.get_named_file(mat.group()),
  95. 'application/x-git-packed-objects-toc', False)
  96. services = {'git-upload-pack': UploadPackHandler,
  97. 'git-receive-pack': ReceivePackHandler}
  98. def get_info_refs(req, backend, mat):
  99. params = cgi.parse_qs(req.environ['QUERY_STRING'])
  100. service = params.get('service', [None])[0]
  101. if service:
  102. handler_cls = services.get(service, None)
  103. if handler_cls is None:
  104. yield req.forbidden('Unsupported service %s' % service)
  105. req.nocache()
  106. req.respond(HTTP_OK, 'application/x-%s-advertisement' % service)
  107. output = StringIO()
  108. dummy_input = StringIO() # GET request, handler doesn't need to read
  109. handler = handler_cls(backend, dummy_input.read, output.write,
  110. stateless_rpc=True, advertise_refs=True)
  111. handler.proto.write_pkt_line('# service=%s\n' % service)
  112. handler.proto.write_pkt_line(None)
  113. handler.handle()
  114. yield output.getvalue()
  115. else:
  116. # non-smart fallback
  117. # TODO: select_getanyfile() (see http-backend.c)
  118. req.nocache()
  119. req.respond(HTTP_OK, 'text/plain')
  120. refs = backend.get_refs()
  121. for name in sorted(refs.iterkeys()):
  122. # get_refs() includes HEAD as a special case, but we don't want to
  123. # advertise it
  124. if name == 'HEAD':
  125. continue
  126. sha = refs[name]
  127. o = backend.repo[sha]
  128. if not o:
  129. continue
  130. yield '%s\t%s\n' % (sha, name)
  131. if isinstance(o, Tag):
  132. while isinstance(o, Tag):
  133. _, sha = o.object
  134. o = backend.repo[sha]
  135. if not o:
  136. continue
  137. yield '%s\t%s^{}\n' % (o.sha(), name)
  138. def get_info_packs(req, backend, mat):
  139. req.nocache()
  140. req.respond(HTTP_OK, 'text/plain')
  141. for pack in backend.object_store.packs:
  142. yield 'P pack-%s.pack\n' % pack.name()
  143. def handle_service_request(req, backend, mat):
  144. service = mat.group().lstrip('/')
  145. handler_cls = services.get(service, None)
  146. if handler_cls is None:
  147. yield req.forbidden('Unsupported service %s' % service)
  148. req.nocache()
  149. req.respond(HTTP_OK, 'application/x-%s-response' % service)
  150. output = StringIO()
  151. input = req.environ['wsgi.input']
  152. handler = handler_cls(backend, input.read, output.write, stateless_rpc=True)
  153. handler.handle()
  154. yield output.getvalue()
  155. class HTTPGitRequest(object):
  156. """Class encapsulating the state of a single git HTTP request.
  157. :ivar environ: the WSGI environment for the request.
  158. """
  159. def __init__(self, environ, start_response):
  160. self.environ = environ
  161. self._start_response = start_response
  162. self._cache_headers = []
  163. self._headers = []
  164. def add_header(self, name, value):
  165. """Add a header to the response."""
  166. self._headers.append((name, value))
  167. def respond(self, status=HTTP_OK, content_type=None, headers=None):
  168. """Begin a response with the given status and other headers."""
  169. if headers:
  170. self._headers.extend(headers)
  171. if content_type:
  172. self._headers.append(('Content-Type', content_type))
  173. self._headers.extend(self._cache_headers)
  174. self._start_response(status, self._headers)
  175. def not_found(self, message):
  176. """Begin a HTTP 404 response and return the text of a message."""
  177. self._cache_headers = []
  178. self.respond(HTTP_NOT_FOUND, 'text/plain')
  179. return message
  180. def forbidden(self, message):
  181. """Begin a HTTP 403 response and return the text of a message."""
  182. self._cache_headers = []
  183. self.respond(HTTP_FORBIDDEN, 'text/plain')
  184. return message
  185. def nocache(self):
  186. """Set the response to never be cached by the client."""
  187. self._cache_headers = [
  188. ('Expires', 'Fri, 01 Jan 1980 00:00:00 GMT'),
  189. ('Pragma', 'no-cache'),
  190. ('Cache-Control', 'no-cache, max-age=0, must-revalidate'),
  191. ]
  192. def cache_forever(self):
  193. """Set the response to be cached forever by the client."""
  194. now = time.time()
  195. self._cache_headers = [
  196. ('Date', date_time_string(now)),
  197. ('Expires', date_time_string(now + 31536000)),
  198. ('Cache-Control', 'public, max-age=31536000'),
  199. ]
  200. class HTTPGitApplication(object):
  201. """Class encapsulating the state of a git WSGI application.
  202. :ivar backend: the Backend object backing this application
  203. """
  204. services = {
  205. ('GET', re.compile('/HEAD$')): get_text_file,
  206. ('GET', re.compile('/info/refs$')): get_info_refs,
  207. ('GET', re.compile('/objects/info/alternates$')): get_text_file,
  208. ('GET', re.compile('/objects/info/http-alternates$')): get_text_file,
  209. ('GET', re.compile('/objects/info/packs$')): get_info_packs,
  210. ('GET', re.compile('/objects/([0-9a-f]{2})/([0-9a-f]{38})$')): get_loose_object,
  211. ('GET', re.compile('/objects/pack/pack-([0-9a-f]{40})\\.pack$')): get_pack_file,
  212. ('GET', re.compile('/objects/pack/pack-([0-9a-f]{40})\\.idx$')): get_idx_file,
  213. ('POST', re.compile('/git-upload-pack$')): handle_service_request,
  214. ('POST', re.compile('/git-receive-pack$')): handle_service_request,
  215. }
  216. def __init__(self, backend):
  217. self.backend = backend
  218. def __call__(self, environ, start_response):
  219. path = environ['PATH_INFO']
  220. method = environ['REQUEST_METHOD']
  221. req = HTTPGitRequest(environ, start_response)
  222. # environ['QUERY_STRING'] has qs args
  223. handler = None
  224. for smethod, spath in self.services.iterkeys():
  225. if smethod != method:
  226. continue
  227. mat = spath.search(path)
  228. if mat:
  229. handler = self.services[smethod, spath]
  230. break
  231. if handler is None:
  232. return req.not_found('Sorry, that method is not supported')
  233. return handler(req, self.backend, mat)