ó
BÜXQc           @   s  d  Z  d d l Z d d l Z d d l m Z d d l m Z e e f Z d d l m Z d d l	 Z	 d „  Z
 e d „ Z d „  Z e j d	 ƒ Z e j d
 ƒ Z e j d ƒ Z e j d ƒ Z d „  Z e j d ƒ Z d „  Z d „  Z d „  Z d „  Z e d k re ƒ  n  d S(   s4  Utility functions for HTTP header value parsing and construction.

Copyright 1997-1998, Gisle Aas
Copyright 2002-2006, John J. Lee

This code is free software; you can redistribute it and/or modify it
under the terms of the BSD or ZPL 2.1 licenses (see the file
COPYING.txt included with the distribution).

iÿÿÿÿN(   t
   StringType(   t   UnicodeType(   t	   http2timec         C   sO   t  j j t j |  ƒ d ƒ d } d d g } | rE | d g 7} n  | | k S(   Ni   i   s   .htms   .htmls   .xhtml(   t   ost   patht   splitextt   _rfc3986t   urlsplit(   t   urlt   allow_xhtmlt   extt	   html_exts(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   is_html_file_extension   s
    #c         C   sŽ   |  s t  | | ƒ St |  ƒ } t | ƒ d k  r> t  | | ƒ S| d } | d } | d } d g } | r„ | d d d d g 7} n  | | k S(   sM   
    ct_headers: Sequence of Content-Type headers
    url: Response URL

    i   i    s	   text/htmls
   text/xhtmls   text/xmls   application/xmls   application/xhtml+xml(   R   t   split_header_wordst   len(   t
   ct_headersR   R	   t   headerst   first_headert   first_parametert   ctt
   html_types(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   is_html   s    


	c         C   s+   |  j  d ƒ \ } } |  j |  |  j | S(   s)   Return unmatched part of re.Match object.i    (   t   spant   string(   t   matcht   startt   end(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt	   unmatched4   s    s   ^\s*([^=\s;,]+)s&   ^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"s   ^\s*=\s*([^\s;,]*)s   \\(.)c   
      C   s¹  t  |  ƒ t k s t ‚ g  } x”|  D]Œ} | } g  } xa| ršt j | ƒ } | rt | ƒ } | j d ƒ } t j | ƒ } | rµ t | ƒ } | j d ƒ } t j	 d | ƒ } nE t
 j | ƒ } | rô t | ƒ } | j d ƒ } | j ƒ  } n d } | j | | f ƒ q: | j ƒ  j d ƒ rT| j ƒ  d } | rK| j | ƒ n  g  } q: t j d d | ƒ \ } }	 |	 d k s‘t d | | | f ‚ | } q: W| r% | j | ƒ q% q% W| S(	   sm  Parse header values into a list of lists containing key,value pairs.

    The function knows how to deal with ",", ";" and "=" as well as quoted
    values after "=".  A list of space separated tokens are parsed as if they
    were separated by ";".

    If the header_values passed as argument contains multiple values, then they
    are treated as if they were a single value separated by comma ",".

    This means that this function is useful for parsing header fields that
    follow this syntax (BNF as from the HTTP/1.1 specification, but we relax
    the requirement for tokens).

      headers           = #header
      header            = (token | parameter) *( [";"] (token | parameter))

      token             = 1*<any CHAR except CTLs or separators>
      separators        = "(" | ")" | "<" | ">" | "@"
                        | "," | ";" | ":" | "\" | <">
                        | "/" | "[" | "]" | "?" | "="
                        | "{" | "}" | SP | HT

      quoted-string     = ( <"> *(qdtext | quoted-pair ) <"> )
      qdtext            = <any TEXT except <">>
      quoted-pair       = "\" CHAR

      parameter         = attribute "=" value
      attribute         = token
      value             = token | quoted-string

    Each header is represented by a list of key/value pairs.  The value for a
    simple token (not part of a parameter) is None.  Syntactically incorrect
    headers will not necessarily be parsed as you would want.

    This is easier to describe with some examples:

    >>> split_header_words(['foo="bar"; port="80,81"; discard, bar=baz'])
    [[('foo', 'bar'), ('port', '80,81'), ('discard', None)], [('bar', 'baz')]]
    >>> split_header_words(['text/html; charset="iso-8859-1"'])
    [[('text/html', None), ('charset', 'iso-8859-1')]]
    >>> split_header_words([r'Basic realm="\"foo\bar\""'])
    [[('Basic', None), ('realm', '"foobar"')]]

    i   s   \1t   ,s   ^[=\s;]*t    i    s&   split_header_words bug: '%s', '%s', %sN(   t   typet   STRING_TYPESt   AssertionErrort   token_ret   searchR   t   groupt   quoted_value_ret	   escape_ret   subt   value_ret   rstript   Nonet   appendt   lstript
   startswitht   ret   subn(
   t   header_valuest   resultt   textt	   orig_textt   pairst   mt   namet   valuet   non_junkt   nr_junk_chars(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyR   =   sF    -	 	
 s   ([\"\\])c         C   sÒ   g  } x¼ |  D]´ } g  } x† | D]~ \ } } | d k	 r‘ t j d | ƒ si t j d | ƒ } d | } n  | d k r~ | } q‘ d | | f } n  | j | ƒ q  W| r | j d j | ƒ ƒ q q Wd j | ƒ S(   s²  Do the inverse of the conversion done by split_header_words.

    Takes a list of lists of (key, value) pairs and produces a single header
    value.  Attribute values are quoted if needed.

    >>> join_header_words([[("text/plain", None), ("charset", "iso-8859/1")]])
    'text/plain; charset="iso-8859/1"'
    >>> join_header_words([[("text/plain", None)], [("charset", "iso-8859/1")]])
    'text/plain, charset="iso-8859/1"'

    s   ^\w+$s   \\\1s   "%s"s   %s=%ss   ; s   , N(   R)   R-   R"   t   join_escape_reR&   R*   t   join(   t   listsR   R3   t   attrt   kt   v(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   join_header_words“   s    	 c         C   s<   |  j  d ƒ r |  d }  n  |  j d ƒ r8 |  d  }  n  |  S(   Nt   "i   iÿÿÿÿ(   R,   t   endswith(   R1   (    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   strip_quotes¯   s
    c         C   sx  d } g  } xe|  D]]} g  } t  } t j d | ƒ } x
t t | ƒ ƒ D]ö } | | } | j ƒ  } | d	 k rx qJ n  d
 | k r” | d }	 }
 n' t j d | d ƒ \ }	 }
 |	 j ƒ  }	 | d k r-|	 j ƒ  } | | k rè | }	 n  |	 d k r	t	 |
 ƒ }
 t
 } n  |	 d k r-t t	 |
 ƒ ƒ }
 q-n  | j |	 |
 f ƒ qJ W| r | s`| j d ƒ n  | j | ƒ q q W| S(   s5  Ad-hoc parser for Netscape protocol cookie-attributes.

    The old Netscape cookie format for Set-Cookie can for instance contain
    an unquoted "," in the expires field, so we have to use this ad-hoc
    parser instead of split_header_words.

    XXX This may not make the best possible effort to parse all the crap
    that Netscape Cookie headers contain.  Ronald Tschalar's HTTPClient
    parser is probably better, so could do worse than following that if
    this ever gives any trouble.

    Currently, this is also used for parsing RFC 2109 cookies.

    t   expirest   domainR   t   securet   versiont   ports   max-ages   ;\s*R   t   =s   \s*=\s*i   i    t   0(   s   expiress   domains   paths   secures   versions   ports   max-ageN(   s   versionRI   (   t   FalseR-   t   splitt   rangeR   R(   R)   R+   t   lowerRB   t   TrueR   R*   (   t
   ns_headerst   known_attrsR0   t	   ns_headerR3   t   version_sett   paramst   iit   paramR=   R>   t   lc(    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   parse_ns_headers¶   s>     
 		c          C   s%   d d  l  }  d d  l } |  j | ƒ S(   Niÿÿÿÿ(   t   doctestt   _headersutilt   testmod(   RX   RY   (    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   _testì   s    t   __main__(   t   __doc__R   R-   t   typesR    R   R   t   _utilR   R   R   RJ   R   R   t   compileR!   R$   R'   R%   R   R9   R?   RB   RW   R[   t   __name__(    (    (    s;   /scratch/rashmi/Condor_Script/src/mechanize/_headersutil.pyt   <module>
   s*   			U			6	