kopia lustrzana https://github.com/cirospaciari/socketify.py
add one form of preserving query strings in req and also add some examples
rodzic
48e92f600e
commit
8ae98364c0
|
@ -7,38 +7,34 @@ async def get_user(authorization):
|
||||||
return { 'greeting': 'Hello, World' }
|
return { 'greeting': 'Hello, World' }
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def auth(route, queries=[]):
|
def auth(route):
|
||||||
#in async query string, arguments and headers are only valid until the first await
|
#in async query string, arguments and headers are only valid until the first await
|
||||||
async def auth_middleware(res, req):
|
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()
|
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()
|
params = req.get_parameters()
|
||||||
|
#get queries will preserve all queries inside req after await
|
||||||
#preserve desired query string data
|
queries = req.get_queries()
|
||||||
query_data = {}
|
|
||||||
for query in queries:
|
|
||||||
value = req.get_query(query)
|
|
||||||
if value:
|
|
||||||
query_data[query] = value
|
|
||||||
|
|
||||||
user = await get_user(headers.get('authorization', None))
|
user = await get_user(headers.get('authorization', None))
|
||||||
if user:
|
if user:
|
||||||
return route(res, req, user, query_data)
|
return route(res, req, user)
|
||||||
|
|
||||||
return res.write_status(403).cork_end("not authorized")
|
return res.write_status(403).cork_end("not authorized")
|
||||||
|
|
||||||
return auth_middleware
|
return auth_middleware
|
||||||
|
|
||||||
|
|
||||||
def home(res, req, user=None, query={}):
|
def home(res, req, user=None):
|
||||||
theme = query.get("theme_color", "light")
|
theme = req.get_query("theme_color")
|
||||||
|
theme = theme if theme else "light"
|
||||||
greeting = user.get('greeting', None)
|
greeting = user.get('greeting', None)
|
||||||
user_id = req.get_parameter(0)
|
user_id = req.get_parameter(0)
|
||||||
res.cork_end(f"{greeting} <br/> theme: {theme} <br/> id: {user_id}")
|
res.cork_end(f"{greeting} <br/> theme: {theme} <br/> id: {user_id}")
|
||||||
|
|
||||||
app = App()
|
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.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
|
||||||
app.run()
|
app.run()
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,9 @@ def user(res, req):
|
||||||
try:
|
try:
|
||||||
if int(req.get_parameter(0)) == 1:
|
if int(req.get_parameter(0)) == 1:
|
||||||
return res.end("Hello user with id 1!")
|
return res.end("Hello user with id 1!")
|
||||||
|
# get_parameters returns an array of parameters
|
||||||
|
# params = req.get_parameters()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# invalid user tells to go, to the next route valid route (not found)
|
# invalid user tells to go, to the next route valid route (not found)
|
||||||
req.set_yield(1)
|
req.set_yield(1)
|
||||||
|
@ -55,6 +58,10 @@ def delayed(res, req):
|
||||||
#get parameters, query, headers anything you need here
|
#get parameters, query, headers anything you need here
|
||||||
delay = req.get_query("delay")
|
delay = req.get_query("delay")
|
||||||
delay = 1 if delay == None else float(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
|
#tell response to run this in the event loop
|
||||||
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
||||||
res.run_async(delayed_hello(delay, res))
|
res.run_async(delayed_hello(delay, res))
|
||||||
|
|
|
@ -869,6 +869,7 @@ class AppRequest:
|
||||||
self._ptr = ffi.new_handle(self)
|
self._ptr = ffi.new_handle(self)
|
||||||
self._headers = None
|
self._headers = None
|
||||||
self._params = None
|
self._params = None
|
||||||
|
self._query = None
|
||||||
|
|
||||||
|
|
||||||
def get_cookie(self, name):
|
def get_cookie(self, name):
|
||||||
|
@ -958,7 +959,25 @@ class AppRequest:
|
||||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
except Exception: #invalid utf-8
|
except Exception: #invalid utf-8
|
||||||
return None
|
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):
|
def get_query(self, key):
|
||||||
|
if self._query:
|
||||||
|
return self._query.get(key, None)
|
||||||
buffer = ffi.new("char**")
|
buffer = ffi.new("char**")
|
||||||
|
|
||||||
if isinstance(key, str):
|
if isinstance(key, str):
|
||||||
|
|
Ładowanie…
Reference in New Issue