
    io              	       (   U d 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	m
Z
 ddl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mZ d	dlmZ i Zdaded<   ddZd Z G d dej<                        Zd Z de!de!de"de#z  de!z  f   fdZ$de!de!fdZ%y)z#Implementations for a favicon proxy    )CallableN)	HTTPError)get_setting)new_hmac
is_hmac_of)SearxEngineResponseException)sxng_request   )DEFAULT_RESOLVER_MAP)cacheFaviconProxyConfigCFGc                     | a y )N)r   )cfgs    %/root/searxng/searx/favicons/proxy.pyinitr      s    
C    c                  v    i } t        dd       }|r(t        j                  |      }|r|d|j                   i} | S )Nzsearch.favicon_resolverzsearx.favicons.resolvers.)r   r   get__name__)dnamefuncs      r   _initial_resolver_mapr   "   sE    
A5t<D#''-24==/BCAHr   c                       e Zd ZU dZdZeed<   	  ed      Ze	ed<   	  ed      Z
eed<   	  ej                  e      Zee	e	f   ed	<   	 d
e	dedz  fdZ ed      dz   Ze	ed<   dZe	ed<   d Zd Zy)r   z#Configuration of the favicon proxy.i:	 max_agezserver.secret_key
secret_keyzoutgoing.request_timeoutresolver_timeout)default_factoryresolver_mapr   returnNc                     | j                   j                  |      }|y|j                  d      \  }}}t        j                  |      }t        ||      }|t        d| d      |S )zReturns the callable object (function) of the resolver with the
        ``name``.  If no resolver is registered for the ``name``, ``None`` is
        returned.
        N.z	resolver z is not implemented)r    r   
rpartition	importlibimport_modulegetattr
ValueError)selfr   fqnmod_name_	func_namemodr   s           r   get_resolverzFaviconProxyConfig.get_resolverD   sr    
 ##D);!$!4!Y%%h/sI&<y-@ABBr   zui.static_pathz%/themes/{theme}/img/empty_favicon.svgfavicon_pathzimage/svg+xmlfavicon_mime_typec                 x    t        j                   | j                  j                  di |      | j                  fS )z5Returns pathname and mimetype of the default favicon. )pathlibPathr0   formatr1   )r)   replacementss     r   faviconzFaviconProxyConfig.faviconV   s:     LL1**11ALAB""
 	
r   c           	         dj                  fdt        t        j                               t              D              }t
        j                  |      }||S t        j                  d
i \  }}|j                  dd      5 }|j                         }ddd       t        j                  j                  |      }d| d	| }|t
        |<   |S # 1 sw Y   ;xY w)z.Returns data image URL of the default favicon.z, c              3   2   K   | ]  }| d |      yw):Nr3   ).0xr7   s     r   	<genexpr>z6FaviconProxyConfig.favicon_data_url.<locals>.<genexpr>`   s!     k11\!_$56ks   )keyNrutf-8)encodingdata:z;utf8,r3   )joinsortedlistkeysstrDEFAULT_FAVICON_URLr   r   r8   openreadurllibparsequote)r)   r7   	cache_keydata_urlfavmimetypefs    `     r   favicon_data_urlz#FaviconProxyConfig.favicon_data_url]   s     IIktLL]L]L_G`fi@jkk	&**95O3l3XXXcGX, 	 vvxH	  <<%%h/8*F8*5)1I&	  	 s   CC)r   
__module____qualname____doc__r   int__annotations__r   r   rH   r   msgspecfieldr   r    dictr   r/   r0   r1   r8   rT   r3   r   r   r   r   ,   s    -#GS#
 ""56J6 ((BCcCL $17==AV#WL$sCx.WC
 D  $$458__L#_,s,
r   c                     t         j                  j                  d      } | rd| v ryt        t        j
                  | j                         t         j                  j                  dd            syt         j                  j                  d      }|r |t        j                  j                         vryt        ||       \  }}|;|9t        j                  ||      }dt        j                   |j                  d	<   |S t         j                  j                  d
      }t        j!                  |      \  }}t        j"                  |j$                  |j&                  |      S )a'  REST API of SearXNG's favicon proxy service

    ::

        /favicon_proxy?authority=<...>&h=<...>

    ``authority``:
      Domain name :rfc:`3986` / see :py:obj:`favicon_url`

    ``h``:
      HMAC :rfc:`2104`, build up from the :ref:`server.secret_key <settings
      server>` setting.

    	authority/) i  hr`   favicon_resolver)rR   zmax-age=zCache-Controlthemerc   )r	   argsr   r   r   r   encodepreferences	get_valuer    rG   search_faviconflaskResponser   headersr8   send_from_directoryparentr   )r^   resolverdatamimeresprc   rQ   rR   s           r   favicon_proxyrs   p   s&    !!%%k2I y( c2&
 ''112DEHxs'7'7'<'<'>>)4JD$D,~~dT2*23;;-(@_% $$..w7EKKeK,MC$$SZZHMMr   ro   r^   r!   c                 6   d\  }}t         j                  |       }|||fS t        j                  | |      }||S 	  ||t         j                        \  }}||d\  }}t        j                  j                  | |||       ||fS # t
        t        f$ r Y 7w xY w)a-  Sends the request to the favicon resolver and returns a tuple for the
    favicon.  The tuple consists of ``(data, mime)``, if the resolver has not
    determined a favicon, both values are ``None``.

    ``data``:
      Binary data of the favicon.

    ``mime``:
      Mime type of the favicon.

    NN)timeout)r   r/   r   CACHEr   r   r   set)ro   r^   rp   rq   r   	data_mimes         r   ri   ri      s     JD$H%D|Tz Hi0I)S-A-AB
d<4<%JD$
 
KKOOHit4:	 34 s   $B BBc                 J   t         j                  j                  d      }|r |t        j                  j                         vryt        j                  ||       }|dk(  r5t         j                  j                  d      }t        j                  |      S |*|\  }}d| dt        t        j                  |      d       S t        t        j                  | j                               }t        j                   d	      }t"        j$                  j'                  | |d
      }| d| S )a  Function to generate the image URL used for favicons in SearXNG's result
    lists.  The ``authority`` argument (aka netloc / :rfc:`3986`) is usually a
    (sub-) domain name.  This function is used in the HTML (jinja) templates.

    .. code:: html

       <div class="favicon">
          <img src="{{ favicon_url(result.parsed_url.netloc) }}">
       </div>

    The returned URL is a route to :py:obj:`favicon_proxy` REST API.

    If the favicon is already in the cache, the returned URL is a `data URL`_
    (something like ``data:image/png;base64,...``).  By generating a data url from
    the :py:obj:`.cache.FaviconCache`, additional HTTP roundtripps via the
    :py:obj:`favicon_proxy` are saved.  However, it must also be borne in mind
    that data urls are not cached in the client (web browser).

    .. _data URL: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs

    rb   r`   ru   rc   rd   rC   z;base64,rA   rs   )r^   ra   ?)r	   rg   rh   r   r    rG   r   rw   rT   rH   base64	b64encoder   r   rf   rj   url_forrL   rM   	urlencode)	r^   ro   ry   rc   rp   rq   ra   	proxy_urlquerys	            r   favicon_urlr      s    . ''112DEHxs'7'7'<'<'>>Hi0IL ((227;##%#00
dtfHS)9)9$)?%I$JKK!1!1!34Ao.ILL""#CDE[%!!r   )r   r   )&rW   typingr   r%   r|   r4   urllib.parserL   rj   httpxr   rZ   searxr   searx.webutilsr   r   searx.exceptionsr   searx.extended_typesr	   	resolversr   r`   r   rI   r   rY   r   r   Structr   rs   rH   tuplebytesri   r   r3   r   r   <module>r      s    )          / 9 - +     
A AH,N^!S !S !U4%<PS;S5T !H*"3 *"3 *"r   