From 22d1bfbc91a46134dd6c9410b86a3cb3ba250887 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Wed, 2 Jan 2013 16:28:28 +0000 Subject: Handle batch sha1/file queries using POST requests This commit adds support for resolving multiple refs into SHA1s and requesting multiple files at once, each using a single POST request. The (repo, ref) and (repo, ref, filename) parameters are passed to the POST requests as JSON dictionaries in a list. The response to both types of requests is a JSON list with the same dictionaries again, to which a "sha1" and "data" field are added, respectively. The file contents returned by "POST /1.0/files" requests are base64-encoded and need to be decoded at the receiver's end. This is because the contents may be binary or contain quotes and therefore cause JSON syntax errors. --- morph-cache-server | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/morph-cache-server b/morph-cache-server index 04a5710c..d3e42c62 100755 --- a/morph-cache-server +++ b/morph-cache-server @@ -16,7 +16,9 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import base64 import cliapp +import json import logging import os import urllib @@ -197,6 +199,31 @@ class MorphCacheServer(cliapp.Application): except Exception, e: response.status = 404 logging.debug('%s' % e) + + @app.post('/sha1s') + def sha1s(): + result = [] + for pair in request.json: + repo = pair['repo'] + ref = pair['ref'] + try: + sha1, tree = repo_cache.resolve_ref(repo, ref) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'sha1': '%s' % sha1, + 'tree': '%s' % tree + }) + except Exception, e: + logging.debug('%s' % e) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'error': '%s' % e + }) + response.set_header('Cache-Control', 'no-cache') + response.set_header('Content-Type', 'application/json') + return json.dumps(result) @app.get('/files') def file(): @@ -211,6 +238,32 @@ class MorphCacheServer(cliapp.Application): response.status = 404 logging.debug('%s' % e) + @app.post('/files') + def files(): + result = [] + for pair in request.json: + repo = pair['repo'] + ref = pair['ref'] + filename = pair['filename'] + try: + content = repo_cache.cat_file(repo, ref, filename) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'filename': '%s' % filename, + 'data': '%s' % base64.b64encode(content), + }) + except Exception, e: + logging.debug('%s' % e) + result.append({ + 'repo': '%s' % repo, + 'ref': '%s' % ref, + 'filename': '%s' % filename, + 'error': '%s' % e + }) + response.set_header('Content-Type', 'application/json') + return json.dumps(result) + @app.get('/trees') def tree(): repo = self._unescape_parameter(request.query.repo) -- cgit v1.2.1