Exemple #1
0
 def function(event, *args, **kwargs):
     path = events.page_path(event)
     if bucket_prefix:
         path = bucket_prefix.rstrip("/") + "/" + path.lstrip("/")
     if not path.lower().endswith("html"):
         obj = client.head_object(Bucket=bucket_name, Key=path)
         if obj.get("ContentType", "") not in TEXT_MIME_TYPES:
             if bucket_is_public:
                 url = "https://s3.amazonaws.com/{bucket_name}/{path}".format(
                     bucket_name=bucket_name, path=path)
             else:
                 # have to generate a presigned URL instead of the usual public one
                 url = client.generate_presigned_url(
                     ClientMethod="get_object",
                     Params={
                         "Bucket": bucket_name,
                         "Key": path
                     },
                     ExpiresIn=3600)
             return redirect(url)
     obj = client.get_object(Bucket=bucket_name, Key=path)
     body = obj["Body"].read()
     ctype = obj.get("ContentType", "text/html")
     return make_response(body,
                          code=200,
                          headers={"Content-Type": ctype},
                          prettify_html=True)
Exemple #2
0
 def function(event, *args, **kwargs):
     path = events.page_path(event)
     if bucket_prefix:
         path = bucket_prefix.rstrip("/") + "/" + path.lstrip("/")
     try:
         obj = client.head_object(Bucket=bucket_name, Key=path)
         return True, {}
     except:
         traceback.print_exc()
         return False, {}
Exemple #3
0
def get_debug_blob(event):
    templates = env.list_templates()
    event = add_event_params(event)
    event["debug"] = {}
    event["debug"]["matchers"] = response_core.get_matchers_debug_blob()
    event["debug"]["events.base_path(event)"] = events.base_path(event)
    event["debug"]["events.page_path(event)"] = events.page_path(event)
    return "<pre>\n{}\n\nAvailable Jinja2 templates:\n\n{}\n</pre>".format(
        snekjson.dumps(event, indent=2, sort_keys=True,
                       ignore_type_error=True).replace(">", "&gt").replace(
                           "<", "&lt"), "\n".join(templates))
Exemple #4
0
def add_event_params(event, *args, **kwargs):
    params = {}
    event["queryStringParameters"] = event.get("queryStringParameters") if event.get("queryStringParameters") else {}
    params.update(event["queryStringParameters"])
    if event["httpMethod"] == "POST" and event.get("body"):
        body = event["body"]
        update_lists(params, parse_body(body))
    cookie_dict = {}
    try:
        cookies = http.cookies.SimpleCookie()
        cookies.load(event["headers"].get("Cookie",""))
        for k in cookies:
            morsel = cookies[k]
            cookie_dict[morsel.key] = morsel.value
    except:
        traceback.print_exc()
    params = listify_dict(params)
    params = {"kwargs":params}
    params["single_kwargs"] = unlistify_dict(params["kwargs"])
    params["cookies"] = cookie_dict
    params["path"] = {}
    params["path"]["raw"] = event["path"]
    params["path"]["base"] = events.base_path(event)
    params["path"]["page"] = events.page_path(event)
    params["path"]["full"] = "https://" + event["headers"]["Host"] + params["path"]["raw"]
    params["path"]["full_base"] = "https://" + event["headers"]["Host"] + params["path"]["base"]
    if "STATIC_BUCKET" in os.environ and "STATIC_PATH" in os.environ:
        params["path"]["static_base"] = "https://s3.amazonaws.com/{STATIC_BUCKET}/{STATIC_PATH}".format(**os.environ)
    else:
        params["path"]["static_base"] = params["path"]["base"]
    params["http"] = {}
    params["http"]["Referer"] = event.get("headers",{}).get("Referer","")
    params["http"]["Referer"] = params["http"]["Referer"] if params["http"]["Referer"] else params["path"]["full_base"]
    params["http"]["User-Agent"] = event.get("headers",{}).get("User-Agent")
    params["http"]["Method"] = event["httpMethod"]
    params["redirect"] = params["single_kwargs"].get("redirect", params["http"]["Referer"])
    params["redirect"] = params["redirect"] if params["redirect"] else params["path"]["full_base"]
    params["objects"] = {}
    event["params"] = params
    return event
Exemple #5
0
 def matcher_function(self, event):
     path = events.page_path(event)
     response = self.matcher.match(path)
     if response:
         return (True, response.groupdict())
     return False, None