Skip to content
This repository was archived by the owner on Jun 7, 2021. It is now read-only.

Commit 6206535

Browse files
authored
Handle invalid POST to action resources. (#67)
1 parent 67ad4a4 commit 6206535

3 files changed

Lines changed: 56 additions & 50 deletions

File tree

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
ifaddr>=0.1.0
2-
jsonschema==2.6.*; python_version < '3.5'
2+
jsonschema==2.6.*; python_version == '2.7'
33
jsonschema>=3.2.0; python_version >= '3.5'
44
pyee>=7.0.0
5-
tornado==5.1.*; python_version < '3.5'
5+
tornado==5.1.*; python_version == '2.7'
66
tornado>=6.0.0; python_version >= '3.5'
77
zeroconf==0.19.*; python_version == '2.7'
8-
zeroconf>=0.21.0; python_version >= '3.4'
8+
zeroconf>=0.26.0; python_version >= '3.5'

setup.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,15 @@
2424
'zeroconf==0.19.*',
2525
])
2626
elif sys.version_info.major == 3:
27-
if sys.version_info.minor < 5:
28-
requirements.extend([
29-
'jsonschema==2.6.*',
30-
'tornado==5.1.*',
31-
'zeroconf>=0.21.0',
32-
])
33-
else:
34-
requirements.extend([
35-
'jsonschema>=3.2.0',
36-
'tornado>=6.0.0',
37-
'zeroconf>=0.21.0',
38-
])
27+
requirements.extend([
28+
'jsonschema>=3.2.0',
29+
'tornado>=6.0.0',
30+
'zeroconf>=0.26.0',
31+
])
3932

4033
setup(
4134
name='webthing',
42-
version='0.12.2',
35+
version='0.12.3',
4336
description='HTTP Web Thing implementation',
4437
long_description=long_description,
4538
url='https://github.com/mozilla-iot/webthing-python',
@@ -53,7 +46,6 @@
5346
'Intended Audience :: Developers',
5447
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
5548
'Programming Language :: Python :: 2.7',
56-
'Programming Language :: Python :: 3.4',
5749
'Programming Language :: Python :: 3.5',
5850
'Programming Language :: Python :: 3.6',
5951
'Programming Language :: Python :: 3.7',
@@ -64,5 +56,5 @@
6456
'Source': 'https://github.com/mozilla-iot/webthing-python',
6557
'Tracker': 'https://github.com/mozilla-iot/webthing-python/issues',
6658
},
67-
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4',
59+
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4',
6860
)

webthing/server.py

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -504,24 +504,31 @@ def post(self, thing_id='0'):
504504
self.set_status(400)
505505
return
506506

507-
response = {}
508-
for action_name, action_params in message.items():
509-
input_ = None
510-
if 'input' in action_params:
511-
input_ = action_params['input']
507+
keys = list(message.keys())
508+
if len(keys) != 1:
509+
self.set_status(400)
510+
return
512511

513-
action = thing.perform_action(action_name, input_)
514-
if action:
515-
response.update(action.as_action_description())
512+
action_name = keys[0]
513+
action_params = message[action_name]
514+
input_ = None
515+
if 'input' in action_params:
516+
input_ = action_params['input']
516517

517-
# Start the action
518-
tornado.ioloop.IOLoop.current().spawn_callback(
519-
perform_action,
520-
action,
521-
)
518+
action = thing.perform_action(action_name, input_)
519+
if action:
520+
response = action.as_action_description()
522521

523-
self.set_status(201)
524-
self.write(json.dumps(response))
522+
# Start the action
523+
tornado.ioloop.IOLoop.current().spawn_callback(
524+
perform_action,
525+
action,
526+
)
527+
528+
self.set_status(201)
529+
self.write(json.dumps(response))
530+
else:
531+
self.set_status(400)
525532

526533

527534
class ActionHandler(BaseHandler):
@@ -560,27 +567,34 @@ def post(self, thing_id='0', action_name=None):
560567
self.set_status(400)
561568
return
562569

563-
response = {}
564-
for name, action_params in message.items():
565-
if name != action_name:
566-
continue
570+
keys = list(message.keys())
571+
if len(keys) != 1:
572+
self.set_status(400)
573+
return
574+
575+
if keys[0] != action_name:
576+
self.set_status(400)
577+
return
567578

568-
input_ = None
569-
if 'input' in action_params:
570-
input_ = action_params['input']
579+
action_params = message[action_name]
580+
input_ = None
581+
if 'input' in action_params:
582+
input_ = action_params['input']
571583

572-
action = thing.perform_action(name, input_)
573-
if action:
574-
response.update(action.as_action_description())
584+
action = thing.perform_action(action_name, input_)
585+
if action:
586+
response = action.as_action_description()
575587

576-
# Start the action
577-
tornado.ioloop.IOLoop.current().spawn_callback(
578-
perform_action,
579-
action,
580-
)
588+
# Start the action
589+
tornado.ioloop.IOLoop.current().spawn_callback(
590+
perform_action,
591+
action,
592+
)
581593

582-
self.set_status(201)
583-
self.write(json.dumps(response))
594+
self.set_status(201)
595+
self.write(json.dumps(response))
596+
else:
597+
self.set_status(400)
584598

585599

586600
class ActionIDHandler(BaseHandler):

0 commit comments

Comments
 (0)