示例#1
0
def proxy(path=''):
    global SITE_NAME
    excluded_headers = [
        'content-encoding', 'content-length', 'transfer-encoding',
        'connection', 'host'
    ]
    try:
        target = f'{SITE_NAME}{path}'
        if request.method == 'GET':
            response = Http_Proxy(target=target,
                                  method='GET',
                                  headers=request.headers).make_request()
            #transformed_body = response.get('body').decode('utf-8').encode('utf-8')
            transformed_body = apply_transformations(response.get('body'),
                                                     response.get('headers'))
            return Response(transformed_body, response.get('statusCode'),
                            response.get('headers'))

        elif request.method == 'POST':
            response = Http_Proxy(target=target,
                                  method='POST',
                                  headers=request.headers,
                                  body=request.get_data()).make_request()
            return Response(response.get('body'), response.get('statusCode'),
                            response.get('headers'))

    except Exception as error:
        return f'{error}'
def proxy(path=''):
    global SITE_NAME
    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection','host']
    try:
        #print(request.headers)
        target= f'{SITE_NAME}{path}'
        if request.method=='GET':
            response = Http_Proxy(target=target,method='GET',headers=request.headers).make_request()
            return Response(response.get('body'), response.get('statusCode'),response.get('headers'))


        elif request.method=='POST':
            #print(f'in POST: {request.get_data()}')
            #from osbot_utils.utils.Dev import Dev
            #Dev.pprint(request.headers)
            response = Http_Proxy(target=target, method='POST', headers=request.headers,body=request.get_data()).make_request()
            #print(response)
            return Response(response.get('body'), response.get('statusCode'), response.get('headers'))

            # headers = {}
            # for (key,value) in request.headers:
            #     if key.lower() != 'host':
            #         headers[key] = value
            # #print(headers)
            # #print('content type: ', request.headers.get('Content-Type'))
            # if request.headers.get('Content-Type')== 'application/json':
            #     print('POST : ', request.get_json())
            #     resp = requests.post(f'{SITE_NAME}{path}',json=request.get_json(), headers=headers)
            # else:
            #     resp = requests.post(f'{SITE_NAME}{path}', data=request.get_data(), headers=headers)
            # headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
            # response = Response(resp.content, resp.status_code, headers)
            # return response
        elif request.method=='PUT':
            headers = {}
            for (key,value) in request.headers:
                if key.lower() != 'host':
                    headers[key] = value
            #print(headers)
            #print('content type: ', request.headers.get('Content-Type'))
            if request.headers.get('Content-Type')== 'application/json':
                #print('PUT : ', request.get_json())
                resp = requests.put(f'{SITE_NAME}{path}',json=request.get_json(), headers=headers)
            else:
                resp = requests.post(f'{SITE_NAME}{path}', data=request.get_data(), headers=headers)
            headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
            response = Response(resp.content, resp.status_code, headers)
            return response
        elif request.method=='DELETE':
            resp = requests.delete(f'{SITE_NAME}{path}').content
            headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
            response = Response(resp.content, resp.status_code, headers)
            return response
    except Exception as error:
        return f'{error}'