Skip to content

Commit edf2c9d

Browse files
committed
split out handle_get_protocol_info
1 parent 6a6963c commit edf2c9d

File tree

2 files changed

+71
-64
lines changed

2 files changed

+71
-64
lines changed

dlna.py

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
import struct
1313
import subprocess
1414
import uuid
15-
from helpers import is_safe_path, send_device_description, send_scpd_xml
15+
from helpers import (
16+
is_safe_path,
17+
send_device_description,
18+
send_scpd_xml,
19+
handle_get_protocol_info,
20+
)
1621
from http.server import BaseHTTPRequestHandler
1722
from urllib.parse import unquote, urlparse, quote
1823

@@ -743,7 +748,7 @@ def handle_soap_request(self, post_data, soap_action=""):
743748
if "Browse" in soap_data or "Browse" in soap_action:
744749
self.handle_browse_request(soap_data)
745750
elif "GetProtocolInfo" in soap_data or "GetProtocolInfo" in soap_action:
746-
self.handle_get_protocol_info()
751+
handle_get_protocol_info(self)
747752
elif (
748753
"GetCurrentConnectionIDs" in soap_data
749754
or "GetCurrentConnectionIDs" in soap_action
@@ -801,67 +806,6 @@ def handle_soap_request(self, post_data, soap_action=""):
801806
traceback.print_exc()
802807
self.send_error(500, "Internal server error")
803808

804-
def handle_get_protocol_info(self):
805-
"""Handle ConnectionManager GetProtocolInfo requests"""
806-
try:
807-
print("Handling GetProtocolInfo request")
808-
809-
# Define supported protocols with expanded format support
810-
source_protocols = [
811-
# Video formats
812-
"http-get:*:video/mp4:DLNA.ORG_PN=AVC_MP4_MP_SD_AAC_MULT5;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
813-
"http-get:*:video/x-msvideo:DLNA.ORG_PN=AVI;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
814-
"http-get:*:video/x-matroska:DLNA.ORG_PN=MATROSKA;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
815-
"http-get:*:video/quicktime:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
816-
"http-get:*:video/x-ms-wmv:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
817-
"http-get:*:video/x-flv:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
818-
"http-get:*:video/webm:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
819-
"http-get:*:video/x-m4v:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
820-
"http-get:*:video/3gpp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
821-
# Audio formats
822-
"http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
823-
"http-get:*:audio/wav:DLNA.ORG_PN=LPCM;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
824-
"http-get:*:audio/mp4:DLNA.ORG_PN=AAC_ISO_320;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
825-
"http-get:*:audio/x-m4a:DLNA.ORG_PN=AAC_ISO_320;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
826-
"http-get:*:audio/flac:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
827-
"http-get:*:audio/ogg:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
828-
"http-get:*:audio/x-ms-wma:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
829-
"http-get:*:audio/aiff:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
830-
# Image formats
831-
"http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
832-
"http-get:*:image/png:DLNA.ORG_PN=PNG_LRG;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
833-
"http-get:*:image/gif:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
834-
"http-get:*:image/bmp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
835-
"http-get:*:image/tiff:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
836-
"http-get:*:image/webp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
837-
]
838-
839-
source_info = ",".join(source_protocols)
840-
sink_info = "" # This server doesn't act as a sink
841-
842-
response = f"""<?xml version="1.0" encoding="utf-8"?>
843-
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
844-
<s:Body>
845-
<u:GetProtocolInfoResponse xmlns:u="urn:schemas-upnp-org:service:ConnectionManager:1">
846-
<Source>{source_info}</Source>
847-
<Sink>{sink_info}</Sink>
848-
</u:GetProtocolInfoResponse>
849-
</s:Body>
850-
</s:Envelope>"""
851-
852-
self.send_response(200)
853-
self.send_header("Content-Type", "text/xml; charset=utf-8")
854-
self.send_header("Content-Length", str(len(response)))
855-
self.end_headers()
856-
self.wfile.write(response.encode())
857-
if self.verbose:
858-
print("Sent GetProtocolInfo response")
859-
860-
except Exception as e:
861-
print(f"Error handling GetProtocolInfo: {e}")
862-
traceback.print_exc()
863-
self.send_error(500, "Internal server error")
864-
865809
def handle_get_current_connection_ids(self):
866810
"""Handle ConnectionManager GetCurrentConnectionIDs requests"""
867811
try:

helpers.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import traceback
23
from constants import SERVER_MANUFACTURER, SERVER_VERSION, SERVER_AGENT
34

45

@@ -382,4 +383,66 @@ def send_scpd_xml(self, service_type):
382383
self.send_header("Access-Control-Allow-Origin", "*")
383384
self.send_header("Server", SERVER_AGENT)
384385
self.end_headers()
385-
self.wfile.write(scpd_xml.encode())
386+
self.wfile.write(scpd_xml.encode())
387+
388+
389+
def handle_get_protocol_info(self):
390+
"""Handle ConnectionManager GetProtocolInfo requests"""
391+
try:
392+
print("Handling GetProtocolInfo request")
393+
394+
# Define supported protocols with expanded format support
395+
source_protocols = [
396+
# Video formats
397+
"http-get:*:video/mp4:DLNA.ORG_PN=AVC_MP4_MP_SD_AAC_MULT5;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
398+
"http-get:*:video/x-msvideo:DLNA.ORG_PN=AVI;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
399+
"http-get:*:video/x-matroska:DLNA.ORG_PN=MATROSKA;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
400+
"http-get:*:video/quicktime:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
401+
"http-get:*:video/x-ms-wmv:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
402+
"http-get:*:video/x-flv:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
403+
"http-get:*:video/webm:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
404+
"http-get:*:video/x-m4v:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
405+
"http-get:*:video/3gpp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
406+
# Audio formats
407+
"http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
408+
"http-get:*:audio/wav:DLNA.ORG_PN=LPCM;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
409+
"http-get:*:audio/mp4:DLNA.ORG_PN=AAC_ISO_320;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
410+
"http-get:*:audio/x-m4a:DLNA.ORG_PN=AAC_ISO_320;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
411+
"http-get:*:audio/flac:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
412+
"http-get:*:audio/ogg:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
413+
"http-get:*:audio/x-ms-wma:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
414+
"http-get:*:audio/aiff:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000",
415+
# Image formats
416+
"http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
417+
"http-get:*:image/png:DLNA.ORG_PN=PNG_LRG;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
418+
"http-get:*:image/gif:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
419+
"http-get:*:image/bmp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
420+
"http-get:*:image/tiff:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
421+
"http-get:*:image/webp:DLNA.ORG_OP=01;DLNA.ORG_FLAGS=00D00000000000000000000000000000",
422+
]
423+
424+
source_info = ",".join(source_protocols)
425+
sink_info = "" # This server doesn't act as a sink
426+
427+
response = f"""<?xml version="1.0" encoding="utf-8"?>
428+
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
429+
<s:Body>
430+
<u:GetProtocolInfoResponse xmlns:u="urn:schemas-upnp-org:service:ConnectionManager:1">
431+
<Source>{source_info}</Source>
432+
<Sink>{sink_info}</Sink>
433+
</u:GetProtocolInfoResponse>
434+
</s:Body>
435+
</s:Envelope>"""
436+
437+
self.send_response(200)
438+
self.send_header("Content-Type", "text/xml; charset=utf-8")
439+
self.send_header("Content-Length", str(len(response)))
440+
self.end_headers()
441+
self.wfile.write(response.encode())
442+
if self.verbose:
443+
print("Sent GetProtocolInfo response")
444+
445+
except Exception as e:
446+
print(f"Error handling GetProtocolInfo: {e}")
447+
traceback.print_exc()
448+
self.send_error(500, "Internal server error")

0 commit comments

Comments
 (0)