o
    DZ'                     @   s   d dl Z d dlZd dlZz
d dlmZmZ W n ey+   d dlmZ d dlmZ Y nw d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ G dd de	ZdS )    N)Requesturlopen)r   )r   )AbstractReader)MibInfo)decode)__version__)error)debugc                   @   s.   e Zd ZdZdZdddZdd Zd	d
 ZdS )
HttpReaderzFetch ASN.1 MIB text by name from a web site.

    *HttpReader* class instance tries to download ASN.1 MIB files
    by name and return their contents to caller.
    z@mib@   Fc                 C   sT   d|rdpd||t |f | _t| dttjd tjd tjd tjf | _dS )	a  Create an instance of *HttpReader* bound to specific URL.

           Note:
               The `http_proxy` and `https_proxy` environment variables are
               respected by the underlying `urllib` stdlib module.

           Args:
               host (str): domain name or IP address of web server
               port (int): TCP port web server is listening
               locationTemplate (str): location part of the URL optionally containing @mib@
                   magic placeholder to be replaced with MIB name. If @mib@ magic is not present,
                   MIB name is appended to `locationTemplate`

           Keyword Args:
               timeout (int): response timeout
               ssl (bool): access HTTPS web site
        z%s://%s:%d%sZhttpsZhttpzpysmi-%s; python-%s.%s.%s; %sr         N)	r   _urlsocketZsetdefaulttimeoutpysmi_versionsysversion_infoplatform_user_agent)selfZhostZportZlocationTemplateZtimeoutZssl r   9/usr/lib/python3/dist-packages/pysmi/reader/httpclient.py__init__#   s   

zHttpReader.__init__c                 C   s   | j S )N)r   )r   r   r   r   __str__>   s   zHttpReader.__str__c           	   
   C   s  d| j d}t|}tjtj@ otd|  | |D ]\}}| j| jv r/| j| j|}n| j| }tjtj@ o@td|  zt	||d}t
|}W n tyi   tjtj@ oftd|t d f  Y qw tjtj@ owtd|j  |jd	krztt|d
d}W n ty   tjtj@ otdt d   t }Y nw tjtj@ otd||d
f  t||||dt|| jf  S  t||||dt|| jf  S qtjd| | d)Nz
text/plain)ZAcceptz
User-Agentzlooking for MIB %sztrying to fetch MIB from %s)headerszfailed to fetch MIB from %s: %sr   zHTTP response %s   zLast-Modifiedz%a, %d %b %Y %H:%M:%S %Zzmalformed HTTP headers: %sz fetching source MIB %s, mtime %s)pathfilenamemtimezsource MIB %s not found)reader)r   r   r	   loggerZ
flagReaderZgetMibVariants	MIB_MAGICr   replacer   r   	Exceptionr   exc_infocodetimemktimestrptimeZ	getheaderr   readZ
maxMibSizer   ZPySmiReaderFileNotFoundError)	r   Zmibnamer   ZmibaliasZmibfileZurlZreqZresponser   r   r   r   getDataA   sL   
$
 $$zHttpReader.getDataN)r   F)__name__
__module____qualname____doc__r"   r   r   r+   r   r   r   r   r
      s    
r
   )r   r   r'   Zurllib2r   r   ImportErrorZurllib.requestZpysmi.reader.baser   Zpysmi.mibinfor   Zpysmi.compatr   Zpysmir   r   r   r	   r
   r   r   r   r   <module>   s    