o
    R+ i,                     @   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r8|r-|r-td||f  tj	j
|||d d}||| |S tj	j
|||d dS | jtjjjkrk|r`|r`t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
set_tunnelHTTPSHTTPSConnection
ValueError)r   r   r   Zconnection_timeout_in_millis
proxy_host
proxy_portconn r%   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/baidubce/http/bce_http_client.py_get_connection(   s4   
	



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_requestI   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_headersk   s   rC   Fc	           %   
   C   s  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	}z$|
du rt# |tj< || j|||||tj < |dkr|d	ur|$| t%|||| j&| j'| j(}t d||||| t)|||||| j*}|+ }tj,rGt|t-rGg }|D ] \}}|d.d}|d.d}|/ }|0||f q$|}t d|j1|f  t2 }| j3r`|4t5| n|6t5| | j7r|dkrd|j1  kr}dk rn n`dd |D }d|v r|8d}t d| t9|}|j:tjj;j:krt<j=j>|j?| j&d d}n|j:tjj@j:krt<j=jA|j?| j&d d}ntd| |jBd|jCd |jD ddd |E }|D ]}|||r |W S q|W S  tFy[ }  z]|d	ur|G  |0dHdd  tIJ K D  t| tLr$| jM}!| jN}"| jO}#nd	}!d	}"d	}#| jPQ| |rA| jPR| |}$tST|$d!  ntUd"|dH|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+request auto follow redirect location is %sr   )r   r   GET?r(   c                 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   )Vr   r   r   r   r   ZSDK_VERSIONsysversionplatformreplacerB   r   Z
USER_AGENTZBCE_DATEZendpointZbackup_endpointZHOSTr.   r   encodeZ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   
getheadersPY3listdecodelowerappendstatusr	   Zunder_line_headersZset_metadata_from_headersdictZ'set_metadata_from_headers_no_underlinedZauto_follow_redirectgetr   r   r   r   r   r   netlocr   r    requestpathqueryr4   	Exceptionclosejoin	traceback
format_exc
splitlinesr   rQ   status_codecodeZretry_policyZshould_retryZ%get_delay_before_next_retry_in_millistimesleepr
   )%configZsign_functionZresponse_handler_functionsr5   rf   r8   r7   paramsZuse_backup_endpoint
user_agentZshould_get_new_dateZrequest_endpointoffsetr   r   r   Zencoded_paramsr6   Zretries_attemptederrorsr$   http_responseZheaders_listZ
temp_headsr:   r;   responseZheaders_maprL   
parsed_urlZredirect_connZhandler_functionerQ   rn   ro   Zdelay_in_millisr%   r%   r&   send_requestw   s
  












(




"r{   )NN)F)#__doc__Zfuture.utilsr   r   r   builtinsr   r   logginghttp.clientr   rS   rp   rk   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   

!"