2022-04-12 11:09:35 +08:00
|
|
|
import aiohttp
|
|
|
|
from aiohttp import web, BasicAuth
|
2022-03-21 16:30:00 +08:00
|
|
|
from aiohttp import client
|
|
|
|
from .baseProcessor import *
|
|
|
|
|
2022-03-22 11:45:21 +08:00
|
|
|
class ProxyProcessor(BaseProcessor):
|
2022-03-21 16:30:00 +08:00
|
|
|
@classmethod
|
|
|
|
def isMe(self,name):
|
|
|
|
return name=='proxy'
|
|
|
|
|
|
|
|
async def path_call(self, request, params={}):
|
|
|
|
await self.set_run_env(request)
|
|
|
|
path = self.path
|
|
|
|
url = self.resource.entireUrl(request, path)
|
|
|
|
ns = self.run_ns
|
|
|
|
ns.update(params)
|
|
|
|
te = self.run_ns['tmpl_engine']
|
|
|
|
txt = await te.render(url,**ns)
|
|
|
|
data = json.loads(txt)
|
2022-06-23 14:58:03 +08:00
|
|
|
self.debug('proxyProcessor: data=%s' % data)
|
2022-03-21 16:30:00 +08:00
|
|
|
return data
|
|
|
|
|
|
|
|
async def datahandle(self,request):
|
2022-03-22 12:20:18 +08:00
|
|
|
chunk_size = 40960
|
2022-03-21 16:30:00 +08:00
|
|
|
d = await self.path_call(request)
|
|
|
|
reqH = request.headers.copy()
|
2022-04-12 11:09:35 +08:00
|
|
|
auth = None
|
|
|
|
if d.get('user') and d.get('password'):
|
|
|
|
auth = BasicAuth(d['user'], d['password'])
|
2022-03-21 16:30:00 +08:00
|
|
|
async with client.request(
|
|
|
|
request.method,
|
|
|
|
d['url'],
|
2022-04-12 11:09:35 +08:00
|
|
|
auth=auth,
|
2022-03-21 16:30:00 +08:00
|
|
|
headers = reqH,
|
|
|
|
allow_redirects=False,
|
2022-03-22 11:50:47 +08:00
|
|
|
data=await request.read()) as res:
|
2022-03-21 16:30:00 +08:00
|
|
|
headers = res.headers.copy()
|
2022-03-22 12:41:30 +08:00
|
|
|
# body = await res.read()
|
2022-03-22 12:37:43 +08:00
|
|
|
self.retResponse = web.StreamResponse(
|
2022-03-21 16:30:00 +08:00
|
|
|
headers = headers,
|
2022-03-22 12:18:18 +08:00
|
|
|
status = res.status
|
2022-03-22 12:41:30 +08:00
|
|
|
# ,body=body
|
2022-03-21 16:30:00 +08:00
|
|
|
)
|
2022-03-22 12:41:30 +08:00
|
|
|
await self.retResponse.prepare(request)
|
2022-03-22 12:18:18 +08:00
|
|
|
async for chunk in res.content.iter_chunked(chunk_size):
|
|
|
|
await self.retResponse.write(chunk)
|
2022-06-23 14:58:03 +08:00
|
|
|
self.debug('proxy: datahandle() finish')
|
2022-03-21 16:41:03 +08:00
|
|
|
|
2022-03-21 16:30:00 +08:00
|
|
|
|
|
|
|
def setheaders(self):
|
|
|
|
pass
|
|
|
|
|