[PATCH] bug fix: Make URI parameter is not None before encoding it

From: Aline Manera <alinefm@br.ibm.com> The following error is raised when the user tried to reboot the host system: Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [25/Feb/2014:14:05:28] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 79, in wrapper for arg in self.model_args]) AttributeError: 'NoneType' object has no attribute 'encode' It is because /host resource has no parameter and fails to encode() the None value. So fix it by verifying the parameter is not None in order to make the encode() call. Signed-off-by: Aline Manera <alinefm@br.ibm.com> --- src/kimchi/control/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 91a70ae..5d1f380 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -75,9 +75,13 @@ class Resource(object): fn = getattr(self.model, model_fn(self, action_name)) ident = fn(*model_args) self._redirect(ident) - uri_params = tuple([urllib2.quote(arg.encode('utf-8'), safe="") - for arg in self.model_args]) - raise internal_redirect(self.uri_fmt % uri_params) + uri_params = [] + for arg in self.model_args: + if arg is None: + arg = '' + uri_params.append(urllib2.quote(arg.encode('utf-8'), + safe="")) + raise internal_redirect(self.uri_fmt % tuple(uri_params)) except MissingParameter, e: raise cherrypy.HTTPError(400, e.message) except InvalidParameter, e: -- 1.7.10.4

Reviewed-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> Has checked, the indent of /host is None, it is different with other resource. I think your code is OK. also I think the follow code maybe also OK, but I do not test. This depends on which you prefer. Thanks. $ git diff diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py index 50edc71..3acf567 100644 --- a/src/kimchi/control/host.py +++ b/src/kimchi/control/host.py @@ -34,8 +34,8 @@ from kimchi.template import render @UrlSubNode("host", True, ['POST']) class Host(Resource): def __init__(self, model, id=None): - super(Host, self).__init__(model, id) - self.uri_fmt = '/host/%s' + super(Host, self).__init__(model, "host") + self.uri_fmt = '/%s' self.reboot = self.generate_action_handler('reboot') self.shutdown = self.generate_action_handler('shutdown') self.stats = HostStats(self.model) On 02/26/2014 01:20 AM, Aline Manera wrote:
From: Aline Manera <alinefm@br.ibm.com>
The following error is raised when the user tried to reboot the host system:
Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [25/Feb/2014:14:05:28] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 79, in wrapper for arg in self.model_args]) AttributeError: 'NoneType' object has no attribute 'encode'
It is because /host resource has no parameter and fails to encode() the None value. So fix it by verifying the parameter is not None in order to make the encode() call.
Signed-off-by: Aline Manera <alinefm@br.ibm.com> --- src/kimchi/control/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 91a70ae..5d1f380 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -75,9 +75,13 @@ class Resource(object): fn = getattr(self.model, model_fn(self, action_name)) ident = fn(*model_args) self._redirect(ident) - uri_params = tuple([urllib2.quote(arg.encode('utf-8'), safe="") - for arg in self.model_args]) - raise internal_redirect(self.uri_fmt % uri_params) + uri_params = [] + for arg in self.model_args: + if arg is None: + arg = '' + uri_params.append(urllib2.quote(arg.encode('utf-8'), + safe="")) + raise internal_redirect(self.uri_fmt % tuple(uri_params)) except MissingParameter, e: raise cherrypy.HTTPError(400, e.message) except InvalidParameter, e:
-- Thanks and best regards! Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com> IBM Linux Technology Center

于 2014/2/26 1:20, Aline Manera 写道:
From: Aline Manera <alinefm@br.ibm.com>
The following error is raised when the user tried to reboot the host system:
Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [25/Feb/2014:14:05:28] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 79, in wrapper for arg in self.model_args]) AttributeError: 'NoneType' object has no attribute 'encode'
It is because /host resource has no parameter and fails to encode() the None value. So fix it by verifying the parameter is not None in order to make the encode() call.
Signed-off-by: Aline Manera <alinefm@br.ibm.com> --- src/kimchi/control/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 91a70ae..5d1f380 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -75,9 +75,13 @@ class Resource(object): fn = getattr(self.model, model_fn(self, action_name)) ident = fn(*model_args) self._redirect(ident) - uri_params = tuple([urllib2.quote(arg.encode('utf-8'), safe="") - for arg in self.model_args]) - raise internal_redirect(self.uri_fmt % uri_params) + uri_params = [] + for arg in self.model_args: + if arg is None: + arg = '' + uri_params.append(urllib2.quote(arg.encode('utf-8'), + safe=""))
You can change the lines above into: for arg in self.model_args: if arg is not None: uri_params.append(urllib2.quote(arg.encode('utf-8'), safe=""))
+ raise internal_redirect(self.uri_fmt % tuple(uri_params)) except MissingParameter, e: raise cherrypy.HTTPError(400, e.message) except InvalidParameter, e:

On 02/26/2014 04:44 AM, Shu Ming wrote:
于 2014/2/26 1:20, Aline Manera 写道:
From: Aline Manera <alinefm@br.ibm.com>
The following error is raised when the user tried to reboot the host system:
Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [25/Feb/2014:14:05:28] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 79, in wrapper for arg in self.model_args]) AttributeError: 'NoneType' object has no attribute 'encode'
It is because /host resource has no parameter and fails to encode() the None value. So fix it by verifying the parameter is not None in order to make the encode() call.
Signed-off-by: Aline Manera <alinefm@br.ibm.com> --- src/kimchi/control/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 91a70ae..5d1f380 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -75,9 +75,13 @@ class Resource(object): fn = getattr(self.model, model_fn(self, action_name)) ident = fn(*model_args) self._redirect(ident) - uri_params = tuple([urllib2.quote(arg.encode('utf-8'), safe="") - for arg in self.model_args]) - raise internal_redirect(self.uri_fmt % uri_params) + uri_params = [] + for arg in self.model_args: + if arg is None: + arg = '' + uri_params.append(urllib2.quote(arg.encode('utf-8'), + safe=""))
You can change the lines above into:
for arg in self.model_args: if arg is not None: uri_params.append(urllib2.quote(arg.encode('utf-8'), safe=""))
ok
+ raise internal_redirect(self.uri_fmt % tuple(uri_params)) except MissingParameter, e: raise cherrypy.HTTPError(400, e.message) except InvalidParameter, e:

On 02/26/2014 03:23 PM, Aline Manera wrote:
On 02/26/2014 04:44 AM, Shu Ming wrote:
于 2014/2/26 1:20, Aline Manera 写道:
From: Aline Manera <alinefm@br.ibm.com>
The following error is raised when the user tried to reboot the host system:
Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [25/Feb/2014:14:05:28] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 79, in wrapper for arg in self.model_args]) AttributeError: 'NoneType' object has no attribute 'encode'
It is because /host resource has no parameter and fails to encode() the None value. So fix it by verifying the parameter is not None in order to make the encode() call.
Signed-off-by: Aline Manera <alinefm@br.ibm.com> --- src/kimchi/control/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 91a70ae..5d1f380 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -75,9 +75,13 @@ class Resource(object): fn = getattr(self.model, model_fn(self, action_name)) ident = fn(*model_args) self._redirect(ident) - uri_params = tuple([urllib2.quote(arg.encode('utf-8'), safe="") - for arg in self.model_args]) - raise internal_redirect(self.uri_fmt % uri_params) + uri_params = [] + for arg in self.model_args: + if arg is None: + arg = '' + uri_params.append(urllib2.quote(arg.encode('utf-8'), + safe=""))
You can change the lines above into:
for arg in self.model_args: if arg is not None: uri_params.append(urllib2.quote(arg.encode('utf-8'), safe=""))
ok
In fact, it is not possible. The internal_redirect needs all the args. So we can consider "". When I tried it I got: Request Headers: AUTHORIZATION: Basic YWxpbmVmbTowaG5lKjI4ZGljSA== Content-Length: 2 HOST: localhost:8000 Remote-Addr: 127.0.0.1 ACCEPT: application/json USER-AGENT: curl/7.27.0 Content-Type: application/json [26/Feb/2014:15:25:09] HTTP Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/usr/lib/python2.7/dist-packages/cherrypy/lib/encoding.py", line 188, in __call__ self.body = self.oldhandler(*args, **kwargs) File "/usr/lib/python2.7/dist-packages/cherrypy/_cpdispatch.py", line 34, in __call__ return self.callable(*self.args, **self.kwargs) File "/home/alinefm/kimchi/src/kimchi/control/base.py", line 78, in wrapper raise internal_redirect(self.uri_fmt % tuple(uri_params)) TypeError: not enough arguments for format string TypeError: not enough arguments for format string
+ raise internal_redirect(self.uri_fmt % tuple(uri_params)) except MissingParameter, e: raise cherrypy.HTTPError(400, e.message) except InvalidParameter, e:
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel
participants (3)
-
Aline Manera
-
Sheldon
-
Shu Ming