This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Pass the -d/--directory command-line option to http.server.CGIHTTPRequestHandler
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Mariatta, eric.araujo, maggyero, miss-islington
Priority: normal Keywords: patch

Created on 2022-01-19 17:39 by maggyero, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30701 merged maggyero, 2022-01-19 17:39
PR 31102 merged miss-islington, 2022-02-03 15:51
PR 31103 merged miss-islington, 2022-02-03 15:51
Messages (7)
msg410973 - (view) Author: Géry (maggyero) * Date: 2022-01-19 17:39
The API of [`http.server`](https://docs.python.org/3/library/http.server.html) supports the `directory` optional parameter for `CGIHTTPRequestHandler` (which is inherited from `SimpleHTTPRequestHandler`). The CLI of `http.server` supports the corresponding `-d/--directory` option.

The `-d/--directory` option is passed to `SimpleHTTPRequestHandler` as the `directory` argument:

> python -m http.server --directory /tmp/

But the `-d/--directory` option is not passed to `CGIHTTPRequestHandler` (which is enabled with the `--cgi` option):

> python -m http.server --directory /tmp/ --cgi

So the option is ignored in that case.
msg411306 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2022-01-22 23:40
The use of partial may cause bugs!

Here is reported that the `protocol` parameter (of the `test` function) is ignored because it’s set on the partial object instead of the handler class: https://bugs.python.org/issue46285
msg411393 - (view) Author: Géry (maggyero) * Date: 2022-01-23 18:29
Thanks for mentioning this issue @merwok.

As you pointed out, function `test` in module [`http.server`](https://github.com/python/cpython/blob/main/Lib/http/server.py) expects a real request handler class argument (`SimpleHTTPRequestHandler` or `CGIHTTPRequestHandler`), not a partial object `partial(SimpleHTTPRequestHandler, directory=args.directory)` or `partial(CGIHTTPRequestHandler, directory=args.directory)`, so that the assignment of `protocol_version` class attribute in test is not ignored.

The partial object in the `if __name__ == '__main__'` branch of module `http.server` was introduced in the first place to pass the directory argument to the request handler class’s `__init__` method called in method `BaseServer.finish_request` of module [`socketserver`](https://github.com/python/cpython/blob/main/Lib/socketserver.py):

    def finish_request(self, request, client_address):
        """Finish one request by instantiating RequestHandlerClass."""
        self.RequestHandlerClass(request, client_address, self)

But `BaseServer.finish_request` is a factory method of `BaseServer` (the abstract creator) so it is *designed* to be overridden in subclasses to customize the instantiation of the request handler class `BaseRequestHandler` (the abstract product). So the proper way to instantiate `SimpleHTTPRequestHandler` and `CGIHTTPRequestHandler` with the `directory` argument is to override `BaseServer.finish_request`.

I have just updated my PR to implement this. It fixes both [#46285](https://bugs.python.org/issue46285) and [#46436](https://bugs.python.org/issue46436).
msg412446 - (view) Author: miss-islington (miss-islington) Date: 2022-02-03 15:51
New changeset 2d080347d74078a55c47715d232d1ab8dc8cd603 by Géry Ogam in branch 'main':
bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
https://github.com/python/cpython/commit/2d080347d74078a55c47715d232d1ab8dc8cd603
msg413257 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2022-02-14 20:12
New changeset 502ad3930ee8fcf76026edfc06a33621363cebea by Miss Islington (bot) in branch '3.9':
bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701) 
https://github.com/python/cpython/commit/502ad3930ee8fcf76026edfc06a33621363cebea
msg413258 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2022-02-14 20:12
New changeset b27195332e91e932501f16cf9877761b218a9c99 by Miss Islington (bot) in branch '3.10':
bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
https://github.com/python/cpython/commit/b27195332e91e932501f16cf9877761b218a9c99
msg413267 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2022-02-14 22:28
Thanks again!
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90594
2022-02-14 22:28:00eric.araujosetstatus: open -> closed
resolution: fixed
messages: + msg413267

stage: patch review -> resolved
2022-02-14 20:12:41Mariattasetmessages: + msg413258
2022-02-14 20:12:25Mariattasetnosy: + Mariatta
messages: + msg413257
2022-02-03 15:57:34eric.araujolinkissue46285 dependencies
2022-02-03 15:51:22miss-islingtonsetpull_requests: + pull_request29286
2022-02-03 15:51:17miss-islingtonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request29285
2022-02-03 15:51:14miss-islingtonsetnosy: + miss-islington
messages: + msg412446
2022-01-23 18:29:33maggyerosetmessages: + msg411393
2022-01-22 23:40:45eric.araujosetnosy: + eric.araujo
messages: + msg411306
2022-01-21 06:54:51maggyerosetversions: + Python 3.9
2022-01-19 20:00:09maggyerosetnosy: - docs@python
2022-01-19 17:39:40maggyerocreate