summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/render_servlet.py
blob: eee9f7e0f6d2843b230924c9feba792ca03d4fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from fnmatch import fnmatch
import logging
import mimetypes
import os
import traceback

from appengine_wrappers import IsDevServer
from branch_utility import BranchUtility
from file_system import FileNotFoundError
from server_instance import ServerInstance
from servlet import Servlet, Response
import svn_constants

def _IsBinaryMimetype(mimetype):
  return any(mimetype.startswith(prefix)
             for prefix in ['audio', 'image', 'video'])

class RenderServlet(Servlet):
  '''Servlet which renders templates.
  '''
  class Delegate(object):
    def CreateServerInstanceForChannel(self, channel):
      raise NotImplementedError()

  def __init__(self, request, delegate, default_channel='stable'):
    Servlet.__init__(self, request)
    self._delegate = delegate
    self._default_channel = default_channel

  def Get(self):
    path_with_channel, headers = (self._request.path, self._request.headers)

    # Redirect "extensions" and "extensions/" to "extensions/index.html", etc.
    if (os.path.splitext(path_with_channel)[1] == '' and
        path_with_channel.find('/') == -1):
      path_with_channel += '/'
    if path_with_channel.endswith('/'):
      return Response.Redirect('/%sindex.html' % path_with_channel)

    channel, path = BranchUtility.SplitChannelNameFromPath(path_with_channel)

    if channel == self._default_channel:
      return Response.Redirect('/%s' % path)

    if channel is None:
      channel = self._default_channel

    server_instance = self._delegate.CreateServerInstanceForChannel(channel)

    canonical_path = (
        server_instance.path_canonicalizer.Canonicalize(path).lstrip('/'))
    if path != canonical_path:
      redirect_path = (canonical_path if channel is None else
                       '%s/%s' % (channel, canonical_path))
      return Response.Redirect('/%s' % redirect_path)

    templates = server_instance.template_data_source_factory.Create(
        self._request, path)

    content = None
    content_type = None

    try:
      if fnmatch(path, 'extensions/examples/*.zip'):
        content = server_instance.example_zipper.Create(
            path[len('extensions/'):-len('.zip')])
        content_type = 'application/zip'
      elif path.startswith('extensions/examples/'):
        mimetype = mimetypes.guess_type(path)[0] or 'text/plain'
        content = server_instance.content_cache.GetFromFile(
            '%s/%s' % (svn_constants.DOCS_PATH, path[len('extensions/'):]),
            binary=_IsBinaryMimetype(mimetype))
        content_type = mimetype
      elif path.startswith('static/'):
        mimetype = mimetypes.guess_type(path)[0] or 'text/plain'
        content = server_instance.content_cache.GetFromFile(
            ('%s/%s' % (svn_constants.DOCS_PATH, path)),
            binary=_IsBinaryMimetype(mimetype))
        content_type = mimetype
      elif path.endswith('.html'):
        content = templates.Render(path)
        content_type = 'text/html'
    except FileNotFoundError as e:
      logging.warning(traceback.format_exc())
      content = None

    headers = {'x-frame-options': 'sameorigin'}
    if content is None:
      return Response.NotFound(templates.Render('404'), headers=headers)

    if not content:
      logging.error('%s had empty content' % path)

    headers.update({
      'content-type': content_type,
      'cache-control': 'max-age=300',
    })
    return Response.Ok(content, headers=headers)