def normalize_headers(response_headers, strict=True):
 category = {}
 for idx in range(len(response_headers)):
  (key, val) = response_headers[idx]
  head = get_header(key, strict)
  if (not head):
   newhead = '-'.join([x.capitalize() for x in key.replace('_', '-').split('-')])
   response_headers[idx] = (newhead, val)
   category[newhead] = 4
   continue
  response_headers[idx] = (str(head), val)
  category[str(head)] = head.sort_order
 def key_func(item):
  value = item[0]
  return (category[value], value)
 response_headers.sort(key=key_func)

def normalize_headers ( response_headers , strict = True ) : category = { } for idx in range ( len ( response_headers ) ) : ( key , val ) = response_headers [ idx ] head = get_header ( key , strict ) if ( not head ) : newhead = '-' . join ( [ x . capitalize ( ) for x in key . replace ( '_' , '-' ) . split ( '-' ) ] ) response_headers [ idx ] = ( newhead , val ) category [ newhead ] = 4 continue response_headers [ idx ] = ( str ( head ) , val ) category [ str ( head ) ] = head . sort_order def key_func ( item ) : value = item [ 0 ] return ( category [ value ] , value ) response_headers . sort ( key = key_func )
sort headers as suggested by rfc 2616 this alters the underlying response_headers to use the common name for each header; as well as sorting them with general headers first .