def test_reset_charset_after_setting(self): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = 'koi8-r' resp.charset = None self.assertIsNone(resp.charset)
def test_setting_content_type(self): req = self.make_request('GET', '/') resp = StreamResponse(req) resp.content_type = 'text/html' self.assertEqual('text/html', resp.headers['content-type'])
def test_reset_charset(self): req = self.make_request('GET', '/') resp = StreamResponse(req) resp.content_type = 'text/html' resp.charset = None self.assertIsNone(resp.charset)
def test_setting_charset(self): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = 'koi8-r' self.assertEqual('text/html; charset=koi8-r', resp.headers['content-type'])
def test_reset_charset_after_setting(): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = 'koi8-r' resp.charset = None assert resp.charset is None
async def prepare_download(self, disposition=None, filename=None, content_type=None, size=None, **kwargs): if disposition is None: disposition = self.request.query.get('disposition', 'attachment') try: file = self.field.get(self.field.context or self.context) except AttributeError: file = None if file is None and filename is None: raise HTTPNotFound(content={ 'message': 'File or custom filename required to download' }) cors_renderer = app_settings['cors_renderer'](self.request) headers = await cors_renderer.get_headers() headers.update({ 'Content-Disposition': '{}; filename="{}"'.format( disposition, filename or file.filename) }) download_resp = StreamResponse(headers=headers) download_resp.content_type = content_type or file.guess_content_type() if size or file.size: download_resp.content_length = size or file.size await download_resp.prepare(self.request) return download_resp
async def hello(request): resp = StreamResponse() name = request.match_info.get('name', 'Anonymous') answer = ('Hello, ' + name).encode('utf8') resp.content_length = len(answer) resp.content_type = 'text/plain' await resp.prepare(request) resp.write(answer) await resp.write_eof() return resp
async def intro(request): txt = textwrap.dedent("""\ Type {url}/hello/John {url}/simple or {url}/change_body in browser url bar """).format(url='127.0.0.1:8080') binary = txt.encode('utf8') resp = StreamResponse() resp.content_length = len(binary) resp.content_type = 'text/plain' await resp.prepare(request) resp.write(binary) return resp
async def __call__(self): if hasattr(self.context, 'file_path'): filepath = str(self.context.file_path.absolute()) filename = self.context.file_path.name with open(filepath, 'rb') as f: resp = StreamResponse(headers={ 'CONTENT-DISPOSITION': 'attachment; filename="%s"' % filename }) resp.content_type = mimetypes.guess_type(filename) data = f.read() resp.content_length = len(data) await resp.prepare(self.request) resp.write(data) return resp
def test_reset_charset(): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = None assert resp.charset is None
def test_setting_charset(): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = 'koi8-r' assert 'text/html; charset=koi8-r' == resp.headers['content-type']
def test_setting_content_type(): resp = StreamResponse() resp.content_type = 'text/html' assert 'text/html' == resp.headers['content-type']
def test_reset_charset(self): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = None self.assertIsNone(resp.charset)
def test_setting_content_type(self): resp = StreamResponse() resp.content_type = 'text/html' self.assertEqual('text/html', resp.headers['content-type'])
def test_setting_charset(): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = 'koi8-r' assert 'text/html; charset=koi8-r' == resp.headers['content-type']
async def package_handler(request): """docstring for package_handler""" _response = StreamResponse() if request.query.__contains__('action'): action = request.query['action'] else: action = None if action is not None: if action == 'index': return await _package_index() elif action == 'count': local_index_count = request.query['local_index'] if local_index_count is not None: server_index_count = '2' if local_index_count == server_index_count: return json_response({'result': 'True'}) else: return json_response({'result': 'False'}) elif action == 'download': package_name = request.query['package_name'] _response_download = StreamResponse() _body = '<div>Download for package <a href=#>{0}</a> will start soon</div>'.format( package_name) _response_download.content_length = len(_body) _response_download.content_type = 'text/html' binary = _body.encode('utf8') await _response_download.prepare(request) await _response_download.write(binary) return _response_download else: return Response( body='Invalid value provided for parameter "action"') else: _index_url = request.app.router['package-handler'].url_for( ).with_query({'action': 'index'}) _ahref_index = '<li>Get package <a href="{0}">Index</a></li>'.format( _index_url.human_repr()) _count_url = request.app.router['package-handler'].url_for( ).with_query({ 'action': 'count', 'local_index': '2' }) _ahref_count = '<li>Get package <a href="{0}">count</a></li>'.format( _count_url.human_repr()) _download_url = request.app.router['package-handler'].url_for( ).with_query({ 'action': 'download', 'package_name': 'package1' }) _ahref_download = '<li>Get <a href="{0}">{1}</a></li>'.format( _download_url.human_repr(), 'Package1') _list = '<div><ul>{0}{1}{2}</ul></div>'.format(_ahref_index, _ahref_count, _ahref_download) _message = '<center><div>Welcome to package index service. You have below options to work with this service:</div></center><div/><div/>{0}'.format( _list) h_body = '<html><head><title>PackageService</title></head><body>{0}</body></html>'.format( _message) _response.content_length = len(h_body) _response.content_type = 'text/html' binary = h_body.encode('utf8') await _response.prepare(request) await _response.write(binary) return _response
def test_reset_charset() -> None: resp = StreamResponse() resp.content_type = "text/html" resp.charset = None assert resp.charset is None
def test_setting_charset() -> None: resp = StreamResponse() resp.content_type = "text/html" resp.charset = "koi8-r" assert "text/html; charset=koi8-r" == resp.headers["content-type"]
def test_setting_content_type() -> None: resp = StreamResponse() resp.content_type = "text/html" assert "text/html" == resp.headers["content-type"]
def test_setting_content_type(): resp = StreamResponse() resp.content_type = 'text/html' assert 'text/html' == resp.headers['content-type']
def test_setting_content_type(self): resp = StreamResponse() resp.content_type = "text/html" self.assertEqual("text/html", resp.headers["content-type"])
def test_reset_charset(): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = None assert resp.charset is None
def test_reset_charset(self): resp = StreamResponse() resp.content_type = 'text/html' resp.charset = None self.assertIsNone(resp.charset)
def test_setting_content_type(self): resp = StreamResponse() resp.content_type = 'text/html' self.assertEqual('text/html', resp.headers['content-type'])
async def GET_AttributeValue(request): """HTTP method to return an attribute value""" log.request(request) app = request.app log.info("GET_AttributeValue") collection = getRequestCollectionName( request) # returns datasets|groups|datatypes obj_id = request.match_info.get('id') if not obj_id: msg = "Missing object id" log.warn(msg) raise HTTPBadRequest(reason=msg) if not isValidUuid(obj_id, obj_class=collection): msg = f"Invalid object id: {obj_id}" log.warn(msg) raise HTTPBadRequest(reason=msg) attr_name = request.match_info.get('name') validateAttributeName(attr_name) username, pswd = getUserPasswordFromRequest(request) if username is None and app['allow_noauth']: username = "******" else: await validateUserPassword(app, username, pswd) domain = getDomainFromRequest(request) if not isValidDomain(domain): msg = f"Invalid domain value: {domain}" log.warn(msg) raise HTTPBadRequest(reason=msg) bucket = getBucketForDomain(domain) # get domain JSON domain_json = await getDomainJson(app, domain) verifyRoot(domain_json) # TBD - verify that the obj_id belongs to the given domain await validateAction(app, domain, obj_id, username, "read") req = getDataNodeUrl(app, obj_id) req += '/' + collection + '/' + obj_id + "/attributes/" + attr_name log.debug("get Attribute: " + req) params = {} if bucket: params["bucket"] = bucket dn_json = await http_get(app, req, params=params) log.debug("got attributes json from dn for obj_id: " + str(dn_json)) attr_shape = dn_json["shape"] log.debug(f"attribute shape: {attr_shape}") if attr_shape["class"] == 'H5S_NULL': msg = "Null space attributes can not be read" log.warn(msg) raise HTTPBadRequest(reason=msg) accept_type = getAcceptType(request) response_type = accept_type # will adjust later if binary not possible type_json = dn_json["type"] shape_json = dn_json["shape"] item_size = getItemSize(type_json) if item_size == 'H5T_VARIABLE' and accept_type != "json": msg = "Client requested binary, but only JSON is supported for variable length data types" log.info(msg) response_type = "json" if response_type == "binary": arr_dtype = createDataType(type_json) # np datatype np_shape = getShapeDims(shape_json) try: arr = jsonToArray(np_shape, arr_dtype, dn_json["value"]) except ValueError: msg = "Bad Request: input data doesn't match selection" log.warn(msg) raise HTTPBadRequest(reason=msg) output_data = arr.tobytes() log.debug( f"GET AttributeValue - returning {len(output_data)} bytes binary data" ) cors_domain = config.get("cors_domain") # write response try: resp = StreamResponse() resp.content_type = "application/octet-stream" resp.content_length = len(output_data) # allow CORS if cors_domain: resp.headers['Access-Control-Allow-Origin'] = cors_domain resp.headers[ 'Access-Control-Allow-Methods'] = "GET, POST, DELETE, PUT, OPTIONS" resp.headers[ 'Access-Control-Allow-Headers'] = "Content-Type, api_key, Authorization" await resp.prepare(request) await resp.write(output_data) except Exception as e: log.error(f"Got exception: {e}") raise HTTPInternalServerError() finally: await resp.write_eof() else: resp_json = {} if "value" in dn_json: resp_json["value"] = dn_json["value"] hrefs = [] obj_uri = '/' + collection + '/' + obj_id attr_uri = obj_uri + '/attributes/' + attr_name hrefs.append({'rel': 'self', 'href': getHref(request, attr_uri)}) hrefs.append({'rel': 'home', 'href': getHref(request, '/')}) hrefs.append({'rel': 'owner', 'href': getHref(request, obj_uri)}) resp_json["hrefs"] = hrefs resp = await jsonResponse(request, resp_json) log.response(request, resp=resp) return resp
def test_setting_charset(self): resp = StreamResponse() resp.content_type = "text/html" resp.charset = "koi8-r" self.assertEqual("text/html; charset=koi8-r", resp.headers["content-type"])
async def image_proxy(request, url, referer=None): if not referer or is_referer_force_url(url): referer = get_referer_of_url(url) LOG.info(f'proxy image {url} referer={referer}') session = response = None async def do_cleanup(): nonlocal session, response if response: response.close() if session: await session.close() try: user_agent = DEFAULT_USER_AGENT if callable(user_agent): user_agent = user_agent() headers = {'User-Agent': user_agent} for h in PROXY_REQUEST_HEADERS: if h in request.headers: headers[h] = request.headers[h] referer_headers = dict(headers) referer_headers['Referer'] = referer session = _create_aiohttp_client_session() # 先尝试发带Referer的请求,不行再尝试不带Referer response = await get_response(session, url, referer_headers) if response.status in REFERER_DENY_STATUS: LOG.info(f'proxy image {url!r} referer={referer!r} ' f'failed {response.status}, will try without referer') response.close() response = await get_response(session, response.url, headers) is_chunked = response.headers.get('Transfer-Encoding', '').lower() == 'chunked' # using chunked encoding is forbidden for HTTP/1.0 if is_chunked and request.version < HttpVersion11: version = 'HTTP/{0.major}.{0.minor}'.format(request.version) error_msg = f"using chunked encoding is forbidden for {version}" LOG.info(f'proxy image {url!r} referer={referer!r} failed: {error_msg}') response.close() raise ImageProxyError(error_msg) except ImageProxyError as ex: await do_cleanup() return ex.to_response() except Exception: await do_cleanup() raise try: my_response = StreamResponse(status=response.status) # 'Content-Length', 'Content-Type', 'Transfer-Encoding' if is_chunked: my_response.enable_chunked_encoding() elif response.headers.get('Transfer-Encoding'): my_response.headers['Transfer-Encoding'] = response.headers['Transfer-Encoding'] if response.headers.get('Content-Length'): content_length = int(response.headers['Content-Length']) if content_length > MAX_IMAGE_SIZE: message = 'image too large, size={}'.format(content_length) return json_response({'message': message}, status=413) my_response.content_length = content_length if response.headers.get('Content-Type'): my_response.content_type = response.headers['Content-Type'] for h in PROXY_RESPONSE_HEADERS: if h in response.headers: my_response.headers[h] = response.headers[h] await my_response.prepare(request) except Exception: await do_cleanup() raise try: content_length = 0 async for chunk in response.content.iter_chunked(8 * 1024): content_length += len(chunk) if content_length > MAX_IMAGE_SIZE: LOG.warning(f'image too large, abort the response, url={url!r}') my_response.force_close() break await my_response.write(chunk) await my_response.write_eof() except _IMAGE_NETWORK_ERROR_S as ex: msg = "image proxy failed {}: {} url={!r}".format(type(ex).__name__, ex, url) LOG.warning(msg) finally: await do_cleanup() my_response.force_close() await my_response.write_eof() return my_response