File: app.py

package info (click to toggle)
pycurl 7.45.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,752 kB
  • sloc: python: 8,663; ansic: 6,891; makefile: 202; sh: 183
file content (134 lines) | stat: -rw-r--r-- 3,952 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
# vi:ts=4:et

import time as _time, sys
import flask
import werkzeug
try:
    import json
except ImportError:
    import simplejson as json

py3 = sys.version_info[0] == 3

app = flask.Flask(__name__)
app.debug = True

@app.route('/success')
def ok():
    return 'success'

@app.route('/short_wait')
def short_wait():
    _time.sleep(0.1)
    return 'success'

@app.route('/status/403')
def forbidden():
    return flask.Response('forbidden', 403)

@app.route('/status/404')
def not_found():
    return flask.Response('not found', 404)

@app.route('/postfields', methods=['GET', 'POST'])
def postfields():
    return json.dumps(dict(flask.request.form))

@app.route('/raw_utf8', methods=['POST'])
def raw_utf8():
    data = flask.request.data.decode('utf8')
    return json.dumps(data)

def xconvert_file(key, file):
    return {
        'key': key,
        'name': file.name,
        'filename': file.filename,
        'headers': file.headers,
        'content_type': file.content_type,
        'content_length': file.content_length,
        'data': file.read(),
    }

def convert_file(key, file):
    return {
        'name': file.name,
        'filename': file.filename,
        'data': file.read().decode(),
    }

@app.route('/files', methods=['POST'])
def files():
    files = [convert_file(key, flask.request.files[key]) for key in flask.request.files]
    return json.dumps(files)

@app.route('/header')
def header():
    return flask.request.headers.get(flask.request.args['h'], '')

# This is a hacky endpoint to test non-ascii text being given to libcurl
# via headers.
# HTTP RFC requires headers to be latin1-encoded.
# Any string can be decoded as latin1; here we encode the header value
# back into latin1 to obtain original bytestring, then decode it in utf-8.
# Thanks to bdarnell for the idea: https://github.com/pycurl/pycurl/issues/124
@app.route('/header_utf8')
def header_utf8():
    header_value = flask.request.headers.get(flask.request.args['h'], '' if py3 else b'')
    if py3:
        # header_value is a string, headers are decoded in latin1
        header_value = header_value.encode('latin1').decode('utf8')
    else:
        # header_value is a binary string, decode in utf-8 directly
        header_value = header_value.decode('utf8')
    return header_value

@app.route('/param_utf8_hack', methods=['POST'])
def param_utf8_hack():
    param = flask.request.form['p']
    return param

def pause_writer(interval):
    yield 'part1'
    _time.sleep(interval)
    yield 'part2'

@app.route('/pause')
def pause():
    return pause_writer(0.5)

@app.route('/long_pause')
def long_pause():
    return pause_writer(1)

@app.route('/utf8_body')
def utf8_body():
    # bottle encodes the body
    return 'Дружба народов'

@app.route('/invalid_utf8_body')
def invalid_utf8_body():
    return flask.Response(b'\xb3\xd2\xda\xcd\xd7', 200)

@app.route('/set_cookie_invalid_utf8')
def set_cookie_invalid_utf8():
    response = flask.Response('cookie set')
    # WARNING: The original bottle test passed '\xb3\xd2\xda\xcd\xd7...' as string
    # Presumably bottle encoded that as utf-8 in the response.
    # Flask on the other hand encodes such strings as latin-1 (chars in == bytes out).
    # In order to make the test pass I replicate the original bottle behavior by utf-8->latin1 roundtrip.
    response.headers['Set-Cookie'] = '\xb3\xd2\xda\xcd\xd7=%96%A6g%9Ay%B0%A5g%A7tm%7C%95%9A'.encode('utf-8').decode('latin-1')
    return response

@app.route('/content_type_invalid_utf8')
def content_type_invalid_utf8():
    response = flask.Response('content type set')
    # See the WARNING in set_cookie_invalid_utf8
    response.headers['Content-Type'] = '\xb3\xd2\xda\xcd\xd7'.encode('utf-8').decode('latin-1')
    return response

werkzeug.http.HTTP_STATUS_CODES[555] = '\xb3\xd2\xda\xcd\xd7'
@app.route('/status_invalid_utf8')
def status_invalid_utf8():
    return flask.Response(status=555)