mirror of
https://github.com/chubin/cheat.sh.git
synced 2024-11-26 22:45:38 +03:00
stateful queries support (/:last)
This commit is contained in:
parent
9886550cc6
commit
dc14962f91
21
bin/srv.py
21
bin/srv.py
@ -25,6 +25,8 @@ from limits import Limits
|
||||
from cheat_wrapper import cheat_wrapper
|
||||
from post import process_post_request
|
||||
from options import parse_args
|
||||
|
||||
from stateful_queries import save_query, last_query
|
||||
# pylint: disable=wrong-import-position,wrong-import-order
|
||||
|
||||
if not os.path.exists(os.path.dirname(LOG_FILE)):
|
||||
@ -119,10 +121,15 @@ def answer(topic=None):
|
||||
html_needed = is_html_needed(user_agent)
|
||||
options = parse_args(request.args)
|
||||
|
||||
ip_address = get_request_ip(request)
|
||||
not_allowed = LIMITS.check_ip(ip_address)
|
||||
if not_allowed:
|
||||
return "429 %s\n" % not_allowed, 429
|
||||
request_id = request.cookies.get('id')
|
||||
if topic is not None and topic.lstrip('/') == ':last':
|
||||
if request_id:
|
||||
topic = last_query(request_id)
|
||||
else:
|
||||
return "ERROR: you have to set id for your requests to use /:last\n"
|
||||
else:
|
||||
if request_id:
|
||||
save_query(request_id, topic)
|
||||
|
||||
if request.method == 'POST':
|
||||
process_post_request(request, html_needed)
|
||||
@ -136,6 +143,12 @@ def answer(topic=None):
|
||||
if topic is None:
|
||||
topic = ":firstpage"
|
||||
|
||||
ip_address = get_request_ip(request)
|
||||
if '+' in topic:
|
||||
not_allowed = LIMITS.check_ip(ip_address)
|
||||
if not_allowed:
|
||||
return "429 %s\n" % not_allowed, 429
|
||||
|
||||
result, found = cheat_wrapper(topic, request_options=options, html=is_html_needed(user_agent))
|
||||
|
||||
log_query(ip_address, found, topic, user_agent)
|
||||
|
19
lib/stateful_queries.py
Normal file
19
lib/stateful_queries.py
Normal file
@ -0,0 +1,19 @@
|
||||
"""
|
||||
Support for the stateful queries
|
||||
"""
|
||||
|
||||
import redis
|
||||
|
||||
REDIS = redis.StrictRedis(host='localhost', port=6379, db=1)
|
||||
|
||||
def save_query(client_id, query):
|
||||
"""
|
||||
Save the last query `query` for the client `client_id`
|
||||
"""
|
||||
REDIS.set("l:%s" % client_id, query)
|
||||
|
||||
def last_query(client_id):
|
||||
"""
|
||||
Return the last query for the client `client_id`
|
||||
"""
|
||||
return REDIS.get("l:%s" % client_id)
|
Loading…
Reference in New Issue
Block a user