
    <	                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	Z	 G d de	j                  j                        Zy)	z=Protocol adapter class to allow requests to GET file:// URLs.    )absolute_import)division)unicode_literalsN)filesc                   &    e Zd ZdZed        Zd Zy)LocalFileAdapterz7Protocol Adapter to allow Requests to GET file:// URLs.c                    | j                         dvrt        j                  j                  dfS t        j
                  j                  |      rt        j                  j                  dfS t        j
                  j                  |      st        j                  j                  dfS t	        j                  |t        j                        st        j                  j                  dfS t        j                  j                  dfS )z4Return an HTTP status for the given filesystem path.)getheadzMethod Not AllowedzPath Not A FilezFile Not FoundzAccess DeniedOK)lowerrequestscodesnot_allowedospathisdirbad_requestisfile	not_foundaccessR_OK	forbiddenok)methodr   s     -lib/googlecloudsdk/core/local_file_adapter.py_chkpathzLocalFileAdapter._chkpath   s     ||~_,^^'')===	t	^^''):::WW^^D!^^%%'777YYtRWW%^^%%66^^$$    c                    t        j                  |j                        }t        j                         }| j                  |j                  |      \  |_        |_        |j                  dk(  rK|j                  j                         dk7  r.	 t        j                  t        j                  |            |_        |j"                  |_        ||_        | |_        |S # t        t        f$ r!}d|_        t!        |      |_        Y d}~Ld}~ww xY w)zReturn the file specified by the given request.

    Args:
      req: PreparedRequest
      **kwargs: kwargs can include values for headers, timeout, stream, etc.

    Returns:
      requests.Response object
       r   i  N)r   NormalizePathFromURLpath_urlr   Responser   r   status_codereasonr   ioBytesIOReadBinaryFileContentsrawOSErrorIOErrorstrurlrequest
connection)selfreqkwargsr   responseerrs         r   sendzLocalFileAdapter.send-   s     %%cll3D  "H,0MM#**d,K)H(/s"szz'7'7'9V'C#zz%">">t"DE
 77HLHHO w #"c(#s   
-C D'DDN)__name__
__module____qualname____doc__staticmethodr   r5    r   r   r   r      s    ?% %r   r   )r9   
__future__r   r   r   r&   r   googlecloudsdk.core.utilr   r   adaptersBaseAdapterr   r;   r   r   <module>r@      s8    D &  ' 	 	 * )x((44 )r   