addons-server/services/pfs.py

137 строки
4.4 KiB
Python
Исходник Обычный вид История

import re
import sys
2012-02-21 22:32:06 +04:00
from collections import defaultdict
from email.Utils import formatdate
from string import Template
from time import time
from urlparse import parse_qsl
2012-05-08 21:47:15 +04:00
import jinja2
2012-02-21 22:32:06 +04:00
import olympia.core.logger
2012-02-21 22:32:06 +04:00
from utils import log_configure
2012-05-08 21:54:19 +04:00
# This has to be imported after the settings so statsd knows where to log to.
from django_statsd.clients import statsd
2012-05-08 21:54:19 +04:00
2012-02-21 22:32:06 +04:00
# Go configure the log.
log_configure()
error_log = olympia.core.logger.getLogger('z.pfs')
2012-02-21 22:32:06 +04:00
xml_template = """\
2012-02-21 22:32:06 +04:00
<?xml version="1.0"?>
<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:pfs="http://www.mozilla.org/2004/pfs-rdf#">
<RDF:Description about="urn:mozilla:plugin-results:$mimetype">
<pfs:plugins><RDF:Seq>
<RDF:li resource="urn:mozilla:plugin"/>
2012-02-21 22:32:06 +04:00
</RDF:Seq></pfs:plugins>
</RDF:Description>
<RDF:Description about="urn:mozilla:plugin">
2012-02-21 22:32:06 +04:00
<pfs:updates><RDF:Seq>
<RDF:li resource="urn:mozilla:plugin:updates"/>
2012-02-21 22:32:06 +04:00
</RDF:Seq></pfs:updates>
</RDF:Description>
<RDF:Description about="urn:mozilla:plugin:updates">
2012-02-21 22:32:06 +04:00
<pfs:name>$name</pfs:name>
<pfs:requestedMimetype>$mimetype</pfs:requestedMimetype>
<pfs:manualInstallationURL>$manualInstallationURL</pfs:manualInstallationURL>
</RDF:Description>
</RDF:RDF>
"""
quicktime_re = re.compile(r'^image/(pict|png|tiff|x-(macpaint|pict|png|quicktime|sgi|targa|tiff))$')
java_re = re.compile(r'^application/x-java-.*$')
2012-05-08 21:54:19 +04:00
2012-02-21 22:32:06 +04:00
def get_output(data):
2012-05-08 21:47:15 +04:00
g = defaultdict(str, [(k, jinja2.escape(v)) for k, v in data.iteritems()])
2012-02-21 22:32:06 +04:00
required = ['mimetype', 'appID', 'appVersion', 'clientOS', 'chromeLocale']
# Some defaults we override depending on what we find below.
plugin = dict(mimetype='-1', name='-1',
manualInstallationURL='')
2012-02-21 22:32:06 +04:00
# Special case for mimetype if they are provided.
plugin['mimetype'] = g['mimetype'] or '-1'
output = Template(xml_template)
for s in required:
if s not in data:
# A sort of 404, matching what was returned in the original PHP.
return output.substitute(plugin)
# Figure out what plugins we've got, and what plugins we know where
# to get.
# Begin our huge and embarrassing if-else statement.
if g['mimetype'] in ['application/x-shockwave-flash',
2014-09-12 00:02:27 +04:00
'application/futuresplash']:
# Tell the user where they can go to get the installer.
2012-02-21 22:32:06 +04:00
plugin.update(
name='Adobe Flash Player',
manualInstallationURL='https://get.adobe.com/flashplayer/')
elif g['mimetype'] == 'application/x-director':
2012-02-21 22:32:06 +04:00
plugin.update(
name='Adobe Shockwave Player',
manualInstallationURL='https://get.adobe.com/shockwave/')
elif quicktime_re.match(g['mimetype']):
2012-02-21 22:32:06 +04:00
# Well, we don't have a plugin that can handle any of those
# mimetypes, but the Apple Quicktime plugin can. Point the user to
# the Quicktime download page.
plugin.update(
name='Apple Quicktime',
InstallerShowsUI='true',
manualInstallationURL='https://www.apple.com/quicktime/download/')
elif java_re.match(g['mimetype']):
# We don't want to link users directly to the Java plugin because
# we want to warn them about ongoing security problems first. Link
# to SUMO.
2012-02-21 22:32:06 +04:00
plugin.update(
name='Java Runtime Environment',
manualInstallationURL='https://support.mozilla.org/kb/use-java-plugin-to-view-interactive-content')
2012-02-21 22:32:06 +04:00
# End ridiculously huge and embarrassing if-else block.
return output.substitute(plugin)
def format_date(secs):
return '%s GMT' % formatdate(time() + secs)[:25]
def get_headers(length):
return [('Content-Type', 'text/xml; charset=utf-8'),
2012-02-21 22:32:06 +04:00
('Cache-Control', 'public, max-age=3600'),
('Last-Modified', format_date(0)),
('Expires', format_date(3600)),
('Content-Length', str(length))]
def log_exception(data):
(typ, value, traceback) = sys.exc_info()
error_log.error(u'Type: %s, %s. Query: %s' % (typ, value, data))
def application(environ, start_response):
status = '200 OK'
2012-05-08 21:54:19 +04:00
with statsd.timer('services.pfs'):
data = dict(parse_qsl(environ['QUERY_STRING']))
try:
output = get_output(data).encode('utf-8')
2012-05-08 21:54:19 +04:00
start_response(status, get_headers(len(output)))
except:
log_exception(data)
raise
return [output]