diff --git a/httpbin/core.py b/httpbin/core.py index af22285..e60e90d 100644 --- a/httpbin/core.py +++ b/httpbin/core.py @@ -571,14 +571,43 @@ def image(): """Returns a simple image of the type suggest by the Accept header.""" headers = get_headers() - if headers['accept'].lower() == 'image/png' or headers['accept'].lower() == 'image/*': - return Response(base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=='), headers={'Content-Type': 'image/png'}) - elif headers['accept'].lower() == 'image/jpeg': - return Response(base64.b64decode('/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k='), headers={'Content-Type': 'image/jpeg'}) + accept = headers['accept'].lower() + + if 'image/webp' in accept: + return image_webp() + elif 'image/jpeg' in accept: + return image_jpeg() + elif 'image/png' in accept or 'image/*' in accept: + return image_png() else: return status_code(404) +@app.route('/image/png') +def image_png(): + data = resource('images/pig_icon.png') + return Response(data, headers={'Content-Type': 'image/png'}) + + +@app.route('/image/jpeg') +def image_jpeg(): + data = resource('images/jackal.jpg') + return Response(data, headers={'Content-Type': 'image/jpeg'}) + + +@app.route('/image/webp') +def image_webp(): + data = resource('images/wolf_1.webp') + return Response(data, headers={'Content-Type': 'image/webp'}) + + +def resource(filename): + path = os.path.join( + tmpl_dir, + filename) + return open(path, 'rb').read() + + @app.route("/xml") def xml(): response = make_response(render_template("sample.xml")) diff --git a/httpbin/templates/httpbin.1.html b/httpbin/templates/httpbin.1.html index ee96383..2a430a5 100644 --- a/httpbin/templates/httpbin.1.html +++ b/httpbin/templates/httpbin.1.html @@ -42,6 +42,10 @@
  • /bytes/:n Generates n random bytes of binary data, accepts optional seed integer parameter.
  • /stream-bytes/:n Streams n random bytes of binary data, accepts optional seed and chunk_size integer parameters.
  • /links/:n Returns page containing n HTML links.
  • +
  • /image Returns page containing an image.
  • +
  • /image/png Returns page containing a PNG image.
  • +
  • /image/jpeg Returns page containing a JPEG image.
  • +
  • /image/webp Returns page containing a WEBP image.
  • /forms/post HTML form that submits to /post
  • /xml Returns some XML
  • diff --git a/httpbin/templates/images/jackal.jpg b/httpbin/templates/images/jackal.jpg new file mode 100644 index 0000000..a4e824c Binary files /dev/null and b/httpbin/templates/images/jackal.jpg differ diff --git a/httpbin/templates/images/pig_icon.png b/httpbin/templates/images/pig_icon.png new file mode 100644 index 0000000..cc0c128 Binary files /dev/null and b/httpbin/templates/images/pig_icon.png differ diff --git a/httpbin/templates/images/wolf_1.webp b/httpbin/templates/images/wolf_1.webp new file mode 100644 index 0000000..37f5392 Binary files /dev/null and b/httpbin/templates/images/wolf_1.webp differ