Пример #1
0
	async def getToken(self, request, kw):
		token = kw.get("token", "")
		language = toExistLanguage(kw.get("language", "en"))
		if token:
			user = await token2user(token)
			if user:
				request.__user__ = user
				vipEndDate = user.get("vipEndDate", "")
				read = hasVip(vipEndDate)
				request.__user__["read"] = read
				request.__user__["request_uuid"] = request_uuid()
				request.__user__["language"] = language
			else:
				if self._auth:
					r = {"error": 403, "data": {}, "message": _("403_LOGIN",language)}
					resp = to_response(r)
					return resp
				else:
					request.__user__ = {}
					request.__user__["language"] = language
		else:
			if self._auth:
				r = {"error": 403, "data": {}, "message": _("403_LOGIN",language)}
				resp = to_response(r)
				return resp
			else:
				request.__user__ = {}
				request.__user__["language"] = language

		return True
Пример #2
0
	async def response(request):
		request.__user__ = {}
		r = await handler(request)
		if isinstance(r, web.StreamResponse):
			return r
		
		if isinstance(r, bytes):
			return to_response(r)
		if isinstance(r, str):
			return to_response(r)
		if isinstance(r, dict):
			template = r.get('__template__')
			if template is None:
				return to_response(r)
			else:
				r['__user__'] = request.__user__
				body=app['__templating__'].get_template(template).render(**r)
				return to_response(body, content_type = 'text/html;charset=utf-8')
		if isinstance(r, int) and r >= 100 and r < 600:
			return to_response(r)
		if isinstance(r, tuple) and len(r) == 2:
			t, m = r
			if isinstance(t, int) and t >= 100 and t < 600:
				return to_response(t, m)
			return to_response(r)
Пример #3
0
	async def request_frequency(self, request, kw):
		remote_ip = request.remote
		path_url = request.path
		language = toExistLanguage(kw.get("language", "en"))
		res = await frequency(remote_ip, path_url)
		if res:
			r = {"error": 416, "data": {}, "message": _("416_REQ_FREQUENT", language)}
			resp = to_response(r)
			return resp
		else:
			return True
Пример #4
0
	def  __call__(self, request):

		kw = None
		if self._has_var_kw_arg or self._has_named_kw_arg or self._required_kw_args or self._named_kw_args:
			if request.method in ('POST',"OPTIONS"):
				if not request.content_type:
					r = {"error": 404, "data": {}, "message": 'Missing Content-Type'}
					resp = to_response(r, method=request.method)
					return resp

				ct = request.content_type.lower()
				if ct.startswith('application/json'):
					params = yield from request.json()
					if not isinstance(params, dict):
						r = {"error": 404, "data": {}, "message": 'JSON Body must be object'}
						resp = to_response(r, method=request.method)
						return resp
					kw = params

				elif ct.startswith('application/x-www-form-urlencoded') or ct.startswith('multipart/form-data'):
					params = yield from request.post()
					kw = dict(**params)
				else:
					r = {"error": 404, "data": {}, "message": 'Unsupported Content-Type:%s' % request.content_type}
					resp = to_response(r, method=request.method)
					return resp

			if request.method == 'GET':
				qs = request.query_string
				if qs:
					kw = dict()
					for k, v in parse.parse_qs(qs, True).items():
						kw[k] = v[0]

		if kw is None:
			kw = dict(**request.match_info)
		else:
			if not self._has_var_kw_arg and self._named_kw_args:
				copy =  dict()
				for name in self._named_kw_args:
					if name in kw:
						copy[name] = kw[name]
				kw = copy
			for k, v in request.match_info.items():
				if k in kw:
					error('Duplicate arg name in named arg and kw args: %s' % k)
				kw[k] = v

		if self._has_request_arg:
			kw['request'] = request

		language = toExistLanguage(kw.get("language", "en"))
		if self._required_kw_args:
			for name in self._required_kw_args:
				if not name in kw:
					r = {"error": 500, "data": {}, "message": _("500_SERVER_ERROR", language)}
					resp = to_response(r, method=request.method)
					return resp

		request_info('call with args: %s' % str(kw))
		try:
			res = yield from self.request_frequency(request, kw)
			if res == True:
				resp = yield from self.getToken(request, kw)
				if resp == True:
					if "token" in kw:
						del kw["token"]
					if "language" in kw:
						del kw["language"]
					r = yield from self._func(**kw)
					return r
				else:
					return resp
			else:
				return res
		except Exception as e:
			errMsg = getErrorMsg()
			error("__call__ api error is: %s"%str(errMsg))
			r = {"error": 500, "data": {}, "message": _("500_SERVER_ERROR", language)}
			resp = to_response(r, method=request.method)
			return resp