Browse Source

Always set the Content-Type header

Closes #43.
Stan Janssen 3 years ago
parent
commit
e3f84e2588
2 changed files with 11 additions and 3 deletions
  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):
     async def _ensure_client_session(self):
         if not self.client_session:
         if not self.client_session:
+            headers = {'content-type': 'application/xml'}
             if self.cert_path:
             if self.cert_path:
                 ssl_context = ssl.create_default_context(cafile=self.ca_file,
                 ssl_context = ssl.create_default_context(cafile=self.ca_file,
                                                          purpose=ssl.Purpose.CLIENT_AUTH)
                                                          purpose=ssl.Purpose.CLIENT_AUTH)
                 ssl_context.load_cert_chain(self.cert_path, self.key_path, self.passphrase)
                 ssl_context.load_cert_chain(self.cert_path, self.key_path, self.passphrase)
                 ssl_context.check_hostname = False
                 ssl_context.check_hostname = False
                 connector = aiohttp.TCPConnector(ssl=ssl_context)
                 connector = aiohttp.TCPConnector(ssl=ssl_context)
-                self.client_session = aiohttp.ClientSession(connector=connector)
+                self.client_session = aiohttp.ClientSession(connector=connector, headers=headers)
             else:
             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.
         Handle all incoming POST requests.
         """
         """
-        content = await request.read()
         try:
         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
             # Validate the message to the XML Schema
             message_tree = validate_xml_schema(content)
             message_tree = validate_xml_schema(content)