Dulwich.io dulwich / c38a5a1
Return a 404 not found error when repository is not found. Jelmer Vernooń≥ 17 days ago
3 changed file(s) with 47 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
88
99 * Fix output format of ``porcelain.diff`` to match that of
1010 C Git. (Boris Feld)
11
12 * Return a 404 not found error when repository is not found.
1113
1214 0.19.11 2019-02-07
1315
276276 self.assertContentTypeEquals('text/plain')
277277 self.assertFalse(self._req.cached)
278278
279 def test_get_info_refs_not_found(self):
280 self._environ['QUERY_STRING'] = ''
281
282 objects = []
283 refs = {}
284 backend = _test_backend(objects, refs=refs)
285
286 mat = re.search('info/refs', '/foo/info/refs')
287 self.assertEqual(
288 ['No git repository was found at /foo'],
289 list(get_info_refs(self._req, backend, mat)))
290 self.assertEqual(HTTP_NOT_FOUND, self._status)
291 self.assertContentTypeEquals('text/plain')
292
279293 def test_get_info_packs(self):
280294 class TestPackData(object):
281295
340354 if content_length is not None:
341355 self._environ['CONTENT_LENGTH'] = content_length
342356 mat = re.search('.*', '/git-upload-pack')
357
358 class Backend(object):
359 def open_repository(self, path):
360 return None
343361 handler_output = b''.join(
344 handle_service_request(self._req, 'backend', mat))
362 handle_service_request(self._req, Backend(), mat))
345363 write_output = self._output.getvalue()
346364 # Ensure all output was written via the write callback.
347365 self.assertEqual(b'', handler_output)
362380
363381 def test_get_info_refs_unknown(self):
364382 self._environ['QUERY_STRING'] = 'service=git-evil-handler'
365 content = list(get_info_refs(self._req, b'backend', None))
383
384 class Backend(object):
385 def open_repository(self, url):
386 return None
387
388 mat = re.search('.*', '/git-evil-pack')
389 content = list(get_info_refs(self._req, Backend(), mat))
366390 self.assertFalse(b'git-evil-handler' in b"".join(content))
367391 self.assertEqual(HTTP_FORBIDDEN, self._status)
368392 self.assertFalse(self._req.cached)
371395 self._environ['wsgi.input'] = BytesIO(b'foo')
372396 self._environ['QUERY_STRING'] = 'service=git-upload-pack'
373397
398 class Backend(object):
399
400 def open_repository(self, url):
401 return None
402
374403 mat = re.search('.*', '/git-upload-pack')
375 handler_output = b''.join(get_info_refs(self._req, b'backend', mat))
404 handler_output = b''.join(get_info_refs(self._req, Backend(), mat))
376405 write_output = self._output.getvalue()
377406 self.assertEqual((b'001e# service=git-upload-pack\n'
378407 b'0000'
4646 ReceivableProtocol,
4747 )
4848 from dulwich.repo import (
49 NotGitRepository,
4950 Repo,
5051 )
5152 from dulwich.server import (
172173 def get_info_refs(req, backend, mat):
173174 params = parse_qs(req.environ['QUERY_STRING'])
174175 service = params.get('service', [None])[0]
176 try:
177 repo = get_repo(backend, mat)
178 except NotGitRepository as e:
179 yield req.not_found(str(e))
180 return
175181 if service and not req.dumb:
176182 handler_cls = req.handlers.get(service.encode('ascii'), None)
177183 if handler_cls is None:
193199 req.nocache()
194200 req.respond(HTTP_OK, 'text/plain')
195201 logger.info('Emulating dumb info/refs')
196 repo = get_repo(backend, mat)
197202 for text in generate_info_refs(repo):
198203 yield text
199204
235240 if handler_cls is None:
236241 yield req.forbidden('Unsupported service')
237242 return
243 try:
244 get_repo(backend, mat)
245 except NotGitRepository as e:
246 yield req.not_found(str(e))
247 return
238248 req.nocache()
239249 write = req.respond(HTTP_OK, 'application/x-%s-result' % service)
240250 proto = ReceivableProtocol(req.environ['wsgi.input'].read, write)
251 # TODO(jelmer): Find a way to pass in repo, rather than having handler_cls
252 # reopen.
241253 handler = handler_cls(backend, [url_prefix(mat)], proto, http_req=req)
242254 handler.handle()
243255