web.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. # web.py -- WSGI smart-http server
  2. # Copyright (C) 2010 Google, Inc.
  3. # Copyright (C) 2012 Jelmer Vernooij <jelmer@jelmer.uk>
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as published by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """HTTP server for dulwich that implements the git smart HTTP protocol."""
  23. import os
  24. import re
  25. import sys
  26. import time
  27. from collections.abc import Iterator
  28. from io import BytesIO
  29. from typing import BinaryIO, Callable, ClassVar, Optional, cast
  30. from urllib.parse import parse_qs
  31. from wsgiref.simple_server import (
  32. ServerHandler,
  33. WSGIRequestHandler,
  34. WSGIServer,
  35. make_server,
  36. )
  37. from dulwich import log_utils
  38. from .protocol import ReceivableProtocol
  39. from .repo import BaseRepo, NotGitRepository, Repo
  40. from .server import (
  41. DEFAULT_HANDLERS,
  42. Backend,
  43. DictBackend,
  44. generate_info_refs,
  45. generate_objects_info_packs,
  46. )
  47. logger = log_utils.getLogger(__name__)
  48. # HTTP error strings
  49. HTTP_OK = "200 OK"
  50. HTTP_NOT_FOUND = "404 Not Found"
  51. HTTP_FORBIDDEN = "403 Forbidden"
  52. HTTP_ERROR = "500 Internal Server Error"
  53. NO_CACHE_HEADERS = [
  54. ("Expires", "Fri, 01 Jan 1980 00:00:00 GMT"),
  55. ("Pragma", "no-cache"),
  56. ("Cache-Control", "no-cache, max-age=0, must-revalidate"),
  57. ]
  58. def cache_forever_headers(now: Optional[float] = None) -> list[tuple[str, str]]:
  59. """Generate headers for caching forever.
  60. Args:
  61. now: Timestamp to use as base (defaults to current time)
  62. Returns:
  63. List of (header_name, header_value) tuples for caching forever
  64. """
  65. if now is None:
  66. now = time.time()
  67. return [
  68. ("Date", date_time_string(now)),
  69. ("Expires", date_time_string(now + 31536000)),
  70. ("Cache-Control", "public, max-age=31536000"),
  71. ]
  72. def date_time_string(timestamp: Optional[float] = None) -> str:
  73. """Convert a timestamp to an HTTP date string.
  74. Args:
  75. timestamp: Unix timestamp to convert (defaults to current time)
  76. Returns:
  77. HTTP date string in RFC 1123 format
  78. """
  79. # From BaseHTTPRequestHandler.date_time_string in BaseHTTPServer.py in the
  80. # Python 2.6.5 standard library, following modifications:
  81. # - Made a global rather than an instance method.
  82. # - weekdayname and monthname are renamed and locals rather than class
  83. # variables.
  84. # Copyright (c) 2001-2010 Python Software Foundation; All Rights Reserved
  85. weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  86. months = [
  87. None,
  88. "Jan",
  89. "Feb",
  90. "Mar",
  91. "Apr",
  92. "May",
  93. "Jun",
  94. "Jul",
  95. "Aug",
  96. "Sep",
  97. "Oct",
  98. "Nov",
  99. "Dec",
  100. ]
  101. if timestamp is None:
  102. timestamp = time.time()
  103. year, month, day, hh, mm, ss, wd = time.gmtime(timestamp)[:7]
  104. return "%s, %02d %3s %4d %02d:%02d:%02d GMD" % ( # noqa: UP031
  105. weekdays[wd],
  106. day,
  107. months[month],
  108. year,
  109. hh,
  110. mm,
  111. ss,
  112. )
  113. def url_prefix(mat: re.Match[str]) -> str:
  114. """Extract the URL prefix from a regex match.
  115. Args:
  116. mat: A regex match object.
  117. Returns: The URL prefix, defined as the text before the match in the
  118. original string. Normalized to start with one leading slash and end
  119. with zero.
  120. """
  121. return "/" + mat.string[: mat.start()].strip("/")
  122. def get_repo(backend: "Backend", mat: re.Match[str]) -> BaseRepo:
  123. """Get a Repo instance for the given backend and URL regex match."""
  124. return cast(BaseRepo, backend.open_repository(url_prefix(mat)))
  125. def send_file(
  126. req: "HTTPGitRequest", f: Optional[BinaryIO], content_type: str
  127. ) -> Iterator[bytes]:
  128. """Send a file-like object to the request output.
  129. Args:
  130. req: The HTTPGitRequest object to send output to.
  131. f: An open file-like object to send; will be closed.
  132. content_type: The MIME type for the file.
  133. Returns: Iterator over the contents of the file, as chunks.
  134. """
  135. if f is None:
  136. yield req.not_found("File not found")
  137. return
  138. try:
  139. req.respond(HTTP_OK, content_type)
  140. while True:
  141. data = f.read(10240)
  142. if not data:
  143. break
  144. yield data
  145. except OSError:
  146. yield req.error("Error reading file")
  147. finally:
  148. f.close()
  149. def _url_to_path(url: str) -> str:
  150. return url.replace("/", os.path.sep)
  151. def get_text_file(
  152. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  153. ) -> Iterator[bytes]:
  154. """Send a plain text file from the repository.
  155. Args:
  156. req: The HTTP request object
  157. backend: The git backend
  158. mat: The regex match for the requested path
  159. Returns:
  160. Iterator yielding file contents as bytes
  161. """
  162. req.nocache()
  163. path = _url_to_path(mat.group())
  164. logger.info("Sending plain text file %s", path)
  165. return send_file(req, get_repo(backend, mat).get_named_file(path), "text/plain")
  166. def get_loose_object(
  167. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  168. ) -> Iterator[bytes]:
  169. """Send a loose git object.
  170. Args:
  171. req: The HTTP request object
  172. backend: The git backend
  173. mat: The regex match containing object path segments
  174. Returns:
  175. Iterator yielding object contents as bytes
  176. """
  177. sha = (mat.group(1) + mat.group(2)).encode("ascii")
  178. logger.info("Sending loose object %s", sha)
  179. object_store = get_repo(backend, mat).object_store
  180. if not object_store.contains_loose(sha):
  181. yield req.not_found("Object not found")
  182. return
  183. try:
  184. data = object_store[sha].as_legacy_object()
  185. except OSError:
  186. yield req.error("Error reading object")
  187. return
  188. req.cache_forever()
  189. req.respond(HTTP_OK, "application/x-git-loose-object")
  190. yield data
  191. def get_pack_file(
  192. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  193. ) -> Iterator[bytes]:
  194. """Send a git pack file.
  195. Args:
  196. req: The HTTP request object
  197. backend: The git backend
  198. mat: The regex match for the requested pack file
  199. Returns:
  200. Iterator yielding pack file contents as bytes
  201. """
  202. req.cache_forever()
  203. path = _url_to_path(mat.group())
  204. logger.info("Sending pack file %s", path)
  205. return send_file(
  206. req,
  207. get_repo(backend, mat).get_named_file(path),
  208. "application/x-git-packed-objects",
  209. )
  210. def get_idx_file(
  211. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  212. ) -> Iterator[bytes]:
  213. """Send a git pack index file.
  214. Args:
  215. req: The HTTP request object
  216. backend: The git backend
  217. mat: The regex match for the requested index file
  218. Returns:
  219. Iterator yielding index file contents as bytes
  220. """
  221. req.cache_forever()
  222. path = _url_to_path(mat.group())
  223. logger.info("Sending pack file %s", path)
  224. return send_file(
  225. req,
  226. get_repo(backend, mat).get_named_file(path),
  227. "application/x-git-packed-objects-toc",
  228. )
  229. def get_info_refs(
  230. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  231. ) -> Iterator[bytes]:
  232. """Send git info/refs for discovery.
  233. Args:
  234. req: The HTTP request object
  235. backend: The git backend
  236. mat: The regex match for the info/refs request
  237. Returns:
  238. Iterator yielding refs advertisement or info/refs contents
  239. """
  240. params = parse_qs(req.environ["QUERY_STRING"])
  241. service = params.get("service", [None])[0]
  242. try:
  243. repo = get_repo(backend, mat)
  244. except NotGitRepository as e:
  245. yield req.not_found(str(e))
  246. return
  247. if service and not req.dumb:
  248. handler_cls = req.handlers.get(service.encode("ascii"), None)
  249. if handler_cls is None:
  250. yield req.forbidden("Unsupported service")
  251. return
  252. req.nocache()
  253. write = req.respond(HTTP_OK, f"application/x-{service}-advertisement")
  254. proto = ReceivableProtocol(BytesIO().read, write)
  255. handler = handler_cls(
  256. backend,
  257. [url_prefix(mat)],
  258. proto,
  259. stateless_rpc=True,
  260. advertise_refs=True,
  261. )
  262. handler.proto.write_pkt_line(b"# service=" + service.encode("ascii") + b"\n")
  263. handler.proto.write_pkt_line(None)
  264. handler.handle()
  265. else:
  266. # non-smart fallback
  267. # TODO: select_getanyfile() (see http-backend.c)
  268. req.nocache()
  269. req.respond(HTTP_OK, "text/plain")
  270. logger.info("Emulating dumb info/refs")
  271. yield from generate_info_refs(repo)
  272. def get_info_packs(
  273. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  274. ) -> Iterator[bytes]:
  275. """Send git info/packs file listing available packs.
  276. Args:
  277. req: The HTTP request object
  278. backend: The git backend
  279. mat: The regex match for the info/packs request
  280. Returns:
  281. Iterator yielding pack listing as bytes
  282. """
  283. req.nocache()
  284. req.respond(HTTP_OK, "text/plain")
  285. logger.info("Emulating dumb info/packs")
  286. return generate_objects_info_packs(get_repo(backend, mat))
  287. def _chunk_iter(f: BinaryIO) -> Iterator[bytes]:
  288. while True:
  289. line = f.readline()
  290. length = int(line.rstrip(), 16)
  291. chunk = f.read(length + 2)
  292. if length == 0:
  293. break
  294. yield chunk[:-2]
  295. class ChunkReader:
  296. """Reader for chunked transfer encoding streams."""
  297. def __init__(self, f: BinaryIO) -> None:
  298. self._iter = _chunk_iter(f)
  299. self._buffer: list[bytes] = []
  300. def read(self, n: int) -> bytes:
  301. """Read n bytes from the chunked stream.
  302. Args:
  303. n: Number of bytes to read
  304. Returns:
  305. Up to n bytes of data
  306. """
  307. while sum(map(len, self._buffer)) < n:
  308. try:
  309. self._buffer.append(next(self._iter))
  310. except StopIteration:
  311. break
  312. f = b"".join(self._buffer)
  313. ret = f[:n]
  314. self._buffer = [f[n:]]
  315. return ret
  316. class _LengthLimitedFile:
  317. """Wrapper class to limit the length of reads from a file-like object.
  318. This is used to ensure EOF is read from the wsgi.input object once
  319. Content-Length bytes are read. This behavior is required by the WSGI spec
  320. but not implemented in wsgiref as of 2.5.
  321. """
  322. def __init__(self, input: BinaryIO, max_bytes: int) -> None:
  323. self._input = input
  324. self._bytes_avail = max_bytes
  325. def read(self, size: int = -1) -> bytes:
  326. """Read up to size bytes from the limited input.
  327. Args:
  328. size: Maximum number of bytes to read, or -1 for all available
  329. Returns:
  330. Up to size bytes of data
  331. """
  332. if self._bytes_avail <= 0:
  333. return b""
  334. if size == -1 or size > self._bytes_avail:
  335. size = self._bytes_avail
  336. self._bytes_avail -= size
  337. return self._input.read(size)
  338. # TODO: support more methods as necessary
  339. def handle_service_request(
  340. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  341. ) -> Iterator[bytes]:
  342. """Handle a git service request (upload-pack or receive-pack).
  343. Args:
  344. req: The HTTP request object
  345. backend: The git backend
  346. mat: The regex match for the service request
  347. Returns:
  348. Iterator yielding service response as bytes
  349. """
  350. service = mat.group().lstrip("/")
  351. logger.info("Handling service request for %s", service)
  352. handler_cls = req.handlers.get(service.encode("ascii"), None)
  353. if handler_cls is None:
  354. yield req.forbidden("Unsupported service")
  355. return
  356. try:
  357. get_repo(backend, mat)
  358. except NotGitRepository as e:
  359. yield req.not_found(str(e))
  360. return
  361. req.nocache()
  362. write = req.respond(HTTP_OK, f"application/x-{service}-result")
  363. if req.environ.get("HTTP_TRANSFER_ENCODING") == "chunked":
  364. read = ChunkReader(req.environ["wsgi.input"]).read
  365. else:
  366. read = req.environ["wsgi.input"].read
  367. proto = ReceivableProtocol(read, write)
  368. # TODO(jelmer): Find a way to pass in repo, rather than having handler_cls
  369. # reopen.
  370. handler = handler_cls(backend, [url_prefix(mat)], proto, stateless_rpc=True)
  371. handler.handle()
  372. class HTTPGitRequest:
  373. """Class encapsulating the state of a single git HTTP request.
  374. Attributes:
  375. environ: the WSGI environment for the request.
  376. """
  377. def __init__(
  378. self, environ, start_response, dumb: bool = False, handlers=None
  379. ) -> None:
  380. self.environ = environ
  381. self.dumb = dumb
  382. self.handlers = handlers
  383. self._start_response = start_response
  384. self._cache_headers: list[tuple[str, str]] = []
  385. self._headers: list[tuple[str, str]] = []
  386. def add_header(self, name, value) -> None:
  387. """Add a header to the response."""
  388. self._headers.append((name, value))
  389. def respond(
  390. self,
  391. status: str = HTTP_OK,
  392. content_type: Optional[str] = None,
  393. headers: Optional[list[tuple[str, str]]] = None,
  394. ):
  395. """Begin a response with the given status and other headers."""
  396. if headers:
  397. self._headers.extend(headers)
  398. if content_type:
  399. self._headers.append(("Content-Type", content_type))
  400. self._headers.extend(self._cache_headers)
  401. return self._start_response(status, self._headers)
  402. def not_found(self, message: str) -> bytes:
  403. """Begin a HTTP 404 response and return the text of a message."""
  404. self._cache_headers = []
  405. logger.info("Not found: %s", message)
  406. self.respond(HTTP_NOT_FOUND, "text/plain")
  407. return message.encode("ascii")
  408. def forbidden(self, message: str) -> bytes:
  409. """Begin a HTTP 403 response and return the text of a message."""
  410. self._cache_headers = []
  411. logger.info("Forbidden: %s", message)
  412. self.respond(HTTP_FORBIDDEN, "text/plain")
  413. return message.encode("ascii")
  414. def error(self, message: str) -> bytes:
  415. """Begin a HTTP 500 response and return the text of a message."""
  416. self._cache_headers = []
  417. logger.error("Error: %s", message)
  418. self.respond(HTTP_ERROR, "text/plain")
  419. return message.encode("ascii")
  420. def nocache(self) -> None:
  421. """Set the response to never be cached by the client."""
  422. self._cache_headers = NO_CACHE_HEADERS
  423. def cache_forever(self) -> None:
  424. """Set the response to be cached forever by the client."""
  425. self._cache_headers = cache_forever_headers()
  426. class HTTPGitApplication:
  427. """Class encapsulating the state of a git WSGI application.
  428. Attributes:
  429. backend: the Backend object backing this application
  430. """
  431. services: ClassVar[
  432. dict[
  433. tuple[str, re.Pattern],
  434. Callable[[HTTPGitRequest, Backend, re.Match], Iterator[bytes]],
  435. ]
  436. ] = {
  437. ("GET", re.compile("/HEAD$")): get_text_file,
  438. ("GET", re.compile("/info/refs$")): get_info_refs,
  439. ("GET", re.compile("/objects/info/alternates$")): get_text_file,
  440. ("GET", re.compile("/objects/info/http-alternates$")): get_text_file,
  441. ("GET", re.compile("/objects/info/packs$")): get_info_packs,
  442. (
  443. "GET",
  444. re.compile("/objects/([0-9a-f]{2})/([0-9a-f]{38})$"),
  445. ): get_loose_object,
  446. (
  447. "GET",
  448. re.compile("/objects/pack/pack-([0-9a-f]{40})\\.pack$"),
  449. ): get_pack_file,
  450. (
  451. "GET",
  452. re.compile("/objects/pack/pack-([0-9a-f]{40})\\.idx$"),
  453. ): get_idx_file,
  454. ("POST", re.compile("/git-upload-pack$")): handle_service_request,
  455. ("POST", re.compile("/git-receive-pack$")): handle_service_request,
  456. }
  457. def __init__(
  458. self, backend, dumb: bool = False, handlers=None, fallback_app=None
  459. ) -> None:
  460. self.backend = backend
  461. self.dumb = dumb
  462. self.handlers = dict(DEFAULT_HANDLERS)
  463. self.fallback_app = fallback_app
  464. if handlers is not None:
  465. self.handlers.update(handlers)
  466. def __call__(self, environ, start_response):
  467. path = environ["PATH_INFO"]
  468. method = environ["REQUEST_METHOD"]
  469. req = HTTPGitRequest(
  470. environ, start_response, dumb=self.dumb, handlers=self.handlers
  471. )
  472. # environ['QUERY_STRING'] has qs args
  473. handler = None
  474. for smethod, spath in self.services.keys():
  475. if smethod != method:
  476. continue
  477. mat = spath.search(path)
  478. if mat:
  479. handler = self.services[smethod, spath]
  480. break
  481. if handler is None:
  482. if self.fallback_app is not None:
  483. return self.fallback_app(environ, start_response)
  484. else:
  485. return [req.not_found("Sorry, that method is not supported")]
  486. return handler(req, self.backend, mat)
  487. class GunzipFilter:
  488. """WSGI middleware that unzips gzip-encoded requests before
  489. passing on to the underlying application.
  490. """
  491. def __init__(self, application) -> None:
  492. self.app = application
  493. def __call__(self, environ, start_response):
  494. import gzip
  495. if environ.get("HTTP_CONTENT_ENCODING", "") == "gzip":
  496. environ["wsgi.input"] = gzip.GzipFile(
  497. filename=None, fileobj=environ["wsgi.input"], mode="rb"
  498. )
  499. del environ["HTTP_CONTENT_ENCODING"]
  500. if "CONTENT_LENGTH" in environ:
  501. del environ["CONTENT_LENGTH"]
  502. return self.app(environ, start_response)
  503. class LimitedInputFilter:
  504. """WSGI middleware that limits the input length of a request to that
  505. specified in Content-Length.
  506. """
  507. def __init__(self, application) -> None:
  508. self.app = application
  509. def __call__(self, environ, start_response):
  510. # This is not necessary if this app is run from a conforming WSGI
  511. # server. Unfortunately, there's no way to tell that at this point.
  512. # TODO: git may used HTTP/1.1 chunked encoding instead of specifying
  513. # content-length
  514. content_length = environ.get("CONTENT_LENGTH", "")
  515. if content_length:
  516. environ["wsgi.input"] = _LengthLimitedFile(
  517. environ["wsgi.input"], int(content_length)
  518. )
  519. return self.app(environ, start_response)
  520. def make_wsgi_chain(*args, **kwargs):
  521. """Factory function to create an instance of HTTPGitApplication,
  522. correctly wrapped with needed middleware.
  523. """
  524. app = HTTPGitApplication(*args, **kwargs)
  525. wrapped_app = LimitedInputFilter(GunzipFilter(app))
  526. return wrapped_app
  527. class ServerHandlerLogger(ServerHandler):
  528. """ServerHandler that uses dulwich's logger for logging exceptions."""
  529. def log_exception(self, exc_info) -> None:
  530. """Log an exception using dulwich's logger.
  531. Args:
  532. exc_info: Exception information tuple
  533. """
  534. logger.exception(
  535. "Exception happened during processing of request",
  536. exc_info=exc_info,
  537. )
  538. def log_message(self, format, *args) -> None:
  539. """Log a message using dulwich's logger.
  540. Args:
  541. format: Format string for the message
  542. *args: Arguments for the format string
  543. """
  544. logger.info(format, *args)
  545. def log_error(self, *args) -> None:
  546. """Log an error using dulwich's logger.
  547. Args:
  548. *args: Error message components
  549. """
  550. logger.error(*args)
  551. class WSGIRequestHandlerLogger(WSGIRequestHandler):
  552. """WSGIRequestHandler that uses dulwich's logger for logging exceptions."""
  553. def log_exception(self, exc_info) -> None:
  554. """Log an exception using dulwich's logger.
  555. Args:
  556. exc_info: Exception information tuple
  557. """
  558. logger.exception(
  559. "Exception happened during processing of request",
  560. exc_info=exc_info,
  561. )
  562. def log_message(self, format, *args) -> None:
  563. """Log a message using dulwich's logger.
  564. Args:
  565. format: Format string for the message
  566. *args: Arguments for the format string
  567. """
  568. logger.info(format, *args)
  569. def log_error(self, *args) -> None:
  570. """Log an error using dulwich's logger.
  571. Args:
  572. *args: Error message components
  573. """
  574. logger.error(*args)
  575. def handle(self) -> None:
  576. """Handle a single HTTP request."""
  577. self.raw_requestline = self.rfile.readline()
  578. if not self.parse_request(): # An error code has been sent, just exit
  579. return
  580. handler = ServerHandlerLogger(
  581. self.rfile,
  582. self.wfile, # type: ignore
  583. self.get_stderr(),
  584. self.get_environ(),
  585. )
  586. handler.request_handler = self # type: ignore # backpointer for logging
  587. handler.run(self.server.get_app()) # type: ignore
  588. class WSGIServerLogger(WSGIServer):
  589. """WSGIServer that uses dulwich's logger for error handling."""
  590. def handle_error(self, request, client_address) -> None:
  591. """Handle an error."""
  592. logger.exception(
  593. f"Exception happened during processing of request from {client_address!s}"
  594. )
  595. def main(argv=sys.argv) -> None:
  596. """Entry point for starting an HTTP git server."""
  597. import optparse
  598. parser = optparse.OptionParser()
  599. parser.add_option(
  600. "-l",
  601. "--listen_address",
  602. dest="listen_address",
  603. default="localhost",
  604. help="Binding IP address.",
  605. )
  606. parser.add_option(
  607. "-p",
  608. "--port",
  609. dest="port",
  610. type=int,
  611. default=8000,
  612. help="Port to listen on.",
  613. )
  614. options, args = parser.parse_args(argv)
  615. if len(args) > 1:
  616. gitdir = args[1]
  617. else:
  618. gitdir = os.getcwd()
  619. log_utils.default_logging_config()
  620. backend = DictBackend({"/": Repo(gitdir)})
  621. app = make_wsgi_chain(backend)
  622. server = make_server(
  623. options.listen_address,
  624. options.port,
  625. app,
  626. handler_class=WSGIRequestHandlerLogger,
  627. server_class=WSGIServerLogger,
  628. )
  629. logger.info(
  630. "Listening for HTTP connections on %s:%d",
  631. options.listen_address,
  632. options.port,
  633. )
  634. server.serve_forever()
  635. if __name__ == "__main__":
  636. main()