
    `Kh0                        d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ dZ ej                  d      Z ed	      Z ed
      Z ej                  dej&                  ej(                  z        Zdee   dee   fdZ ej                  d      j0                  Zdej4                  e   defdZdedefdZdedee	eee   f      fdZdee   dee	eee   f      fdZy)z
Internal cookie handling helpers.

This module contains internal utilities for cookie parsing and manipulation.
These are not part of the public API and may change without notice.
    N)Morsel)ListOptionalSequenceTuplecast   )internal_logger)parse_set_cookie_headersparse_cookie_header preserve_morsel_with_coded_valuez.^[!#$%&\'()*+\-./0-9:<=>?@A-Z\[\]^_`a-z{|}~]+$)
pathdomainzmax-ageexpiressecurehttponlysamesitepartitionedversioncomment)r   r   r   a  
    \s*                            # Optional whitespace at start of cookie
    (?P<key>                       # Start of group 'key'
    # aiohttp has extended to include [] for compatibility with real-world cookies
    [\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=\[\]]+?   # Any word of at least one letter
    )                              # End of group 'key'
    (                              # Optional group: there may not be a value.
    \s*=\s*                          # Equal Sign
    (?P<val>                         # Start of group 'val'
    "(?:[^\\"]|\\.)*"                  # Any double-quoted string (properly closed)
    |                                  # or
    "[^";]*                            # Unmatched opening quote (differs from SimpleCookie - issue #7993)
    |                                  # or
    # Special case for "expires" attr - RFC 822, RFC 850, RFC 1036, RFC 1123
    (\w{3,6}day|\w{3}),\s              # Day of the week or abbreviated day (with comma)
    [\w\d\s-]{9,11}\s[\d:]{8}\s        # Date and time in specific format
    (GMT|[+-]\d{4})                     # Timezone: GMT or RFC 2822 offset like -0000, +0100
                                        # NOTE: RFC 2822 timezone support is an aiohttp extension
                                        # for issue #4493 - SimpleCookie does NOT support this
    |                                  # or
    # ANSI C asctime() format: "Wed Jun  9 10:18:14 2021"
    # NOTE: This is an aiohttp extension for issue #4327 - SimpleCookie does NOT support this format
    \w{3}\s+\w{3}\s+[\s\d]\d\s+\d{2}:\d{2}:\d{2}\s+\d{4}
    |                                  # or
    [\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=\[\]]*      # Any word or empty string
    )                                # End of group 'val'
    )?                             # End of optional value group
    \s*                            # Any number of spaces.
    (\s+|;|$)                      # Ending either at space, semicolon, or EOS.
    cookiereturnc                     t        d| j                  | j                  t                           }|j	                  | j                  | j
                  | j                  d       |S )a^  
    Preserve a Morsel's coded_value exactly as received from the server.

    This function ensures that cookie encoding is preserved exactly as sent by
    the server, which is critical for compatibility with old servers that have
    strict requirements about cookie formats.

    This addresses the issue described in https://github.com/aio-libs/aiohttp/pull/1453
    where Python's SimpleCookie would re-encode cookies, breaking authentication
    with certain servers.

    Args:
        cookie: A Morsel object from SimpleCookie

    Returns:
        A Morsel object with preserved coded_value

    zMorsel[str]keyvaluecoded_value)r   getr   r   __setstate__r   r   )r   mrsl_vals     u/var/www/CtrlAgent-Voice-Telephony-Channel/python-server/venv/lib/python3.12/site-packages/aiohttp/_cookie_helpers.pyr   r   U   sP    & M6::fjj&(#CDH
 

V\\&BTBTU O    z\\(?:([0-3][0-7][0-7])|(.))mc                 F    | d   rt        t        | d   d            S | d   S )z
    Replace function for _unquote_sub regex substitution.

    Handles escaped characters in cookie values:
    - Octal sequences are converted to their character representation
    - Other escaped characters are unescaped by removing the backslash
    r	         )chrint)r#   s    r!   _unquote_replacer)   v   s)     	t3qtQ<  Q4Kr"   r   c                 p    t        |       dk  r| S | d   dk7  s| d   dk7  r| S | dd } t        t        |       S )z
    Unquote a cookie value.

    Vendored from http.cookies._unquote to ensure compatibility.

    Note: The original implementation checked for None, but we've removed
    that check since all callers already ensure the value is not None.
    r&   r   "r	   )len_unquote_subr)   )r   s    r!   _unquoter/      sL     5zA~Qx3%)s* !BKE (%00r"   headerc                    | sg S g }d}t        |       }||k  rt        j                  | |      }|s	 |S |j                  d      }|j                  d      xs d}|j	                  d      }|rt
        j                  |      st        j                  d|       t               }|j                  |t        |      |d       |j                  ||f       ||k  r|S )aN  
    Parse a Cookie header according to RFC 6265 Section 5.4.

    Cookie headers contain only name-value pairs separated by semicolons.
    There are no attributes in Cookie headers - even names that match
    attribute names (like 'path' or 'secure') should be treated as cookies.

    This parser uses the same regex-based approach as parse_set_cookie_headers
    to properly handle quoted values that may contain semicolons.

    Args:
        header: The Cookie header value to parse

    Returns:
        List of (name, Morsel) tuples for compatibility with SimpleCookie.update()
    r   r   val z+Can not load cookie: Illegal cookie name %rr   )r-   _COOKIE_PATTERNmatchgroupend_COOKIE_NAME_REr
   warningr   r   r/   append)r0   cookiesinr5   r   r   morsels           r!   r   r      s    " 	-/G	AFA
a%%%fa00 N- kk% E"(bIIaL ///4##$QSVW %h 	(5/%H	
 	V}%5 a%8 Nr"   headersc                 0   g }| D ]  }|sd}t        |      }d}d}d|cxk  r|k  s$n 't        j                  ||      }|s@|j                  d      |j                  d      }	}|j	                  d      }|j                         }
|d   dk(  r|sv||
dd }|t        v r|	xs d||<   n|
t        v rW|s|
t        v r8||
d	k(  r+t        j                  d
k  rt        j                  ||
d       nd||
<   n|	|t        |	      ||
<   nx|	t|t        v st        j                  |      st        j                  d|       d}n@t!               }|j#                  |t        |	      |	d       |j%                  ||f       d}n~d|cxk  r|k  rd  |S )a  
    Parse cookie headers using a vendored version of SimpleCookie parsing.

    This implementation is based on SimpleCookie.__parse_string to ensure
    compatibility with how SimpleCookie parses cookies, including handling
    of malformed cookies with missing semicolons.

    This function is used for both Cookie and Set-Cookie headers in order to be
    forgiving. Ideally we would have followed RFC 6265 Section 5.2 (for Cookie
    headers) and RFC 6265 Section 4.2.1 (for Set-Cookie headers), but the
    real world data makes it impossible since we need to be a bit more forgiving.

    NOTE: This implementation differs from SimpleCookie in handling unmatched quotes.
    SimpleCookie will stop parsing when it encounters a cookie value with an unmatched
    quote (e.g., 'cookie="value'), causing subsequent cookies to be silently dropped.
    This implementation handles unmatched quotes more gracefully to prevent cookie loss.
    See https://github.com/aio-libs/aiohttp/issues/7993
    r   NFr   r2   $r	   r3   r   )      Tz,Can not load cookies: Illegal cookie name %rr   )r-   r4   r5   r6   r7   lower_COOKIE_KNOWN_ATTRS_COOKIE_BOOL_ATTRSsysversion_infodict__setitem__r/   r8   r
   r9   r   r   r:   )r?   parsed_cookiesr0   r<   r=   current_morselmorsel_seenr5   r   r   	lower_keyattr_lower_keys               r!   r   r      s   & 57N G K041jqj#))&!4EU+U[[-?C		!A		I1v}" !-%.qr]N%)<<9>"~611" 22%1$5#:J:JW:T ,,^YM8<N95]#/08N9-" --_5J5J35O#++F &*N &,XN #// #huoeT #))3*?@"&K { 1jqjjGR r"   )__doc__rerG   http.cookiesr   typingr   r   r   r   r   logr
   __all__compiler8   	frozensetrE   rF   VERBOSEASCIIr4   strr   subr.   Matchr)   r/   r   r    r"   r!   <module>r^      s8   
 
  8 8   "**NO  )  "**< JJ? FVC[ VC[ < rzz89==
 
# 
1C 1C 1:4 4U3s3C-D(E 4n^hsm ^U3sCS=T8U ^r"   