From f79720ef2fda49d3ccbc92613ebb75ef4719cafc Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Mon, 01 Oct 2012 06:28:33 +0000 Subject: Do not return meaning-less data from commands --- diff --git a/sugar_network/local/ipc_client.py b/sugar_network/local/ipc_client.py index 20a13aa..7c7ae46 100644 --- a/sugar_network/local/ipc_client.py +++ b/sugar_network/local/ipc_client.py @@ -26,5 +26,4 @@ class Router(router.Router): def call(self, request, response): request.access_level = ad.ACCESS_LOCAL - response.content_type = 'application/json' return router.Router.call(self, request, response) diff --git a/sugar_network/local/mounts.py b/sugar_network/local/mounts.py index a08e693..f19505d 100644 --- a/sugar_network/local/mounts.py +++ b/sugar_network/local/mounts.py @@ -79,7 +79,8 @@ class LocalMount(ad.VolumeCommands, _Mount): volume.connect(self._events_cb) - @ad.property_command(method='GET', cmd='get_blob') + @ad.property_command(method='GET', cmd='get_blob', + mime_type='application/json') def get_blob(self, document, guid, prop, request=None): directory = self.volume[document] prop = directory.metadata[prop] @@ -114,7 +115,8 @@ class HomeMount(LocalMount): def name(self): return _('Home') - @ad.property_command(method='GET', cmd='get_blob') + @ad.property_command(method='GET', cmd='get_blob', + mime_type='application/json') def get_blob(self, document, guid, prop, request=None): if document == 'implementation' and prop == 'data': path = activities.guid_to_path(guid) @@ -275,7 +277,7 @@ class RemoteMount(ad.CommandsProcessor, _Mount, _ProxyCommands): try: return ad.CommandsProcessor.call(self, request, response) except ad.CommandNotFound: - return self._client.call(request) + return self._client.call(request, response) return self._proxy_call(request, response, super_call) @@ -286,7 +288,8 @@ class RemoteMount(ad.CommandsProcessor, _Mount, _ProxyCommands): else: self._connections.kill() - @ad.property_command(method='GET', cmd='get_blob') + @ad.property_command(method='GET', cmd='get_blob', + mime_type='application/json') def get_blob(self, document, guid, prop): def download(path, seqno): @@ -308,7 +311,8 @@ class RemoteMount(ad.CommandsProcessor, _Mount, _ProxyCommands): if pass_ownership and exists(path): os.unlink(path) - @ad.property_command(method='GET') + @ad.property_command(method='GET', + mime_type='application/json') def get_prop(self, document, guid, prop, response): directory = self._home_volume[document] prop = directory.metadata[prop] @@ -384,7 +388,8 @@ class NodeMount(LocalMount, _ProxyCommands): def call(self, request, response): return self._proxy_call(request, response, super(NodeMount, self).call) - @ad.property_command(method='GET', cmd='get_blob') + @ad.property_command(method='GET', cmd='get_blob', + mime_type='application/json') def get_blob(self, document, guid, prop, request=None): meta = LocalMount.get_blob(self, document, guid, prop) if meta is None: diff --git a/sugar_network/local/mountset.py b/sugar_network/local/mountset.py index ef74648..c8a3337 100644 --- a/sugar_network/local/mountset.py +++ b/sugar_network/local/mountset.py @@ -63,7 +63,8 @@ class Mountset(dict, ad.CommandsProcessor, Commands, SyncCommands): mount.set_mounted(False) dict.__delitem__(self, mountpoint) - @ad.volume_command(method='GET', cmd='mounts') + @ad.volume_command(method='GET', cmd='mounts', + mime_type='application/json') def mounts(self): result = [] for path, mount in self.items(): @@ -75,7 +76,8 @@ class Mountset(dict, ad.CommandsProcessor, Commands, SyncCommands): }) return result - @ad.volume_command(method='GET', cmd='mounted') + @ad.volume_command(method='GET', cmd='mounted', + mime_type='application/json') def mounted(self, mountpoint): mount = self.get(mountpoint) if mount is None: diff --git a/sugar_network/node/commands.py b/sugar_network/node/commands.py index f0226c7..ba5b6ed 100644 --- a/sugar_network/node/commands.py +++ b/sugar_network/node/commands.py @@ -64,12 +64,12 @@ class NodeCommands(ad.VolumeCommands, Commands): def connect(self, callback, condition=None, **kwargs): self.volume.connect(callback, condition) - @ad.volume_command(method='GET') - def hello(self, response): - response.content_type = 'text/html' + @ad.volume_command(method='GET', mime_type='text/html') + def hello(self): return _HELLO_HTML - @ad.volume_command(method='GET', cmd='stat') + @ad.volume_command(method='GET', cmd='stat', + mime_type='application/json') def stat(self): return {'guid': self._guid, 'master': self._is_master, diff --git a/sugar_network/resources/user.py b/sugar_network/resources/user.py index 0c89c3a..f98c416 100644 --- a/sugar_network/resources/user.py +++ b/sugar_network/resources/user.py @@ -56,7 +56,8 @@ class User(ad.Document): def birthday(self, value): return value - @ad.document_command(method='GET', cmd='stats-info') + @ad.document_command(method='GET', cmd='stats-info', + mime_type='application/json') def _stats_info(self, request): enforce(request.principal == self['guid'], ad.Forbidden, 'Operation is permitted only for authors') diff --git a/sugar_network/resources/volume.py b/sugar_network/resources/volume.py index 52b8857..b7680a1 100644 --- a/sugar_network/resources/volume.py +++ b/sugar_network/resources/volume.py @@ -185,7 +185,8 @@ class Commands(object): def connect(self, callback, condition=None, **kwargs): raise NotImplementedError() - @ad.volume_command(method='GET', cmd='subscribe') + @ad.volume_command(method='GET', cmd='subscribe', + mime_type='application/json') def subscribe(self, request, response, only_commits=False): """Subscribe to Server-Sent Events. diff --git a/sugar_network/toolkit/http.py b/sugar_network/toolkit/http.py index f801942..d130c2a 100644 --- a/sugar_network/toolkit/http.py +++ b/sugar_network/toolkit/http.py @@ -149,7 +149,7 @@ class Client(object): return response - def call(self, request): + def call(self, request, response=None): params = request.copy() method = params.pop('method') document = params.pop('document') @@ -162,9 +162,13 @@ class Client(object): if prop: path.append(prop) - response = self.request(method, path, data=request.content, + reply = self.request(method, path, data=request.content, params=params, headers={'Content-Type': 'application/json'}) - return self._decode_response(response) + + if response is not None: + response.content_type = reply.headers['Content-Type'] + + return self._decode_response(reply) def download(self, url_path, out_path, seqno=None, extract=False): if isdir(out_path): diff --git a/sugar_network/toolkit/router.py b/sugar_network/toolkit/router.py index 545704c..88113cd 100644 --- a/sugar_network/toolkit/router.py +++ b/sugar_network/toolkit/router.py @@ -341,7 +341,10 @@ class _Response(ad.Response): @content_type.setter def content_type(self, value): - self['Content-Type'] = value + if value: + self['Content-Type'] = value + elif 'Content-Type' in self: + del self['Content-Type'] @property def last_modified(self): diff --git a/tests/units/router.py b/tests/units/router.py index 44333f2..2ec35d4 100755 --- a/tests/units/router.py +++ b/tests/units/router.py @@ -132,10 +132,14 @@ class RouterTest(tests.Test): def get_binary(self, response): pass - @ad.volume_command(cmd='2') + @ad.volume_command(cmd='2', mime_type='application/json') def get_json(self, response): pass + @ad.volume_command(cmd='3') + def no_get(self, response): + pass + cp = CommandsProcessor() router = Router(cp) @@ -155,6 +159,14 @@ class RouterTest(tests.Test): lambda *args: None) self.assertEqual('null', ''.join([i for i in response])) + response = router({ + 'PATH_INFO': '/', + 'REQUEST_METHOD': 'GET', + 'QUERY_STRING': 'cmd=3', + }, + lambda *args: None) + self.assertEqual('', ''.join([i for i in response])) + def test_Register(self): self.fork(self.restful_server, [User, Document]) -- cgit v0.9.1