1
0
mirror of https://github.com/pypiserver/pypiserver synced 2024-11-09 16:45:51 +01:00

Merge pull request #74 from ankostis/cachectrl

(2) Add Cache-control header to enable pip caching
This commit is contained in:
Kostis Anagnostopoulos 2015-02-17 00:41:08 +01:00
commit ce63a21a04
4 changed files with 43 additions and 5 deletions

@ -11,7 +11,9 @@ def app(root=None,
log_req_frmt="%(bottle.request)s",
log_res_frmt="%(status)s",
log_err_frmt="%(body)s: %(exception)s \n%(traceback)s",
welcome_file=None):
welcome_file=None,
cache_control=None,
):
import sys, os
from pypiserver import core
sys.modules.pop("pypiserver._app", None)
@ -29,7 +31,9 @@ def app(root=None,
_app.configure(root=root, redirect_to_fallback=redirect_to_fallback, fallback_url=fallback_url,
authenticated=authenticated, password_file=password_file, overwrite=overwrite,
log_req_frmt=log_req_frmt, log_res_frmt=log_res_frmt, log_err_frmt=log_err_frmt,
welcome_file=welcome_file)
welcome_file=welcome_file,
cache_control=cache_control,
)
_app.app.module = _app
bottle.debug(True)

@ -63,7 +63,9 @@ def configure(root=None,
log_req_frmt=None,
log_res_frmt=None,
log_err_frmt=None,
welcome_file=None):
welcome_file=None,
cache_control=None,
):
global packages
log.info("Starting(%s)", dict(root=root,
@ -75,7 +77,8 @@ def configure(root=None,
welcome_file=welcome_file,
log_req_frmt=log_req_frmt,
log_res_frmt=log_res_frmt,
log_err_frmt=log_err_frmt))
log_err_frmt=log_err_frmt,
cache_control=cache_control))
config.authenticated = authenticated
@ -103,6 +106,7 @@ def configure(root=None,
config.redirect_to_fallback = redirect_to_fallback
config.fallback_url = fallback_url
config.cache_control = cache_control
if password_file:
from passlib.apache import HtpasswdFile
config.htpasswdfile = HtpasswdFile(password_file)
@ -314,7 +318,10 @@ def server_static(filename):
for x in entries:
f = x.relfn.replace("\\", "/")
if f == filename:
return static_file(filename, root=x.root, mimetype=mimetypes.guess_type(filename)[0])
response = static_file(filename, root=x.root, mimetype=mimetypes.guess_type(filename)[0])
if config.cache_control:
response.set_header("Cache-Control", "public, max-age=%s" % config.cache_control)
return response
return HTTPError(404)

@ -250,6 +250,11 @@ pypi-server understands the following options:
a format-string selecting Http-Error properties to log; set to '%s' to see them all.
[Default: %(body)s: %(exception)s \n%(traceback)s]
--cache-control AGE
Add "Cache-Control: max-age=AGE, public" header to package downloads.
Pip 6+ needs this for caching.
pypi-server -h
pypi-server --help
show this help message
@ -300,6 +305,7 @@ def main(argv=None):
log_res_frmt = None
log_err_frmt = None
welcome_file = None
cache_control = None
update_dry_run = True
update_directory = None
@ -322,6 +328,7 @@ def main(argv=None):
"log-res-frmt=",
"log-err-frmt=",
"welcome=",
"cache-control=",
"version",
"help"
])
@ -379,6 +386,8 @@ def main(argv=None):
log_res_frmt = v
elif k == "--log-err-frmt":
log_err_frmt = v
elif k == "--cache-control":
cache_control = v
elif k == "-v":
verbosity += 1
elif k in ("-h", "--help"):
@ -412,6 +421,7 @@ def main(argv=None):
overwrite=overwrite,
log_req_frmt=log_req_frmt, log_res_frmt=log_res_frmt, log_err_frmt=log_err_frmt,
welcome_file=welcome_file,
cache_control=cache_control,
)
server = server or "auto"
sys.stdout.write("This is pypiserver %s serving %r on http://%s:%s\n\n" % (__version__, ", ".join(roots), host, port))

@ -227,3 +227,20 @@ def test_simple_index_list_name_with_underscore_no_egg(root, testapp):
assert len(resp.html("a")) == 2
hrefs = set([x["href"] for x in resp.html("a")])
assert hrefs == set(["foo_bar/", "foo-bar/"])
def test_no_cache_control_set(root, _app, testapp):
assert not _app.config.cache_control
root.join("foo_bar-1.0.tar.gz").write("")
resp = testapp.get("/packages/foo_bar-1.0.tar.gz")
assert "Cache-Control" not in resp.headers
def test_cache_control_set(root):
from pypiserver import app
AGE = 86400
app_with_cache = webtest.TestApp(app(root=root.strpath, cache_control=AGE))
root.join("foo_bar-1.0.tar.gz").write("")
resp = app_with_cache.get("/packages/foo_bar-1.0.tar.gz")
assert "Cache-Control" in resp.headers
assert resp.headers["Cache-Control"] == 'public, max-age=%s' % AGE