#NEWLINE# def _url_collapse_path( path ) : #NEWLINE# #INDENT# "\n    Given a URL path, remove extra '/'s and '.' path elements and collapse\n    any '..' references and returns a colllapsed path.\n\n    Implements something akin to RFC-2396 5.2 step 6 to parse relative paths.\n    The utility of this function is limited to is_cgi method and helps\n    preventing some security attacks.\n\n    Returns: A tuple of (head, tail) where tail is everything after the final /\n    and head is everything before it.  Head will always start with a '/' and,\n    if it contains anything else, never have a trailing '/'.\n\n    Raises: IndexError if too many '..' occur within the path.\n\n    " #NEWLINE# path_parts = path . split ( '/' ) #NEWLINE# head_parts = [ ] #NEWLINE# for part in path_parts [ : ( - 1 ) ] : #NEWLINE# #INDENT# if ( part == '..' ) : #NEWLINE# #INDENT# head_parts . pop ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( part and ( part != '.' ) ) : #NEWLINE# #INDENT# head_parts . append ( part ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if path_parts : #NEWLINE# #INDENT# tail_part = path_parts . pop ( ) #NEWLINE# if tail_part : #NEWLINE# #INDENT# if ( tail_part == '..' ) : #NEWLINE# #INDENT# head_parts . pop ( ) #NEWLINE# tail_part = '' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tail_part == '.' ) : #NEWLINE# #INDENT# tail_part = '' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# tail_part = '' #NEWLINE# #UNINDENT# splitpath = ( ( '/' + '/' . join ( tail_part ) ) , tail_part ) #NEWLINE# collapsed_path = '/' . join ( splitpath ) #NEWLINE# return collapsed_path