def create_paste(): try: body = urlparse.parse_qs( request.body.read(int(settings.MAX_SIZE * 1.1))) except ValueError: return {'status': 'error', 'message': u"Wrong data payload."} try: content = unicode(''.join(body['content']), 'utf8') except (UnicodeDecodeError, KeyError): return { 'status': 'error', 'message': u"Encoding error: the paste couldn't be saved." } if '{"iv":' not in content: # reject silently non encrypted content return {'status': 'error', 'message': u"Wrong data payload."} if content: # check size of the paste. if more than settings return error # without saving paste. prevent from unusual use of the # system. need to be improved if len(content) < settings.MAX_SIZE: expiration = body.get('expiration', [u'burn_after_reading'])[0] paste = Paste(expiration=expiration, content=content, uuid_length=settings.PASTE_ID_LENGTH) paste.save() # display counter if settings.DISPLAY_COUNTER: #increment paste counter paste.increment_counter() # if refresh time elapsed pick up new counter value now = datetime.now() timeout = (GLOBAL_CONTEXT['refresh_counter'] + timedelta(seconds=settings.REFRESH_COUNTER)) if timeout < now: GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() GLOBAL_CONTEXT['refresh_counter'] = now return {'status': 'ok', 'paste': paste.uuid} return { 'status': 'error', 'message': u"Serveur error: the paste couldn't be saved. " u"Please try later." }
def create_paste(): try: body = urlparse.parse_qs(request.body.read(int(settings.MAX_SIZE * 1.1))) except ValueError: return {'status': 'error', 'message': u"Wrong data payload."} try: content = unicode(''.join(body['content']), 'utf8') except (UnicodeDecodeError, KeyError): return {'status': 'error', 'message': u"Encoding error: the paste couldn't be saved."} if '{"iv":' not in content: # reject silently non encrypted content return {'status': 'error', 'message': u"Wrong data payload."} if content: # check size of the paste. if more than settings return error # without saving paste. prevent from unusual use of the # system. need to be improved if len(content) < settings.MAX_SIZE: expiration = body.get('expiration', [u'burn_after_reading'])[0] paste = Paste(expiration=expiration, content=content, uuid_length=settings.PASTE_ID_LENGTH) paste.save() # display counter if settings.DISPLAY_COUNTER: #increment paste counter paste.increment_counter() # if refresh time elapsed pick up new counter value now = datetime.now() timeout = (GLOBAL_CONTEXT['refresh_counter'] + timedelta(seconds=settings.REFRESH_COUNTER)) if timeout < now: GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() GLOBAL_CONTEXT['refresh_counter'] = now return {'status': 'ok', 'paste': paste.uuid} return {'status': 'error', 'message': u"Serveur error: the paste couldn't be saved. " u"Please try later."}
def create_paste(): try: content = unicode(request.forms.get('content', ''), 'utf8') except UnicodeDecodeError: return {'status': 'error', 'message': u"Encoding error: the paste couldn't be saved."} if '{"iv":' not in content: # reject silently non encrypted content return '' if content: # check size of the paste. if more than settings return error without saving paste. # prevent from unusual use of the system. # need to be improved if len(content) < settings.MAX_SIZE: expiration = request.forms.get('expiration', u'burn_after_reading') paste = Paste(expiration=expiration, content=content) paste.save() # display counter if settings.DISPLAY_COUNTER: #increment paste counter paste.increment_counter() # if refresh time elapsed pick up new counter value now = datetime.now() timeout = (GLOBAL_CONTEXT['refresh_counter'] + timedelta(seconds=settings.REFRESH_COUNTER)) if timeout < now: GLOBAL_CONTEXT['pastes_count'] = Paste.get_pastes_count() GLOBAL_CONTEXT['refresh_counter'] = now return {'status': 'ok', 'paste': paste.uuid} return {'status': 'error', 'message': u"Serveur error: the paste couldn't be saved. Please try later."}
def create_paste(): try: body = urlparse.parse_qs(request.body.read(int(settings.MAX_SIZE * 1.1))) except ValueError: return {"status": "error", "message": u"Wrong data payload."} try: content = unicode("".join(body["content"]), "utf8") except (UnicodeDecodeError, KeyError): return {"status": "error", "message": u"Encoding error: the paste couldn't be saved."} if '{"iv":' not in content: # reject silently non encrypted content return {"status": "error", "message": u"Wrong data payload."} if content: # check size of the paste. if more than settings return error # without saving paste. prevent from unusual use of the # system. need to be improved if len(content) < settings.MAX_SIZE: expiration = body.get("expiration", [u"burn_after_reading"])[0] paste = Paste(expiration=expiration, content=content) paste.save() # display counter if settings.DISPLAY_COUNTER: # increment paste counter paste.increment_counter() # if refresh time elapsed pick up new counter value now = datetime.now() timeout = GLOBAL_CONTEXT["refresh_counter"] + timedelta(seconds=settings.REFRESH_COUNTER) if timeout < now: GLOBAL_CONTEXT["pastes_count"] = Paste.get_pastes_count() GLOBAL_CONTEXT["refresh_counter"] = now return {"status": "ok", "paste": paste.uuid} return {"status": "error", "message": u"Serveur error: the paste couldn't be saved. " u"Please try later."}
# importable from utils import settings, SettingsValidationError import bottle from bottle import (Bottle, run, static_file, view, request) import clize from paste import Paste from utils import drop_privileges, dmerge app = Bottle() GLOBAL_CONTEXT = { 'settings': settings, 'pastes_count': Paste.get_pastes_count(), 'refresh_counter': datetime.now() } @app.route('/') @view('home') def index(): return GLOBAL_CONTEXT @app.route('/faq/') @view('faq') def faq(): return GLOBAL_CONTEXT
# add project dir and libs dir to the PYTHON PATH to ensure they are # importable from utils import settings, SettingsValidationError import bottle from bottle import (Bottle, run, static_file, view, request) import clize from paste import Paste from utils import drop_privileges, dmerge app = Bottle() GLOBAL_CONTEXT = { 'settings': settings, 'pastes_count': Paste.get_pastes_count(), 'refresh_counter': datetime.now() } @app.route('/') @view('home') def index(): return GLOBAL_CONTEXT @app.route('/faq/') @view('faq') def faq(): return GLOBAL_CONTEXT
# add project dir and libs dir to the PYTHON PATH to ensure they are # importable from utils import settings import bottle from bottle import Bottle, run, static_file, view, request import clize from paste import Paste from utils import drop_privileges, dmerge app = Bottle() GLOBAL_CONTEXT = {"settings": settings, "pastes_count": Paste.get_pastes_count(), "refresh_counter": datetime.now()} @app.route("/") @view("home") def index(): return GLOBAL_CONTEXT @app.route("/faq/") @view("faq") def faq(): return GLOBAL_CONTEXT @app.route("/paste/create", method="POST")