
    R1iD                        d 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 dd
lmZ ddlmZ ddlmZ ddlmZ  G d d      Z G d d      Z G d de      Z G d de      Z G d de      Z
 G d de      Zy)z
Parsers are used to parse the content of incoming HTTP requests.

They give us a generic way of being able to handle various media types
on the request, such as form content or json encoded data.
    N)settings)StopFutureHandlers)	QueryDict)	ChunkIter)MultiPartParser)MultiPartParserError)	renderers)parse_header_parameters)
ParseError)api_settings)jsonc                       e Zd Zd Zy)DataAndFilesc                      || _         || _        y )N)datafiles)selfr   r   s      Z/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/rest_framework/parsers.py__init__zDataAndFiles.__init__   s    	
    N)__name__
__module____qualname__r    r   r   r   r      s    r   r   c                       e Zd ZdZdZddZy)
BaseParserz
    All parsers should extend `BaseParser`, specifying a `media_type`
    attribute, and overriding the `.parse()` method.
    Nc                     t        d      )z
        Given a stream to read from, return the parsed representation.
        Should return parsed data, or a `DataAndFiles` object consisting of the
        parsed data and files.
        z.parse() must be overridden.)NotImplementedError)r   stream
media_typeparser_contexts       r   parsezBaseParser.parse%   s     ""@AAr   NNr   r   r   __doc__r    r"   r   r   r   r   r      s     JBr   r   c                   L    e Zd ZdZdZej                  Zej                  Z
ddZy)
JSONParserz&
    Parses JSON-serialized data.
    zapplication/jsonNc                 @   |xs i }|j                  dt        j                        }	  t        j                  |      |      }| j
                  rt        j                  nd}t        j                  ||      S # t        $ r}t        dt        |      z        d}~ww xY w)zX
        Parses the incoming bytestream as JSON and returns the resulting data.
        encodingN)parse_constantzJSON parse error - %s)getr   DEFAULT_CHARSETcodecs	getreaderstrictr   strict_constantload
ValueErrorr   str)r   r   r    r!   r)   decoded_streamr*   excs           r   r"   zJSONParser.parse6   s     (-2!%%j(2J2JK	A7V--h7?N59[[T11dN99^NKK 	A4s3x?@@	As   AA8 8	BBBr#   )r   r   r   r%   r    r	   JSONRendererrenderer_classr   STRICT_JSONr/   r"   r   r   r   r'   r'   .   s*     $J++N%%FAr   r'   c                       e Zd ZdZdZddZy)
FormParserz
    Parser for form data.
    z!application/x-www-form-urlencodedNc                     |xs i }|j                  dt        j                        }t        |j	                         |      S )zt
        Parses the incoming bytestream as a URL encoded form,
        and returns the resulting QueryDict.
        r)   )r)   )r+   r   r,   r   read)r   r   r    r!   r)   s        r   r"   zFormParser.parseK   s9    
 (-2!%%j(2J2JK::r   r#   r$   r   r   r   r:   r:   E   s     5J;r   r:   c                       e Zd ZdZdZddZy)r   zF
    Parser for multipart form data, which may include file data.
    zmultipart/form-dataNc                 Z   |xs i }|d   }|j                  dt        j                        }|j                  j	                         }||d<   |j
                  }	 t        ||||      }|j                         \  }	}
t        |	|
      S # t        $ r}t        dt        |      z        d}~ww xY w)a	  
        Parses the incoming bytestream as a multipart encoded form,
        and returns a DataAndFiles object.

        `.data` will be a `QueryDict` containing all the form parameters.
        `.files` will be a `QueryDict` containing all the form files.
        requestr)   CONTENT_TYPEzMultipart form parse error - %sN)r+   r   r,   METAcopyupload_handlersDjangoMultiPartParserr"   r   r   r   r3   )r   r   r    r!   r?   r)   metarC   parserr   r   r5   s               r   r"   zMultiPartParser.parse[   s     (-2 +!%%j(2J2JK||  ")^!11	K*4(SF ,,.KD%e,,# 	K>SIJJ	Ks   ,B 	B*B%%B*r#   r$   r   r   r   r   r   U   s     'JKr   r   c                   ,    e Zd ZdZdZdddZd	dZd Zy)
FileUploadParserz&
    Parser for file upload data.
    z*/*zFFileUpload parse error - none of upload handlers can handle the streamz`Missing filename. Request should include a Content-Disposition header with a filename parameter.)	unhandledno_filenameNc           	      t   |xs i }|d   }|j                  dt        j                        }|j                  }|j                  }| j                  |||      }|st        | j                  d         |j                  d|j                  dd            }		 t        |j                  d|j                  dd	                  }
|D ],  }|j                  |||
d
|      }|t        i d|d   i      c S  |D cg c]  }|j                  s|j                   }}t        dg|z         }t        ||      }d	gt!        |      z  }t#        |      D ]  \  }}	 |j%                  d
||	|
|        |D ]F  }t#        |      D ]6  \  }}t!        |      }|j)                  |||         }||xx   |z  cc<   |6 F H t#        |      D ]+  \  }}|j+                  ||         }|t        i d|i      c S  t        | j                  d         # t        t        f$ r d
}
Y Zw xY wc c}w # t&        $ r |d
|dz    }Y  w xY w)a  
        Treats the incoming bytestream as a raw file upload and returns
        a `DataAndFiles` object.

        `.data` will be None (we expect request body to be a file content).
        `.files` will be a `QueryDict` containing one 'file' element.
        r?   r)   rJ   HTTP_CONTENT_TYPEr@    HTTP_CONTENT_LENGTHCONTENT_LENGTHr   Nfile   irI   )r+   r   r,   rA   rC   get_filenamer   errorsintr2   	TypeErrorhandle_raw_inputr   
chunk_sizeminr   len	enumeratenew_filer   receive_data_chunkfile_complete)r   r   r    r!   r?   r)   rE   rC   filenamecontent_typecontent_lengthhandlerresultxpossible_sizesrW   chunkscountersindexchunkchunk_lengthfile_objs                         r   r"   zFileUploadParser.parse|   sv    (-2 +!%%j(2J2JK||!11$$VZHT[[788 xx 3 $ <>	" *?*.((3CQ*G"I JN 'G--f.2.<.2.6	8F
 !#B(;<< ' 1@P11<<!,,P+78
6:.3_--'8NE7  x!/; 9 E"+O"<w"5z225(5/J</= #=  (8NE7,,Xe_=H##B(:;; 9
 [122Q I& 	"!N	" Q & "1*519"=s0   +H 4HHH"HH"H76H7c                     	 |d   d   S # t         $ r Y nw xY w	 |d   j                  }t        |d         \  }}d|v r|d   S |d   S # t        t         t        f$ r Y yw xY w)z
        Detects the uploaded file name. First searches a 'filename' url kwarg.
        Then tries to parse Content-Disposition header.
        kwargsr^   r?   HTTP_CONTENT_DISPOSITIONz	filename*N)KeyErrorrA   r
   AttributeErrorr2   )r   r   r    r!   rE   dispositionparamss          r   rR   zFileUploadParser.get_filename   s    
	!(+J77 			!),11D"9$?Y:Z"[Kf$k**j))*5 		s#   
 	(A A AAr#   )r   r   r   r%   r    rS   r"   rR   r   r   r   rH   rH   r   s'     J]yF
A3Fr   rH   )r%   r-   django.confr   django.core.files.uploadhandlerr   django.httpr   django.http.multipartparserr   r   rD   r   rest_frameworkr	   rest_framework.compatr
   rest_framework.exceptionsr   rest_framework.settingsr   rest_framework.utilsr   r   r   r'   r:   rH   r   r   r   <module>r{      s~       > ! 1- < $ 9 0 0 % B B A A.; ; Kj K:_z _r   