o
    mi                     @  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 [HTTPRedirectHandler](https://github.com/python/cpython/blob/b58bc8c2a9a316891a5ea1a0487aebfc86c2793a/Lib/urllib/request.py#L641-L751) from urllib.request.

    Args:
        request: The original request that resulted in the redirect.
        http_error: The response to the original request that indicates a
            redirect should occur and contains the new location.

    Returns:
        A new request object to the location indicated by the response.

    Raises:
        HTTPError: the supplied `http_error` if the redirect request
            cannot be created.
        ValueError: If the response code is None.
        ValueError: If the response does not contain a `Location` header
            or the `Location` header is not a string.
        HTTPError: If the scheme of the new location is not `http`,
            `https`, or `ftp`.
        HTTPError: If there are too many redirects or a redirect loop.
    Z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Zcontent_headersr   L/home/kim/smarthome/.venv/lib/python3.10/site-packages/rdflib/_networking.py
<dictcomp>A   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_urlZnew_url_parts
newheadersnew_requestvisitedr   r   r   _make_redirect_request   sV   





r9   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 )aA  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.

    Args:
        request: The request to open.

    Returns:
        The response to the request.
    i4  )      N)r
   r   r'   sysversion_infor9   _urlopen)r   errorr7   r   r   r   r>   b   s   

r>   )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   r9   r>   r   r   r   r   <module>   s    
U