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

Pass the -d/--directory command-line option to http.server.CGIHTTPRequestHandler #90594

Closed
maggyero mannequin opened this issue Jan 19, 2022 · 7 comments
Closed

Pass the -d/--directory command-line option to http.server.CGIHTTPRequestHandler #90594

maggyero mannequin opened this issue Jan 19, 2022 · 7 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@maggyero
Copy link
Mannequin

maggyero mannequin commented Jan 19, 2022

BPO 46436
Nosy @merwok, @Mariatta, @maggyero, @miss-islington
PRs
  • bpo-46436: Fix command-line option -d/--directory in module http.server #30701
  • [3.10] bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701) #31102
  • [3.9] bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701) #31103
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-02-14.22:28:00.223>
    created_at = <Date 2022-01-19.17:39:40.486>
    labels = ['type-bug', 'library', '3.9', '3.10', '3.11']
    title = 'Pass the -d/--directory command-line option to http.server.CGIHTTPRequestHandler'
    updated_at = <Date 2022-02-14.22:28:00.222>
    user = 'https://github.com/maggyero'

    bugs.python.org fields:

    activity = <Date 2022-02-14.22:28:00.222>
    actor = 'eric.araujo'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-02-14.22:28:00.223>
    closer = 'eric.araujo'
    components = ['Library (Lib)']
    creation = <Date 2022-01-19.17:39:40.486>
    creator = 'maggyero'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46436
    keywords = ['patch']
    message_count = 7.0
    messages = ['410973', '411306', '411393', '412446', '413257', '413258', '413267']
    nosy_count = 4.0
    nosy_names = ['eric.araujo', 'Mariatta', 'maggyero', 'miss-islington']
    pr_nums = ['30701', '31102', '31103']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46436'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jan 19, 2022

    The API of http.server 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.

    @maggyero maggyero mannequin added 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error 3.9 only security fixes labels Jan 19, 2022
    @merwok
    Copy link
    Member

    merwok commented Jan 22, 2022

    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

    @maggyero
    Copy link
    Mannequin Author

    maggyero mannequin commented Jan 23, 2022

    Thanks for mentioning this issue @merwok.

    As you pointed out, function test in module http.server 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:

        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 bpo-46285 and bpo-46436.

    @miss-islington
    Copy link
    Contributor

    New changeset 2d08034 by Géry Ogam in branch 'main':
    bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
    2d08034

    @Mariatta
    Copy link
    Member

    New changeset 502ad39 by Miss Islington (bot) in branch '3.9':
    bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
    502ad39

    @Mariatta
    Copy link
    Member

    New changeset b271953 by Miss Islington (bot) in branch '3.10':
    bpo-46436: Fix command-line option -d/--directory in module http.server (GH-30701)
    b271953

    @merwok
    Copy link
    Member

    merwok commented Feb 14, 2022

    Thanks again!

    @merwok merwok closed this as completed Feb 14, 2022
    @merwok merwok closed this as completed Feb 14, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants