2
0

test_client.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. # test_client.py -- Compatibility tests for git client.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Compatibility tests between the Dulwich client and the cgit server."""
  22. import copy
  23. import http.server
  24. import os
  25. import select
  26. import signal
  27. import stat
  28. import subprocess
  29. import sys
  30. import tarfile
  31. import tempfile
  32. import threading
  33. from contextlib import suppress
  34. from io import BytesIO
  35. from typing import NoReturn
  36. from unittest.mock import patch
  37. from urllib.parse import unquote
  38. from dulwich import client, file, index, objects, protocol, repo
  39. from dulwich.porcelain import tag_create
  40. from .. import SkipTest, expectedFailure
  41. from .utils import (
  42. _DEFAULT_GIT,
  43. CompatTestCase,
  44. check_for_daemon,
  45. import_repo_to_dir,
  46. rmtree_ro,
  47. run_git_or_fail,
  48. )
  49. if sys.platform == "win32":
  50. import ctypes
  51. class DulwichClientTestBase:
  52. """Tests for client/server compatibility."""
  53. def setUp(self) -> None:
  54. self.gitroot = os.path.dirname(
  55. import_repo_to_dir("server_new.export").rstrip(os.sep)
  56. )
  57. self.dest = os.path.join(self.gitroot, "dest")
  58. file.ensure_dir_exists(self.dest)
  59. run_git_or_fail(["init", "--quiet", "--bare"], cwd=self.dest)
  60. def tearDown(self) -> None:
  61. rmtree_ro(self.gitroot)
  62. def assertDestEqualsSrc(self) -> None:
  63. repo_dir = os.path.join(self.gitroot, "server_new.export")
  64. dest_repo_dir = os.path.join(self.gitroot, "dest")
  65. with repo.Repo(repo_dir) as src:
  66. with repo.Repo(dest_repo_dir) as dest:
  67. self.assertReposEqual(src, dest)
  68. def _client(self) -> NoReturn:
  69. raise NotImplementedError
  70. def _build_path(self) -> NoReturn:
  71. raise NotImplementedError
  72. def _do_send_pack(self) -> None:
  73. c = self._client()
  74. srcpath = os.path.join(self.gitroot, "server_new.export")
  75. with repo.Repo(srcpath) as src:
  76. sendrefs = dict(src.get_refs())
  77. del sendrefs[b"HEAD"]
  78. c.send_pack(
  79. self._build_path("/dest"),
  80. lambda _: sendrefs,
  81. src.generate_pack_data,
  82. )
  83. def test_send_pack(self) -> None:
  84. self._do_send_pack()
  85. self.assertDestEqualsSrc()
  86. def test_send_pack_nothing_to_send(self) -> None:
  87. self._do_send_pack()
  88. self.assertDestEqualsSrc()
  89. # nothing to send, but shouldn't raise either.
  90. self._do_send_pack()
  91. @staticmethod
  92. def _add_file(repo, tree_id, filename, contents):
  93. tree = repo[tree_id]
  94. blob = objects.Blob()
  95. blob.data = contents.encode("utf-8")
  96. repo.object_store.add_object(blob)
  97. tree.add(filename.encode("utf-8"), stat.S_IFREG | 0o644, blob.id)
  98. repo.object_store.add_object(tree)
  99. return tree.id
  100. def test_send_pack_from_shallow_clone(self) -> None:
  101. c = self._client()
  102. server_new_path = os.path.join(self.gitroot, "server_new.export")
  103. run_git_or_fail(["config", "http.uploadpack", "true"], cwd=server_new_path)
  104. run_git_or_fail(["config", "http.receivepack", "true"], cwd=server_new_path)
  105. remote_path = self._build_path("/server_new.export")
  106. with repo.Repo(self.dest) as local:
  107. result = c.fetch(remote_path, local, depth=1)
  108. for r in result.refs.items():
  109. local.refs.set_if_equals(r[0], None, r[1])
  110. tree_id = local[local.head()].tree
  111. for filename, contents in [
  112. ("bar", "bar contents"),
  113. ("zop", "zop contents"),
  114. ]:
  115. tree_id = self._add_file(local, tree_id, filename, contents)
  116. commit_id = local.do_commit(
  117. message=b"add " + filename.encode("utf-8"),
  118. committer=b"Joe Example <joe@example.com>",
  119. tree=tree_id,
  120. )
  121. sendrefs = dict(local.get_refs())
  122. del sendrefs[b"HEAD"]
  123. c.send_pack(remote_path, lambda _: sendrefs, local.generate_pack_data)
  124. with repo.Repo(server_new_path) as remote:
  125. self.assertEqual(remote.head(), commit_id)
  126. def test_send_without_report_status(self) -> None:
  127. c = self._client()
  128. c._send_capabilities.remove(b"report-status")
  129. srcpath = os.path.join(self.gitroot, "server_new.export")
  130. with repo.Repo(srcpath) as src:
  131. sendrefs = dict(src.get_refs())
  132. del sendrefs[b"HEAD"]
  133. c.send_pack(
  134. self._build_path("/dest"),
  135. lambda _: sendrefs,
  136. src.generate_pack_data,
  137. )
  138. self.assertDestEqualsSrc()
  139. def make_dummy_commit(self, dest):
  140. b = objects.Blob.from_string(b"hi")
  141. dest.object_store.add_object(b)
  142. t = index.commit_tree(dest.object_store, [(b"hi", b.id, 0o100644)])
  143. c = objects.Commit()
  144. c.author = c.committer = b"Foo Bar <foo@example.com>"
  145. c.author_time = c.commit_time = 0
  146. c.author_timezone = c.commit_timezone = 0
  147. c.message = b"hi"
  148. c.tree = t
  149. dest.object_store.add_object(c)
  150. return c.id
  151. def disable_ff_and_make_dummy_commit(self):
  152. # disable non-fast-forward pushes to the server
  153. dest = repo.Repo(os.path.join(self.gitroot, "dest"))
  154. run_git_or_fail(
  155. ["config", "receive.denyNonFastForwards", "true"], cwd=dest.path
  156. )
  157. commit_id = self.make_dummy_commit(dest)
  158. return dest, commit_id
  159. def compute_send(self, src):
  160. sendrefs = dict(src.get_refs())
  161. del sendrefs[b"HEAD"]
  162. return sendrefs, src.generate_pack_data
  163. def test_send_pack_one_error(self) -> None:
  164. dest, dummy_commit = self.disable_ff_and_make_dummy_commit()
  165. dest.refs[b"refs/heads/master"] = dummy_commit
  166. repo_dir = os.path.join(self.gitroot, "server_new.export")
  167. with repo.Repo(repo_dir) as src:
  168. sendrefs, gen_pack = self.compute_send(src)
  169. c = self._client()
  170. result = c.send_pack(
  171. self._build_path("/dest"), lambda _: sendrefs, gen_pack
  172. )
  173. self.assertEqual(
  174. {
  175. b"refs/heads/branch": None,
  176. b"refs/heads/master": "non-fast-forward",
  177. },
  178. result.ref_status,
  179. )
  180. def test_send_pack_multiple_errors(self) -> None:
  181. dest, dummy = self.disable_ff_and_make_dummy_commit()
  182. # set up for two non-ff errors
  183. branch, master = b"refs/heads/branch", b"refs/heads/master"
  184. dest.refs[branch] = dest.refs[master] = dummy
  185. repo_dir = os.path.join(self.gitroot, "server_new.export")
  186. with repo.Repo(repo_dir) as src:
  187. sendrefs, gen_pack = self.compute_send(src)
  188. c = self._client()
  189. result = c.send_pack(
  190. self._build_path("/dest"), lambda _: sendrefs, gen_pack
  191. )
  192. self.assertEqual(
  193. {branch: "non-fast-forward", master: "non-fast-forward"},
  194. result.ref_status,
  195. )
  196. def test_archive(self) -> None:
  197. c = self._client()
  198. f = BytesIO()
  199. c.archive(self._build_path("/server_new.export"), b"HEAD", f.write)
  200. f.seek(0)
  201. tf = tarfile.open(fileobj=f)
  202. self.assertEqual(["baz", "foo"], tf.getnames())
  203. def test_fetch_pack(self) -> None:
  204. c = self._client()
  205. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  206. result = c.fetch(self._build_path("/server_new.export"), dest)
  207. self.assertEqual(
  208. {b"HEAD": b"refs/heads/master"},
  209. result.symrefs,
  210. )
  211. for r in result.refs.items():
  212. dest.refs.set_if_equals(r[0], None, r[1])
  213. self.assertDestEqualsSrc()
  214. def test_fetch_pack_with_nondefault_symref(self) -> None:
  215. c = self._client()
  216. src = repo.Repo(os.path.join(self.gitroot, "server_new.export"))
  217. src.refs.add_if_new(b"refs/heads/main", src.refs[b"refs/heads/master"])
  218. src.refs.set_symbolic_ref(b"HEAD", b"refs/heads/main")
  219. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  220. result = c.fetch(self._build_path("/server_new.export"), dest)
  221. self.assertEqual(
  222. {b"HEAD": b"refs/heads/main"},
  223. result.symrefs,
  224. )
  225. for r in result.refs.items():
  226. dest.refs.set_if_equals(r[0], None, r[1])
  227. self.assertDestEqualsSrc()
  228. def test_get_refs_with_peeled_tag(self) -> None:
  229. tag_create(
  230. os.path.join(self.gitroot, "server_new.export"),
  231. b"v1.0",
  232. message=b"tagging",
  233. annotated=True,
  234. )
  235. c = self._client()
  236. refs = c.get_refs(self._build_path("/server_new.export"))
  237. self.assertEqual(
  238. [
  239. b"HEAD",
  240. b"refs/heads/branch",
  241. b"refs/heads/master",
  242. b"refs/tags/v1.0",
  243. b"refs/tags/v1.0^{}",
  244. ],
  245. sorted(refs.keys()),
  246. )
  247. def test_get_refs_with_ref_prefix(self) -> None:
  248. c = self._client()
  249. refs = c.get_refs(
  250. self._build_path("/server_new.export"), ref_prefix=[b"refs/heads"]
  251. )
  252. self.assertEqual(
  253. [
  254. b"refs/heads/branch",
  255. b"refs/heads/master",
  256. ],
  257. sorted(refs.keys()),
  258. )
  259. def test_fetch_pack_depth(self) -> None:
  260. c = self._client()
  261. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  262. result = c.fetch(self._build_path("/server_new.export"), dest, depth=1)
  263. for r in result.refs.items():
  264. dest.refs.set_if_equals(r[0], None, r[1])
  265. self.assertEqual(
  266. dest.get_shallow(),
  267. {
  268. b"35e0b59e187dd72a0af294aedffc213eaa4d03ff",
  269. b"514dc6d3fbfe77361bcaef320c4d21b72bc10be9",
  270. },
  271. )
  272. def test_repeat(self) -> None:
  273. c = self._client()
  274. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  275. result = c.fetch(self._build_path("/server_new.export"), dest)
  276. for r in result.refs.items():
  277. dest.refs.set_if_equals(r[0], None, r[1])
  278. self.assertDestEqualsSrc()
  279. result = c.fetch(self._build_path("/server_new.export"), dest)
  280. for r in result.refs.items():
  281. dest.refs.set_if_equals(r[0], None, r[1])
  282. self.assertDestEqualsSrc()
  283. def test_fetch_empty_pack(self) -> None:
  284. c = self._client()
  285. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  286. result = c.fetch(self._build_path("/server_new.export"), dest)
  287. for r in result.refs.items():
  288. dest.refs.set_if_equals(r[0], None, r[1])
  289. self.assertDestEqualsSrc()
  290. def dw(refs, **kwargs):
  291. return list(refs.values())
  292. result = c.fetch(
  293. self._build_path("/server_new.export"),
  294. dest,
  295. determine_wants=dw,
  296. )
  297. for r in result.refs.items():
  298. dest.refs.set_if_equals(r[0], None, r[1])
  299. self.assertDestEqualsSrc()
  300. def test_incremental_fetch_pack(self) -> None:
  301. self.test_fetch_pack()
  302. dest, dummy = self.disable_ff_and_make_dummy_commit()
  303. dest.refs[b"refs/heads/master"] = dummy
  304. c = self._client()
  305. repo_dir = os.path.join(self.gitroot, "server_new.export")
  306. with repo.Repo(repo_dir) as dest:
  307. result = c.fetch(self._build_path("/dest"), dest)
  308. for r in result.refs.items():
  309. dest.refs.set_if_equals(r[0], None, r[1])
  310. self.assertDestEqualsSrc()
  311. def test_fetch_pack_no_side_band_64k(self) -> None:
  312. if protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH >= 2:
  313. raise SkipTest("side-band-64k cannot be disabled with git protocol v2")
  314. c = self._client()
  315. c._fetch_capabilities.remove(b"side-band-64k")
  316. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  317. result = c.fetch(self._build_path("/server_new.export"), dest)
  318. for r in result.refs.items():
  319. dest.refs.set_if_equals(r[0], None, r[1])
  320. self.assertDestEqualsSrc()
  321. def test_fetch_pack_zero_sha(self) -> None:
  322. # zero sha1s are already present on the client, and should
  323. # be ignored
  324. c = self._client()
  325. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  326. result = c.fetch(
  327. self._build_path("/server_new.export"),
  328. dest,
  329. lambda refs, **kwargs: [protocol.ZERO_SHA],
  330. )
  331. for r in result.refs.items():
  332. dest.refs.set_if_equals(r[0], None, r[1])
  333. def test_send_remove_branch(self) -> None:
  334. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  335. dummy_commit = self.make_dummy_commit(dest)
  336. dest.refs[b"refs/heads/master"] = dummy_commit
  337. dest.refs[b"refs/heads/abranch"] = dummy_commit
  338. sendrefs = dict(dest.refs)
  339. sendrefs[b"refs/heads/abranch"] = b"00" * 20
  340. del sendrefs[b"HEAD"]
  341. def gen_pack(have, want, ofs_delta=False, progress=None):
  342. return 0, []
  343. c = self._client()
  344. self.assertEqual(dest.refs[b"refs/heads/abranch"], dummy_commit)
  345. c.send_pack(self._build_path("/dest"), lambda _: sendrefs, gen_pack)
  346. self.assertNotIn(b"refs/heads/abranch", dest.refs)
  347. def test_send_new_branch_empty_pack(self) -> None:
  348. with repo.Repo(os.path.join(self.gitroot, "dest")) as dest:
  349. dummy_commit = self.make_dummy_commit(dest)
  350. dest.refs[b"refs/heads/master"] = dummy_commit
  351. dest.refs[b"refs/heads/abranch"] = dummy_commit
  352. sendrefs = {b"refs/heads/bbranch": dummy_commit}
  353. def gen_pack(have, want, ofs_delta=False, progress=None):
  354. return 0, []
  355. c = self._client()
  356. self.assertEqual(dest.refs[b"refs/heads/abranch"], dummy_commit)
  357. c.send_pack(self._build_path("/dest"), lambda _: sendrefs, gen_pack)
  358. self.assertEqual(dummy_commit, dest.refs[b"refs/heads/abranch"])
  359. def test_get_refs(self) -> None:
  360. c = self._client()
  361. refs = c.get_refs(self._build_path("/server_new.export"))
  362. repo_dir = os.path.join(self.gitroot, "server_new.export")
  363. with repo.Repo(repo_dir) as dest:
  364. self.assertDictEqual(dest.refs.as_dict(), refs)
  365. class DulwichTCPClientTest(CompatTestCase, DulwichClientTestBase):
  366. def setUp(self) -> None:
  367. CompatTestCase.setUp(self)
  368. DulwichClientTestBase.setUp(self)
  369. if check_for_daemon(limit=1):
  370. raise SkipTest(
  371. f"git-daemon was already running on port {protocol.TCP_GIT_PORT}"
  372. )
  373. fd, self.pidfile = tempfile.mkstemp(
  374. prefix="dulwich-test-git-client", suffix=".pid"
  375. )
  376. os.fdopen(fd).close()
  377. args = [
  378. _DEFAULT_GIT,
  379. "daemon",
  380. "--verbose",
  381. "--export-all",
  382. f"--pid-file={self.pidfile}",
  383. f"--base-path={self.gitroot}",
  384. "--enable=receive-pack",
  385. "--enable=upload-archive",
  386. "--listen=localhost",
  387. "--reuseaddr",
  388. self.gitroot,
  389. ]
  390. self.process = subprocess.Popen(
  391. args,
  392. cwd=self.gitroot,
  393. stdout=subprocess.PIPE,
  394. stderr=subprocess.PIPE,
  395. )
  396. if not check_for_daemon():
  397. raise SkipTest("git-daemon failed to start")
  398. def tearDown(self) -> None:
  399. with open(self.pidfile) as f:
  400. pid = int(f.read().strip())
  401. if sys.platform == "win32":
  402. PROCESS_TERMINATE = 1
  403. handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, pid)
  404. ctypes.windll.kernel32.TerminateProcess(handle, -1)
  405. ctypes.windll.kernel32.CloseHandle(handle)
  406. else:
  407. with suppress(OSError):
  408. os.kill(pid, signal.SIGKILL)
  409. os.unlink(self.pidfile)
  410. self.process.wait()
  411. self.process.stdout.close()
  412. self.process.stderr.close()
  413. DulwichClientTestBase.tearDown(self)
  414. CompatTestCase.tearDown(self)
  415. def _client(self):
  416. return client.TCPGitClient("localhost")
  417. def _build_path(self, path):
  418. return path
  419. if sys.platform == "win32" and protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH < 2:
  420. @expectedFailure
  421. def test_fetch_pack_no_side_band_64k(self) -> None:
  422. DulwichClientTestBase.test_fetch_pack_no_side_band_64k(self)
  423. def test_send_remove_branch(self) -> None:
  424. # This test fails intermittently on my machine, probably due to some sort
  425. # of race condition. Probably also related to #1015
  426. self.skipTest("skip flaky test; see #1015")
  427. @patch("dulwich.protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH", new=0)
  428. class DulwichTCPClientTestGitProtov0(DulwichTCPClientTest):
  429. pass
  430. class TestSSHVendor:
  431. @staticmethod
  432. def run_command(
  433. host,
  434. command,
  435. username=None,
  436. port=None,
  437. password=None,
  438. key_filename=None,
  439. protocol_version=None,
  440. ):
  441. cmd, path = command.split(" ")
  442. cmd = cmd.split("-", 1)
  443. path = path.replace("'", "")
  444. env = dict(os.environ)
  445. if protocol_version is None:
  446. protocol_version = protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH
  447. if protocol_version > 0:
  448. env["GIT_PROTOCOL"] = f"version={protocol_version}"
  449. p = subprocess.Popen(
  450. [*cmd, path],
  451. bufsize=0,
  452. stdin=subprocess.PIPE,
  453. stdout=subprocess.PIPE,
  454. stderr=subprocess.PIPE,
  455. env=env,
  456. )
  457. return client.SubprocessWrapper(p)
  458. class DulwichMockSSHClientTest(CompatTestCase, DulwichClientTestBase):
  459. def setUp(self) -> None:
  460. CompatTestCase.setUp(self)
  461. DulwichClientTestBase.setUp(self)
  462. self.real_vendor = client.get_ssh_vendor
  463. client.get_ssh_vendor = TestSSHVendor
  464. def tearDown(self) -> None:
  465. DulwichClientTestBase.tearDown(self)
  466. CompatTestCase.tearDown(self)
  467. client.get_ssh_vendor = self.real_vendor
  468. def _client(self):
  469. return client.SSHGitClient("localhost")
  470. def _build_path(self, path):
  471. return self.gitroot + path
  472. @patch("dulwich.protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH", new=0)
  473. class DulwichMockSSHClientTestGitProtov0(DulwichMockSSHClientTest):
  474. pass
  475. class DulwichSubprocessClientTest(CompatTestCase, DulwichClientTestBase):
  476. def setUp(self) -> None:
  477. CompatTestCase.setUp(self)
  478. DulwichClientTestBase.setUp(self)
  479. def tearDown(self) -> None:
  480. DulwichClientTestBase.tearDown(self)
  481. CompatTestCase.tearDown(self)
  482. def _client(self):
  483. return client.SubprocessGitClient()
  484. def _build_path(self, path):
  485. return self.gitroot + path
  486. @patch("dulwich.protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH", new=0)
  487. class DulwichSubprocessClientTestGitProtov0(DulwichSubprocessClientTest):
  488. pass
  489. class GitHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
  490. """HTTP Request handler that calls out to 'git http-backend'."""
  491. # Make rfile unbuffered -- we need to read one line and then pass
  492. # the rest to a subprocess, so we can't use buffered input.
  493. rbufsize = 0
  494. def do_POST(self) -> None:
  495. self.run_backend()
  496. def do_GET(self) -> None:
  497. self.run_backend()
  498. def send_head(self):
  499. return self.run_backend()
  500. def log_request(self, code="-", size="-") -> None:
  501. # Let's be quiet, the test suite is noisy enough already
  502. pass
  503. def run_backend(self) -> None:
  504. """Call out to git http-backend."""
  505. # Based on CGIHTTPServer.CGIHTTPRequestHandler.run_cgi:
  506. # Copyright (c) 2001-2010 Python Software Foundation;
  507. # All Rights Reserved
  508. # Licensed under the Python Software Foundation License.
  509. rest = self.path
  510. # find an explicit query string, if present.
  511. i = rest.rfind("?")
  512. if i >= 0:
  513. rest, query = rest[:i], rest[i + 1 :]
  514. else:
  515. query = ""
  516. env = copy.deepcopy(os.environ)
  517. env["SERVER_SOFTWARE"] = self.version_string()
  518. env["SERVER_NAME"] = self.server.server_name
  519. env["GATEWAY_INTERFACE"] = "CGI/1.1"
  520. env["SERVER_PROTOCOL"] = self.protocol_version
  521. env["SERVER_PORT"] = str(self.server.server_port)
  522. env["GIT_PROJECT_ROOT"] = self.server.root_path
  523. env["GIT_HTTP_EXPORT_ALL"] = "1"
  524. env["REQUEST_METHOD"] = self.command
  525. uqrest = unquote(rest)
  526. env["PATH_INFO"] = uqrest
  527. env["SCRIPT_NAME"] = "/"
  528. if query:
  529. env["QUERY_STRING"] = query
  530. host = self.address_string()
  531. if host != self.client_address[0]:
  532. env["REMOTE_HOST"] = host
  533. env["REMOTE_ADDR"] = self.client_address[0]
  534. authorization = self.headers.get("authorization")
  535. if authorization:
  536. authorization = authorization.split()
  537. if len(authorization) == 2:
  538. import base64
  539. import binascii
  540. env["AUTH_TYPE"] = authorization[0]
  541. if authorization[0].lower() == "basic":
  542. try:
  543. authorization = base64.decodestring(authorization[1])
  544. except binascii.Error:
  545. pass
  546. else:
  547. authorization = authorization.split(":")
  548. if len(authorization) == 2:
  549. env["REMOTE_USER"] = authorization[0]
  550. # XXX REMOTE_IDENT
  551. content_type = self.headers.get("content-type")
  552. if content_type:
  553. env["CONTENT_TYPE"] = content_type
  554. length = self.headers.get("content-length")
  555. if length:
  556. env["CONTENT_LENGTH"] = length
  557. referer = self.headers.get("referer")
  558. if referer:
  559. env["HTTP_REFERER"] = referer
  560. accept = []
  561. for line in self.headers.getallmatchingheaders("accept"):
  562. if line[:1] in "\t\n\r ":
  563. accept.append(line.strip())
  564. else:
  565. accept = accept + line[7:].split(",")
  566. env["HTTP_ACCEPT"] = ",".join(accept)
  567. ua = self.headers.get("user-agent")
  568. if ua:
  569. env["HTTP_USER_AGENT"] = ua
  570. co = self.headers.get("cookie")
  571. if co:
  572. env["HTTP_COOKIE"] = co
  573. proto = self.headers.get("Git-Protocol")
  574. if proto:
  575. env["GIT_PROTOCOL"] = proto
  576. # XXX Other HTTP_* headers
  577. # Since we're setting the env in the parent, provide empty
  578. # values to override previously set values
  579. for k in (
  580. "QUERY_STRING",
  581. "REMOTE_HOST",
  582. "CONTENT_LENGTH",
  583. "HTTP_USER_AGENT",
  584. "HTTP_COOKIE",
  585. "HTTP_REFERER",
  586. ):
  587. env.setdefault(k, "")
  588. self.wfile.write(b"HTTP/1.1 200 Script output follows\r\n")
  589. self.wfile.write((f"Server: {self.server.server_name}\r\n").encode("ascii"))
  590. self.wfile.write((f"Date: {self.date_time_string()}\r\n").encode("ascii"))
  591. decoded_query = query.replace("+", " ")
  592. try:
  593. nbytes = int(length)
  594. except (TypeError, ValueError):
  595. nbytes = -1
  596. if self.command.lower() == "post":
  597. if nbytes > 0:
  598. data = self.rfile.read(nbytes)
  599. elif self.headers.get("transfer-encoding") == "chunked":
  600. chunks = []
  601. while True:
  602. line = self.rfile.readline()
  603. length = int(line.rstrip(), 16)
  604. chunk = self.rfile.read(length + 2)
  605. chunks.append(chunk[:-2])
  606. if length == 0:
  607. break
  608. data = b"".join(chunks)
  609. env["CONTENT_LENGTH"] = str(len(data))
  610. else:
  611. raise AssertionError
  612. else:
  613. data = None
  614. env["CONTENT_LENGTH"] = "0"
  615. # throw away additional data [see bug #427345]
  616. while select.select([self.rfile._sock], [], [], 0)[0]:
  617. if not self.rfile._sock.recv(1):
  618. break
  619. args = ["http-backend"]
  620. if "=" not in decoded_query:
  621. args.append(decoded_query)
  622. stdout = run_git_or_fail(args, input=data, env=env, stderr=subprocess.PIPE)
  623. self.wfile.write(stdout)
  624. class HTTPGitServer(http.server.HTTPServer):
  625. allow_reuse_address = True
  626. def __init__(self, server_address, root_path) -> None:
  627. http.server.HTTPServer.__init__(self, server_address, GitHTTPRequestHandler)
  628. self.root_path = root_path
  629. self.server_name = "localhost"
  630. def get_url(self) -> str:
  631. return f"http://{self.server_name}:{self.server_port}/"
  632. class DulwichHttpClientTest(CompatTestCase, DulwichClientTestBase):
  633. min_git_version = (1, 7, 0, 2)
  634. def setUp(self) -> None:
  635. CompatTestCase.setUp(self)
  636. DulwichClientTestBase.setUp(self)
  637. self._httpd = HTTPGitServer(("localhost", 0), self.gitroot)
  638. self.addCleanup(self._httpd.shutdown)
  639. threading.Thread(target=self._httpd.serve_forever).start()
  640. run_git_or_fail(["config", "http.uploadpack", "true"], cwd=self.dest)
  641. run_git_or_fail(["config", "http.receivepack", "true"], cwd=self.dest)
  642. def tearDown(self) -> None:
  643. DulwichClientTestBase.tearDown(self)
  644. CompatTestCase.tearDown(self)
  645. self._httpd.shutdown()
  646. self._httpd.socket.close()
  647. def _client(self):
  648. return client.HttpGitClient(self._httpd.get_url())
  649. def _build_path(self, path):
  650. return path
  651. def test_archive(self) -> NoReturn:
  652. raise SkipTest("exporting archives not supported over http")
  653. @patch("dulwich.protocol.DEFAULT_GIT_PROTOCOL_VERSION_FETCH", new=0)
  654. class DulwichHttpClientTestGitProtov0(DulwichHttpClientTest):
  655. pass