o
    #j/                     @   s  d Z ddlmZmZmZ ddlmZ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 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 zddlmZ W n eym   ddlmZ Y nw eeZdddZdd Z dd Z!	dddZ"dS )z=
This module provide http request function for bce services.
    )	iteritemsiterkeys
itervalues)strbytesN)compat)utils)BceResponse)BceHttpClientError)BceServerError)BceClientError)http_headers)urlparsec                 C   s   t |}| jtjjjkr2|r'|r'td||f  tj	j
|||d d}|S tj	j
|||d dS | jtjjjkre|rZ|rZtd||f  tj	j|||d d}||| |S tj	j|||d dS td|  )z
    :param protocol
    :type protocol: baidubce.protocol.Protocol
    :param endpoint
    :type endpoint: str
    :param connection_timeout_in_millis
    :type connection_timeout_in_millis int
    zUsing proxy host: %s, port: %d  )hostporttimeout7Invalid protocol: %s, either HTTP or HTTPS is expected.)r   convert_to_stringnamebaidubceprotocolHTTP_loggerdebughttpclientHTTPConnectionHTTPSHTTPSConnection
set_tunnel
ValueError)r   r   r   Zconnection_timeout_in_millis
proxy_host
proxy_portconn r%   ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/baidubce/http/bce_http_client.py_get_connection(   s2   
	



r'   c                 C   s   t |}t |}| j||ddd t|D ]\}}t|}t|}| || q|   |ryt|t	t
frB| | |  S t|tj }d}	|	|k ry||	 }
|
|krY|}
||
}|sjtd|	tj|f | | |	t|7 }	|	|k sO|  S )NT	skip_hostskip_accept_encodingr   z9Insufficient data, only %d bytes available while %s is %d)r   r   
putrequestr   r   Zconvert_to_standard_string	putheader
endheaders
isinstancer   r   sendintr   CONTENT_LENGTHreadr   lengetresponse)r$   http_methoduriheadersbodysend_buf_sizekvtotalsentsizebufr%   r%   r&   _send_http_requestH   s:   






r@   c                 C   sB   t | D ]\}}t|ttfrdt|v rtd||f qdS )zW
    check value in headers, if 
 in value, raise
    :param headers:
    :return:
       
z-There should not be any "\n" in header[%s]:%sN)r   r.   r   r   r   convert_to_bytesr   )r7   r:   r;   r%   r%   r&   check_headersj   s   rC   Fc	           &   
   C   sV  t d||||| |pi }dttjtjtjf }	|		dd}	t
|	}	|	|tj< d}
tj|vr4d}
| j}|r<| j}||tj< t|trL|tj}|sTd|tj< nt|trat||tj< ntj|vrmtdtj d	}t|d
r}t|dr}| }t|| j\}}}||tj< || jjkr|tj  dt
| 7  < || j|||||tj < t!|d}t|dkr|d | }n|}t"| d}g }	 d	}d	}zc|
du rt# |tj< || j|||||tj < |dkr|d	ur|$| t%|||| j&| j'| j(}t d||||| t)|||||| j*}|+ }tj,rIt|t-rIg }|D ] \}}|d.d}|d.d}|/ }|0||f q&|}t d|j1|f  t2 }| j3rb|4t5| n|6t5| | j7r |dkr d|j1  krdk r n ndd |D }d|v r z|8  W n t9y   t :d Y nw z|;  W n t9y   t :d Y nw |<d}t d| t=|}|j>tjj?j>krt@jAjB|jC| j&d d}n|j>tjjDj>krt@jAjE|jC| j&d d}ntd| |jF}|jGr|d|jG 7 }|jHd|ddd  |Id!|jC |J  |K }|D ]} | ||r. |W S q"|W S  t9y }! zf|d	urD|;  |d	urM|;  |0dLd"d# tMN O D  t|!tPrn|!jQ}"|!jR}#|!jS}$nd	}"d	}#d	}$| jTU|!|r| jTV|!|}%tWX|%d$  ntYd%|dL|f |!|#|$|"d&W Y d	}!~!nd	}!~!ww |d'7 }q)(aS  
    Send request to BCE services.

    :param config
    :type config: baidubce.BceClientConfiguration

    :param sign_function:

    :param response_handler_functions:
    :type response_handler_functions: list

    :param request:
    :type request: baidubce.internal.InternalRequest

    :return:
    :rtype: baidubce.BceResponse
    s   %s request start: %s %s, %s, %szbce-sdk-python/%s/%s/%s
 FTr   s   No %s is specified.Ntellseek   :   ?z?request args: method=%s, uri=%s, headers=%s, patams=%s, body=%szlatin-1zutf-8z%request return: status=%d, headers=%ss   GETi,  i  c                 S   s   i | ]\}}||qS r%   r%   ).0r:   r;   r%   r%   r&   
<dictcomp>   s    z send_request.<locals>.<dictcomp>locationzignore read response body errorz&ignore close response connection errorz+request auto follow redirect location is %sr   )r   r   ?GETr(   Hostc                 s   s    | ]}d | V  qdS )z>>>>Nr%   )rJ   liner%   r%   r&   	<genexpr>  s    zsend_request.<locals>.<genexpr>g     @@zEUnable to execute HTTP request. Retried %d times. All trace backs:
%s)
request_id   )Zr   r   r   r   r   ZSDK_VERSIONsysversionplatformreplacerB   r   Z
USER_AGENTZBCE_DATEZendpointZbackup_endpointZHOSTr.   r   encodeDEFAULT_ENCODINGr1   r   r3   r!   hasattrrF   r   Zparse_host_portr   default_portcredentialsZAUTHORIZATIONZget_canonical_querystringrC   Zget_canonical_timerG   r'   Zconnection_timeout_in_millsr"   r#   r@   r9   
getheadersZPY3listdecodelowerappendstatusr	   Zunder_line_headersZset_metadata_from_headersdictZ'set_metadata_from_headers_no_underlinedZauto_follow_redirectr2   	Exceptioninfoclosegetr   r   r   r   r   r   netlocr   r   pathqueryr+   r,   r-   r4   join	traceback
format_exc
splitlinesr   rR   status_codecodeZretry_policyZshould_retryZ%get_delay_before_next_retry_in_millistimesleepr
   )&configZsign_functionZresponse_handler_functionsr5   ri   r8   r7   paramsZuse_backup_endpoint
user_agentZshould_get_new_dateZrequest_endpointoffsetr   r   r   Zencoded_paramsr6   Zretries_attemptederrorsr$   Zredirect_connhttp_responseZheaders_listZ
temp_headsr:   r;   responseZheaders_maprL   
parsed_urlZredirect_uriZhandler_functionerR   ro   rp   Zdelay_in_millisr%   r%   r&   send_requestv   s.  












(





"r|   )NN)F)#__doc__Zfuture.utilsr   r   r   builtinsr   r   logginghttp.clientr   rT   rq   rl   r   r   r   Zbaidubce.bce_responser	   Zbaidubce.exceptionr
   r   r   Zbaidubce.httpr   urllib.parser   ImportError	getLogger__name__r   r'   r@   rC   r|   r%   r%   r%   r&   <module>   s6   

 "