def merge(basepath, relpath, has_authority):
    """Merge two URI path components into a single path component.

    Follows rules specified in Section 5.2.3 of RFC 3986.

    The algorithm in the RFC treats the empty basepath edge case
    differently for URIs with and without an authority section, which
    is why the third argument is necessary.
    """
    if has_authority and basepath == '':
        return '/' + relpath
    slash = basepath.rfind('/')
    return basepath[:slash+1] + relpath