def url_join(*fragments):
    """ Join fragments of a URL, dealing with slashes."""
    if len(fragments) == 0:
        return ''
    buff = []
    for fragment in fragments:
        stripped = fragment.strip('/')
        if len(stripped) > 0:
            buff.append(stripped)
            buff.append('/')

    last = fragments[-1]
    # add a trailing '/' if present.
    if len(last) > 0 and last[-1] != '/':
        buff.pop()
    return ''.join(buff)