Пример #1
0
def serve_static_file(filename):
    """Serve static content (images, fonts, etc) referred to in .css/.js."""
    # Protect against exposing /etc/passwd!
    if '..' in filename or filename.startswith('/'):
        flask.abort(404)
    abspath = project_root.join(filename)

    try:
        response = flask.send_file(abspath, add_etags=False)

        # Let's add last-modified here.
        response.last_modified = os.path.getmtime(abspath)
        # Apparently make_conditional() has to happen after last-modified
        # is set?  So we can't just pass conditional=True to send_file.
        # cf. https://github.com/mitsuhiko/flask/issues/637
        response.make_conditional(flask.request)

        # Firefox refuses to load fonts if we don't set a CORS header.
        # Let's do that.  In fact, let's set it for all content that
        # it makes sense for: javascript and fonts (for firefox).
        cors_extensions = ('.otf', '.woff', 'woff2', '.eot', '.ttf',  # fonts
                           '.svg', '.js', '.css')
        if filename.endswith(cors_extensions):
            response.headers.add('Access-Control-Allow-Origin', '*')

        return response
    except IOError:
        flask.abort(404)
Пример #2
0
def ping():
    """Simple handler used to check if the server is running.

    If 'genfiles' arg is specified, only respond with a 200 if our
    genfiles dir is the same as the specified one.

    See: kake.server_client.start_server
    """
    if flask.request.args.get('genfiles'):
        if project_root.join('genfiles') != flask.request.args.get('genfiles'):
            flask.abort(400)
            return

    return flask.Response('pong', mimetype='text/plain')
Пример #3
0
def serve_sourcemap(filename):
    """The sourcemap is automatically created along with its file."""
    # This forces the .map file to get made too, if filename has one.
    non_map_response = serve_genfile(filename)
    # We'll say the .map file was last modified when its corresponding
    # .js/.css file was.
    last_modified_time = non_map_response.headers['Last-modified']

    abspath = project_root.join('genfiles', filename + '.map')
    try:
        with open(abspath) as f:
            content = f.read()
    except (IOError, OSError):
        flask.abort(404)

    # The consensus is that sourcemap files should have type json:
    #    http://stackoverflow.com/questions/18809567/what-is-the-correct-mime-type-for-min-map-javascript-source-files
    response = flask.Response(content, mimetype='application/json')
    _add_caching_headers(response, last_modified_time)

    # TODO(jlfwong): We always return a 200 for sourcemap files, when really we
    # should be returning a 304 sometimes based on the If-Modified-Since
    # header, like we do for non-sourcemap files.
    return response