Fix http server headers Connection:keep-alive and Content-Type for XMLs

This commit is contained in:
croneter 2021-10-18 15:44:00 +02:00
parent e6171127dc
commit 8c64e2a17c
2 changed files with 30 additions and 14 deletions

View file

@ -29,7 +29,6 @@ def getXArgsDeviceInfo(options=None, include_token=True):
""" """
xargs = { xargs = {
'Accept': '*/*', 'Accept': '*/*',
'Connection': 'keep-alive',
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
# "Access-Control-Allow-Origin": "*", # "Access-Control-Allow-Origin": "*",
'Accept-Language': xbmc.getLanguage(xbmc.ISO_639_1), 'Accept-Language': xbmc.getLanguage(xbmc.ISO_639_1),

View file

@ -107,18 +107,31 @@ class MyHandler(BaseHTTPRequestHandler):
sub_mgr.update_command_id(self.headers.get( sub_mgr.update_command_id(self.headers.get(
'X-Plex-Client-Identifier', self.client_address[0]), 'X-Plex-Client-Identifier', self.client_address[0]),
params.get('commandID')) params.get('commandID'))
conntype = self.headers.get('Connection', '')
if conntype.lower() == 'keep-alive':
headers = {
'Connection': 'Keep-Alive',
'Keep-Alive': 'timeout=20'
}
else:
headers = {'Connection': 'Close'}
if request_path == "version": if request_path == "version":
self.response( self.response(
"PlexKodiConnect Plex Companion: Running\nVersion: %s" "PlexKodiConnect Plex Companion: Running\nVersion: %s"
% v.ADDON_VERSION) % v.ADDON_VERSION,
headers)
elif request_path == "verify": elif request_path == "verify":
self.response("XBMC JSON connection test:\n" + js.ping()) self.response("XBMC JSON connection test:\n" + js.ping(),
headers)
elif request_path == 'resources': elif request_path == 'resources':
self.response( self.response(
RESOURCES_XML.format( RESOURCES_XML.format(
title=v.DEVICENAME, title=v.DEVICENAME,
machineIdentifier=v.PKC_MACHINE_IDENTIFIER), machineIdentifier=v.PKC_MACHINE_IDENTIFIER),
clientinfo.getXArgsDeviceInfo(include_token=False)) clientinfo.getXArgsDeviceInfo(options=headers,
include_token=False))
elif request_path == 'player/timeline/poll': elif request_path == 'player/timeline/poll':
# Plex web does polling if connected to PKC via Companion # Plex web does polling if connected to PKC via Companion
# Only reply if there is indeed something playing # Only reply if there is indeed something playing
@ -153,7 +166,7 @@ class MyHandler(BaseHTTPRequestHandler):
'Access-Control-Expose-Headers': 'Access-Control-Expose-Headers':
'X-Plex-Client-Identifier', 'X-Plex-Client-Identifier',
'Content-Type': 'text/xml;charset=utf-8' 'Content-Type': 'text/xml;charset=utf-8'
}) }.update(headers))
elif not sub_mgr.stop_sent_to_web: elif not sub_mgr.stop_sent_to_web:
sub_mgr.stop_sent_to_web = True sub_mgr.stop_sent_to_web = True
LOG.debug('Signaling STOP to Plex Web') LOG.debug('Signaling STOP to Plex Web')
@ -167,7 +180,7 @@ class MyHandler(BaseHTTPRequestHandler):
'Access-Control-Expose-Headers': 'Access-Control-Expose-Headers':
'X-Plex-Client-Identifier', 'X-Plex-Client-Identifier',
'Content-Type': 'text/xml;charset=utf-8' 'Content-Type': 'text/xml;charset=utf-8'
}) }.update(headers))
else: else:
# Fail connection with HTTP 500 error - has been open too long # Fail connection with HTTP 500 error - has been open too long
self.response( self.response(
@ -180,11 +193,13 @@ class MyHandler(BaseHTTPRequestHandler):
'Access-Control-Expose-Headers': 'Access-Control-Expose-Headers':
'X-Plex-Client-Identifier', 'X-Plex-Client-Identifier',
'Content-Type': 'text/xml;charset=utf-8' 'Content-Type': 'text/xml;charset=utf-8'
}, }.update(headers),
code=500) code=500)
elif "/subscribe" in request_path: elif "/subscribe" in request_path:
self.response(v.COMPANION_OK_MESSAGE, headers['Content-Type'] = 'text/xml;charset=utf-8'
clientinfo.getXArgsDeviceInfo(include_token=False)) headers = clientinfo.getXArgsDeviceInfo(options=headers,
include_token=False)
self.response(v.COMPANION_OK_MESSAGE, headers)
protocol = params.get('protocol') protocol = params.get('protocol')
host = self.client_address[0] host = self.client_address[0]
port = params.get('port') port = params.get('port')
@ -196,17 +211,19 @@ class MyHandler(BaseHTTPRequestHandler):
uuid, uuid,
command_id) command_id)
elif "/unsubscribe" in request_path: elif "/unsubscribe" in request_path:
self.response(v.COMPANION_OK_MESSAGE, headers['Content-Type'] = 'text/xml;charset=utf-8'
clientinfo.getXArgsDeviceInfo(include_token=False)) headers = clientinfo.getXArgsDeviceInfo(options=headers,
include_token=False)
self.response(v.COMPANION_OK_MESSAGE, headers)
uuid = self.headers.get('X-Plex-Client-Identifier') \ uuid = self.headers.get('X-Plex-Client-Identifier') \
or self.client_address[0] or self.client_address[0]
sub_mgr.remove_subscriber(uuid) sub_mgr.remove_subscriber(uuid)
else: else:
# Throw it to companion.py # Throw it to companion.py
companion.process_command(request_path, params) companion.process_command(request_path, params)
headers = clientinfo.getXArgsDeviceInfo(include_token=False) headers['Content-Type'] = 'text/xml;charset=utf-8'
headers['Content-Type'] = 'text/xml' headers = clientinfo.getXArgsDeviceInfo(options=headers,
self.response(XML_OK, headers) include_token=False)
self.response(v.COMPANION_OK_MESSAGE, headers) self.response(v.COMPANION_OK_MESSAGE, headers)