GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Directory listing in SimpleHTTPRequestHandler does not work well in non-UTF-8 locale · Issue #133889 · python/cpython

Directory listing in SimpleHTTPRequestHandler does not work well in non-UTF-8 locale #133889

New issue
Copy link
New issue
Copy link
Closed
Closed
Directory listing in SimpleHTTPRequestHandler does not work well in non-UTF-8 locale#133889
Copy link
Assignees
serhiy-storchaka
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@serhiy-storchaka
serhiy-storchaka
opened on May 11, 2025
Issue body actions

For directory, SimpleHTTPRequestHandler generates an index.html page containing a list of files. It uses the filesystem encoding for the page, which is reasonable, because file names are encoded with that encoding. The problem is that the directory patch, included in the title, can contain a query part of the URL, which may be not encodable with the filesystem encoding.

This causes test failure when running in non-UTF8 locale:

$ LC_ALL=uk_UA ./python -m test -vuall test_httpservers -m test_undecodable_parameter
...
test_undecodable_parameter (test.test_httpservers.SimpleHTTPServerTestCase.test_undecodable_parameter) ... ----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 48062)
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/socketserver.py", line 318, in _handle_request_noblock
    self.process_request(request, client_address)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/socketserver.py", line 349, in process_request
    self.finish_request(request, client_address)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/socketserver.py", line 362, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 721, in __init__
    super().__init__(*args, **kwargs)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/socketserver.py", line 766, in __init__
    self.handle()
    ~~~~~~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 485, in handle
    self.handle_one_request()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 473, in handle_one_request
    method()
    ~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 725, in do_GET
    f = self.send_head()
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 769, in send_head
    return self.list_directory(path)
           ~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/serhiy/py/cpython/Lib/http/server.py", line 874, in list_directory
    encoded = '\n'.join(r).encode(enc, 'surrogateescape')
  File "/home/serhiy/py/cpython/Lib/encodings/koi8_u.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 178: character maps to <undefined>
encoding with 'koi8-u' codec failed
----------------------------------------
ERROR

======================================================================
ERROR: test_undecodable_parameter (test.test_httpservers.SimpleHTTPServerTestCase.test_undecodable_parameter)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/serhiy/py/cpython/Lib/test/test_httpservers.py", line 559, in test_undecodable_parameter
    response = self.request(self.base_url + '/?x=%bb').read()
               ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/test/test_httpservers.py", line 131, in request
    return self.connection.getresponse()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/client.py", line 1430, in getresponse
    response.begin()
    ~~~~~~~~~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/client.py", line 331, in begin
    version, status, reason = self._read_status()
                              ~~~~~~~~~~~~~~~~~^^
  File "/home/serhiy/py/cpython/Lib/http/client.py", line 300, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
                             " response")
http.client.RemoteDisconnected: Remote end closed connection without response

----------------------------------------------------------------------

I suspect that there may also be issues if some files in the directory have non-decodable or the path of the directory is non-decodable, but I have not tested this yet.

Linked PRs

  • gh-133889: Encode file paths in utf-8 by default in list_directory #133975
  • gh-133889: Improve tests for SimpleHTTPRequestHandler #134102
  • [3.14] gh-133889: Improve tests for SimpleHTTPRequestHandler (GH-134102) #134121
  • [3.13] gh-133889: Improve tests for SimpleHTTPRequestHandler (GH-134102) #134122
  • gh-133889: Only show the path of the URL in the SimpleHTTPRequestHandler page #134135
  • [3.14] gh-133889: Only show the path of the URL in the SimpleHTTPRequestHandler page (GH-134135) #134190
  • [3.13] gh-133889: Only show the path of the URL in the SimpleHTTPRequestHandler page (GH-134135) #134191

Activity

  1. serhiy-storchaka
    added
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    on May 11, 2025
  2. serhiy-storchaka
    self-assigned this
    on May 11, 2025
  3. serhiy-storchaka
    added a parent issue on May 11, 2025
    • Several tests fail when system encoding is latin-1 #133677
  4. serhiy-storchaka
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on May 11, 2025
  5. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on May 11, 2025
  6. StanFromIreland commented on May 13, 2025

    @StanFromIreland
    StanFromIreland
    on May 13, 2025
    Member
    More actions

    There is no good way that guarantees it will work with simple locale, so why not encode in utf-8 instead? It is becoming default in Python anyway, and I believe is the default in the majority of web browsers. We could make it optional to use system encoding, and default to the web standard?

  7. serhiy-storchaka
    added 2 commits that reference this issue on May 16, 2025

    pythongh-133889: Improve tests for SimpleHTTPRequestHandler

    94129e9

    gh-133889: Improve tests for SimpleHTTPRequestHandler (GH-134102)

    fcaf009
  8. miss-islington
    added 2 commits that reference this issue on May 17, 2025

    pythongh-133889: Improve tests for SimpleHTTPRequestHandler (pythonGH…

    89042e4

    pythongh-133889: Improve tests for SimpleHTTPRequestHandler (pythonGH…

    4f80c25
  9. serhiy-storchaka
    added 2 commits that reference this issue on May 17, 2025

    [3.13] gh-133889: Improve tests for SimpleHTTPRequestHandler (GH-134102…

    6dae082

    [3.14] gh-133889: Improve tests for SimpleHTTPRequestHandler (GH-134102…

    5cdad8c
  10. serhiy-storchaka commented on May 17, 2025

    @serhiy-storchaka
    serhiy-storchaka
    on May 17, 2025
    MemberAuthor
    More actions

    Always using the UTF-8 encoding, as well as the "xmlcharrefreplace" errors handler will fix the failure. But it will also change the page representation. In long run it may be good, but note that currently you get the same binary representation of file names independently from the locale of the server.

    On other hand, I think that applying urllib.parse.unquote() to the whole URL is incorrect. The path can contain ? or # (percent-encoded in the URL), after unquoting it will look as wrong URL. Since query and fragment are ignored in any case, I think that they should not be shown in the page title. Then we will not have a problem of encoding non-encodable characters in a page.

    Reacted by Stan Ulbrych
  11. serhiy-storchaka
    added 3 commits that reference this issue on May 17, 2025

    pythongh-133889: Only show the path of the URL in the SimpleHTTPServe…

    edf9a90

    pythongh-133889: Only show the path of the URL in the SimpleHTTPReque…

    f84c542

    gh-133889: Only show the path of the URL in the SimpleHTTPRequestHand…

    5cbc8c6
  12. miss-islington
    added 2 commits that reference this issue on May 18, 2025

    pythongh-133889: Only show the path of the URL in the SimpleHTTPReque…

    3a5010d

    pythongh-133889: Only show the path of the URL in the SimpleHTTPReque…

    3bc8a59
  13. serhiy-storchaka
    added 2 commits that reference this issue on May 18, 2025

    [3.14] gh-133889: Only show the path of the URL in the SimpleHTTPRequ…

    7073587

    [3.13] gh-133889: Only show the path of the URL in the SimpleHTTPRequ…

    821a87e
  14. serhiy-storchaka
    closed this as completedon May 18, 2025
  15. Pranjal095
    added 2 commits that reference this issue on Jul 12, 2025

    pythongh-133889: Improve tests for SimpleHTTPRequestHandler (pythonGH…

    dc13d83

    pythongh-133889: Only show the path of the URL in the SimpleHTTPReque…

    9e9a220
  16. taegyunkim
    added 2 commits that reference this issue on Aug 4, 2025

    pythongh-133889: Improve tests for SimpleHTTPRequestHandler (pythonGH…

    ea45a25

    pythongh-133889: Only show the path of the URL in the SimpleHTTPReque…

    66c3903
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @serhiy-storchaka
    serhiy-storchaka

Labels

3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.