Ejemplo n.º 1
0
def post_response(request_dict, keep_connection, client_ip):
    if request_dict['Content-Type'][0] != 'multipart/form-data':
        return header_maker('400', keep_connection=False)
    boundary = None
    for content_info in request_dict['Content-Type']:
        if content_info.startswith('boundary='):
            boundary = content_info[9:]
    if boundary is None:
        return header_maker('400', keep_connection=False)
    info_list = request_dict['body'].split('--' + boundary)
    info_list = info_list[1:-1]
    for index, item in enumerate(info_list):
        info_list[index] = item[2:-2]
    contents_dicts = [{} for thank_you_frenkel in range(len(info_list))]
    for info_num, info in enumerate(info_list):
        contents_dicts[info_num]['header'] = info.split('\r\n\r\n')[0]
        contents_dicts[info_num]['data'] = info.split('\r\n\r\n')[1]
    for item_num, item in enumerate(contents_dicts):
        header = item['header'].split('\r\n')
        for line in header:
            if line.startswith('Content-Disposition: form-data; '):
                variables = line[32:].split('; ')
                variables_dict = {}
                for variable in variables:
                    variables_dict[variable.split('=')[0]] = variable.split('=')[1][1:-1]
        contents_dicts[item_num].update(variables_dict)
    file_ending = None
    file_name = None
    file_password = None
    file_data = None
    for content in contents_dicts:
        if content['name'] == 'name':
            file_name = content['data']
        elif content['name'] == 'password':
            file_password = content['data']
        elif content['name'] == 'file':
            if len(content['filename'].split('.')) == 1:
                file_ending = ''
            else:
                file_ending = content['filename'].split('.')[-1]
            file_data = content['data']
    if file_name is None or Utility.all_chars(file_name.replace('%20', ' '), ' ') or file_data is None:
        return header_maker('400', keep_connection=False)
    if file_ending != '':
        file_name = '%s.%s' % (file_name, file_ending)
    if file_password is None:
        file_password = ''
    file_saved = MemoryHandler.hash_save_file(file_name, file_password, file_data)
    if file_saved:
        message = '<i>file saved, you can get it at:</i><br><u>http://%s/getfile?name=%s' % (MemoryHandler.get_server_settings()['domain'], file_name.replace(' ', '%20'))
        if file_password != '':
            message += '&password='******' ', '%20')
        message += '</u>'
    else:
        message = 'file not saved'
    got_file, replay_file = MemoryHandler.get_server_file('uploadfile.html')
    if not got_file:
        return header_maker('500', keep_connection=False)
    new_replay_text = Utility.add_to_mark(replay_file, message)
    return header_maker('200', content_len=str(len(new_replay_text)), download_file=False, keep_connection=keep_connection, file_name='uploadfile.html') + new_replay_text
Ejemplo n.º 2
0
def get_response(request_dict, keep_connection, client_ip):
    parameter_dict = {}
    message = ''
    file_name = ''
    url = request_dict['header'][1].split('?')
    if url[0].startswith('/getfile') and len(url) == 2:
        parameters_list = url[1].replace(';', '&').split('&')
        for parameter in parameters_list:
            if len(parameter.split('=')) == 2:
                parameter_dict[parameter.split('=')[0]] = parameter.split('=')[1]
        if 'name' not in parameter_dict:
            return header_maker('400', keep_connection=False)
        file_name = parameter_dict['name'].replace('%20', ' ')
        file_password = ''
        if 'password' in parameter_dict:
            file_password = parameter_dict['password'].replace('%20', ' ')
        has_file, requested_file = MemoryHandler.hash_get_file(file_name, file_password)
        if has_file:
            file_text = requested_file.read()
            file_name = os.path.basename(requested_file.name)
            requested_file.close()
            print 'Sending the file "%s" to: %s' % (file_name, client_ip)
            return header_maker('200', content_len=str(len(file_text)), download_file=True, keep_connection=keep_connection, file_name=file_name) + file_text
        else:
            print 'Can`t get the file "%s" for: %s' % (file_name, client_ip)
            file_name = 'getfile.html'
            message = 'Wrong file name or password'
    if file_name == '':
        if url[0] == '/':
            file_name = 'index.html'
        elif url[0] == '/getfile':
            file_name = 'getfile.html'
        elif url[0] == '/uploadfile':
            file_name = 'uploadfile.html'
        elif url[0] == '/delfile':
            file_name = 'delfile.html'
        else:
            file_name = url[0][1:]
    file_found, file_data = MemoryHandler.get_server_file(file_name)
    if file_found:
        if file_name == 'getfile.html':
            file_data = Utility.add_to_mark(file_data, message)
        print 'Sending the server file "%s" to: %s' % (file_name, client_ip)
        return header_maker('200', content_len=str(len(file_data)), download_file=False, keep_connection=True, file_name=file_name) + file_data
    return header_maker('404', keep_connection=False)
Ejemplo n.º 3
0
def delete_response(request_dict, keep_connection):
    if request_dict['Content-Type'][0] != 'multipart/form-data':
        return header_maker('400', keep_connection=False)
    boundary = None
    for content_info in request_dict['Content-Type']:
        if content_info.startswith('boundary='):
            boundary = content_info[9:]
    if boundary is None:
        return header_maker('400', keep_connection=False)
    info_list = request_dict['body'].split('--' + boundary)
    info_list = info_list[1:-1]
    for index, item in enumerate(info_list):
        info_list[index] = item[2:-2]
    contents_dicts = [{} for thank_you_frenkel in range(len(info_list))]
    for info_num, info in enumerate(info_list):
        contents_dicts[info_num]['header'] = info.split('\r\n\r\n')[0]
        contents_dicts[info_num]['data'] = info.split('\r\n\r\n')[1]
    for item_num, item in enumerate(contents_dicts):
        header = item['header'].split('\r\n')
        for line in header:
            if line.startswith('Content-Disposition: form-data; '):
                variables = line[32:].split('; ')
                variables_dict = {}
                for variable in variables:
                    variables_dict[variable.split('=')[0]] = variable.split('=')[1][1:-1]
        contents_dicts[item_num].update(variables_dict)
    file_name = None
    file_password = None
    for content in contents_dicts:
        if content['name'] == 'name':
            file_name = content['data']
        elif content['name'] == 'password':
            file_password = content['data']
    if file_name is None:
        return header_maker('400', keep_connection=False)
    if file_password is None:
        file_password = ''
    deleted_file, message = MemoryHandler.hash_delete_file(file_name, file_password)
    got_file, replay_file = MemoryHandler.get_server_file('delfile.html')
    if not got_file:
        return header_maker('500', keep_connection=False)
    new_replay_text = Utility.add_to_mark(replay_file, message)
    return header_maker('200', content_len=str(len(new_replay_text)), download_file=False, keep_connection=keep_connection, file_name='uploadfile.html') + new_replay_text