From 6080ca1fc814e92891bbe01ba6881135b214ec2f Mon Sep 17 00:00:00 2001 From: Barun Date: Wed, 19 Mar 2014 21:05:43 +0530 Subject: [PATCH] Add /deflate endpoint returns response with deflate encoded body(Content-Encoding='deflate'). --- README.md | 3 ++- httpbin/core.py | 11 ++++++++++- httpbin/filters.py | 25 +++++++++++++++++++++++++ httpbin/templates/httpbin.1.html | 3 ++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0defe8d..e22cbb0 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Freely hosted in [HTTP](http://httpbin.org) & - [`/put`](http://hurl.it/hurls/18016368fa5e5eef80db935f5dae615d5858a4a4/a512d904f5da64df9627ee998c040f7874d6436a) Returns PUT data. - [`/delete`](http://hurl.it/hurls/6faafea5191f77172fca4cfe1505739230d5f769/bc255ffc69e04c2c8b968822c59544746bbb872c) Returns DELETE data - [`/gzip`](http://httpbin.org/gzip) Returns gzip-encoded data. +- [`/deflate`](http://httpbin.org/deflate) Returns deflate-encoded data. - [`/status/:code`](http://httpbin.org/status/418) Returns given HTTP Status code. - [`/response-headers?key=val`](http://httpbin.org/response-headers?Content-Type=text/plain;%20charset=UTF-8&Server=httpbin) Returns given response headers. - [`/redirect/:n`](http://httpbin.org/redirect/6) 302 Redirects *n* times. @@ -43,7 +44,7 @@ Freely hosted in [HTTP](http://httpbin.org) & Testing an HTTP Library can become difficult sometimes. PostBin.org is fantastic for testing POST requests, but not much else. This exists to cover all kinds of HTTP -scenarios. Additional endpoints are being considered (e.g. `/deflate`). +scenarios. Additional endpoints are being considered. All endpoint responses are JSON-encoded. diff --git a/httpbin/core.py b/httpbin/core.py index e323c11..61cef3d 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -168,6 +168,15 @@ def view_gzip_encoded_content(): 'origin', 'headers', method=request.method, gzipped=True)) +@app.route('/deflate') +@filters.deflate +def view_deflate_encoded_content(): + """Returns Deflate-Encoded Data.""" + + return jsonify(get_dict( + 'origin', 'headers', method=request.method, deflated=True)) + + @app.route('/redirect/') def redirect_n_times(n): """301 Redirects n times.""" @@ -385,7 +394,7 @@ def drip(): delay = float(args.get('delay', 0)) if delay > 0: time.sleep(delay) - + def generate_bytes(): for i in xrange(numbytes): yield bytes(chr(42)) diff --git a/httpbin/filters.py b/httpbin/filters.py index 494deca..dec0ba2 100644 --- a/httpbin/filters.py +++ b/httpbin/filters.py @@ -8,6 +8,7 @@ This module provides response filter decorators. """ import gzip as gzip2 +import zlib from cStringIO import StringIO from decimal import Decimal @@ -63,3 +64,27 @@ def gzip(f, *args, **kwargs): return gzip_data + +@decorator +def deflate(f, *args, **kwargs): + """Deflate Flask Response Decorator.""" + + data = f(*args, **kwargs) + + if isinstance(data, Response): + content = data.data + else: + content = data + + deflater = zlib.compressobj() + deflated_data = deflater.compress(content) + deflated_data += deflater.flush() + + if isinstance(data, Response): + data.data = deflated_data + data.headers['Content-Encoding'] = 'deflate' + data.headers['Content-Length'] = str(len(data.data)) + + return data + + return deflated_data diff --git a/httpbin/templates/httpbin.1.html b/httpbin/templates/httpbin.1.html index d19c0c0..f615dee 100644 --- a/httpbin/templates/httpbin.1.html +++ b/httpbin/templates/httpbin.1.html @@ -17,6 +17,7 @@
  • /patch Returns PATCH data.
  • /delete Returns DELETE data
  • /gzip Returns gzip-encoded data.
  • +
  • /deflate Returns deflate-encoded data.
  • /status/:code Returns given HTTP Status code.
  • /response-headers?key=val Returns given response headers.
  • /redirect/:n 302 Redirects n times.
  • @@ -42,7 +43,7 @@

    Testing an HTTP Library can become difficult sometimes. Postbin is fantastic for testing POST requests, but not much else. This exists to cover all kinds of HTTP -scenarios. Additional endpoints are being considered (e.g. /deflate).

    +scenarios. Additional endpoints are being considered.

    All endpoint responses are JSON-encoded.