lfs_server.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # lfs_server.py -- Simple Git LFS server implementation
  2. # Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
  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 published 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. """Simple Git LFS server implementation for testing."""
  22. import hashlib
  23. import json
  24. import tempfile
  25. import typing
  26. from collections.abc import Mapping
  27. from http.server import BaseHTTPRequestHandler, HTTPServer
  28. from typing import Optional
  29. from .lfs import LFSStore
  30. class LFSRequestHandler(BaseHTTPRequestHandler):
  31. """HTTP request handler for LFS operations."""
  32. server: "LFSServer" # Type annotation for the server attribute
  33. def send_json_response(
  34. self, status_code: int, data: Mapping[str, typing.Any]
  35. ) -> None:
  36. """Send a JSON response."""
  37. response = json.dumps(data).encode("utf-8")
  38. self.send_response(status_code)
  39. self.send_header("Content-Type", "application/vnd.git-lfs+json")
  40. self.send_header("Content-Length", str(len(response)))
  41. self.end_headers()
  42. self.wfile.write(response)
  43. def do_POST(self) -> None:
  44. """Handle POST requests."""
  45. if self.path == "/objects/batch":
  46. self.handle_batch()
  47. elif self.path.startswith("/objects/") and self.path.endswith("/verify"):
  48. self.handle_verify()
  49. else:
  50. self.send_error(404, "Not Found")
  51. def do_PUT(self) -> None:
  52. """Handle PUT requests (uploads)."""
  53. if self.path.startswith("/objects/"):
  54. self.handle_upload()
  55. else:
  56. self.send_error(404, "Not Found")
  57. def do_GET(self) -> None:
  58. """Handle GET requests (downloads)."""
  59. if self.path.startswith("/objects/"):
  60. self.handle_download()
  61. else:
  62. self.send_error(404, "Not Found")
  63. def handle_batch(self) -> None:
  64. """Handle batch API requests."""
  65. content_length = int(self.headers["Content-Length"])
  66. request_data = self.rfile.read(content_length)
  67. try:
  68. batch_request = json.loads(request_data)
  69. except json.JSONDecodeError:
  70. self.send_error(400, "Invalid JSON")
  71. return
  72. operation = batch_request.get("operation")
  73. objects = batch_request.get("objects", [])
  74. if operation not in ["download", "upload"]:
  75. self.send_error(400, "Invalid operation")
  76. return
  77. response_objects = []
  78. for obj in objects:
  79. oid = obj.get("oid")
  80. size = obj.get("size")
  81. if not oid or size is None:
  82. response_objects.append(
  83. {
  84. "oid": oid,
  85. "size": size,
  86. "error": {"code": 400, "message": "Missing oid or size"},
  87. }
  88. )
  89. continue
  90. response_obj = {
  91. "oid": oid,
  92. "size": size,
  93. }
  94. if operation == "download":
  95. # Check if object exists
  96. if self._object_exists(oid):
  97. response_obj["actions"] = {
  98. "download": {
  99. "href": f"http://{self.headers['Host']}/objects/{oid}",
  100. "header": {"Accept": "application/octet-stream"},
  101. }
  102. }
  103. else:
  104. response_obj["error"] = {"code": 404, "message": "Object not found"}
  105. else: # upload
  106. response_obj["actions"] = {
  107. "upload": {
  108. "href": f"http://{self.headers['Host']}/objects/{oid}",
  109. "header": {"Content-Type": "application/octet-stream"},
  110. },
  111. "verify": {
  112. "href": f"http://{self.headers['Host']}/objects/{oid}/verify"
  113. },
  114. }
  115. response_objects.append(response_obj)
  116. self.send_json_response(200, {"objects": response_objects})
  117. def handle_download(self) -> None:
  118. """Handle object download requests."""
  119. # Extract OID from path
  120. path_parts = self.path.strip("/").split("/")
  121. if len(path_parts) != 2:
  122. self.send_error(404, "Not Found")
  123. return
  124. oid = path_parts[1]
  125. try:
  126. with self.server.lfs_store.open_object(oid) as f:
  127. content = f.read()
  128. self.send_response(200)
  129. self.send_header("Content-Type", "application/octet-stream")
  130. self.send_header("Content-Length", str(len(content)))
  131. self.end_headers()
  132. self.wfile.write(content)
  133. except KeyError:
  134. self.send_error(404, "Object not found")
  135. def handle_upload(self) -> None:
  136. """Handle object upload requests."""
  137. # Extract OID from path
  138. path_parts = self.path.strip("/").split("/")
  139. if len(path_parts) != 2:
  140. self.send_error(404, "Not Found")
  141. return
  142. oid = path_parts[1]
  143. content_length = int(self.headers["Content-Length"])
  144. # Read content in chunks
  145. chunks = []
  146. remaining = content_length
  147. while remaining > 0:
  148. chunk_size = min(8192, remaining)
  149. chunk = self.rfile.read(chunk_size)
  150. if not chunk:
  151. break
  152. chunks.append(chunk)
  153. remaining -= len(chunk)
  154. # Calculate SHA256
  155. content = b"".join(chunks)
  156. calculated_oid = hashlib.sha256(content).hexdigest()
  157. # Verify OID matches
  158. if calculated_oid != oid:
  159. self.send_error(400, f"OID mismatch: expected {oid}, got {calculated_oid}")
  160. return
  161. # Check if object already exists
  162. if not self._object_exists(oid):
  163. # Store the object only if it doesn't exist
  164. self.server.lfs_store.write_object(chunks)
  165. self.send_response(200)
  166. self.end_headers()
  167. def handle_verify(self) -> None:
  168. """Handle object verification requests."""
  169. # Extract OID from path
  170. path_parts = self.path.strip("/").split("/")
  171. if len(path_parts) != 3 or path_parts[2] != "verify":
  172. self.send_error(404, "Not Found")
  173. return
  174. oid = path_parts[1]
  175. content_length = int(self.headers.get("Content-Length", 0))
  176. if content_length > 0:
  177. request_data = self.rfile.read(content_length)
  178. try:
  179. verify_request = json.loads(request_data)
  180. # Optionally validate size
  181. if "size" in verify_request:
  182. # Could verify size matches stored object
  183. pass
  184. except json.JSONDecodeError:
  185. pass
  186. # Check if object exists
  187. if self._object_exists(oid):
  188. self.send_response(200)
  189. self.end_headers()
  190. else:
  191. self.send_error(404, "Object not found")
  192. def _object_exists(self, oid: str) -> bool:
  193. """Check if an object exists in the store."""
  194. try:
  195. # Try to open the object - if it exists, close it immediately
  196. with self.server.lfs_store.open_object(oid):
  197. return True
  198. except KeyError:
  199. return False
  200. def log_message(self, format: str, *args: object) -> None:
  201. """Override to suppress request logging during tests."""
  202. if self.server.log_requests:
  203. super().log_message(format, *args)
  204. class LFSServer(HTTPServer):
  205. """Simple LFS server for testing."""
  206. def __init__(
  207. self,
  208. server_address: tuple[str, int],
  209. lfs_store: LFSStore,
  210. log_requests: bool = False,
  211. ) -> None:
  212. """Initialize LFSServer.
  213. Args:
  214. server_address: Tuple of (host, port) to bind to
  215. lfs_store: LFS store instance to use
  216. log_requests: Whether to log incoming requests
  217. """
  218. super().__init__(server_address, LFSRequestHandler)
  219. self.lfs_store = lfs_store
  220. self.log_requests = log_requests
  221. def run_lfs_server(
  222. host: str = "localhost",
  223. port: int = 0,
  224. lfs_dir: Optional[str] = None,
  225. log_requests: bool = False,
  226. ) -> tuple[LFSServer, str]:
  227. """Run an LFS server.
  228. Args:
  229. host: Host to bind to
  230. port: Port to bind to (0 for random)
  231. lfs_dir: Directory for LFS storage (temp dir if None)
  232. log_requests: Whether to log HTTP requests
  233. Returns:
  234. Tuple of (server, url) where url is the base URL for the server
  235. """
  236. if lfs_dir is None:
  237. lfs_dir = tempfile.mkdtemp()
  238. lfs_store = LFSStore.create(lfs_dir)
  239. server = LFSServer((host, port), lfs_store, log_requests)
  240. # Get the actual port if we used 0
  241. actual_port = server.server_address[1]
  242. url = f"http://{host}:{actual_port}"
  243. return server, url