Skip to content

Commit

Permalink
Hide the environment, unless otherwise asked.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Sep 28, 2011
1 parent f97214c commit bb094d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
25 changes: 22 additions & 3 deletions httpbin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
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__)


Expand Down Expand Up @@ -55,7 +66,6 @@ def view_user_agent():


@app.route('/get', methods=('GET',))
@filters.x_runtime
@filters.json
def view_get():
"""Returns GET Data."""
Expand Down Expand Up @@ -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/<name>/<value>')
Expand Down
27 changes: 24 additions & 3 deletions httpbin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@

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."""
Expand All @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit bb094d4

Please sign in to comment.