Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static_file() throws away response headers #580

Open
bbradshaw opened this issue Feb 5, 2014 · 3 comments · May be fixed by #1373
Open

static_file() throws away response headers #580

bbradshaw opened this issue Feb 5, 2014 · 3 comments · May be fixed by #1373

Comments

@bbradshaw
Copy link

Trying to figure out why this toy code wasn't working:

@app.route('/')
def get_basic_file():
        response.set_cookie('basic_cookie', 1)
    return static_file('index.html', 'path')

It seems the static_file() method never looks at the original response object.

@bbradshaw
Copy link
Author

So, it's possible to work around the issue like:

@app.route('/')
def get_basic_file():
    this_response = static_file('index.html', 'path')
    this_response.set_cookie('basic_cookie', 1)
    return this_response

But that isn't documented and seems to go against the general 'automagic' feel of this module, IMHO.

@camconn
Copy link

camconn commented Jul 8, 2015

I looked around in the bottle docs, and the BaseResponse.set_cookie behavior is well defined.

However, static_file is only documented in bottle.py right now, and has no reference in the docs even though it's used several times.

# Starting at line 2460
def static_file(filename, root,
                mimetype='auto',
                download=False,
                charset='UTF-8'):
    """ Open a file in a safe way and return :exc:`HTTPResponse` with status
        code 200, 305, 403 or 404. The ``Content-Type``, ``Content-Encoding``,
        ``Content-Length`` and ``Last-Modified`` headers are set if possible.
        Special support for ``If-Modified-Since``, ``Range`` and ``HEAD``
        requests.
        :param filename: Name or path of the file to send.
        :param root: Root path for file lookups. Should be an absolute directory
            path.
        :param mimetype: Defines the content-type header (default: guess from
            file extension)
        :param download: If True, ask the browser to open a `Save as...` dialog
            instead of opening the file with the associated program. You can
            specify a custom filename as a string. If not specified, the
            original filename is used (default: False).
        :param charset: The charset to use for files with a ``text/*``
            mime-type. (default: UTF-8)
    """

    ...

This definitely needs to be added to the docs though. I'll write up a PR documenting this.

@birlorg
Copy link

birlorg commented May 2, 2022

It's in the docs now, and as of version 0.12.19 this still happens.

a fix, in static_file, replace the headers=dict() line with headers=response.headers

That will solve this issue.

birlorg added a commit to birlorg/bottle that referenced this issue May 2, 2022
This fixes bottlepy#580 

It seems the current release and the dev versions are different.  This dev version adds a headers kwarg, which is not how the rest of bottle works, which uses `response.headers()`.  This fixes bottlepy#580, which was already, somewhat fixed, but also changes it to not need a headers kwarg, just copy from the existing response headers.

Either way, you can close bottlepy#580 now.
@birlorg birlorg linked a pull request May 2, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants