Esempio n. 1
0
def find_target_post(target_url):
    current_app.logger.debug("looking for target post at %s", target_url)

    # follow redirects if necessary
    redirect_url = urllib.request.urlopen(target_url).geturl()
    if redirect_url and redirect_url != target_url:
        current_app.logger.debug("followed redirection to %s", redirect_url)
        target_url = redirect_url

    parsed_url = urllib.parse.urlparse(target_url)

    if not parsed_url:
        current_app.logger.warn("Could not parse target_url of received webmention: %s", target_url)
        return None

    try:
        # FIXME this is a less-than-perfect fix for hosting from a
        # subdirectory. The url_map may have some clever work-around.
        parsed_site_root = urllib.parse.urlparse(get_settings().site_url)
        site_prefix = parsed_site_root.path
        if site_prefix.endswith("/"):
            site_prefix = site_prefix[:-1]
        if not parsed_url.path.startswith(parsed_site_root.path):
            raise NotFound

        urls = current_app.url_map.bind(get_settings().site_url)
        path = parsed_url.path[len(site_prefix) :]
        current_app.logger.debug("target path with no prefix %s", path)
        endpoint, args = urls.match(path)
        current_app.logger.debug("found match for target url %r: %r", endpoint, args)
    except NotFound:
        current_app.logger.warn("Webmention could not find target for %s", parsed_url.path)
        return None

    post = None
    if endpoint == "views.post_by_path":
        year = args.get("year")
        month = args.get("month")
        slug = args.get("slug")
        post = Post.load_by_path("{}/{:02d}/{}".format(year, month, slug))

    elif endpoint == "views.post_by_date":
        post_type = args.get("post_type")
        year = args.get("year")
        month = args.get("month")
        day = args.get("day")
        index = args.get("index")
        post = Post.load_by_date(post_type, year, month, day, index)

    elif endpoint == "views.post_by_old_date":
        post_type = args.get("post_type")
        yymmdd = args.get("yymmdd")
        year = int("20" + yymmdd[0:2])
        month = int(yymmdd[2:4])
        day = int(yymmdd[4:6])
        post = Post.load_by_date(post_type, year, month, day, index)

    elif endpoint == "views.post_by_id":
        dbid = args.get("dbid")
        post = Post.load_by_id(dbid)

    if not post:
        current_app.logger.warn("Webmention target points to unknown post: {}".format(args)),

    return post
Esempio n. 2
0
def find_target_post(target_url):
    current_app.logger.debug("looking for target post at %s", target_url)

    # follow redirects if necessary
    redirect_url = urllib.request.urlopen(target_url).geturl()
    if redirect_url and redirect_url != target_url:
        current_app.logger.debug("followed redirection to %s", redirect_url)
        target_url = redirect_url

    parsed_url = urllib.parse.urlparse(target_url)

    if not parsed_url:
        current_app.logger.warn(
            "Could not parse target_url of received webmention: %s",
            target_url)
        return None

    try:
        # FIXME this is a less-than-perfect fix for hosting from a
        # subdirectory. The url_map may have some clever work-around.
        parsed_site_root = urllib.parse.urlparse(get_settings().site_url)
        site_prefix = parsed_site_root.path
        if site_prefix.endswith('/'):
            site_prefix = site_prefix[:-1]
        if not parsed_url.path.startswith(parsed_site_root.path):
            raise NotFound

        urls = current_app.url_map.bind(get_settings().site_url)
        path = parsed_url.path[len(site_prefix):]
        current_app.logger.debug('target path with no prefix %s', path)
        endpoint, args = urls.match(path)
        current_app.logger.debug('found match for target url %r: %r', endpoint,
                                 args)
    except NotFound:
        current_app.logger.warn('Webmention could not find target for %s',
                                parsed_url.path)
        return None

    post = None
    if endpoint == 'views.post_by_path':
        year = args.get('year')
        month = args.get('month')
        slug = args.get('slug')
        post = Post.load_by_path('{}/{:02d}/{}'.format(year, month, slug))

    elif endpoint == 'views.post_by_date':
        post_type = args.get('post_type')
        year = args.get('year')
        month = args.get('month')
        day = args.get('day')
        index = args.get('index')
        post = Post.load_by_date(post_type, year, month, day, index)

    elif endpoint == 'views.post_by_old_date':
        post_type = args.get('post_type')
        yymmdd = args.get('yymmdd')
        year = int('20' + yymmdd[0:2])
        month = int(yymmdd[2:4])
        day = int(yymmdd[4:6])
        post = Post.load_by_date(post_type, year, month, day, index)

    elif endpoint == 'views.post_by_id':
        dbid = args.get('dbid')
        post = Post.load_by_id(dbid)

    if not post:
        current_app.logger.warn(
            "Webmention target points to unknown post: {}".format(args)),

    return post