44"""
55import asyncio
66from http import HTTPStatus
7- import json
87import logging
98from urllib .parse import parse_qs , urlparse
109import uuid
1110
1211from cryptography .exceptions import InvalidSignature , InvalidTag
1312from cryptography .hazmat .primitives import serialization
1413from cryptography .hazmat .primitives .asymmetric import ed25519 , x25519
15- from cryptography . hazmat . primitives . ciphers . aead import ChaCha20Poly1305
14+ from chacha20poly1305_reuseable import ChaCha20Poly1305Reusable as ChaCha20Poly1305
1615
1716from pyhap import tlv
1817from pyhap .const import (
2524from pyhap .util import long_to_bytes
2625
2726from .hap_crypto import hap_hkdf , pad_tls_nonce
28- from .util import to_hap_json
27+ from .util import to_hap_json , from_hap_json
2928
3029# iOS will terminate the connection if it does not respond within
3130# 10 seconds, so we only allow 9 seconds to avoid this.
@@ -145,6 +144,7 @@ def __init__(self, accessory_handler, client_address):
145144 self .command = None
146145 self .headers = None
147146 self .request_body = None
147+ self .parsed_url = None
148148
149149 self .response = None
150150
@@ -199,6 +199,7 @@ def dispatch(self, request, body=None):
199199 self .command = request .method .decode ()
200200 self .headers = {k .decode (): v .decode () for k , v in request .headers }
201201 self .request_body = body
202+ self .parsed_url = urlparse (self .path )
202203 response = HAPResponse ()
203204 self .response = response
204205
@@ -210,7 +211,7 @@ def dispatch(self, request, body=None):
210211 self .headers ,
211212 )
212213
213- path = urlparse ( self .path ) .path
214+ path = self .parsed_url .path
214215 try :
215216 getattr (self , self .HANDLERS [self .command ][path ])()
216217 except UnprivilegedRequestException :
@@ -584,7 +585,7 @@ def handle_get_characteristics(self):
584585 raise UnprivilegedRequestException
585586
586587 # Check that char exists and ...
587- params = parse_qs (urlparse ( self .path ) .query )
588+ params = parse_qs (self .parsed_url .query )
588589 response = self .accessory_handler .get_characteristics (
589590 params ["id" ][0 ].split ("," )
590591 )
@@ -612,7 +613,7 @@ def handle_set_characteristics(self):
612613 self .send_response (HTTPStatus .UNAUTHORIZED )
613614 return
614615
615- requested_chars = json . loads (self .request_body .decode ("utf-8" ))
616+ requested_chars = from_hap_json (self .request_body .decode ("utf-8" ))
616617 logger .debug (
617618 "%s: Set characteristics content: %s" , self .client_address , requested_chars
618619 )
@@ -637,7 +638,7 @@ def handle_prepare(self):
637638 self .send_response (HTTPStatus .UNAUTHORIZED )
638639 return
639640
640- request = json . loads (self .request_body .decode ("utf-8" ))
641+ request = from_hap_json (self .request_body .decode ("utf-8" ))
641642 logger .debug ("%s: prepare content: %s" , self .client_address , request )
642643
643644 response = self .accessory_handler .prepare (request , self .client_address )
@@ -742,7 +743,7 @@ def _send_tlv_pairing_response(self, data):
742743
743744 def handle_resource (self ):
744745 """Get a snapshot from the camera."""
745- data = json . loads (self .request_body .decode ("utf-8" ))
746+ data = from_hap_json (self .request_body .decode ("utf-8" ))
746747
747748 if self .accessory_handler .accessory .category == CATEGORY_BRIDGE :
748749 accessory = self .accessory_handler .accessory .accessories .get (data ["aid" ])
0 commit comments