def func(*args, **kwargs): resp = f(*args, **kwargs) try: # assume response instance resp.headers except AttributeError: # try wrapping string try: resp = Response(resp) except Exception: # give up return resp if headers: for header, value in headers.items(): resp.headers.add(header, value) if '_nc' in request.args: resp.cache_control.no_cache = True else: if cache_private: resp.cache_control.private = True if cache_max_age: if not resp.cache_control.private: resp.cache_control.public = True resp.cache_control.max_age = cache_max_age # Always add ETag to cached responses resp.add_etag() resp.make_conditional(request) return resp
def etag_match(inputs, rsp): headers = inputs.get("headers", None) etag = headers.get("Etag", None) etags_match = True if etag is not None: tmp = Response(json.dumps(rsp), status=400, content_type="application/json") tmp.add_etag() etags_match = werkzeug.http.unquote_etag(etag) == tmp.get_etag() return etags_match
def site_js(): l = get_locale() js_i18n = cache.get('site_js_%s' % (l,)) if not js_i18n: js_i18n = render_template('site.js') cache.set('site_js_%s' % (l,), js_i18n, timeout=60 * 60) r = Response(js_i18n, headers={ 'Content-type': 'application/javascript', 'Cache-control': 'private, max-age=3600' }) r.add_etag() r.make_conditional(request) return r
def site_js(): l = get_locale() js_i18n = cache.get('site_js_%s' % (l, )) if not js_i18n: js_i18n = render_template('site.js') cache.set('site_js_%s' % (l, ), js_i18n, timeout=60 * 60) r = Response(js_i18n, headers={ 'Content-type': 'application/javascript', 'Cache-control': 'private, max-age=3600' }) r.add_etag() r.make_conditional(request) return r
def get_badge(): projects, job_names = get_args(request.args) if (not projects and not job_names) or not projects: return Response("Invaild Request") url = genarate_zuul_url(projects, job_names) success = check_the_result(url, projects, job_names) response = Response(RESP_TYPE[success], mimetype='image/svg+xml') response.cache_control.no_cache = True response.cache_control.no_store = True response.cache_control.private = True response.cache_control.max_age = 0 response.expires = datetime.datetime(1984, 1, 1) response.headers['X-Content-Type-Options'] = 'nosniff' response.add_etag() return response
def avatar_v1(address): try: address = aioxmpp.JID.fromstr(address) except (ValueError, TypeError): return abort(400, "bad address") metadata = db.session.query( model.Avatar.hash_, model.Avatar.last_updated, model.Avatar.mime_type, ).filter(model.Avatar.address == address, ).one_or_none() if metadata is None: return abort(404, "no avatar stored") hash_, last_updated, mime_type = metadata response = Response(mimetype=mime_type) response.status_code = 500 response.add_etag(hash_) response.last_modified = last_updated response.expires = datetime.utcnow() + CACHE_AVATAR_TTL response.headers["Content-Security-Policy"] = \ "frame-ancestors 'none'; default-src 'none'; style-src 'unsafe-inline'" if (request.if_none_match.contains(hash_) or (request.if_modified_since is not None and last_updated <= request.if_modified_since)): response.status_code = 304 return response if request.method == "HEAD": # do not fetch the data, only its size length, = db.session.query(sqlalchemy.func.length( model.Avatar.data), ).filter( model.Avatar.address == address).one() response.status_code = 200 response.content_length = length return response data, = db.session.query( model.Avatar.data, ).filter(model.Avatar.address == address).one() response.data = data response.status_code = 200 return response
def profile_profile_entry_id(profile_entry_id): global _profile_service inputs = log_and_extract_input(demo, {"parameters": profile_entry_id}) rsp_data = None try: profile_service = _get_profile_service() logger.error("/api/profile/profile_entry_id: _profile_service = " + str(profile_service)) if inputs["method"] == "GET": rsp = profile_service.get_by_profile_entry_id(profile_entry_id) if rsp is not None: rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "NO PROFILE ENTRIES WITH THAT PROFILE ENTRY ID FOUND" elif inputs["method"] == "DELETE": rsp = profile_service.get_by_profile_entry_id(profile_entry_id) if rsp is not None: rsp = profile_service.delete_profile_entry(profile_entry_id) rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "NO PROFILE ENTRIES WITH THAT PROFILE ENTRY ID FOUND TO DELETE" elif inputs["method"] == "PUT": body = inputs.get("body", None) rsp = profile_service.get_by_profile_entry_id(profile_entry_id) if rsp is not None: if body is None: rsp_data = None rsp_status = 404 rsp_txt = "Body Not Received" else: # body["id"] = rsp["id"] rsp = profile_service.update_profile_entry( body, profile_entry_id, rsp) rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "PROFILE ENTRY NOT FOUND" else: rsp_data = None rsp_status = 501 rsp_txt = "NOT IMPLEMENTED" if rsp_data is not None: full_rsp = Response(json.dumps(rsp_data), status=rsp_status, content_type="application/json") full_rsp.add_etag() else: full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") except Exception as e: log_msg = "/profile/: Exception = " + str(e) logger.error(log_msg) rsp_status = 500 # rsp_txt = "INTERNAL SERVER ERROR. Please take COMSE6156 -- Cloud Native Applications." rsp_txt = str(e) full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") log_response("/profile/profile_entry_id", rsp_status, rsp_data, rsp_txt) return full_rsp
def user_email(email): global _user_service inputs = log_and_extract_input(demo, {"parameters": email}) rsp_data = None try: user_service = _get_user_service() logger.error("/api/user/email: _user_service = " + str(user_service)) if inputs["method"] == "GET": rsp = user_service.get_by_email(email) if rsp is not None: rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "NOT FOUND" elif inputs["method"] == "DELETE": rsp = user_service.get_by_email(email) if rsp is not None: if rsp["status"] == "DELETED": rsp_data = None rsp_status = 404 rsp_txt = "User Account " + rsp[ "email"] + "has already been deleted" else: rsp = user_service.delete_user(email) rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "USER NOT FOUND" elif inputs["method"] == "PUT": body = inputs.get("body", None) rsp = user_service.get_by_email(email) if rsp is not None: if rsp["status"] == "DELETED": rsp_data = None rsp_status = 404 rsp_txt = "User Account " + rsp["email"] + "is deleted" elif body is None: rsp_data = None rsp_status = 404 rsp_txt = "Body Not Received" elif not etag_match(inputs, rsp): rsp_data = None rsp_status = 404 rsp_txt = "ETag did not match, underlying data has changed already." else: body["id"] = rsp["id"] rsp = user_service.update_user(body, email) rsp_data = rsp rsp_status = 200 rsp_txt = "OK" else: rsp_data = None rsp_status = 404 rsp_txt = "USER NOT FOUND" else: rsp_data = None rsp_status = 501 rsp_txt = "NOT IMPLEMENTED" if rsp_data is not None: full_rsp = Response(json.dumps(rsp_data), status=rsp_status, content_type="application/json") full_rsp.add_etag() else: full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") except Exception as e: log_msg = "/email: Exception = " + str(e) logger.error(log_msg) rsp_status = 500 rsp_txt = "INTERNAL SERVER ERROR. Please take COMSE6156 -- Cloud Native Applications." full_rsp = Response(rsp_txt, status=rsp_status, content_type="text/plain") log_response("/email", rsp_status, rsp_data, rsp_txt) return full_rsp
def css(): res = Response('*{font-family:Courier}', mimetype='text/css') res.headers['Cache-Control'] = 'public, max-age=31536000' res.add_etag() return res