def do_OPTIONS(self): target = f'{self.proxy_target}{self.path}' http_proxy = Http_Proxy(target=target, method='OPTIONS', headers=self.headers) response = http_proxy.make_request() self.handle_response(response)
def do_GET(self): """Respond to a GET request.""" target = f'{self.proxy_target}{self.path}' http_proxy = Http_Proxy(target=target, method='GET', headers=self.headers) response = http_proxy.make_request() self.handle_response(response)
def test_request_post__postman_echo(self): target = 'https://postman-echo.com/post?foo1=bar1&foo2=bar2' data = {'some':'data'} headers = {'Content-Type': 'application/json'} http_proxy = Http_Proxy(target=target, method='POST', body=data, headers=headers) body = json.loads(http_proxy.request_post().get('body')) self.assertEqual(body.get('args'), {'foo1': 'bar1', 'foo2': 'bar2'}) self.assertEqual(body.get('data'), 'some=data')
def do_PUT(self): # todo: refactor these methods (do_GET, do_POST, do_PUT) since most have the same code """Respond to a PUT request.""" target = f'{self.proxy_target}{self.path}' content_len = int(self.headers.get('content-length', 0)) if content_len > 0: post_body = self.rfile.read(content_len) else: post_body = '' http_proxy = Http_Proxy(target=target, method='PUT', body=post_body, headers=self.headers) response = http_proxy.make_request() self.handle_response(response)
def do_POST(self): """Respond to a POST request.""" target = f'{self.proxy_target}{self.path}' content_len = int(self.headers.get('content-length', 0)) if content_len > 0: post_body = self.rfile.read(content_len) else: post_body = '' http_proxy = Http_Proxy(target=target, method='POST', body=post_body, headers=self.headers) response = http_proxy.make_request() self.handle_response(response)
def __init__(self, event): """ :param event: data received from Lambda functions """ self.lambda_data = { 'body' : event.get('body' , {} ), 'path' : event.get('path' , '' ), 'method' : event.get('httpMethod' , '' ), 'domain_prefix' : event.get('requestContext', {}).get('domainPrefix' ), 'headers' : event.get('headers' , {} )} self.target = self.domain_parser(event.get('domain_prefix'), self.lambda_data.get('path')) self.http_proxy = Http_Proxy(body = self.lambda_data.get('body' ), headers = self.lambda_data.get('headers' ), method = self.lambda_data.get('method' ), target = self.target)
class Lambda_Event: def __init__(self, event): """ :param event: data received from Lambda functions """ self.lambda_data = { 'body' : event.get('body' , {} ), 'path' : event.get('path' , '' ), 'method' : event.get('httpMethod' , '' ), 'domain_prefix' : event.get('requestContext', {}).get('domainPrefix' ), 'headers' : event.get('headers' , {} )} self.target = self.domain_parser(event.get('domain_prefix'), self.lambda_data.get('path')) self.http_proxy = Http_Proxy(body = self.lambda_data.get('body' ), headers = self.lambda_data.get('headers' ), method = self.lambda_data.get('method' ), target = self.target) def domain_parser(self, domain_prefix, path=''): if domain_prefix == CONST_STACKOVERFLOW : target_domain = CONST_SITE_STACKOVERFLOW elif domain_prefix == CONST_GLASSWALL : target_domain = CONST_SITE_GLASSWALL elif domain_prefix == CONST_GW_PROXY : target_domain = CONST_DEFAULT_SITE elif domain_prefix is not None : target_domain = domain_prefix.replace("_", ".") else : target_domain = CONST_DEFAULT_SITE parsed_path = urlparse(path or '') url = urlunparse(ParseResult(scheme='https' , netloc=target_domain , path =parsed_path.path, params=parsed_path.params, query =parsed_path.query, fragment=parsed_path.fragment)) return url def get_response(self): return self.http_proxy.make_request()
class test_Http_Proxy(TestCase): def setUp(self): self.http_proxy = Http_Proxy(body=None, headers={}, method=None, target=None) def test_ctor(self): self.assertIsInstance(self.http_proxy, Http_Proxy) self.assertIs(type(self.http_proxy).__name__, 'Http_Proxy') def test_bad_request(self): self.assertEqual(self.http_proxy.bad_request('el-body'), { 'statusCode': 400, 'headers': {}, 'body': b'el-body' }) def test_make_request(self): http_proxy = Http_Proxy( target='https://postman-echo.com/get?foo1=bar1&foo2=bar2')
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': response = Http_Proxy(target=target, method='POST', headers=request.headers, body=request.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}'
def setUp(self): self.http_proxy = Http_Proxy(body=None, headers={}, method=None, target=None)
def test_make_request(self): http_proxy = Http_Proxy( target='https://postman-echo.com/get?foo1=bar1&foo2=bar2')
def test_request_get__glasswall(self): target = 'https://glasswallsolutions.com' http_proxy = Http_Proxy(target=target, method='GET') body = http_proxy.request_get().get('body') assert '<title>Unparalleled Protection' in body.decode()
def test_server_ok(self): is_base_64 = False headers = {'an': 'header'} body = 'el-body' self.assertEqual(Http_Proxy().ok(headers, body,is_base_64), {'isBase64Encoded': is_base_64, 'statusCode': 200, 'headers':headers, 'body': 'el-body'})
def test_server_error(self): self.assertEqual(Http_Proxy().server_error('el-body'), {'statusCode': 500, 'headers': {}, 'body': 'el-body'})
def test_request_get__postman_echo(self): target = 'https://postman-echo.com/get?foo1=bar1&foo2=bar2' http_proxy = Http_Proxy(target=target, method='GET') body = http_proxy.request_get().get('body') print(body)