diff --git a/examples/middleware_async.py b/examples/middleware_async.py index 514ad15..54f6a84 100644 --- a/examples/middleware_async.py +++ b/examples/middleware_async.py @@ -7,38 +7,34 @@ async def get_user(authorization): return { 'greeting': 'Hello, World' } return None -def auth(route, queries=[]): +def auth(route): #in async query string, arguments and headers are only valid until the first await async def auth_middleware(res, req): - #get_headers will preserve headers (and cookies) after await + #get_headers will preserve headers (and cookies) inside req, after await headers = req.get_headers() - #get_parameters will preserve all params after await + #get_parameters will preserve all params inside req after await params = req.get_parameters() - - #preserve desired query string data - query_data = {} - for query in queries: - value = req.get_query(query) - if value: - query_data[query] = value + #get queries will preserve all queries inside req after await + queries = req.get_queries() user = await get_user(headers.get('authorization', None)) if user: - return route(res, req, user, query_data) + return route(res, req, user) return res.write_status(403).cork_end("not authorized") return auth_middleware -def home(res, req, user=None, query={}): - theme = query.get("theme_color", "light") +def home(res, req, user=None): + theme = req.get_query("theme_color") + theme = theme if theme else "light" greeting = user.get('greeting', None) user_id = req.get_parameter(0) res.cork_end(f"{greeting}
theme: {theme}
id: {user_id}") app = App() -app.get("/user/:id", auth(home, ['theme_color'])) +app.get("/user/:id", auth(home)) app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port)) app.run() diff --git a/examples/router_and_basics.py b/examples/router_and_basics.py index 9974001..495dcfc 100644 --- a/examples/router_and_basics.py +++ b/examples/router_and_basics.py @@ -39,6 +39,9 @@ def user(res, req): try: if int(req.get_parameter(0)) == 1: return res.end("Hello user with id 1!") + # get_parameters returns an array of parameters + # params = req.get_parameters() + finally: # invalid user tells to go, to the next route valid route (not found) req.set_yield(1) @@ -55,6 +58,10 @@ def delayed(res, req): #get parameters, query, headers anything you need here delay = req.get_query("delay") delay = 1 if delay == None else float(delay) + + #get queries returns an dict with all query string + # queries = req.get_queries() + #tell response to run this in the event loop #abort handler is grabed here, so responses only will be send if res.aborted == False res.run_async(delayed_hello(delay, res)) diff --git a/src/socketify/socketify.py b/src/socketify/socketify.py index 3325232..7d46191 100644 --- a/src/socketify/socketify.py +++ b/src/socketify/socketify.py @@ -869,6 +869,7 @@ class AppRequest: self._ptr = ffi.new_handle(self) self._headers = None self._params = None + self._query = None def get_cookie(self, name): @@ -958,7 +959,25 @@ class AppRequest: return ffi.unpack(buffer_address, length).decode("utf-8") except Exception: #invalid utf-8 return None + + def get_queries(self): + try: + if self._query: + return self._query + + url = self.get_url() + query = self.get_full_url()[len(url):] + if full_url.startswith("?"): + query = query[1:] + self._query = parse_qs(query, encoding="utf-8") + return self._query + except: + self._query = {} + return None + def get_query(self, key): + if self._query: + return self._query.get(key, None) buffer = ffi.new("char**") if isinstance(key, str):