From a9dca7398cf9db014f825b255a78e776f21cfdd1 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Sat, 11 May 2013 09:55:35 +0100 Subject: [PATCH] Add basic Cache endpoint. --- AUTHORS | 1 + README.md | 1 + httpbin/core.py | 11 +++++++++++ 3 files changed, 13 insertions(+) diff --git a/AUTHORS b/AUTHORS index 0c4f6fc..44f0c9e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -17,3 +17,4 @@ Patches and Suggestions - Flavio Percoco - Radomir Stevanovic (http://github.com/randomir) - Steven Honson +- Cory Benfield (Lukasa) diff --git a/README.md b/README.md index 70ac838..c1032c8 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ Freely hosted in [HTTP](http://httpbin.org) & - [`/html`](http://httpbin.org/html) Renders an HTML Page. - [`/robots.txt`](http://httpbin.org/robots.txt) Returns some robots.txt rules. - [`/deny`](http://httpbin.org/deny) Denied by robots.txt file. +- [`/cache`](http://httpbin.org/cache) Returns 200 unless an If-Modified-Since header is provided, when it returns a 304. ## DESCRIPTION diff --git a/httpbin/core.py b/httpbin/core.py index fc53e69..ed2a065 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -330,5 +330,16 @@ def decode_base64(value): return base64.urlsafe_b64decode(encoded).decode('utf-8') +@app.route('/cache', methods=('GET',)) +def cache(): + """Returns a 304 if an If-Modified-Since header is present. Returns the same as a GET otherwise.""" + if_modified = request.headers.get('If-Modified-Since') + + if if_modified is None: + return view_get() + else: + return status_code(304) + + if __name__ == '__main__': app.run()