import os
import sys
+from dulwich.log_utils import default_logging_config
from dulwich.repo import Repo
from dulwich.server import DictBackend
-from dulwich.web import HTTPGitApplication
-from wsgiref.simple_server import make_server
+from dulwich.web import (
+ logger,
+ HTTPGitApplication,
+ )
+from wsgiref.simple_server import (
+ WSGIRequestHandler,
+ make_server,
+ )
+
+
+class HTTPGitRequestHandler(WSGIRequestHandler):
+ """Handler that uses dulwich's logger for logging exceptions."""
+
+ def log_exception(self, exc_info):
+ logger.exception('Exception happened during processing of request',
+ exc_info=exc_info)
+
+ def log_message(self, format, *args):
+ logger.info(format, *args)
+
+ def log_error(self, *args):
+ logger.error(*args)
+
+
+# TODO: allow serving on other addresses/ports via command-line flag
+LISTEN_ADDR=''
+PORT = 8000
+
if __name__ == "__main__":
if len(sys.argv) > 1:
else:
gitdir = os.getcwd()
+ default_logging_config()
backend = DictBackend({"/": Repo(gitdir)})
app = HTTPGitApplication(backend)
- # TODO: allow serving on other ports via command-line flag
- server = make_server('', 8000, app)
+ server = make_server(LISTEN_ADDR, PORT, app,
+ handler_class=HTTPGitRequestHandler)
+ logger.info('Listening for HTTP connections on %s:%d', LISTEN_ADDR, PORT)
server.serve_forever()
from urlparse import parse_qs
except ImportError:
from dulwich.misc import parse_qs
+from dulwich import log_utils
from dulwich.protocol import (
ReceivableProtocol,
)
)
+logger = log_utils.getLogger(__name__)
+
+
# HTTP error strings
HTTP_OK = '200 OK'
HTTP_NOT_FOUND = '404 Not Found'
def get_text_file(req, backend, mat):
req.nocache()
path = _url_to_path(mat.group())
+ logger.info('Sending plain text file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'text/plain')
def get_loose_object(req, backend, mat):
sha = mat.group(1) + mat.group(2)
+ logger.info('Sending loose object %s', sha)
object_store = get_repo(backend, mat).object_store
if not object_store.contains_loose(sha):
yield req.not_found('Object not found')
def get_pack_file(req, backend, mat):
req.cache_forever()
path = _url_to_path(mat.group())
+ logger.info('Sending pack file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'application/x-git-packed-objects')
def get_idx_file(req, backend, mat):
req.cache_forever()
path = _url_to_path(mat.group())
+ logger.info('Sending pack file %s', path)
return send_file(req, get_repo(backend, mat).get_named_file(path),
'application/x-git-packed-objects-toc')
# TODO: select_getanyfile() (see http-backend.c)
req.nocache()
req.respond(HTTP_OK, 'text/plain')
+ logger.info('Emulating dumb info/refs')
repo = get_repo(backend, mat)
refs = repo.get_refs()
for name in sorted(refs.iterkeys()):
def get_info_packs(req, backend, mat):
req.nocache()
req.respond(HTTP_OK, 'text/plain')
+ logger.info('Emulating dumb info/packs')
for pack in get_repo(backend, mat).object_store.packs:
yield 'P pack-%s.pack\n' % pack.name()
def handle_service_request(req, backend, mat):
service = mat.group().lstrip('/')
+ logger.info('Handling service request for %s', service)
handler_cls = req.handlers.get(service, None)
if handler_cls is None:
yield req.forbidden('Unsupported service %s' % service)
def not_found(self, message):
"""Begin a HTTP 404 response and return the text of a message."""
self._cache_headers = []
+ logger.info('Not found: %s', message)
self.respond(HTTP_NOT_FOUND, 'text/plain')
return message
def forbidden(self, message):
"""Begin a HTTP 403 response and return the text of a message."""
self._cache_headers = []
+ logger.info('Forbidden: %s', message)
self.respond(HTTP_FORBIDDEN, 'text/plain')
return message