web.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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. """Initialize ChunkReader.
  299. Args:
  300. f: Binary file-like object to read from
  301. """
  302. self._iter = _chunk_iter(f)
  303. self._buffer: list[bytes] = []
  304. def read(self, n: int) -> bytes:
  305. """Read n bytes from the chunked stream.
  306. Args:
  307. n: Number of bytes to read
  308. Returns:
  309. Up to n bytes of data
  310. """
  311. while sum(map(len, self._buffer)) < n:
  312. try:
  313. self._buffer.append(next(self._iter))
  314. except StopIteration:
  315. break
  316. f = b"".join(self._buffer)
  317. ret = f[:n]
  318. self._buffer = [f[n:]]
  319. return ret
  320. class _LengthLimitedFile:
  321. """Wrapper class to limit the length of reads from a file-like object.
  322. This is used to ensure EOF is read from the wsgi.input object once
  323. Content-Length bytes are read. This behavior is required by the WSGI spec
  324. but not implemented in wsgiref as of 2.5.
  325. """
  326. def __init__(self, input: BinaryIO, max_bytes: int) -> None:
  327. self._input = input
  328. self._bytes_avail = max_bytes
  329. def read(self, size: int = -1) -> bytes:
  330. """Read up to size bytes from the limited input.
  331. Args:
  332. size: Maximum number of bytes to read, or -1 for all available
  333. Returns:
  334. Up to size bytes of data
  335. """
  336. if self._bytes_avail <= 0:
  337. return b""
  338. if size == -1 or size > self._bytes_avail:
  339. size = self._bytes_avail
  340. self._bytes_avail -= size
  341. return self._input.read(size)
  342. # TODO: support more methods as necessary
  343. def handle_service_request(
  344. req: "HTTPGitRequest", backend: "Backend", mat: re.Match[str]
  345. ) -> Iterator[bytes]:
  346. """Handle a git service request (upload-pack or receive-pack).
  347. Args:
  348. req: The HTTP request object
  349. backend: The git backend
  350. mat: The regex match for the service request
  351. Returns:
  352. Iterator yielding service response as bytes
  353. """
  354. service = mat.group().lstrip("/")
  355. logger.info("Handling service request for %s", service)
  356. handler_cls = req.handlers.get(service.encode("ascii"), None)
  357. if handler_cls is None:
  358. yield req.forbidden("Unsupported service")
  359. return
  360. try:
  361. get_repo(backend, mat)
  362. except NotGitRepository as e:
  363. yield req.not_found(str(e))
  364. return
  365. req.nocache()
  366. write = req.respond(HTTP_OK, f"application/x-{service}-result")
  367. if req.environ.get("HTTP_TRANSFER_ENCODING") == "chunked":
  368. read = ChunkReader(req.environ["wsgi.input"]).read
  369. else:
  370. read = req.environ["wsgi.input"].read
  371. proto = ReceivableProtocol(read, write)
  372. # TODO(jelmer): Find a way to pass in repo, rather than having handler_cls
  373. # reopen.
  374. handler = handler_cls(backend, [url_prefix(mat)], proto, stateless_rpc=True)
  375. handler.handle()
  376. class HTTPGitRequest:
  377. """Class encapsulating the state of a single git HTTP request.
  378. Attributes:
  379. environ: the WSGI environment for the request.
  380. """
  381. def __init__(
  382. self, environ, start_response, dumb: bool = False, handlers=None
  383. ) -> None:
  384. """Initialize HTTPGitRequest.
  385. Args:
  386. environ: WSGI environment dictionary
  387. start_response: WSGI start_response callable
  388. dumb: Whether to use dumb HTTP protocol
  389. handlers: Optional handler overrides
  390. """
  391. self.environ = environ
  392. self.dumb = dumb
  393. self.handlers = handlers
  394. self._start_response = start_response
  395. self._cache_headers: list[tuple[str, str]] = []
  396. self._headers: list[tuple[str, str]] = []
  397. def add_header(self, name, value) -> None:
  398. """Add a header to the response."""
  399. self._headers.append((name, value))
  400. def respond(
  401. self,
  402. status: str = HTTP_OK,
  403. content_type: Optional[str] = None,
  404. headers: Optional[list[tuple[str, str]]] = None,
  405. ):
  406. """Begin a response with the given status and other headers."""
  407. if headers:
  408. self._headers.extend(headers)
  409. if content_type:
  410. self._headers.append(("Content-Type", content_type))
  411. self._headers.extend(self._cache_headers)
  412. return self._start_response(status, self._headers)
  413. def not_found(self, message: str) -> bytes:
  414. """Begin a HTTP 404 response and return the text of a message."""
  415. self._cache_headers = []
  416. logger.info("Not found: %s", message)
  417. self.respond(HTTP_NOT_FOUND, "text/plain")
  418. return message.encode("ascii")
  419. def forbidden(self, message: str) -> bytes:
  420. """Begin a HTTP 403 response and return the text of a message."""
  421. self._cache_headers = []
  422. logger.info("Forbidden: %s", message)
  423. self.respond(HTTP_FORBIDDEN, "text/plain")
  424. return message.encode("ascii")
  425. def error(self, message: str) -> bytes:
  426. """Begin a HTTP 500 response and return the text of a message."""
  427. self._cache_headers = []
  428. logger.error("Error: %s", message)
  429. self.respond(HTTP_ERROR, "text/plain")
  430. return message.encode("ascii")
  431. def nocache(self) -> None:
  432. """Set the response to never be cached by the client."""
  433. self._cache_headers = NO_CACHE_HEADERS
  434. def cache_forever(self) -> None:
  435. """Set the response to be cached forever by the client."""
  436. self._cache_headers = cache_forever_headers()
  437. class HTTPGitApplication:
  438. """Class encapsulating the state of a git WSGI application.
  439. Attributes:
  440. backend: the Backend object backing this application
  441. """
  442. services: ClassVar[
  443. dict[
  444. tuple[str, re.Pattern],
  445. Callable[[HTTPGitRequest, Backend, re.Match], Iterator[bytes]],
  446. ]
  447. ] = {
  448. ("GET", re.compile("/HEAD$")): get_text_file,
  449. ("GET", re.compile("/info/refs$")): get_info_refs,
  450. ("GET", re.compile("/objects/info/alternates$")): get_text_file,
  451. ("GET", re.compile("/objects/info/http-alternates$")): get_text_file,
  452. ("GET", re.compile("/objects/info/packs$")): get_info_packs,
  453. (
  454. "GET",
  455. re.compile("/objects/([0-9a-f]{2})/([0-9a-f]{38})$"),
  456. ): get_loose_object,
  457. (
  458. "GET",
  459. re.compile("/objects/pack/pack-([0-9a-f]{40})\\.pack$"),
  460. ): get_pack_file,
  461. (
  462. "GET",
  463. re.compile("/objects/pack/pack-([0-9a-f]{40})\\.idx$"),
  464. ): get_idx_file,
  465. ("POST", re.compile("/git-upload-pack$")): handle_service_request,
  466. ("POST", re.compile("/git-receive-pack$")): handle_service_request,
  467. }
  468. def __init__(
  469. self, backend, dumb: bool = False, handlers=None, fallback_app=None
  470. ) -> None:
  471. """Initialize HTTPGitApplication.
  472. Args:
  473. backend: Backend object for git operations
  474. dumb: Whether to use dumb HTTP protocol
  475. handlers: Optional handler overrides
  476. fallback_app: Optional fallback WSGI application
  477. """
  478. self.backend = backend
  479. self.dumb = dumb
  480. self.handlers = dict(DEFAULT_HANDLERS)
  481. self.fallback_app = fallback_app
  482. if handlers is not None:
  483. self.handlers.update(handlers)
  484. def __call__(self, environ, start_response):
  485. path = environ["PATH_INFO"]
  486. method = environ["REQUEST_METHOD"]
  487. req = HTTPGitRequest(
  488. environ, start_response, dumb=self.dumb, handlers=self.handlers
  489. )
  490. # environ['QUERY_STRING'] has qs args
  491. handler = None
  492. for smethod, spath in self.services.keys():
  493. if smethod != method:
  494. continue
  495. mat = spath.search(path)
  496. if mat:
  497. handler = self.services[smethod, spath]
  498. break
  499. if handler is None:
  500. if self.fallback_app is not None:
  501. return self.fallback_app(environ, start_response)
  502. else:
  503. return [req.not_found("Sorry, that method is not supported")]
  504. return handler(req, self.backend, mat)
  505. class GunzipFilter:
  506. """WSGI middleware that unzips gzip-encoded requests before passing on to the underlying application.
  507. """
  508. def __init__(self, application) -> None:
  509. self.app = application
  510. def __call__(self, environ, start_response):
  511. import gzip
  512. if environ.get("HTTP_CONTENT_ENCODING", "") == "gzip":
  513. environ["wsgi.input"] = gzip.GzipFile(
  514. filename=None, fileobj=environ["wsgi.input"], mode="rb"
  515. )
  516. del environ["HTTP_CONTENT_ENCODING"]
  517. if "CONTENT_LENGTH" in environ:
  518. del environ["CONTENT_LENGTH"]
  519. return self.app(environ, start_response)
  520. class LimitedInputFilter:
  521. """WSGI middleware that limits the input length of a request to that specified in Content-Length.
  522. """
  523. def __init__(self, application) -> None:
  524. self.app = application
  525. def __call__(self, environ, start_response):
  526. # This is not necessary if this app is run from a conforming WSGI
  527. # server. Unfortunately, there's no way to tell that at this point.
  528. # TODO: git may used HTTP/1.1 chunked encoding instead of specifying
  529. # content-length
  530. content_length = environ.get("CONTENT_LENGTH", "")
  531. if content_length:
  532. environ["wsgi.input"] = _LengthLimitedFile(
  533. environ["wsgi.input"], int(content_length)
  534. )
  535. return self.app(environ, start_response)
  536. def make_wsgi_chain(*args, **kwargs):
  537. """Factory function to create an instance of HTTPGitApplication, correctly wrapped with needed middleware.
  538. """
  539. app = HTTPGitApplication(*args, **kwargs)
  540. wrapped_app = LimitedInputFilter(GunzipFilter(app))
  541. return wrapped_app
  542. class ServerHandlerLogger(ServerHandler):
  543. """ServerHandler that uses dulwich's logger for logging exceptions."""
  544. def log_exception(self, exc_info) -> None:
  545. """Log an exception using dulwich's logger.
  546. Args:
  547. exc_info: Exception information tuple
  548. """
  549. logger.exception(
  550. "Exception happened during processing of request",
  551. exc_info=exc_info,
  552. )
  553. def log_message(self, format, *args) -> None:
  554. """Log a message using dulwich's logger.
  555. Args:
  556. format: Format string for the message
  557. *args: Arguments for the format string
  558. """
  559. logger.info(format, *args)
  560. def log_error(self, *args) -> None:
  561. """Log an error using dulwich's logger.
  562. Args:
  563. *args: Error message components
  564. """
  565. logger.error(*args)
  566. class WSGIRequestHandlerLogger(WSGIRequestHandler):
  567. """WSGIRequestHandler that uses dulwich's logger for logging exceptions."""
  568. def log_exception(self, exc_info) -> None:
  569. """Log an exception using dulwich's logger.
  570. Args:
  571. exc_info: Exception information tuple
  572. """
  573. logger.exception(
  574. "Exception happened during processing of request",
  575. exc_info=exc_info,
  576. )
  577. def log_message(self, format, *args) -> None:
  578. """Log a message using dulwich's logger.
  579. Args:
  580. format: Format string for the message
  581. *args: Arguments for the format string
  582. """
  583. logger.info(format, *args)
  584. def log_error(self, *args) -> None:
  585. """Log an error using dulwich's logger.
  586. Args:
  587. *args: Error message components
  588. """
  589. logger.error(*args)
  590. def handle(self) -> None:
  591. """Handle a single HTTP request."""
  592. self.raw_requestline = self.rfile.readline()
  593. if not self.parse_request(): # An error code has been sent, just exit
  594. return
  595. handler = ServerHandlerLogger(
  596. self.rfile,
  597. self.wfile, # type: ignore
  598. self.get_stderr(),
  599. self.get_environ(),
  600. )
  601. handler.request_handler = self # type: ignore # backpointer for logging
  602. handler.run(self.server.get_app()) # type: ignore
  603. class WSGIServerLogger(WSGIServer):
  604. """WSGIServer that uses dulwich's logger for error handling."""
  605. def handle_error(self, request, client_address) -> None:
  606. """Handle an error."""
  607. logger.exception(
  608. f"Exception happened during processing of request from {client_address!s}"
  609. )
  610. def main(argv=sys.argv) -> None:
  611. """Entry point for starting an HTTP git server."""
  612. import optparse
  613. parser = optparse.OptionParser()
  614. parser.add_option(
  615. "-l",
  616. "--listen_address",
  617. dest="listen_address",
  618. default="localhost",
  619. help="Binding IP address.",
  620. )
  621. parser.add_option(
  622. "-p",
  623. "--port",
  624. dest="port",
  625. type=int,
  626. default=8000,
  627. help="Port to listen on.",
  628. )
  629. options, args = parser.parse_args(argv)
  630. if len(args) > 1:
  631. gitdir = args[1]
  632. else:
  633. gitdir = os.getcwd()
  634. log_utils.default_logging_config()
  635. backend = DictBackend({"/": Repo(gitdir)})
  636. app = make_wsgi_chain(backend)
  637. server = make_server(
  638. options.listen_address,
  639. options.port,
  640. app,
  641. handler_class=WSGIRequestHandlerLogger,
  642. server_class=WSGIServerLogger,
  643. )
  644. logger.info(
  645. "Listening for HTTP connections on %s:%d",
  646. options.listen_address,
  647. options.port,
  648. )
  649. server.serve_forever()
  650. if __name__ == "__main__":
  651. main()