
BXQc           @   s  d  Z  d d l Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z
 d d l m Z m Z d d l m Z d d l m Z m Z d d l m Z d d l m Z d d l Z d d l Z e j d	  j Z e j d
  j Z d Z d Z d e f d     YZ d d$ d     YZ  d e  e j f d     YZ! d e  e
 j" f d     YZ# d   Z$ d e f d     YZ% d e j& f d     YZ' d e f d     YZ( d e f d     YZ) d e f d     YZ* d    Z+ d!   Z, d" e f d#     YZ- d S(%   sU  HTTP related handlers.

Note that some other HTTP handlers live in more specific modules: _auth.py,
_gzip.py, etc.


Copyright 2002-2006 John J Lee <jjl@pobox.com>

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).

iN(   t   StringIO(   t	   HTTPErrort   BaseHandler(   t   is_html(   t   unescapet   unescape_charref(   t   Request(   t   response_seek_wrappert	   mechanizes   mechanize.robotsi   s   latin-1t   EndOfHeadErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR	   0   s    t   AbstractHeadParserc        	   B   sn   e  Z d Z e j Z e Z d	   Z d
   Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z d   Z RS(   t   htmlt   headt   titlet   baset   scriptt   stylet   metat   linkt   objectc         C   s   g  |  _  d  S(   N(   t
   http_equiv(   t   self(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   __init__9   s    c         C   s   d  } } xP | D]H \ } } | d k r; |  j |  } q | d k r |  j |  } q q W| d  k	 r | d  k	 r |  j j | | f  n  d  S(   Ns
   http-equivt   content(   t   Nonet   unescape_attr_if_requiredR   t   append(   R   t   attrsR   R   t   keyt   value(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt
   start_meta<   s    
c         C   s   t     d  S(   N(   R	   (   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   end_headF   s    c         C   s'   |  j  t d | |  j |  j   d  S(   Ns   &%s;(   t   handle_dataR   t   _entitydefst	   _encoding(   R   t   name(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   handle_entityrefI   s    	c         C   s   |  j  t | |  j   d  S(   N(   R"   R   R$   (   R   R%   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   handle_charrefN   s    c         C   s   t  | |  j |  j  S(   N(   R   R#   R$   (   R   R%   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   unescape_attrR   s    c         C   s:   i  } x- | j    D] \ } } |  j |  | | <q W| S(   N(   t   itemsR(   (   R   R   t   escaped_attrsR   t   val(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   unescape_attrsV   s    c         C   s   |  j  d |  d  S(   Ns   &%s;(   R"   (   R   t   ref(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   unknown_entityref]   s    c         C   s   |  j  d |  d  S(   Ns   &#%s;(   R"   (   R   R-   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   unknown_charref`   s    (	   s   htmls   heads   titles   bases   scripts   styles   metas   links   object(   R
   R   t
   head_elemst   htmlentitydefst   name2codepointR#   t   DEFAULT_ENCODINGR$   R   R    R!   R&   R'   R(   R,   R.   R/   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   1   s     			
						t   XHTMLCompatibleHeadParserc           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C   s!   t  j  j |   t j |   d  S(   N(   t
   HTMLParserR   R   (   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   f   s    c         C   s   | |  j  k r t    n  y t |  d |  } WnF t k
 rz y t |  d |  } Wn t k
 rl q X| |  n X| |  d  S(   Nt   start_t   do_(   R0   R	   t   getattrt   AttributeError(   R   t   tagR   t   method(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   handle_starttagj   s    c         C   sQ   | |  j  k r t    n  y t |  d |  } Wn t k
 rE n X|   d  S(   Nt   end_(   R0   R	   R8   R9   (   R   R:   R;   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   handle_endtagy   s    c         C   s   |  j  |  S(   N(   R(   (   R   R%   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    c         C   s   | S(   N(    (   R   R%   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    (   R
   R   R   R<   R>   R   R   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR4   d   s
   			
	t
   HeadParserc           B   s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s   t  s t  d  S(   N(   t   Falset   AssertionError(   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   _not_called   s    c         C   s!   t  j j |   t j |   d  S(   N(   t   sgmllibt
   SGMLParserR   R   (   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    c         C   s8   | |  j  k r t    n  | d k r4 | |  n  d  S(   NR   (   R0   R	   (   R   R:   R;   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR<      s    c         C   s   |  j  | |  j |  d  S(   N(   R<   RB   (   R   R:   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   unknown_starttag   s    c         C   s&   | |  j  k r |   n	 t    d  S(   N(   R0   R	   (   R   R:   R;   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR>      s    
c         C   s   |  j  |  S(   N(   R(   (   R   R%   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    (   R
   R   RB   R   R<   RE   R>   R   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR?      s   					c         C   sX   xN |  j  t  } y | j |  Wn t k
 r7 Pn Xt |  t k r Pq q | j S(   s"   Return a list of key, value pairs.(   t   readt   CHUNKt   feedR	   t   lenR   (   t   fileobjt   parsert   data(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt
   parse_head   s    t   HTTPEquivProcessorc           B   s2   e  Z d  Z d Z e e d  Z d   Z e Z RS(   s7   Append META HTTP-EQUIV headers to regular HTTP headers.i,  c         C   s   | |  _  | |  _ d  S(   N(   t   head_parser_classt   _allow_xhtml(   R   RO   t   i_want_broken_xhtml_support(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    	c         C   s  t  | d  s t |  } n  | j   } | j   } | j d  } t | | |  j  ry. z t | |  j    } Wd  | j	 d  XWn t
 j t j f k
 r qXxf | D][ \ } } | | j | j   <| d | }	 x+ |	 j d  D] }
 | j j |
 d  q Wq Wn  | S(   Nt   seeks   content-typei    s   : s   
(   t   hasattrR   t   infot   geturlt
   getheadersR   RP   RM   RO   RR   R5   t   HTMLParseErrorRC   t   SGMLParseErrort   dictt   lowert   splitt   headersR   (   R   t   requestt   responset   http_messaget   urlt   ct_hdrst   html_headerst   hdrR+   t   textt   line(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   http_response   s(    (	   R
   R   t   __doc__t   handler_orderR?   R@   R   Rf   t   https_response(    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyRN      s   	t   MechanizeRobotFileParserc           B   s5   e  Z d  d d  Z d d  Z d   Z d   Z RS(   t    c         C   s,   t  j j |  |  | |  _ t j |  _ d  S(   N(   t   robotparsert   RobotFileParserR   t   _openert   _sockettimeoutt   _GLOBAL_DEFAULT_TIMEOUTt   _timeout(   R   R`   t   opener(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR      s    	c         C   s4   d d  l  } | d  k r' | j   } n  | |  _  d  S(   Ni(   Rn   R   t   OpenerDirector(   R   Rr   Rn   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt
   set_opener   s    c         C   s   | |  _  d  S(   N(   Rq   (   R   t   timeout(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   set_timeout   s    c         C   si  |  j  d k r |  j   n  t |  j d t d t d |  j } y |  j  j |  } WnI t	 k
 rk } n7 t
 t j t f k
 r } t d |  j | f  d SXg  } | j   } x) | r | j | j    | j   } q W| j } | d k s| d k rt |  _ t d  nN | d	 k r9t |  _ t d
  n, | d k re| ret d  |  j |  n  d S(   s4   Reads the robots.txt URL and feeds it to the parser.t   unverifiablet   visitRu   s   ignoring error opening %r: %sNi  i  s   disallow alli  s	   allow alli   s   parse lines(   Rn   R   Rt   R   R`   t   TrueR@   Rq   t   openR   t   IOErrort   sockett   errort   OSErrort   debug_robotst   readlineR   t   stript   codet   disallow_allt	   allow_allt   parse(   R   t   reqt   ft   exct   linesRe   t   status(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyRF      s6    				
N(   R
   R   R   R   Rt   Rv   RF   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyRj      s   	t   RobotExclusionErrorc           B   s   e  Z d    Z RS(   c         G   s$   t  t j |  f |  | |  _ d  S(   N(   t   applyR   R   R]   (   R   R]   t   args(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   
  s    (   R
   R   R   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   	  s   t   HTTPRobotRulesProcessorc           B   sc   e  Z d  Z y d d l m Z Wn d d l m Z e Z n Xe Z e d  Z	 d   Z
 e
 Z RS(   i   i(   t   HTTPMessage(   t   Messagec         C   s   | |  _  d  |  _ d  |  _ d  S(   N(   t	   rfp_classR   t   rfpt   _host(   R   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR     s    		c      
   C   s  | j    } | d k r | S| j   d k r2 | S| j   } t | d d   } | d  k	 r | j   d k r | j   | k r | S| |  j k r)|  j   |  _ y |  j j |  j	  Wn% t
 k
 r t d |  j j  n X|  j j | d | d  |  j j | j  |  j j   | |  _ n  | j d d  } |  j j | | j    rZ| Sd	 } t | | j   d
 | |  j t    t |    d  S(   Nt   httpt   httpss   /robots.txtt   _origin_reqs'   %r instance does not support set_openers   ://s
   User-agentRk   s    request disallowed by robots.txti  (   s   https   https(   t   get_typet   get_selectort   get_hostR8   R   R   R   R   Rt   t   parentR9   t   debugt	   __class__t   set_urlRv   Ru   RF   t
   get_headert	   can_fetcht   get_full_urlR   t   http_response_classR    (   R   R]   t   schemet   hostt
   origin_reqt   uat   msg(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   http_request  s>    	(   R
   R   Rh   t   httplibR   t	   mimetoolsR   R   Rj   R   R   t   https_request(    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR     s   
	,t   HTTPRefererProcessorc           B   s5   e  Z d  Z d   Z d   Z d   Z e Z e Z RS(   sY  Add Referer header to requests.

    This only makes sense if you use each RefererProcessor for a single
    chain of requests only (so, for example, if you use a single
    HTTPRefererProcessor to fetch a series of URLs extracted from a single
    page, this will break).

    There's a proper implementation of this in mechanize.Browser.

    c         C   s   d  |  _ d  S(   N(   R   t   referer(   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   X  s    c         C   s9   |  j  d  k	 r5 | j d  r5 | j d |  j   n  | S(   Nt   Referer(   R   R   t
   has_headert   add_unredirected_header(   R   R]   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   [  s    c         C   s   | j    |  _ | S(   N(   RU   R   (   R   R]   R^   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyRf   a  s    (   R
   R   Rg   R   R   Rf   R   Ri   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR   M  s   
			c         C   s\   |  j  d  r |  j d  s< |  j  d  rL |  j d  rL |  d d !}  n  t j |  d  S(   Nt   "t   'i   is   latin-1(   t
   startswitht   endswitht   _rfc3986t	   clean_url(   R`   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   clean_refresh_urli  s    c         C   s   |  j  d  } | d k r t |  |   |  | d } } | j  d  } d } | d k r | |  | | d } } t |  } n  | d k s | j   j   d k r t    q n t |   d } } | | f S(   s  
    >>> parse_refresh_header("1; url=http://example.com/")
    (1.0, 'http://example.com/')
    >>> parse_refresh_header("1; url='http://example.com/'")
    (1.0, 'http://example.com/')
    >>> parse_refresh_header("1")
    (1.0, None)
    >>> parse_refresh_header("blah")  # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
    ValueError: invalid literal for float(): blah

    t   ;ii   t   =R`   N(   t   findt   floatR   R   R   RZ   t
   ValueError(   t   refresht   iit   pauset   newurl_spect   jjR   t   newurl(    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   parse_refresh_headerp  s    $t   HTTPRefreshProcessorc           B   s2   e  Z d  Z d Z d e d  Z d   Z e Z RS(   s>  Perform HTTP Refresh redirections.

    Note that if a non-200 HTTP code has occurred (for example, a 30x
    redirect), this processor will do nothing.

    By default, only zero-time Refresh headers are redirected.  Use the
    max_time attribute / constructor argument to allow Refresh with longer
    pauses.  Use the honor_time attribute / constructor argument to control
    whether the requested pause is honoured (with a time.sleep()) or
    skipped in favour of immediate redirection.

    Public attributes:

    max_time: see above
    honor_time: see above

    i  i    c         C   s"   | |  _  | |  _ t j |  _ d  S(   N(   t   max_timet
   honor_timet   timet   sleept   _sleep(   R   R   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR     s    		c   	   
   C   s(  | j  | j | j   } } } | d k r$| j d  r$| j d  d } y t |  \ } } Wn  t k
 r t d |  | SX| d  k r | j	   } n  |  j
 d  k s | |  j
 k r| d k r |  j r |  j |  n  | | d <|  j j d | | d | |  } q$t d |  n  | S(	   Ni   R   i    s   bad Refresh header: %rgMbP?t   locationR   s   Refresh header ignored: %r(   R   R   RT   t   has_keyRV   R   R   R   R   RU   R   R   R   R   R}   (	   R   R]   R^   R   R   t   hdrsR   R   R   (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyRf     s&     
		(   R
   R   Rg   Rh   Ry   R   Rf   Ri   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyR     s
   	(    (.   Rg   R5   t	   cStringIOR    R1   t   loggingRl   R|   R   t   _sgmllib_copyRC   t   _urllib2_forkR   R   t   _headersutilR   t   _htmlR   R   t   _requestR   t	   _responseR   R   Ro   t	   getLoggerR   R   RG   R3   t	   ExceptionR	   R   R4   RD   R?   RM   RN   Rm   Rj   R   R   R   R   R   R   (    (    (    s4   /scratch/rashmi/Condor_Script/src/mechanize/_http.pyt   <module>   sB   
3&	'.?		