Преглед изворни кода

Always set the Content-Type header

Closes #43.
Stan Janssen пре 3 година
родитељ
комит
e3f84e2588
2 измењених фајлова са 11 додато и 3 уклоњено
  1. 3 2
      openleadr/client.py
  2. 8 1
      openleadr/service/vtn_service.py

+ 3 - 2
openleadr/client.py

@@ -827,12 +827,13 @@ class OpenADRClient:
 
     async def _ensure_client_session(self):
         if not self.client_session:
+            headers = {'content-type': 'application/xml'}
             if self.cert_path:
                 ssl_context = ssl.create_default_context(cafile=self.ca_file,
                                                          purpose=ssl.Purpose.CLIENT_AUTH)
                 ssl_context.load_cert_chain(self.cert_path, self.key_path, self.passphrase)
                 ssl_context.check_hostname = False
                 connector = aiohttp.TCPConnector(ssl=ssl_context)
-                self.client_session = aiohttp.ClientSession(connector=connector)
+                self.client_session = aiohttp.ClientSession(connector=connector, headers=headers)
             else:
-                self.client_session = aiohttp.ClientSession()
+                self.client_session = aiohttp.ClientSession(headers=headers)

+ 8 - 1
openleadr/service/vtn_service.py

@@ -44,8 +44,15 @@ class VTNService:
         """
         Handle all incoming POST requests.
         """
-        content = await request.read()
         try:
+            # Check the Content-Type header
+            content_type = request.headers.get('content-type', '')
+            if not content_type.lower().startswith("application/xml"):
+                raise errors.HTTPError(response_code=HTTPStatus.BAD_REQUEST,
+                                       response_description="The Content-Type header must be application/xml, "
+                                                            "you provided {request.headers.get('content-type', '')}")
+            content = await request.read()
+
             # Validate the message to the XML Schema
             message_tree = validate_xml_schema(content)