Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
aisipos wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromaisipos:https-server-cors-issue-135056

Conversation

aisipos
Copy link

@aisiposaisipos commentedJun 3, 2025
edited by github-actionsbot
Loading

As proposed in#135056, Add a --cors command line argument to the stdlib http.server module, which will add anAccess-Control-Allow-Origin: * header to all responses.

Invocation:

python -m http.server --cors

As part of this implementation, add aresponse_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:

  • Thehttp.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.
  • Much of the work in specifying HTTP behavior is handled by the variousRequestHandler classes. However, theHttpServer classes didn't have an easy way to pass arguments down into the instantiated handlers.

As a result, this PR updates bothHTTPServer 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 theresponse_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 aresponse_headers argument, we allow ourselves to have a future possible custom header http argument, such as:

python -m http.server -H 'custom-header: custom-value'

📚 Documentation preview 📚:https://cpython-previews--135057.org.readthedocs.build/

@python-cla-bot
Copy link

python-cla-botbot commentedJun 3, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

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 theskip news label instead.

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.
@aisiposaisiposforce-pushed thehttps-server-cors-issue-135056 branch from3f11652 to0d02fbeCompareJune 3, 2025 05:24
Copy link
Member

@hugovkhugovk left a 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)

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
..versionchanged::3.15
..versionchanged::next

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Fixed in1838da7

@@ -0,0 +1,2 @@
Add a ``--cors`` cli option to ``python -m http.server``. Contributed by
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
Anton I. Sipos
Anton I. Sipos.

@aisipos
Copy link
Author

test_wsgiref is failing. I'll look into it.

This fixes the breakage to HttpServer as used by wsgiref.
@aisipos
Copy link
Author

test_wsgiref fixed ina3256fd. This should fix any backwards incompatibility errors erroneously introduced in the first commit.

@donBarbos
Copy link
Contributor

I think it's worth adding to thisWhat's New entry (./Doc/whatsnew/3.15.rst)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@hugovkhugovkhugovk left review comments

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@aisipos@donBarbos@hugovk

[8]ページ先頭

©2009-2025 Movatter.jp