Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add streaming support for compressed data. #633

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 115 additions & 2 deletions httpbin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import uuid
import argparse

import gzip as gzip2
from six import BytesIO
import zlib
import brotli as _brotli

from flask import (
Flask,
Response,
Expand Down Expand Up @@ -698,7 +703,6 @@ def absolute_redirect_n_times(n):

return _redirect("absolute", n, True)


@app.route("/stream/<int:n>")
def stream_n_messages(n):
"""Stream n JSON responses
Expand All @@ -725,7 +729,6 @@ def generate_stream():

return Response(generate_stream(), headers={"Content-Type": "application/json"})


@app.route(
"/status/<codes>", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "TRACE"]
)
Expand Down Expand Up @@ -1777,6 +1780,116 @@ def a_json_endpoint():
}
)

def split_data_into_chunkes(data, n):
data_size, chunk_size = len(data), int(len(data) / n)
chunks = [ data[i : i + chunk_size] for i in range(0, data_size, chunk_size) ]
# chunks[-1] = data[(n - 1) * chunk_size : -1]
return chunks

def create_data_for_streaming(n):
response = get_dict("url", "args", "headers", "origin")
data = []

for i in range(n):
response["id"] = i
data.append(json.dumps(response) + "\n")

complete_data = ''.join(data)
return complete_data

@app.route("/stream/gzip/<int:n>")
def stream_n_gzip_messages(n):
"""Stream n GZip-encoded JSON responses
---
tags:
- Dynamic data
parameters:
- in: path
name: n
type: int
produces:
- application/json
responses:
200:
description: Streamed GZip-encoded JSON responses.
"""
n = min(n, 100)
complete_data = create_data_for_streaming(n)

gzip_buffer = BytesIO()
gzip_file = gzip2.GzipFile(
mode='wb',
compresslevel=4,
fileobj=gzip_buffer
)
gzip_file.write(complete_data.encode('ascii'))
gzip_file.close()

gzip_data = gzip_buffer.getvalue()
gzipped_array = split_data_into_chunkes(gzip_data, n)

def generate_stream():
for i in range(len(gzipped_array)):
yield (gzipped_array[i])

return Response(generate_stream(), headers={"Content-Type": "application/json", "Content-Encoding": "gzip"})

@app.route("/stream/deflate/<int:n>")
def stream_n_deflate_messages(n):
"""Stream n Deflate-encoded JSON responses
---
tags:
- Dynamic data
parameters:
- in: path
name: n
type: int
produces:
- application/json
responses:
200:
description: Streamed Deflate-encoded JSON responses.
"""
n = min(n, 100)
complete_data = create_data_for_streaming(n)
deflater = zlib.compressobj()
deflated_data = deflater.compress(complete_data.encode('ascii'))
deflated_data += deflater.flush()
deflated_array = split_data_into_chunkes(deflated_data, n)

def generate_stream():
for i in range(len(deflated_array)):
yield (deflated_array[i])

return Response(generate_stream(), headers={"Content-Type": "application/json", "Content-Encoding": "deflate"})

@app.route("/stream/brotli/<int:n>")
def stream_n_brotli_messages(n):
"""Stream n Brotli-encoded JSON responses
---
tags:
- Dynamic data
parameters:
- in: path
name: n
type: int
produces:
- application/json
responses:
200:
description: Streamed Brotli-encoded JSON responses.
"""
n = min(n, 100)
complete_data = create_data_for_streaming(n)
deflated_data = _brotli.compress(complete_data.encode('ascii'))
deflated_array = split_data_into_chunkes(deflated_data, n)

def generate_stream():
for i in range(len(deflated_array)):
yield (deflated_array[i])

return Response(generate_stream(), headers={"Content-Type": "application/json", "Content-Encoding": "br"})


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down
3 changes: 3 additions & 0 deletions httpbin/templates/httpbin.1.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ <h2 id="ENDPOINTS">ENDPOINTS</h2>
<li><a href="{{ url_for('digest_auth', qop='auth', user='user', passwd='passwd', algorithm='MD5', stale_after='never') }}"><code>/digest-auth/:qop/:user/:passwd/:algorithm</code></a> Challenges HTTP Digest Auth.</li>
<li><a href="{{ url_for('digest_auth', qop='auth', user='user', passwd='passwd', algorithm='MD5', stale_after='never') }}"><code>/digest-auth/:qop/:user/:passwd</code></a> Challenges HTTP Digest Auth.</li>
<li><a href="{{ url_for('stream_n_messages', n=20) }}"><code>/stream/:n</code></a> Streams <em>min(n, 100)</em> lines.</li>
<li><a href="{{ url_for('stream_n_gzip_messages', n=20) }}"><code>/stream/gzip:n</code></a> Streams <em>min(n, 100)</em> lines.</li>
<li><a href="{{ url_for('stream_n_deflate_messages', n=20) }}"><code>/stream/deflate:n</code></a> Streams <em>min(n, 100)</em> lines.</li>
<li><a href="{{ url_for('stream_n_brotli_messages', n=20) }}"><code>/stream/brotli:n</code></a> Streams <em>min(n, 100)</em> lines.</li>
<li><a href="{{ url_for('delay_response', delay=3) }}"><code>/delay/:n</code></a> Delays responding for <em>min(n, 10)</em> seconds.</li>
<li><a href="{{ url_for('drip', numbytes=5, duration=5, code=200) }}"><code>/drip?numbytes=n&amp;duration=s&amp;delay=s&amp;code=code</code></a> Drips data over a duration after an optional initial delay, then (optionally) returns with the given status code.</li>
<li><a href="{{ url_for('range_request', numbytes=1024) }}"><code>/range/1024?duration=s&amp;chunk_size=code</code></a> Streams <em>n</em> bytes, and allows specifying a <em>Range</em> header to select a subset of the data. Accepts a <em>chunk_size</em> and request <em>duration</em> parameter.</li>
Expand Down