From bb094d4a2e7116c3af5497b99d5b712ae9001d60 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Tue, 27 Sep 2011 22:02:07 -0400 Subject: [PATCH] Hide the environment, unless otherwise asked. --- httpbin/core.py | 25 ++++++++++++++++++++++--- httpbin/helpers.py | 27 ++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/httpbin/core.py b/httpbin/core.py index b592c3a..a4381c9 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -13,6 +13,17 @@ from . import filters from .helpers import get_headers, status_code, get_dict, check_basic_auth +ENV_COOKIES = ( + '_gauges_unique', + '_gauges_unique_year', + '_gauges_unique_month', + '_gauges_unique_day', + '_gauges_unique_hour', + '__utmz', + '__utma', + '__utmb' +) + app = Flask(__name__) @@ -55,7 +66,6 @@ def view_user_agent(): @app.route('/get', methods=('GET',)) -@filters.x_runtime @filters.json def view_get(): """Returns GET Data.""" @@ -144,10 +154,19 @@ def view_status_code(code): @app.route('/cookies') @filters.json -def view_cookies(): +def view_cookies(hide_env=True): """Returns cookie data.""" - return dict(cookies=request.cookies) + cookies = dict(request.cookies.items()) + + if hide_env and ('show_env' not in request.args): + for key in ENV_COOKIES: + try: + del cookies[key] + except KeyError: + pass + + return dict(cookies=cookies) @app.route('/cookies/set//') diff --git a/httpbin/helpers.py b/httpbin/helpers.py index dde9b10..bea297f 100644 --- a/httpbin/helpers.py +++ b/httpbin/helpers.py @@ -27,6 +27,18 @@ ASCII_ART = """ REDIRECT_LOCATION = '/redirect/1' +ENV_HEADERS = ( + 'X-Varnish', + 'X-Request-Start', + 'X-Heroku-Queue-Depth', + 'X-Real-Ip', + 'X-Forwarded-Proto', + 'X-Heroku-Queue-Wait-Time', + 'X-Forwarded-For', + 'X-Heroku-Dynos-In-Use' +) + + def get_files(): """Returns files dict from request context.""" @@ -39,10 +51,19 @@ def get_files(): return files -def get_headers(): +def get_headers(hide_env=True): """Returns headers dict from request context.""" - return CaseInsensitiveDict(request.headers.items()) + headers = dict(request.headers.items()) + + if hide_env and ('show_env' not in request.args): + for key in ENV_HEADERS: + try: + del headers[key] + except KeyError: + pass + + return CaseInsensitiveDict(headers.items()) def get_dict(*keys, **extras): @@ -55,7 +76,7 @@ def get_dict(*keys, **extras): data = request.data form = request.form - if len(form) == 1 and not data: + if (len(form) == 1) and (not data): if not form.values().pop(): data = form.keys().pop() form = None