POST shouldn't use body as 'params'

This commit is contained in:
jesopo 2018-10-05 23:14:32 +01:00
parent 60ec6fe2db
commit b3f8933f3f

View file

@ -6,7 +6,7 @@ _bot = None
_events = None _events = None
class Handler(http.server.BaseHTTPRequestHandler): class Handler(http.server.BaseHTTPRequestHandler):
timeout = 10 timeout = 10
def _handle(self, method, path, params): def _handle(self, method, path, data="", params={}):
_, _, endpoint = path[1:].partition("/") _, _, endpoint = path[1:].partition("/")
endpoint, _, args = endpoint.partition("/") endpoint, _, args = endpoint.partition("/")
args = list(filter(None, args.split("/"))) args = list(filter(None, args.split("/")))
@ -25,7 +25,7 @@ class Handler(http.server.BaseHTTPRequestHandler):
else: else:
if path.startswith("/api/"): if path.startswith("/api/"):
response = _events.on("api").on(method).on(endpoint response = _events.on("api").on(method).on(endpoint
).call_for_result(params=params, path=args) ).call_for_result(params=params, path=args, data=data)
if response: if response:
response = json.dumps(response, sort_keys=True, response = json.dumps(response, sort_keys=True,
@ -52,14 +52,14 @@ class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
parsed = urllib.parse.urlparse(self.path) parsed = urllib.parse.urlparse(self.path)
get_params = self._decode_params(parsed.query) get_params = self._decode_params(parsed.query)
self._handle("get", parsed.path, get_params) self._handle("get", parsed.path, params=get_params)
def do_POST(self): def do_POST(self):
parsed = urllib.parse.urlparse(self.path) parsed = urllib.parse.urlparse(self.path)
post_params = self._decode_params(parsed.query)
content_length = int(self.headers.get("content-length", 0)) content_length = int(self.headers.get("content-length", 0))
post_body = self.rfile.read(content_length) post_body = self.rfile.read(content_length)
post_params = self._decode_params(post_body) self._handle("post", parsed.path, data=post_body, params=post_params)
self._handle("post", parsed.path, post_params)
@utils.export("botset", {"setting": "rest-api", @utils.export("botset", {"setting": "rest-api",
"help": "Enable/disable REST API", "help": "Enable/disable REST API",