K i>ddlmZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZddlmZmZmZmZmZmZgdZe d Zd d  d-d Zd.d Zej2dZd/dZej2dZd0dZej2dZej2dZd0dZ ej2dZ!ej2dZ"d1dZ# d2dZ$ d3dZ%d4dZ&ej2dZ' d5dZ(d6dZ) d7dZ* d8dZ+d9d Z,e,Z- d:d!Z.d;d"Z/e/Z0 dd%Z4e4Z5d?d&Z6d@d'Z7ej2d(Z8d0d)Z9dAd*Z:dBd+Z;dCd,Z VS !S (a( VS !S ( E vsK8 c T$ #f+   fc "c )FC!G,C%k3CVSQ Q%,FC!G,C%, #f+  - 4 #f+   Lr2cFt|||\}}tt||fS)z Parse a Connection option from ``header`` at the given position. Return the protocol value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. )r=rr r/r0r<rPs r,parse_connection_optionrTs)FC5ID#  $ ' ,,r2c&tt|ddS)z Parse a ``Connection`` header. Return a list of HTTP connection options. Args header: value of the ``Connection`` header. Raises: InvalidHeaderFormat: On invalid inputs. r Connection)rQrTr/s r,rrs -vq, GGr2z>[-!#$%&\'*+.^_`|~0-9a-zA-Z]+(?:/[-!#$%&\'*+.^_`|~0-9a-zA-Z]+)?ctj||}|t|d||tt|j |j fS)z Parse an Upgrade protocol from ``header`` at the given position. Return the protocol value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. zexpected protocol) _protocol_rer5r rrr:r6r;s r,parse_upgrade_protocolrZ sK   vs +E }!+/BFCPP  / <JE3$ %=# r2ct|||\}}t||}g}t||dk(r@t||dz}t|||\}}|j |t||dk(r@t t ||f|fS)a Parse an extension definition from ``header`` at the given position. Return an ``(extension name, parameters)`` pair, where ``parameters`` is a list of ``(name, value)`` pairs, and the new position. Raises: InvalidHeaderFormat: On invalid inputs. ;r)r=r7r1rarMrr )r/r0r<r_ parameters parameters r,parse_extension_itemrfRsFC5ID# FC CJ VS !S (a(3FCM 3)$ VS !S (  %z 2C 77r2c&tt|ddS)a Parse a ``Sec-WebSocket-Extensions`` header. Return a list of WebSocket extensions and their parameters in this format:: [ ( 'extension name', [ ('parameter name', 'parameter value'), .... ] ), ... ] Parameter values are :obj:`None` when no value is provided. Raises: InvalidHeaderFormat: On invalid inputs. rzSec-WebSocket-Extensions)rQrfrWs r,rrks. *FA7Q RRr2c djtt|g|Dcgc]\}}||n|d|c}}zScc}}w)zc Build an extension definition. This is the reverse of :func:`parse_extension_item`. z; r^)joinrstr)r_rdrIs r,build_extension_itemrks] 99 c4 * eMD$q'8 8    sA c2djd|DS)zl Build a ``Sec-WebSocket-Extensions`` header. This is the reverse of :func:`parse_extension`. , c3:K|]\}}t||yw)N)rk).0r_rds r, z"build_extension..s!3C4T:.sri) extensionss r,rrs" 99GQ r2cFt|||\}}tt||fS)z Parse a subprotocol from ``header`` at the given position. Return the subprotocol value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. )r=rrrSs r,parse_subprotocol_itemrts(FC5ID#  T "C ''r2c&tt|ddS)z Parse a ``Sec-WebSocket-Protocol`` header. Return a list of WebSocket subprotocols. Raises: InvalidHeaderFormat: On invalid inputs. rzSec-WebSocket-Protocol)rQrtrWs r,rrs ,fa9Q RRr2c$dj|S)zl Build a ``Sec-WebSocket-Protocol`` header. This is the reverse of :func:`parse_subprotocol`. rmrq) subprotocolss r,rrs 99\ ""r2ct|ts tdt|tr td|D]%}tj |rt d|y)zT Validate that ``subprotocols`` is suitable for :func:`build_subprotocol`. zsubprotocols must be a listz&subprotocols must be a list, not a strzinvalid subprotocol: N) isinstancer TypeErrorrjr9rGr')rw subprotocols r,rrsb lH -566,$@AA#D "";/4[MBC CDr2c>t|}td}d|d|S)z Build a ``WWW-Authenticate`` header for HTTP Basic Auth. Args: realm: Identifier of the protection space. zUTF-8z Basic realm=z , charset=)rJ)realmcharsets r,rrs+  &E!'*G % 7) 44r2z[A-Za-z0-9-._~+/]+=*ctj||}|t|d|||j|j fS)z Parse a token68 from ``header`` at the given position. Return the token value and the new position. Raises: InvalidHeaderFormat: On invalid inputs. zexpected token68) _token68_rer5r r:r6r;s r, parse_token68rsD   fc *E }!+/A63OO ;;=%))+ %%r2c<|t|krt|d||y)z8 Check that parsing reached the end of header. z trailing dataN)r.r )r/r0r<s r, parse_endrs&  S[!+LLr2ct|dd\}}|jdk7rtdd|t||dk7rt dd|||dz }t ||d\}}t ||d tj|jj} |jd d\}}||fS#tj$rtddd wxYw#t$rtdd d wxYw) a! Parse an ``Authorization`` header for HTTP Basic Auth. Return a ``(username, password)`` tuple. Args: header: Value of the ``Authorization`` header. Raises: InvalidHeaderFormat: On invalid inputs. InvalidHeaderValue: On unsupported inputs. r Authorizationbasiczunsupported scheme:  zexpected space after schemerz#expected base64-encoded credentialsNr#z&expected username:password credentials)r=lowerr r1r rrbase64 b64decodeencodedecodebinasciiErrorsplitr')r/schemer0basic_credentials user_passusernamepasswords r,rrs5 fa9KFC ||~  "6( +  &##%! :FC  1HC*63Hs fc?+$$%6%=%=%?@GGI &__S!4( X  >>  1     4  s31B>%C">!C"C9cd|vsJ|d|}tj|jj}d|zS)z Build an ``Authorization`` header for HTTP Basic Auth. This is the reverse of :func:`parse_authorization_basic`. r#zBasic )r b64encoderr)rrrrs r,rr?sQ h  *AhZ(I(()9)9);<CCE ' ''r2) r(rjr)intr*boolrrreturnrj)r/rjr0rrz str | None)r/rjr0rrr)r/rjr0rr<rjrztuple[str, int])rIrjrrj) rNz(Callable[[str, int, str], tuple[T, int]]r/rjr0rr<rjrzlist[T])r/rjr0rr<rjrztuple[ConnectionOption, int])r/rjrzlist[ConnectionOption])r/rjr0rr<rjrztuple[UpgradeProtocol, int])r/rjrzlist[UpgradeProtocol])r/rjr0rr<rjrztuple[ExtensionParameter, int])r/rjr0rr<rjrztuple[ExtensionHeader, int])r/rjrzlist[ExtensionHeader])r_r rdzSequence[ExtensionParameter]rrj)rrzSequence[ExtensionHeader]rrj)r/rjr0rr<rjrztuple[Subprotocol, int])r/rjrzlist[Subprotocol])rwSequence[Subprotocol]rrj)rwrrNone)r}rjrrj)r/rjr0rr<rjrr)r/rjrztuple[str, str])rrjrrjrrj)= __future__rrrr$recollections.abcrtypingrrr exceptionsr r r r r rrr__all__rrr1compiler4r7r9r=r@rArCrFrHrJrQrTrrYrZrrarfrparse_extension_listrkrbuild_extension_listrtrparse_subprotocol_listrbuild_subprotocol_listrrrrrrrr2r,rs"  $**?  CL!&        D 7 "**Y   BJJ6 7  & BJJJ bjj89  D rzz67  BJJ '  5?8? ? ? ?  ?D -  - -(+ -! - H rzzE = ==(+= =$ D ! !!(+!#!H8 88(+8 82S4' %A& ' (  ( ((+ ( ( S+#+ D 5bjj01  & M-` (r2