Пример #1
0
    def do_GET(self):
        global g_param
        try:
            dprint(__name__, 2, "http request header:\n{0}", self.headers)
            dprint(__name__, 2, "http request path:\n{0}", self.path)

            # check for PMS address
            PMSaddress = ''
            # Build PMS address from settings. PSR
            PMSaddress = 'http://' + g_param['CSettings'].getSetting(
                'ip_pms') + ':' + g_param['CSettings'].getSetting('port_pms')

            pms_end = self.path.find(')')
            if self.path.startswith('/PMS(') and pms_end > -1:
                PMSaddress = urllib.unquote_plus(self.path[5:pms_end])
                self.path = self.path[pms_end + 1:]

            # break up path, separate PlexNMT options
            options = {}
            while True:
                cmd_start = self.path.find('&PlexNMT')
                if cmd_start == -1:
                    cmd_start = self.path.find('?PlexNMT')
                cmd_end = self.path.find('&', cmd_start + 1)

                if cmd_start == -1:
                    break
                if cmd_end > -1:
                    cmd = self.path[cmd_start + 1:cmd_end]
                    self.path = self.path[:cmd_start] + self.path[cmd_end:]
                else:
                    cmd = self.path[cmd_start + 1:]
                    self.path = self.path[:cmd_start]

                parts = cmd.split('=', 1)
                if len(parts) == 1:
                    options[parts[0]] = ''
                else:
                    options[parts[0]] = urllib.unquote(parts[1])

            # break up path, separate additional arguments
            # clean path needed for filetype decoding... has to be merged back when forwarded.
            parts = self.path.split('?', 1)
            if len(parts) == 1:
                args = ''
            else:
                self.path = parts[0]
                args = '?' + parts[1]

            # get NMT language setting
            options['NMTLanguage'] = Localize.pickLanguage(
                self.headers.get('Accept-Language', 'en'))

            # add client address - to be used in case UDID is unknown
            options['NMTAddress'] = self.client_address[0]

            # get aTV hard-/software parameters
            #            options['aTVFirmwareVersion'] = self.headers.get('X-Apple-TV-Version', '5.1')
            #            options['aTVScreenResolution'] = self.headers.get('X-Apple-TV-Resolution', '720')

            dprint(__name__, 2, "pms address:\n{0}", PMSaddress)
            dprint(__name__, 2, "cleaned path:\n{0}", self.path)
            dprint(__name__, 2, "PlexNMT options:\n{0}", options)
            dprint(__name__, 2, "additional arguments:\n{0}", args)

            if 'User-Agent' in self.headers:  # and \
                #               'AppleTV' in self.headers['User-Agent']:

                # recieve simple logging messages from the NMT
                if 'PlexConnectNMTLogLevel' in options:
                    dprint('NMTLogger', int(options['PlexConnectNMTLogLevel']),
                           options['PlexNMTLog'])
                    self.send_response(200)
                    self.send_header('Content-type', 'text/plain')
                    self.end_headers()
                    return

                # serve .js files
                # application, main: ignore path, send /assets/js/application.js
                # otherwise: path should be '/js', send /assets/js/*.js
                dirname = path.dirname(self.path)
                basename = path.basename(self.path)
                if basename in ("application.js", "main.js", "javascript-packed.js") or \
                   basename.endswith(".js") and dirname == '/js':
                    if basename in ("main.js", "javascript-packed.js"):
                        basename = "application.js"
                    dprint(__name__, 1, "serving /js/{0}", basename)
                    JS = JSConverter(basename, options)
                    self.send_response(200)
                    self.send_header('Content-type', 'text/javascript')
                    self.end_headers()
                    self.wfile.write(JS)
                    return

                # serve "*.ico" - favicon.ico
                if self.path.endswith(".ico"):
                    dprint(__name__, 1, "serving *.ico: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'image/ico')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "*.jpg" - thumbnails for old-style mainpage
                if self.path.endswith(".jpg"):
                    dprint(__name__, 1, "serving *.jpg: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'image/jpeg')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "*.png" - only png's support transparent colors
                if self.path.endswith(".png"):
                    dprint(__name__, 1, "serving *.png: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'image/png')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "*.js" - js are all from PlexNMT
                if self.path.endswith(".js"):
                    dprint(__name__, 1, "serving *.png: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'text/javascript')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "*.xsl"
                if self.path.endswith(".xsl"):
                    dprint(__name__, 1, "serving *.xsl: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'text/xml')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "*.html"
                if self.path.endswith(".html"):
                    dprint(__name__, 1, "serving *.html: " + self.path)
                    f = open(sys.path[0] + sep + "assets" + self.path, "rb")
                    self.send_response(200)
                    self.send_header('Content-type', 'text/html')
                    self.end_headers()
                    self.wfile.write(f.read())
                    f.close()
                    return

                # serve "/index.html"
                if self.path == "/":
                    dprint(__name__, 1, "serving /index.html")
                    try:
                        f = open(
                            sys.path[0] + sep + "assets" + sep + "index.html",
                            "rb")
                        self.send_response(200)
                        self.send_header('Content-type', 'text/html')
                        self.end_headers()
                        self.wfile.write(f.read())
                        f.close()
                        return
                    except:
                        dprint(__name__, 1, "No index.html; using root of pms")

                # get everything else from XMLConverter
                if True:
                    if self.path == "/pms":
                        self.path = "/"
                    dprint(__name__, 1, "serving .xml: " + self.path)
                    HTML = XMLConverter.XML_PMS2NMT(PMSaddress,
                                                    self.path + args, options)
                    self.send_response(200)
                    self.send_header('Content-type', 'text/html')
                    self.end_headers()
                    self.wfile.write(HTML)
                    return
                """
                # unexpected request
                self.send_error(403,"Access denied: %s" % self.path)
                """

            else:
                self.send_error(
                    403, "Not Serving Client %s" % self.client_address[0])
        except IOError:
            self.send_error(404, "File Not Found: %s" % self.path)