def view_robots_page(request): """Simple Html Page""" response = Response() response.content = ROBOT_TXT response.content_type = 'text/plain' return response
def drip(request): """Drips data over a duration after an optional initial delay.""" args = CaseInsensitiveDict(request.args.items()) duration = float(args.get('duration', 2)) numbytes = int(args.get('numbytes', 10)) code = int(args.get('code', 200)) pause = duration / numbytes delay = float(args.get('delay', 0)) if delay > 0: time.sleep(delay) def generate_bytes(): for i in xrange(numbytes): yield u'*'.encode('utf-8') time.sleep(pause) response = Response(generate_bytes(), headers={ 'Content-Type': 'application/octet-stream', 'Content-Length': str(numbytes), }) response.status_code = code return response
def redirect_to(request): """302 Redirects to the given URL.""" args = CaseInsensitiveDict(request.args.items()) # We need to build the response manually and convert to UTF-8 to prevent # werkzeug from "fixing" the URL. This endpoint should set the Location # header to the exact string supplied. response = Response('') response.status_code = 302 response.headers['Location'] = args['url'].encode('utf-8') return response
def status_code(code): """Returns response object of given status code.""" redirect = dict(headers=dict(location=REDIRECT_LOCATION)) code_map = { 301: redirect, 302: redirect, 303: redirect, 304: dict(data=''), 305: redirect, 307: redirect, 401: dict(headers={'WWW-Authenticate': 'Basic realm="Fake Realm"'}), 402: dict(data='F**k you, pay me!', headers={'x-more-info': 'http://vimeo.com/22053820'}), 406: dict(data=json.dumps({ 'message': 'Client did not request a supported media type.', 'accept': ACCEPTED_MEDIA_TYPES }), headers={'Content-Type': 'application/json'}), 407: dict(headers={'Proxy-Authenticate': 'Basic realm="Fake Realm"'}), 418: dict( # I'm a teapot! data=ASCII_ART, headers={'x-more-info': 'http://tools.ietf.org/html/rfc2324'}), } r = Response() r.status_code = code if code in code_map: m = code_map[code] if 'data' in m: r.data = m['data'] if 'headers' in m: r.headers = m['headers'] return r
def status_code(code): """Returns response object of given status code.""" redirect = dict(headers=dict(location=REDIRECT_LOCATION)) code_map = { 301: redirect, 302: redirect, 303: redirect, 304: dict(data=''), 305: redirect, 307: redirect, 401: dict(headers={'WWW-Authenticate': 'Basic realm="Fake Realm"'}), 402: dict( data='F**k you, pay me!', headers={ 'x-more-info': 'http://vimeo.com/22053820' } ), 406: dict(data=json.dumps({ 'message': 'Client did not request a supported media type.', 'accept': ACCEPTED_MEDIA_TYPES }), headers={ 'Content-Type': 'application/json' }), 407: dict(headers={'Proxy-Authenticate': 'Basic realm="Fake Realm"'}), 418: dict( # I'm a teapot! data=ASCII_ART, headers={ 'x-more-info': 'http://tools.ietf.org/html/rfc2324' } ), } r = Response() r.status_code = code if code in code_map: m = code_map[code] if 'data' in m: r.data = m['data'] if 'headers' in m: r.headers = m['headers'] return r
def random_bytes(request, n): """Returns n random bytes generated with given seed.""" n = int(n) n = min(n, 100 * 1024) # set 100KB limit params = CaseInsensitiveDict(request.args.items()) if 'seed' in params: random.seed(int(params['seed'])) response = Response() # Note: can't just use os.urandom here because it ignores the seed response.data = bytearray(random.randint(0, 255) for i in range(n)) response.content_type = 'application/octet-stream' return response
def relative_redirect_n_times(request, n): """302 Redirects n times.""" n = int(n) assert n > 0 response = Response('') response.status_code = 302 if n == 1: response.headers['Location'] = url_for('view_get') return response response.headers['Location'] = app.url_for('relative_redirect_n_times', n=n - 1) return response
def stream_random_bytes(request, n): """Streams n random bytes generated with given seed, at given chunk size per packet. """ n = int(n) n = min(n, 100 * 1024) # set 100KB limit params = CaseInsensitiveDict(request.args.items()) if 'seed' in params: random.seed(int(params['seed'])) if 'chunk_size' in params: chunk_size = max(1, int(params['chunk_size'])) else: chunk_size = 10 * 1024 def generate_bytes(): chunks = bytearray() for i in xrange(n): chunks.append(random.randint(0, 255)) if len(chunks) == chunk_size: yield (bytes(chunks)) chunks = bytearray() if chunks: yield (bytes(chunks)) headers = {'Content-Type': 'application/octet-stream'} return Response(generate_bytes(), headers=headers)
def relative_redirect_n_times(request, n): """302 Redirects n times.""" n = int(n) assert n > 0 response = Response('') response.status_code = 302 if n == 1: response.headers['Location'] = url_for('view_get') return response response.headers['Location'] = app.url_for( 'relative_redirect_n_times', n=n - 1 ) return response
def stream_n_messages(request, n): """Stream n JSON messages""" n = int(n) response = get_dict(request, 'url', 'args', 'headers', 'origin') n = min(n, 100) def generate_stream(): for i in range(n): response['id'] = i yield json.dumps(response, default=json_dumps_default) + '\n' return Response(generate_stream(), headers={ 'Content-Type': 'application/json', })
def range_request(request, numbytes): """Streams n random bytes generated with given seed, at given chunk size per packet. """ numbytes = int(numbytes) if numbytes <= 0 or numbytes > (100 * 1024): response = Response(headers={ 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes' }) response.status_code = 404 response.content = 'number of bytes must be in the range (0, 10240]' return response params = CaseInsensitiveDict(request.args.items()) if 'chunk_size' in params: chunk_size = max(1, int(params['chunk_size'])) else: chunk_size = 10 * 1024 duration = float(params.get('duration', 0)) pause_per_byte = duration / numbytes request_headers = get_headers(request) first_byte_pos, last_byte_pos = get_request_range(request_headers, numbytes) if ( first_byte_pos > last_byte_pos or first_byte_pos not in xrange(0, numbytes) or last_byte_pos not in xrange(0, numbytes) ): response = Response(headers={ 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes', 'Content-Range': 'bytes */%d' % numbytes }) response.status_code = 416 return response def generate_bytes(): chunks = bytearray() for i in xrange(first_byte_pos, last_byte_pos + 1): # We don't want the resource to change across requests, so we need # to use a predictable data generation function chunks.append(ord('a') + (i % 26)) if len(chunks) == chunk_size: yield(bytes(chunks)) time.sleep(pause_per_byte * chunk_size) chunks = bytearray() if chunks: time.sleep(pause_per_byte * len(chunks)) yield(bytes(chunks)) content_range = 'bytes %d-%d/%d' % (first_byte_pos, last_byte_pos, numbytes) response_headers = { 'Content-Type': 'application/octet-stream', 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes', 'Content-Range': content_range} response = Response(generate_bytes(), headers=response_headers) if (first_byte_pos == 0) and (last_byte_pos == (numbytes - 1)): response.status_code = 200 else: response.status_code = 206 return response
def xml(request): response = Response(render_template('sample.xml')) response.headers['Content-Type'] = 'application/xml' return response
def image_svg(request): data = resource('images/svg_logo.svg') return Response(data, headers={'Content-Type': 'image/svg+xml'})
def image_webp(request): data = resource('images/wolf_1.webp') return Response(data, headers={'Content-Type': 'image/webp'})
def image_jpeg(request): data = resource('images/jackal.jpg') return Response(data, headers={'Content-Type': 'image/jpeg'})
def image_png(request): data = resource('images/pig_icon.png') return Response(data, headers={'Content-Type': 'image/png'})
def range_request(request, numbytes): """Streams n random bytes generated with given seed, at given chunk size per packet. """ numbytes = int(numbytes) if numbytes <= 0 or numbytes > (100 * 1024): response = Response(headers={ 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes' }) response.status_code = 404 response.content = 'number of bytes must be in the range (0, 10240]' return response params = CaseInsensitiveDict(request.args.items()) if 'chunk_size' in params: chunk_size = max(1, int(params['chunk_size'])) else: chunk_size = 10 * 1024 duration = float(params.get('duration', 0)) pause_per_byte = duration / numbytes request_headers = get_headers(request) first_byte_pos, last_byte_pos = get_request_range(request_headers, numbytes) if (first_byte_pos > last_byte_pos or first_byte_pos not in xrange(0, numbytes) or last_byte_pos not in xrange(0, numbytes)): response = Response( headers={ 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes', 'Content-Range': 'bytes */%d' % numbytes }) response.status_code = 416 return response def generate_bytes(): chunks = bytearray() for i in xrange(first_byte_pos, last_byte_pos + 1): # We don't want the resource to change across requests, so we need # to use a predictable data generation function chunks.append(ord('a') + (i % 26)) if len(chunks) == chunk_size: yield (bytes(chunks)) time.sleep(pause_per_byte * chunk_size) chunks = bytearray() if chunks: time.sleep(pause_per_byte * len(chunks)) yield (bytes(chunks)) content_range = 'bytes %d-%d/%d' % (first_byte_pos, last_byte_pos, numbytes) response_headers = { 'Content-Type': 'application/octet-stream', 'ETag': 'range%d' % numbytes, 'Accept-Ranges': 'bytes', 'Content-Range': content_range } response = Response(generate_bytes(), headers=response_headers) if (first_byte_pos == 0) and (last_byte_pos == (numbytes - 1)): response.status_code = 200 else: response.status_code = 206 return response
def view_deny_page(request): """Simple Html Page""" response = Response() response.content = ANGRY_ASCII response.content_type = 'text/plain' return response
def upload(request): _file = request.files['file'] response = Response(_file.read(), content_type=_file.content_type) return response