def setup_cgi(interp, params, argv, post_data=None): get = OrderedDict() post = OrderedDict() files = OrderedDict() space = interp.space query = get_param(params, 'QUERY_STRING') if query is not None: unpack_query(get, query, space) script_name = get_param(params, 'SCRIPT_NAME') initial_server_dict = OrderedDict() if script_name is not None: initial_server_dict['PHP_SELF'] = space.wrap(script_name) cookie = get_param(params, 'HTTP_COOKIE') content_length = get_param(params, 'CONTENT_LENGTH') content_type_set = get_param(params, 'CONTENT_TYPE') content_type = "" boundary = "" for k in all_keys_from(params): initial_server_dict[k] = space.wrap(get_param(params, k)) if content_type_set is not None and content_length is not None: m = search("[; ,]", content_type_set) if m: start = m.start(0) assert start >= 0 content_type = content_type_set[:start] content_type = content_type.lower() else: content_type = content_type_set.lower() if (content_type == 'x-www-form-urlencoded' or content_type == 'application/x-www-form-urlencoded'): content_length = int(content_length) if post_data is None: stdin = interp.open_stdin_stream() post_data = stdin.read(content_length) unpack_query(post, post_data, space) elif content_type == 'multipart/form-data': m = search("boundary", content_type_set) if m: end = m.end(0) assert end >= 0 boundary = content_type_set[end + 1:] if not boundary: interp.warn("Missing boundary, ignoring post") fp = RStringIO() fp.write(post_data) fp.seek(0) post, files = parse_multipart(space, fp, boundary, OrderedDict(), OrderedDict()) fp.close() else: interp.warn("Unknown content type: %s, ignoring post" % content_type) return CGIConfig(space.new_array_from_rdict(get), space.new_array_from_rdict(post), space.new_array_from_rdict(files), initial_server_dict, cookie)
def test_tell(): f = RStringIO() f.write('0123') f.write('456') assert f.tell() == 7 f.seek(2) for i in range(3, 20): f.write('X') assert f.tell() == i assert f.getvalue() == '01XXXXXXXXXXXXXXXXX' f.seek(1) f.close() assert f.tell() == 0