o
    i                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z
 d dlmZmZ d dlmZmZmZ d dlmZ dddZdddZdS )    )annotationsN)Dict)	HTTPError)quote)urljoinurlsplit)HTTPRedirectHandlerRequesturlopen)
addinfourlrequestr	   
http_errorr   returnc                   s8  |j d}|du r|t|tstd|dt|}|jdvr4t||j|j	 d|d|j |j
t|dtjd	}t| j|}d
  fdd| j  D }t||| jdd}t| dr| j}||dtjksqt|tjkrt| j|jtj|j	 |j |j
ni }t| d| t|d| ||dd ||< |S )a  
    Create a new request object for a redirected request.

    The logic is based on `urllib.request.HTTPRedirectHandler` from `this commit <https://github.com/python/cpython/blob/b58bc8c2a9a316891a5ea1a0487aebfc86c2793a/Lib/urllib/request.py#L641-L751>_`.

    :param request: The original request that resulted in the redirect.
    :param http_error: The response to the original request that indicates a
        redirect should occur and contains the new location.
    :return: A new request object to the location indicated by the response.
    :raises HTTPError: the supplied ``http_error`` if the redirect request
        cannot be created.
    :raises ValueError: If the response code is `None`.
    :raises ValueError: If the response does not contain a ``Location`` header
        or the ``Location`` header is not a string.
    :raises HTTPError: If the scheme of the new location is not ``http``,
        ``https``, or ``ftp``.
    :raises HTTPError: If there are too many redirects or a redirect loop.
    LocationNzLocation header z is not a string)httphttpsftp z - Redirection to url z is not allowedz
iso-8859-1)encodingsafe)zcontent-lengthzcontent-typec                   s"   i | ]\}}|   vr||qS  )lower).0kvcontent_headersr   M/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/rdflib/_networking.py
<dictcomp>=   s    z*_make_redirect_request.<locals>.<dictcomp>T)headersorigin_req_hostunverifiableredirect_dictr      )r   get
isinstancestr
ValueErrorr   schemer   codereasonfpurlquotestringpunctuationr   full_urlitemsr	   r    hasattrr"   r   max_repeatslenmax_redirectionsinf_msgsetattr)r   r   new_urlnew_url_parts
newheadersnew_requestvisitedr   r   r   _make_redirect_request   sV   





r<   r   c              
   C  sX   zt | W S  ty+ } z|jdkr&tjdk r&t| |}t|W  Y d}~S  d}~ww )a6  
    This is a shim for `urlopen` that handles HTTP redirects with status code
    308 (Permanent Redirect).

    This function should be removed once all supported versions of Python
    handles the 308 HTTP status code.

    :param request: The request to open.
    :return: The response to the request.
    i4  )      N)r
   r   r)   sysversion_infor<   _urlopen)r   errorr:   r   r   r   rA   ^   s   

rA   )r   r	   r   r   r   r	   )r   r	   r   r   )
__future__r   r-   r?   typingr   urllib.errorr   urllib.parser   r,   r   r   urllib.requestr   r	   r
   urllib.responser   r<   rA   r   r   r   r   <module>   s    
Q