Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.1k
gh-135056: Add a --cors CLI argument to http.server#135057
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
python-cla-botbot commentedJun 3, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Add a --cors command line argument to the stdlib http.server module, which willadd an `Access-Control-Allow-Origin: *` header to all responses. As part of thisimplementation, add a `response_headers` argument to SimpleHTTPRequestHandlerand HttpServer, which allows callers to add addition headers to the response.
3f11652
to0d02fbe
CompareThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
(I'd prefer a general headers option, but will comment on the issue or Discourse topic)
Doc/library/http.server.rst Outdated
@@ -374,6 +374,10 @@ instantiation, of which this module provides three different variants: | |||
.. versionchanged:: 3.9 | |||
The *directory* parameter accepts a :term:`path-like object`. | |||
.. versionchanged:: 3.15 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
..versionchanged::3.15 | |
..versionchanged::next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Fixed in1838da7
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
@@ -0,0 +1,2 @@ | |||
Add a ``--cors`` cli option to ``python -m http.server``. Contributed by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Add a ``--cors``cli option to``python -m http.server``. Contributed by | |
Add a ``--cors``CLI option to:program:`python -m http.server`. Contributed by |
@@ -0,0 +1,2 @@ | |||
Add a ``--cors`` cli option to ``python -m http.server``. Contributed by | |||
Anton I. Sipos |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Anton I. Sipos | |
Anton I. Sipos. |
|
This fixes the breakage to HttpServer as used by wsgiref.
test_wsgiref fixed ina3256fd. This should fix any backwards incompatibility errors erroneously introduced in the first commit. |
I think it's worth adding to this |
Uh oh!
There was an error while loading.Please reload this page.
As proposed in#135056, Add a --cors command line argument to the stdlib http.server module, which will add an
Access-Control-Allow-Origin: *
header to all responses.Invocation:
As part of this implementation, add a
response_headers
argument toSimpleHTTPRequestHandler
andHTTPServer
, which allows callers to add addition headers to the response. Ideally it would have been possible to just have made aCorsHttpServer
class, but a couple of issues made that difficult:http.server
CLI uses more than one HTTP Server class, in order to support TLS/HTTPS. So a single CorsHttpServer child class wouldn't work to support both use cases.RequestHandler
classes. However, theHttpServer
classes didn't have an easy way to pass arguments down into the instantiated handlers.As a result, this PR updates both
HTTPServer
andSimpleHTTPRequestHandler
to accept aresponse_headers
argument, which allows callers to specify an additional set of HTTP headers to pass in the response.HTTPServer
now overridesfinish_request
to pass this new kwarg down to itsRequestHandler
.SimpleHTTPRequestHandler
now accepts aresposnse_headers
kwarg, to optionally specify a dictionary of additional headers to send in the response.Care is taken to not pass the
response_headers
argument to any instance constructors when not provided, to ensure backwards compatibility. I tried to keep the implementation as short and simple as possible.With the addition of a
response_headers
argument, we allow ourselves to have a future possible custom header http argument, such as:📚 Documentation preview 📚:https://cpython-previews--135057.org.readthedocs.build/